I handle images in results.
This commit is contained in:
		
							parent
							
								
									bca6ee01e6
								
							
						
					
					
						commit
						22e4e6b226
					
				| 
						 | 
					@ -17,7 +17,6 @@ def src(doc, code, cls="", root=True):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def img(doc, item, cls="", root=True):
 | 
					def img(doc, item, cls="", root=True):
 | 
				
			||||||
    caption = doc.previous(tokens.CAPTION)
 | 
					 | 
				
			||||||
    text = ""
 | 
					    text = ""
 | 
				
			||||||
    if item.attrs:
 | 
					    if item.attrs:
 | 
				
			||||||
        caption = item.attrs.get('caption')
 | 
					        caption = item.attrs.get('caption')
 | 
				
			||||||
| 
						 | 
					@ -68,17 +67,17 @@ def parse_text_html(doc, token, cls="", root=True):
 | 
				
			||||||
    return f"{token.value}"
 | 
					    return f"{token.value}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def results(doc, token, cls="", root=True):
 | 
					def results(doc, results, cls="", root=True):
 | 
				
			||||||
    if token.value.startswith('[[file:'):
 | 
					    result = ""
 | 
				
			||||||
        return "<img%s src=\"%s\"/>\n" % (
 | 
					    for token in results.value:
 | 
				
			||||||
        cls,
 | 
					        if token.token is tokens.IMAGE:
 | 
				
			||||||
        escape(token.value.strip()[7:-2]),
 | 
					            result += img(doc, token, cls, root=root)
 | 
				
			||||||
    )
 | 
					            return result
 | 
				
			||||||
 | 
					        result += "<blockquote%s>%s</blockquote>\n" % (
 | 
				
			||||||
    return "<blockquote%s>%s</blockquote>\n" % (
 | 
					            cls,
 | 
				
			||||||
        cls,
 | 
					            escape(token.value).replace("\n", "<br />"),
 | 
				
			||||||
        escape(token.value).replace("\n", "<br />"),
 | 
					        )
 | 
				
			||||||
    )
 | 
					    return result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def blockquote(doc, token, cls="", root=True):
 | 
					def blockquote(doc, token, cls="", root=True):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,6 +39,8 @@ def parse_img_or_link(char, step):
 | 
				
			||||||
            char = next(step, None)
 | 
					            char = next(step, None)
 | 
				
			||||||
        char = next(step, None)
 | 
					        char = next(step, None)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if path.startswith('file:'):
 | 
				
			||||||
 | 
					        path = path[5:]
 | 
				
			||||||
    if path.endswith(image_extensions):
 | 
					    if path.endswith(image_extensions):
 | 
				
			||||||
        return False, Token(tokens.IMAGE, [path, alt])
 | 
					        return False, Token(tokens.IMAGE, [path, alt])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -75,7 +75,7 @@ class Document:
 | 
				
			||||||
                yield item
 | 
					                yield item
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if item.token != tokens.LIST:
 | 
					            if item.token != tokens.LIST and item.token != tokens.RESULTS:
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if isinstance(item.value, list):
 | 
					            if isinstance(item.value, list):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,16 +20,17 @@ def test_render_results():
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    expected = [
 | 
					    expected = [
 | 
				
			||||||
        Token(tokens.IMAGE, "['file:test.png', '']", attrs=None),
 | 
					        Token(tokens.IMAGE, ['test.png', ''], attrs=None),
 | 
				
			||||||
        Token(tokens.TEXT, "\n"),
 | 
					        Token(tokens.TEXT, "\n"),
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
    doc = parse(text).doc
 | 
					    doc = parse(text)
 | 
				
			||||||
    assert doc[0].token == tokens.BLANK
 | 
					    tags = doc.doc
 | 
				
			||||||
    assert len(doc[1].value) == len(expected)
 | 
					    assert tags[0].token == tokens.BLANK
 | 
				
			||||||
    assert doc[1].token == tokens.RESULTS
 | 
					    assert len(tags[1].value) == len(expected)
 | 
				
			||||||
 | 
					    assert tags[1].token == tokens.RESULTS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    assert doc[1].value[0] == expected[0]
 | 
					    assert tags[1].value[0].value == expected[0].value
 | 
				
			||||||
    assert doc[1].value[1] == expected[1]
 | 
					    assert tags[1].value[1].value == expected[1].value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    htmlbody = html(doc).read()
 | 
					    htmlbody = html(tags).read()
 | 
				
			||||||
    assert htmlbody == '<img src="test.png"/>\n'
 | 
					    assert htmlbody == '<img style="margin:auto;" src="test.png" alt="" />'
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue