From a66d0e1714813918ed2d58bcec74c46062502f4f Mon Sep 17 00:00:00 2001 From: Miguel Barao Date: Wed, 15 Nov 2017 13:33:21 +0000 Subject: [PATCH] - changes in markdown rendering in tools.py --- tools.py | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------- 1 file changed, 89 insertions(+), 41 deletions(-) diff --git a/tools.py b/tools.py index 062b190..bcda728 100644 --- a/tools.py +++ b/tools.py @@ -2,10 +2,11 @@ from os import path import subprocess import logging +import re import yaml -# import markdown -import mistune +from markdown import markdown +# import mistune from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import html @@ -15,21 +16,94 @@ logger = logging.getLogger(__name__) # --------------------------------------------------------------------------- -class HighlightRenderer(mistune.Renderer): - def block_code(self, code, lang=None): - if lang is None: - return f'\n
{mistune.escape(code)}
\n' - else: - lexer = get_lexer_by_name(lang, stripall=True) - formatter = html.HtmlFormatter() - return highlight(code, lexer, formatter) +# Setup markdown renderer with support for math in LaTeX notation. +# --------------------------------------------------------------------------- +# class MathBlockGrammar(mistune.BlockGrammar): +# block_math = re.compile(r"^\$\$(.*?)\$\$", re.DOTALL) +# latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}", re.DOTALL) + + +# class MathBlockLexer(mistune.BlockLexer): +# default_rules = ['block_math', 'latex_environment'] + mistune.BlockLexer.default_rules + +# def __init__(self, rules=None, **kwargs): +# if rules is None: +# rules = MathBlockGrammar() +# super(MathBlockLexer, self).__init__(rules, **kwargs) + +# def parse_block_math(self, m): +# """Parse a $$math$$ block""" +# self.tokens.append({ +# 'type': 'block_math', +# 'text': m.group(1) +# }) + +# def parse_latex_environment(self, m): +# self.tokens.append({ +# 'type': 'latex_environment', +# 'name': m.group(1), +# 'text': m.group(2) +# }) + + +# class MathInlineGrammar(mistune.InlineGrammar): +# math = re.compile(r"^\$(.+?)\$", re.DOTALL) +# block_math = re.compile(r"^\$\$(.+?)\$\$", re.DOTALL) +# text = re.compile(r'^[\s\S]+?(?=[\\{mistune.escape(code)}\n' +# else: +# lexer = get_lexer_by_name(lang, stripall=True) +# formatter = html.HtmlFormatter() +# return highlight(code, lexer, formatter) + +# def image(self, src, title, text): +# src = 'FIXME' # FIXME +# return super().image(src, title, text) + +# def block_math(self, text): +# return r'\[ %s \]' % text + +# def inline_math(self, text): +# return r'\( %s \)' % text + + +# renderer = HighlightRenderer(hard_wrap=True) +# markdown = mistune.Markdown(renderer=renderer) # --------------------------------------------------------------------------- @@ -83,29 +157,3 @@ def run_script(script, stdin='', timeout=5): # --------------------------------------------------------------------------- def md_to_html(text, q=None): return markdown(text) - -# def md_to_html(text, ref=None, files={}): -# if ref is not None: -# # given q['ref'] and q['files'] replaces references to files by a -# # GET to /file?ref=???;name=??? -# for k in files: -# text = text.replace(k, '/file?ref={};name={}'.format(ref, k)) -# return markdown.markdown(text, extensions=[ -# 'markdown.extensions.tables', -# 'markdown.extensions.fenced_code', -# 'markdown.extensions.codehilite', -# 'markdown.extensions.def_list', -# 'markdown.extensions.sane_lists' -# ]) - -# --------------------------------------------------------------------------- -# def md_to_html_review(text, q): -# for k,f in q['files'].items(): -# text = text.replace(k, '/absfile?name={}'.format(q['files'][k])) -# return markdown.markdown(text, extensions=[ -# 'markdown.extensions.tables', -# 'markdown.extensions.fenced_code', -# 'markdown.extensions.codehilite', -# 'markdown.extensions.def_list', -# 'markdown.extensions.sane_lists' -# ]) -- libgit2 0.21.2