This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MathJax Integration, switching from Misaka to Mistune
- Loading branch information
Showing
4 changed files
with
87 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import re | ||
|
||
|
||
class MathBlockMixin(object): | ||
"""Math mixin for BlockLexer, mix this with BlockLexer:: | ||
class MathBlockLexer(MathBlockMixin, BlockLexer): | ||
def __init__(self, *args, **kwargs): | ||
self.enable_math() | ||
super(MathBlockLexer, self).__init__(*args, **kwargs) | ||
""" | ||
def enable_math(self): | ||
self.rules.block_math = re.compile(r'^\$\$(.*?)\$\$', re.DOTALL) | ||
self.rules.block_latex = re.compile(r'^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}', re.DOTALL) | ||
self.default_rules.extend(['block_math', 'block_latex']) | ||
|
||
def parse_block_math(self, m): | ||
"""Parse a $$math$$ block""" | ||
self.tokens.append({ | ||
'type': 'block_math', | ||
'text': m.group(1) | ||
}) | ||
|
||
def parse_block_latex(self, m): | ||
self.tokens.append({ | ||
'type': 'block_latex', | ||
'name': m.group(1), | ||
'text': m.group(2) | ||
}) | ||
|
||
|
||
class MathInlineMixin(object): | ||
"""Math mixin for InlineLexer, mix this with InlineLexer:: | ||
class MathInlineLexer(InlineLexer, MathInlineMixin): | ||
def __init__(self, *args, **kwargs): | ||
self.enable_math() | ||
super(MathInlineLexer, self).__init__(*args, **kwargs) | ||
""" | ||
|
||
def enable_math(self): | ||
self.rules.math = re.compile(r'^\$(.+?)\$') | ||
self.default_rules.insert(0, 'math') | ||
|
||
def output_math(self, m): | ||
return self.renderer.math(m.group(1)) | ||
|
||
|
||
class MathRendererMixin(object): | ||
def block_math(self, text): | ||
return '$$%s$$' % text | ||
|
||
def block_latex(self, name, text): | ||
return r'\begin{%s}%s\end{%s}' % (name, text, name) | ||
|
||
def math(self, text): | ||
return '$%s$' % text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters