Added secondary image scanning test.
This commit is contained in:
parent
483606e95b
commit
ac899b8ada
|
@ -18,7 +18,9 @@ class Document:
|
|||
if not idx:
|
||||
if default is not None:
|
||||
return default
|
||||
raise AttributeError(f"Attribute of {name} does not exist in document")
|
||||
raise AttributeError(
|
||||
f"Attribute of {name} does not exist in document"
|
||||
)
|
||||
if len(idx) == 1:
|
||||
return self.doc[idx[0]].value
|
||||
return [self.doc[v].value for v in idx]
|
||||
|
@ -40,9 +42,9 @@ class Document:
|
|||
def previous(self, match):
|
||||
if self.pos is 0:
|
||||
return None
|
||||
if self.doc[self.pos-1].token != match:
|
||||
if self.doc[self.pos - 1].token != match:
|
||||
return None
|
||||
return self.doc[self.pos-1]
|
||||
return self.doc[self.pos - 1]
|
||||
|
||||
def filter(self, value):
|
||||
"""Only return types that are of intrest like source blocks"""
|
||||
|
@ -59,12 +61,11 @@ class Document:
|
|||
def images(self):
|
||||
for item in self.__iter__():
|
||||
if item.token == tokens.IMAGE:
|
||||
yield item.value[0]
|
||||
if item.token == tokens.TEXT:
|
||||
if isinstance(item.value, list):
|
||||
for token in item.value:
|
||||
if token.token == tokens.IMAGE:
|
||||
yield token
|
||||
yield item
|
||||
if isinstance(item.value, list):
|
||||
for token in item.value:
|
||||
if token.token == tokens.IMAGE:
|
||||
yield token
|
||||
|
||||
def __len__(self):
|
||||
return len(self.doc)
|
||||
|
@ -73,23 +74,26 @@ class Document:
|
|||
self.index.setdefault(value.token, []).append(len(self.doc))
|
||||
self.doc.append(value)
|
||||
|
||||
|
||||
def parse_attrs(text):
|
||||
attrs = {}
|
||||
value_list = text.split(':')
|
||||
attrs['language'] = value_list.pop(0).strip()
|
||||
value_list = text.split(":")
|
||||
attrs["language"] = value_list.pop(0).strip()
|
||||
for row in value_list:
|
||||
values = row.strip().split(' ')
|
||||
values = row.strip().split(" ")
|
||||
attrs[values[0]] = values[1:]
|
||||
return attrs
|
||||
|
||||
|
||||
def parsebody(text, rx):
|
||||
match = re.search(rx, text)
|
||||
if match:
|
||||
return False, None
|
||||
return rx, text + "\n"
|
||||
|
||||
|
||||
def parseline(text):
|
||||
attrs=None
|
||||
attrs = None
|
||||
for key, (rx, block, s, e, count) in TOKENS.items():
|
||||
match = re.search(rx, text)
|
||||
if not match:
|
||||
|
@ -97,18 +101,15 @@ def parseline(text):
|
|||
value = text[match.end() :]
|
||||
level = len(match.group(0))
|
||||
if count is True:
|
||||
attrs={'depth': level}
|
||||
attrs = {"depth": level}
|
||||
if key == tokens.META:
|
||||
return (
|
||||
block,
|
||||
Token(token=match.group(0)[s:e], value=value),
|
||||
)
|
||||
return (block, Token(token=match.group(0)[s:e], value=value))
|
||||
if key == tokens.SOURCE:
|
||||
return block, Token(token=key, attrs=parse_attrs(value))
|
||||
if key == tokens.TABLE:
|
||||
return block, Token(token=key, value=text+"\n")
|
||||
return block, Token(token=key, value=text + "\n")
|
||||
if key == tokens.BULLET:
|
||||
return block, Token(token=key, value=text+"\n")
|
||||
return block, Token(token=key, value=text + "\n")
|
||||
return block, Token(token=key, value=value, attrs=attrs)
|
||||
|
||||
text = text.strip()
|
||||
|
@ -122,54 +123,54 @@ def parse_text(txt):
|
|||
tokenlist = []
|
||||
|
||||
def img(char, step):
|
||||
if char != '[':
|
||||
if char != "[":
|
||||
return char
|
||||
char = next(step, None)
|
||||
|
||||
if char != '[':
|
||||
if char != "[":
|
||||
return char
|
||||
char = next(step, None)
|
||||
|
||||
path = ''
|
||||
while char not in [']'] + ESCAPE:
|
||||
path = ""
|
||||
while char not in ["]"] + ESCAPE:
|
||||
path += char
|
||||
char = next(step, None)
|
||||
char = next(step, None)
|
||||
|
||||
alt = ''
|
||||
if char == '[':
|
||||
alt = ""
|
||||
if char == "[":
|
||||
char = next(step, None)
|
||||
while char not in [']'] + ESCAPE:
|
||||
while char not in ["]"] + ESCAPE:
|
||||
alt += char
|
||||
char = next(step, None)
|
||||
char = next(step, None)
|
||||
|
||||
if path.endswith(image_extensions):
|
||||
tokenlist.append(Token(tokens.IMAGE, [path, alt]))
|
||||
return ''
|
||||
return ""
|
||||
|
||||
tokenlist.append(Token(tokens.LINK, [path, alt]))
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def emphasis(char, step, end, tag):
|
||||
if not char or char!=end:
|
||||
if not char or char != end:
|
||||
return char
|
||||
char = next(step, None)
|
||||
r = ''
|
||||
r = ""
|
||||
while char and char not in [end] + ESCAPE:
|
||||
r += char
|
||||
char = next(step, None)
|
||||
tokenlist.append(Token(tag, r))
|
||||
return ''
|
||||
return ""
|
||||
|
||||
step = iter(txt)
|
||||
while char is not None:
|
||||
char = next(step, None)
|
||||
char = emphasis(char, step, '*', tokens.BOLD)
|
||||
char = emphasis(char, step, '/', tokens.ITALIC)
|
||||
char = emphasis(char, step, '_', tokens.UNDERLINED)
|
||||
char = emphasis(char, step, '=', tokens.VERBATIM)
|
||||
char = emphasis(char, step, '~', 'PRE')
|
||||
char = emphasis(char, step, "*", tokens.BOLD)
|
||||
char = emphasis(char, step, "/", tokens.ITALIC)
|
||||
char = emphasis(char, step, "_", tokens.UNDERLINED)
|
||||
char = emphasis(char, step, "=", tokens.VERBATIM)
|
||||
char = emphasis(char, step, "~", "PRE")
|
||||
char = img(char, step)
|
||||
if not char:
|
||||
continue
|
||||
|
@ -187,7 +188,7 @@ def parse(stream):
|
|||
doc = Document()
|
||||
block = False
|
||||
for line in stream:
|
||||
line = line.strip('\n')
|
||||
line = line.strip("\n")
|
||||
if block is not False:
|
||||
block, token = parsebody(line, block)
|
||||
if block:
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#+TITLE: Emacs org-mode tables
|
||||
#+AUTHOR: Eric H. Neilsen, Jr.
|
||||
#+EMAIL: neilsen@fnal.gov
|
||||
#+DATE: jkkj
|
||||
#+KEYWORDS: emacs, orgmode, tests
|
||||
#+DESCRIPTION: Test DESCRIPTION
|
||||
#+KEYWORDS: key1, key2
|
||||
|
||||
Images
|
||||
|
||||
[[./images.jpg]]
|
||||
[[./images.jpg][test]]
|
||||
|
|
@ -1,15 +1,30 @@
|
|||
import os
|
||||
import pytest
|
||||
from eorg import tokens
|
||||
from eorg.tokens import Token
|
||||
from eorg.parser import parse
|
||||
|
||||
|
||||
def test_fetch_attribute():
|
||||
with open(os.path.abspath("./tests/fixtures/test.org"), "r") as fp:
|
||||
doc = parse(fp)
|
||||
assert doc.title == ' Emacs org-mode examples'
|
||||
assert doc.title == " Emacs org-mode examples"
|
||||
|
||||
|
||||
def test_fetch_non_existant_attribute():
|
||||
with open(os.path.abspath("./tests/fixtures/test.org"), "r") as fp:
|
||||
doc = parse(fp)
|
||||
with pytest.raises(AttributeError):
|
||||
doc.fake
|
||||
|
||||
|
||||
def test_fetch_image_list():
|
||||
with open(os.path.abspath("./tests/fixtures/test_images.org"), "r") as fp:
|
||||
doc = parse(fp)
|
||||
expected = [
|
||||
Token(tokens.IMAGE, ["./images.jpg", ""]),
|
||||
Token(tokens.IMAGE, ["./images.jpg", "test"]),
|
||||
]
|
||||
|
||||
images = [i for i in doc.images()]
|
||||
assert len(images) == 2
|
||||
|
|
Loading…
Reference in New Issue