Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_math_paragraph():
# https://github.com/ipython/ipython/issues/6724
assert_data('math-paragraph.md')
class WikiInlineGrammar(mistune.InlineGrammar):
# it would take a while for creating the right regex
wiki_link = re.compile(
r'\[\[' # [[
r'([\s\S]+?\|[\s\S]+?)' # Page 2|Page 2
r'\]\](?!\])' # ]]
)
class WikiInlineLexer(mistune.InlineLexer):
default_rules = copy.copy(mistune.InlineLexer.default_rules)
default_rules.insert(3, 'wiki_link')
def __init__(self, renderer, rules=None, **kwargs):
if rules is None:
rules = WikiInlineGrammar()
super(WikiInlineLexer, self).__init__(renderer, rules, **kwargs)
def output_wiki_link(self, m):
text = m.group(1)
alt, link = text.split('|')
return '<a href="%s">%s</a>' % (link, alt)
def test_custom_lexer():
markdown = mistune.Markdown(inline=WikiInlineLexer)
# it would take a while for creating the right regex
wiki_link = re.compile(
r'\[\[' # [[
r'([\s\S]+?\|[\s\S]+?)' # Page 2|Page 2
r'\]\](?!\])' # ]]
)
wiki_short_link = re.compile(
r'\[\[' # [[
r'([\s\S]+?)' # Page 2
r'\]\](?!\])' # ]]
)
class MyInlineLexer(mistune.InlineLexer):
default_rules = copy.copy(mistune.InlineLexer.default_rules)
# Add wiki_link parser to default features
# you can insert it any place you like
default_rules.insert(3, 'wiki_link')
default_rules.insert(3, 'wiki_short_link')
def __init__(self, renderer, rules=None, **kwargs):
if rules is None:
# use the inline grammar
rules = MyInlineGrammar()
super(MyInlineLexer, self).__init__(renderer, rules, **kwargs)
def output_wiki_link(self, m):
text = m.group(1)
alt, link = text.split('|')
self.tokens.append({
'type': 'latex_environment',
'name': m.group(1),
'text': m.group(2)
})
@undoc
class MathInlineGrammar(mistune.InlineGrammar):
math = re.compile("^\$(.+?)\$")
text = re.compile(r'^[\s\S]+?(?=[\\
mention = re.compile(
r'^@(?P[\w.@+-]+)',
flags=re.UNICODE
)
# Override
def hard_wrap(self):
# Adds ":" and "@" as an invalid text character, so we can match emojis and mentions.
self.linebreak = _linebreak
self.text = _text
class InlineLexer(mistune.InlineLexer):
default_rules = copy.copy(mistune.InlineLexer.default_rules)
default_rules.insert(0, 'math')
default_rules.insert(2, 'emoji')
default_rules.insert(2, 'mention')
def __init__(self, renderer, rules=None, **kwargs):
rules = InlineGrammar()
rules.hard_wrap()
super(InlineLexer, self).__init__(renderer, rules, **kwargs)
self.mentions = {}
self._mention_count = 0
def output_math(self, m):
return self.renderer.math(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]+?(?=[\\
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]+?(?=[\\
def parse_block_math(self, m):
self.tokens.append({
'type': 'block_math',
'text': m.group(1)
})
class Markdown(mistune.Markdown):
def output_block_math(self):
return self.renderer.block_math(self.token['text'])
class InlineLexer(mistune.InlineLexer):
# Adds math support
default_rules = ['math'] + mistune.InlineLexer.default_rules
def __init__(self, *args, **kwargs):
super(InlineLexer, self).__init__(*args, **kwargs)
self.enable_math()
def enable_math(self):
self.rules.text = re.compile(
r'^[\s\S]+?(?=[\\
r'\(([\s\S]+?)\)' # (vimeo_id)
)
# regex for embedding youtube videos
# ![:youtube 600x400](G-M7ECt3-zY)
youtube_link = re.compile(
r'!\[' # ![
r':youtube' # :youtube
r'([\s\S]*?)' # widthxheight
r'\]' # ]
r'\(([\s\S]+?)\)' # (youtube_id)
)
class MyInlineLexer(mistune.InlineLexer):
default_rules = copy.copy(mistune.InlineLexer.default_rules)
# Add youtube_link and vimeo_link parser to default features
# you can insert it any place you like
default_rules.insert(1, 'vimeo_link')
default_rules.insert(1, 'youtube_link')
def __init__(self, renderer, rules=None, **kwargs):
if rules is None:
# use the inline grammar
rules = MyInlineGrammar()
super(MyInlineLexer, self).__init__(renderer, rules, **kwargs)
def create_embedded_video(self, m, render_func):
# get the user specified size for the embedded video
size = 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]+?(?=[\\