Lower case attribute tokens, immprove testing framework

This commit is contained in:
Oly 2018-11-29 13:47:29 +00:00
parent 700d01923c
commit d00f2f842c
5 changed files with 12 additions and 18 deletions

View File

@ -59,7 +59,7 @@ TOKENS = {
),
tokens.IMAGE: TokenStruct(start=t_IMG, end_pos=-2),
tokens.CAPTION: TokenStruct(
start=t_CAPTIONS, type=TYPE_ATTRIBUTE, key="CAPTION"
start=t_CAPTIONS, type=TYPE_ATTRIBUTE, key="caption"
),
tokens.SOURCE: TokenStruct(start=t_SRC_BEGIN, end=t_SRC_END),
tokens.TABLE: TokenStruct(
@ -73,17 +73,6 @@ TOKENS = {
tokens.META_OTHER: TokenStruct(
start=t_META_OTHER, start_pos=2, end_pos=-1
),
# tokens.META: (t_META, False, 2, -1, False),
# tokens.COMMENT: (t_COMMENT_BEGIN, t_COMMENT_END, 2, None, False),
# tokens.EXAMPLE: (t_EXAMPLE_BEGIN, t_EXAMPLE_END, 2, None, False),
# tokens.IMAGE: (t_IMG, False, 2, None, False),
# tokens.CAPTION: (t_CAPTIONS, False, 2, None, False),
# tokens.SOURCE: (t_SRC_BEGIN, t_SRC_END, 2, None, False),
# tokens.TABLE: (t_TABLE_START, t_TABLE_END, 0, None, False),
# tokens.BULLET: (t_BULLET_START, t_BULLET_END, 0, None, False),
# tokens.RESULTS: (t_SRC_BEGIN, t_SRC_END, 2, None, False),
# tokens.HEADER: (t_HEADER, False, 1, None, True),
# tokens.META_OTHER: (t_META_OTHER, False, 2, -1, False),
}

View File

@ -5,6 +5,7 @@ from eorg.const import Token, ESCAPE
from eorg import tokens
from eorg.tokens import Token
from pygments import highlight
from pygments.util import ClassNotFound
from pygments.lexers import PythonLexer
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
@ -13,7 +14,7 @@ from pygments.formatters import HtmlFormatter
def src(doc, code, cls="", root=True):
try:
lexer = get_lexer_by_name(code.attrs.get("language", "shell"))
except pygments.util.ClassNotFound as e:
except ClassNotFound as e:
lexer = get_lexer_by_name(code.attrs.get("language", "text"))
return highlight(code.value, lexer, HtmlFormatter(linenos=True))
@ -21,8 +22,10 @@ def src(doc, code, cls="", root=True):
def img(doc, item, cls="", root=True):
caption = doc.previous(tokens.CAPTION)
text = ""
if caption:
text = f'<p class="center-align">{caption.value}</p>'
if item.attrs:
caption = item.attrs.get('caption')
if caption:
text = f'<p class="center-align">{caption}</p>'
return f'<img{cls} style="margin:auto;" src="{item.value[0]}" alt="{item.value[1]}" />{text}'

2
setup.cfg Normal file
View File

@ -0,0 +1,2 @@
[aliases]
test=pytest

View File

@ -49,5 +49,5 @@ setup(
install_requires=["pygments"],
setup_requires=['pytest-runner',],
tests_require=['pytest']
tests_require=['pytest', 'pytest-runner']
)

View File

@ -67,7 +67,7 @@ def test_image_with_caption():
Token(
tokens.IMAGE,
["../../test.jpg", ""],
attrs={"CAPTION": " Test Image"},
attrs={"caption": " Test Image"},
)
]
result = parse(text).doc
@ -84,7 +84,7 @@ def test_image_with_caption():
Token(
tokens.IMAGE,
["../../test.jpg", "test"],
attrs={"CAPTION": " Test Image"},
attrs={"caption": " Test Image"},
)
]
result = parse(text).doc