Commit a66d0e1714813918ed2d58bcec74c46062502f4f
1 parent
c6da5826
Exists in
master
and in
1 other branch
- changes in markdown rendering in tools.py
Showing
1 changed file
with
89 additions
and
41 deletions
Show diff stats
tools.py
@@ -2,10 +2,11 @@ | @@ -2,10 +2,11 @@ | ||
2 | from os import path | 2 | from os import path |
3 | import subprocess | 3 | import subprocess |
4 | import logging | 4 | import logging |
5 | +import re | ||
5 | 6 | ||
6 | import yaml | 7 | import yaml |
7 | -# import markdown | ||
8 | -import mistune | 8 | +from markdown import markdown |
9 | +# import mistune | ||
9 | from pygments import highlight | 10 | from pygments import highlight |
10 | from pygments.lexers import get_lexer_by_name | 11 | from pygments.lexers import get_lexer_by_name |
11 | from pygments.formatters import html | 12 | from pygments.formatters import html |
@@ -15,21 +16,94 @@ logger = logging.getLogger(__name__) | @@ -15,21 +16,94 @@ logger = logging.getLogger(__name__) | ||
15 | 16 | ||
16 | 17 | ||
17 | # --------------------------------------------------------------------------- | 18 | # --------------------------------------------------------------------------- |
18 | -class HighlightRenderer(mistune.Renderer): | ||
19 | - def block_code(self, code, lang=None): | ||
20 | - if lang is None: | ||
21 | - return f'\n<pre><code>{mistune.escape(code)}</code></pre>\n' | ||
22 | - else: | ||
23 | - lexer = get_lexer_by_name(lang, stripall=True) | ||
24 | - formatter = html.HtmlFormatter() | ||
25 | - return highlight(code, lexer, formatter) | 19 | +# Setup markdown renderer with support for math in LaTeX notation. |
20 | +# --------------------------------------------------------------------------- | ||
21 | +# class MathBlockGrammar(mistune.BlockGrammar): | ||
22 | +# block_math = re.compile(r"^\$\$(.*?)\$\$", re.DOTALL) | ||
23 | +# latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}", re.DOTALL) | ||
24 | + | ||
25 | + | ||
26 | +# class MathBlockLexer(mistune.BlockLexer): | ||
27 | +# default_rules = ['block_math', 'latex_environment'] + mistune.BlockLexer.default_rules | ||
28 | + | ||
29 | +# def __init__(self, rules=None, **kwargs): | ||
30 | +# if rules is None: | ||
31 | +# rules = MathBlockGrammar() | ||
32 | +# super(MathBlockLexer, self).__init__(rules, **kwargs) | ||
33 | + | ||
34 | +# def parse_block_math(self, m): | ||
35 | +# """Parse a $$math$$ block""" | ||
36 | +# self.tokens.append({ | ||
37 | +# 'type': 'block_math', | ||
38 | +# 'text': m.group(1) | ||
39 | +# }) | ||
40 | + | ||
41 | +# def parse_latex_environment(self, m): | ||
42 | +# self.tokens.append({ | ||
43 | +# 'type': 'latex_environment', | ||
44 | +# 'name': m.group(1), | ||
45 | +# 'text': m.group(2) | ||
46 | +# }) | ||
47 | + | ||
48 | + | ||
49 | +# class MathInlineGrammar(mistune.InlineGrammar): | ||
50 | +# math = re.compile(r"^\$(.+?)\$", re.DOTALL) | ||
51 | +# block_math = re.compile(r"^\$\$(.+?)\$\$", re.DOTALL) | ||
52 | +# text = re.compile(r'^[\s\S]+?(?=[\\<!\[_*`~$]|https?://| {2,}\n|$)') | ||
53 | + | ||
54 | + | ||
55 | +# class MathInlineLexer(mistune.InlineLexer): | ||
56 | +# default_rules = ['block_math', 'math'] + mistune.InlineLexer.default_rules | ||
57 | + | ||
58 | +# def __init__(self, renderer, rules=None, **kwargs): | ||
59 | +# if rules is None: | ||
60 | +# rules = MathInlineGrammar() | ||
61 | +# super(MathInlineLexer, self).__init__(renderer, rules, **kwargs) | ||
62 | + | ||
63 | +# def output_math(self, m): | ||
64 | +# return self.renderer.inline_math(m.group(1)) | ||
26 | 65 | ||
27 | - def image(self, src, title, text): | ||
28 | - src = 'FIXME' # FIXME | ||
29 | - return super().image(src, title, text) | 66 | +# def output_block_math(self, m): |
67 | +# return self.renderer.block_math(m.group(1)) | ||
30 | 68 | ||
31 | -renderer = HighlightRenderer(hard_wrap=True) | ||
32 | -markdown = mistune.Markdown(renderer=renderer) | 69 | + |
70 | +# class MarkdownWithMath(mistune.Markdown): | ||
71 | +# def __init__(self, renderer, **kwargs): | ||
72 | +# if 'inline' not in kwargs: | ||
73 | +# kwargs['inline'] = MathInlineLexer | ||
74 | +# if 'block' not in kwargs: | ||
75 | +# kwargs['block'] = MathBlockLexer | ||
76 | +# super().__init__(renderer, **kwargs) | ||
77 | + | ||
78 | +# def output_block_math(self): | ||
79 | +# return self.renderer.block_math(self.token['text']) | ||
80 | + | ||
81 | +# def output_latex_environment(self): | ||
82 | +# return self.renderer.latex_environment(self.token['name'], self.token['text']) | ||
83 | + | ||
84 | + | ||
85 | +# class HighlightRenderer(mistune.Renderer): | ||
86 | +# def block_code(self, code, lang=None): | ||
87 | +# if lang is None: | ||
88 | +# return f'\n<pre><code>{mistune.escape(code)}</code></pre>\n' | ||
89 | +# else: | ||
90 | +# lexer = get_lexer_by_name(lang, stripall=True) | ||
91 | +# formatter = html.HtmlFormatter() | ||
92 | +# return highlight(code, lexer, formatter) | ||
93 | + | ||
94 | +# def image(self, src, title, text): | ||
95 | +# src = 'FIXME' # FIXME | ||
96 | +# return super().image(src, title, text) | ||
97 | + | ||
98 | +# def block_math(self, text): | ||
99 | +# return r'\[ %s \]' % text | ||
100 | + | ||
101 | +# def inline_math(self, text): | ||
102 | +# return r'\( %s \)' % text | ||
103 | + | ||
104 | + | ||
105 | +# renderer = HighlightRenderer(hard_wrap=True) | ||
106 | +# markdown = mistune.Markdown(renderer=renderer) | ||
33 | 107 | ||
34 | 108 | ||
35 | # --------------------------------------------------------------------------- | 109 | # --------------------------------------------------------------------------- |
@@ -83,29 +157,3 @@ def run_script(script, stdin='', timeout=5): | @@ -83,29 +157,3 @@ def run_script(script, stdin='', timeout=5): | ||
83 | # --------------------------------------------------------------------------- | 157 | # --------------------------------------------------------------------------- |
84 | def md_to_html(text, q=None): | 158 | def md_to_html(text, q=None): |
85 | return markdown(text) | 159 | return markdown(text) |
86 | - | ||
87 | -# def md_to_html(text, ref=None, files={}): | ||
88 | -# if ref is not None: | ||
89 | -# # given q['ref'] and q['files'] replaces references to files by a | ||
90 | -# # GET to /file?ref=???;name=??? | ||
91 | -# for k in files: | ||
92 | -# text = text.replace(k, '/file?ref={};name={}'.format(ref, k)) | ||
93 | -# return markdown.markdown(text, extensions=[ | ||
94 | -# 'markdown.extensions.tables', | ||
95 | -# 'markdown.extensions.fenced_code', | ||
96 | -# 'markdown.extensions.codehilite', | ||
97 | -# 'markdown.extensions.def_list', | ||
98 | -# 'markdown.extensions.sane_lists' | ||
99 | -# ]) | ||
100 | - | ||
101 | -# --------------------------------------------------------------------------- | ||
102 | -# def md_to_html_review(text, q): | ||
103 | -# for k,f in q['files'].items(): | ||
104 | -# text = text.replace(k, '/absfile?name={}'.format(q['files'][k])) | ||
105 | -# return markdown.markdown(text, extensions=[ | ||
106 | -# 'markdown.extensions.tables', | ||
107 | -# 'markdown.extensions.fenced_code', | ||
108 | -# 'markdown.extensions.codehilite', | ||
109 | -# 'markdown.extensions.def_list', | ||
110 | -# 'markdown.extensions.sane_lists' | ||
111 | -# ]) |