Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add required output_block_math(); add synopsis #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 83 additions & 4 deletions mistune_contrib/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,60 @@
:copyright: (c) 2014 by Hsiaoming Yang.
"""


"""
Usage: define your own block-lexer, inline-lexer, markdown engine and
renderer to customize math rendering.

from mistune import Markdown
from mistune import BlockLexer
from mistune import InlineLexer
from mistune import Renderer

class MathBlockLexer(MathBlockMixin, BlockLexer):
def __init__(self, *args, **kwargs):
super(MathBlockLexer, self).__init__(*args, **kwargs)
self.enable_math()


class MathInlineLexer(InlineLexer, MathInlineMixin):
def __init__(self, *args, **kwargs):
super(MathInlineLexer, self).__init__(*args, **kwargs)
self.enable_math()


class MathRenderer(Renderer, MathRendererMixin):
pass


class MathMarkdown(Markdown, MathMarkdownMixin):
pass

render = MathRenderer()
md = MathMarkdown(render,
inline=MathInlineLexer(render),
block=MathBlockLexer
)

print md('''
block math:

$$
y = x^2
$$

inline math: $ y = \\frac{1}{x} $''')

# output:
# <p>block math:</p>
#
# $$ this is my math:
# y = x^2
# $$
# <p>inline math: $ this is my math: y = \frac{1}{x} $</p>
"""


import re


Expand All @@ -25,7 +79,12 @@ def enable_math(self):
self.rules.block_latex = re.compile(
r'^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}', re.DOTALL
)
self.default_rules.extend(['block_math', 'block_latex'])

# block_math must come before paragraph, or it is rendered as a normal paragraph
rs = self.default_rules
i = rs.index('paragraph')
rs.insert(i, 'block_latex')
rs.insert(i, 'block_math')

def parse_block_math(self, m):
"""Parse a $$math$$ block"""
Expand All @@ -42,6 +101,23 @@ def parse_block_latex(self, m):
})



class MathMarkdownMixin(object):

# block level token requires a output_* function
def output_block_math(self):
body = '\n'
body += self.renderer.block_math(self.token['text'])
body += '\n'
return body

def output_block_latex(self):
body = '\n'
body += self.renderer.block_latex(self.token['name'],
self.token['text'])
body += '\n'
return body

class MathInlineMixin(object):
"""Math mixin for InlineLexer, mix this with InlineLexer::

Expand All @@ -62,10 +138,13 @@ def output_math(self, m):

class MathRendererMixin(object):
def block_math(self, text):
return '$$%s$$' % text
# override with customized math rendering
return '$$ math-renderer-mixin-block: %s$$' % text

def block_latex(self, name, text):
return r'\begin{%s}%s\end{%s}' % (name, text, name)
# override with customized math rendering
return r' math-renderer-mixin-latex: \begin{%s}%s\end{%s}' % (name, text, name)

def math(self, text):
return '$%s$' % text
# override with customized math rendering
return '$ math-renderer-mixin-inline: %s$' % text
60 changes: 60 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from mistune import Markdown
from mistune import BlockLexer
from mistune import InlineLexer
from mistune import Renderer
from mistune_contrib import math

MIXED_CASE = '''The entries of $C$ are given by the exact formula:
$$
C_{ik} = \sum_{j=1}^n A_{ij} B_{jk}
Expand Down Expand Up @@ -43,3 +49,57 @@ def test_mixed():

def test_paragraph():
pass

class MathBlockLexer(math.MathBlockMixin, BlockLexer):
def __init__(self, *args, **kwargs):
super(MathBlockLexer, self).__init__(*args, **kwargs)
self.enable_math()

class MathInlineLexer(InlineLexer, math.MathInlineMixin):
def __init__(self, *args, **kwargs):
super(MathInlineLexer, self).__init__(*args, **kwargs)
self.enable_math()

class MathRenderer(Renderer, math.MathRendererMixin):
pass

class MathMarkdown(Markdown, math.MathMarkdownMixin):
pass

def test_block_math():

md = MathMarkdown(MathRenderer(),
block=MathBlockLexer)

res = md('''
$$
y = x^2
$$
''')

assert 'math-renderer-mixin-block' in res

def test_inline_math():

r = MathRenderer()
md = MathMarkdown(r,
inline=MathInlineLexer(r))

res = md('foo: $ y = x^2 $')

assert 'math-renderer-mixin-inline' in res

def test_latex_math():

md = MathMarkdown(MathRenderer(),
block=MathBlockLexer)

res = md('''
\\begin{foo}
y = x^2
\\end{foo}
''')

print res

assert 'math-renderer-mixin-latex' in res