I handle images in results.

This commit is contained in:
Oliver Marks 2018-12-07 07:15:55 +00:00
parent bca6ee01e6
commit 22e4e6b226
4 changed files with 24 additions and 22 deletions

View File

@ -17,7 +17,6 @@ def src(doc, code, cls="", root=True):
def img(doc, item, cls="", root=True):
caption = doc.previous(tokens.CAPTION)
text = ""
if item.attrs:
caption = item.attrs.get('caption')
@ -68,17 +67,17 @@ def parse_text_html(doc, token, cls="", root=True):
return f"{token.value}"
def results(doc, token, cls="", root=True):
if token.value.startswith('[[file:'):
return "<img%s src=\"%s\"/>\n" % (
cls,
escape(token.value.strip()[7:-2]),
)
return "<blockquote%s>%s</blockquote>\n" % (
cls,
escape(token.value).replace("\n", "<br />"),
)
def results(doc, results, cls="", root=True):
result = ""
for token in results.value:
if token.token is tokens.IMAGE:
result += img(doc, token, cls, root=root)
return result
result += "<blockquote%s>%s</blockquote>\n" % (
cls,
escape(token.value).replace("\n", "<br />"),
)
return result
def blockquote(doc, token, cls="", root=True):

View File

@ -39,6 +39,8 @@ def parse_img_or_link(char, step):
char = next(step, None)
char = next(step, None)
if path.startswith('file:'):
path = path[5:]
if path.endswith(image_extensions):
return False, Token(tokens.IMAGE, [path, alt])

View File

@ -75,7 +75,7 @@ class Document:
yield item
continue
if item.token != tokens.LIST:
if item.token != tokens.LIST and item.token != tokens.RESULTS:
continue
if isinstance(item.value, list):

View File

@ -20,16 +20,17 @@ def test_render_results():
"""
)
expected = [
Token(tokens.IMAGE, "['file:test.png', '']", attrs=None),
Token(tokens.IMAGE, ['test.png', ''], attrs=None),
Token(tokens.TEXT, "\n"),
]
doc = parse(text).doc
assert doc[0].token == tokens.BLANK
assert len(doc[1].value) == len(expected)
assert doc[1].token == tokens.RESULTS
doc = parse(text)
tags = doc.doc
assert tags[0].token == tokens.BLANK
assert len(tags[1].value) == len(expected)
assert tags[1].token == tokens.RESULTS
assert doc[1].value[0] == expected[0]
assert doc[1].value[1] == expected[1]
assert tags[1].value[0].value == expected[0].value
assert tags[1].value[1].value == expected[1].value
htmlbody = html(doc).read()
assert htmlbody == '<img src="test.png"/>\n'
htmlbody = html(tags).read()
assert htmlbody == '<img style="margin:auto;" src="test.png" alt="" />'