Handle language highlighting.

This commit is contained in:
Oliver Marks 2018-10-24 21:50:14 +01:00
parent 0eee46b2ae
commit 9089acfdaa
1 changed files with 9 additions and 9 deletions

View File

@ -6,22 +6,22 @@ from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter from pygments.formatters import HtmlFormatter
def src(doc, code, cls=''): def src(doc, code, cls=''):
lexer = get_lexer_by_name('lisp') lexer = get_lexer_by_name(code.attrs.get('language', 'shell'))
return highlight(code, lexer, HtmlFormatter()) return highlight(code.value, lexer, HtmlFormatter())
def img(doc, item, cls=''): def img(doc, item, cls=''):
caption = doc.previous('CAPTION') caption = doc.previous('CAPTION')
text = '' text = ''
if caption: if caption:
text = f'<p class="center-align">{caption.value}</p>' text = f'<p class="center-align">{caption.value}</p>'
return f'<img{cls} style="margin:auto;" src="{item[0]}" alt="{item[1]}" />{text}' return f'<img{cls} style="margin:auto;" src="{item.value[0]}" alt="{item.value[1]}" />{text}'
def parse_text_html(doc, tokens, cls=''): def parse_text_html(doc, token, cls=''):
if isinstance(tokens, list): if isinstance(token.value, list):
for token in tokens: for item in token.value:
return handle_token(doc, token) return handle_token(doc, item)
return f'<p{cls}>{tokens}</p>' return f'<p{cls}>{token.value}</p>'
builddoc ={ builddoc ={
"HEADER1": ("h2", None), "HEADER1": ("h2", None),
@ -48,7 +48,7 @@ def handle_token(doc, item):
else: else:
cls = '' cls = ''
if callable(tag): if callable(tag):
return tag(doc, item.value, cls) return tag(doc, item, cls)
else: else:
return '<%s%s>%s</%s>\n' % (tag, cls, item.value, tag) return '<%s%s>%s</%s>\n' % (tag, cls, item.value, tag)