Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import mistune
root = os.path.dirname(__file__)
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',
r'^( *\.\..*?)$',
re.DOTALL | re.MULTILINE,
)
rest_code_block = re.compile(
r'^::\s*$',
re.DOTALL | re.MULTILINE,
)
class RestBlockLexer(mistune.BlockLexer):
grammar_class = RestBlockGrammar
default_rules = [
'directive',
'oneline_directive',
'rest_code_block',
] + mistune.BlockLexer.default_rules
def parse_directive(self, m):
self.tokens.append({
'type': 'directive',
'text': m.group(1),
})
def parse_oneline_directive(self, m):
# reuse directive output
self.tokens.append({
'type': 'directive',
'text': m.group(1),
})
def parse_rest_code_block(self, m):
self.tokens.append({
r'(?:\s+min=(?P[0-9]+))?'
r'(?:\s+max=(?P[0-9]+))?'
r'(?:\s+close=(?P[0-9]+)d)?'
r'(?:\s+mode=(?P(default|secret)))?'
r'|(?P[^\]]*))'
r'\])\n'
r'((?:#\s*(?P<title>[^\n]+\n))?'
r'(?P<choices>(?:[0-9]+\.\s*[^\n]+\n){2,})'
r'|(?P<invalid_body>(?:[^\n]+\n)*))'
r'(?:\[/poll\])'
)
class BlockLexer(mistune.BlockLexer):
default_rules = copy.copy(mistune.BlockLexer.default_rules)
default_rules.insert(0, 'block_math')
default_rules.insert(0, 'block_math_brackets')
default_rules.insert(0, 'block_latex')
default_rules.insert(0, 'block_link')
default_rules.insert(0, 'poll')
_sub_block_links = (
'audio_link',
'image_link',
'video_link',
'youtube_link',
'vimeo_link',
'gfycat_link'
)
def __init__(self, rules=None, **kwargs):</title>
def enable_math(self):
self.rules.block_math = re.compile(r'^\$\$(.*?)\$\$', re.DOTALL)
self.default_rules = ['block_math'] + mistune.BlockLexer.default_rules
"""
Support Math features for mistune.
:copyright: (c) 2014 by Hsiaoming Yang.
"""
import re, mistune
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',
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound
from nbconvert.filters.strings import add_anchor
from nbconvert.utils.exceptions import ConversionException
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',
from pygments.util import ClassNotFound
from IPython.nbconvert.filters.strings import add_anchor
from IPython.nbconvert.utils.exceptions import ConversionException
from IPython.utils.decorators import undoc
@undoc
class MathBlockGrammar(mistune.BlockGrammar):
block_math = re.compile(r"^\$\$(.*?)\$\$", re.DOTALL)
latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}",
re.DOTALL)
@undoc
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',
Output as returned by pandoc.
"""
return pandoc(source, markup, 'latex', extra_args=extra_args)
@undoc
class MathBlockGrammar(mistune.BlockGrammar):
block_math = re.compile("^\$\$(.*?)\$\$", re.DOTALL)
latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}",
re.DOTALL)
@undoc
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',
# LICENSE: LGPL v3.0
# markdown stuff
import mistune, re
# Thanks https://github.com/jupyter/nbconvert/blob/master/nbconvert/filters/markdown_mistune.py
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',
r'^( *\.\..*?)$',
re.DOTALL | re.MULTILINE,
)
rest_code_block = re.compile(
r'^::\s*$',
re.DOTALL | re.MULTILINE,
)
class RestBlockLexer(mistune.BlockLexer):
grammar_class = RestBlockGrammar
default_rules = [
'directive',
'oneline_directive',
'rest_code_block',
] + mistune.BlockLexer.default_rules
def parse_directive(self, m):
self.tokens.append({
'type': 'directive',
'text': m.group(1),
})
def parse_oneline_directive(self, m):
# reuse directive output
self.tokens.append({
'type': 'directive',
'text': m.group(1),
})
def parse_rest_code_block(self, m):
self.tokens.append({