mirror of
https://github.com/LearnOpenGL-CN/LearnOpenGL-CN.git
synced 2025-08-23 04:35:28 +08:00
Still attempting
This commit is contained in:
64
mdx_math.py
Normal file
64
mdx_math.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
Math extension for Python-Markdown
|
||||
==================================
|
||||
|
||||
Adds support for displaying math formulas using [MathJax](http://www.mathjax.org/).
|
||||
|
||||
Author: 2015, Dmitry Shachnev <mitya57@gmail.com>.
|
||||
'''
|
||||
|
||||
import markdown
|
||||
|
||||
class MathExtension(markdown.extensions.Extension):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.config = {
|
||||
'enable_dollar_delimiter': [False, 'Enable single-dollar delimiter'],
|
||||
'render_to_span': [False,
|
||||
'Render to span elements rather than script for fallback'],
|
||||
}
|
||||
super(MathExtension, self).__init__(*args, **kwargs)
|
||||
|
||||
def extendMarkdown(self, md, md_globals):
|
||||
def handle_match_inline(m):
|
||||
if self.getConfig('render_to_span'):
|
||||
node = markdown.util.etree.Element('span')
|
||||
node.set('class', 'tex')
|
||||
node.text = ("\\\\(" + markdown.util.AtomicString(m.group(3)) +
|
||||
"\\\\)")
|
||||
else:
|
||||
node = markdown.util.etree.Element('script')
|
||||
node.set('type', 'math/tex')
|
||||
node.text = markdown.util.AtomicString(m.group(3))
|
||||
return node
|
||||
|
||||
def handle_match(m):
|
||||
node = markdown.util.etree.Element('script')
|
||||
node.set('type', 'math/tex; mode=display')
|
||||
if '\\begin' in m.group(2):
|
||||
node.text = markdown.util.AtomicString(m.group(2) + m.group(4) + m.group(5))
|
||||
else:
|
||||
node.text = markdown.util.AtomicString(m.group(3))
|
||||
return node
|
||||
|
||||
inlinemathpatterns = (
|
||||
markdown.inlinepatterns.Pattern(r'(?<!\\|\$)(\$)([^\$]+)(\$)'), # $...$
|
||||
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\()(.+?)(\\\))') # \(...\)
|
||||
)
|
||||
mathpatterns = (
|
||||
markdown.inlinepatterns.Pattern(r'(?<!\\)(\$\$)([^\$]+)(\$\$)'), # $$...$$
|
||||
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\[)(.+?)(\\\])'), # \[...\]
|
||||
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\begin{([a-z]+?\*?)})(.+?)(\\end{\3})')
|
||||
)
|
||||
if not self.getConfig('enable_dollar_delimiter'):
|
||||
inlinemathpatterns = inlinemathpatterns[1:]
|
||||
for i, pattern in enumerate(inlinemathpatterns):
|
||||
pattern.handleMatch = handle_match_inline
|
||||
md.inlinePatterns.add('math-inline-%d' % i, pattern, '<escape')
|
||||
for i, pattern in enumerate(mathpatterns):
|
||||
pattern.handleMatch = handle_match
|
||||
md.inlinePatterns.add('math-%d' % i, pattern, '<escape')
|
||||
|
||||
def makeExtension(*args, **kwargs):
|
||||
return MathExtension(*args, **kwargs)
|
@@ -55,10 +55,6 @@ pages:
|
||||
|
||||
site_name: LearnOpenGL-CN
|
||||
|
||||
python:
|
||||
version: 2
|
||||
setup_py_install: true
|
||||
|
||||
markdown_extensions:
|
||||
- admonition
|
||||
- smarty
|
||||
|
26
setup.py
Normal file
26
setup.py
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from distutils.core import setup
|
||||
|
||||
long_description = \
|
||||
"""This extension adds math formulas support to Python-Markdown_
|
||||
(works with version 2.6 or newer).
|
||||
|
||||
.. _Python-Markdown: https://github.com/waylan/Python-Markdown
|
||||
|
||||
You can find the source on GitHub_.
|
||||
Please refer to the `README file`_ for details on how to use it.
|
||||
|
||||
.. _GitHub: https://github.com/mitya57/python-markdown-math
|
||||
.. _`README file`: https://github.com/mitya57/python-markdown-math/blob/master/README.md
|
||||
"""
|
||||
|
||||
setup(name='python-markdown-math',
|
||||
description='Math extension for Python-Markdown',
|
||||
long_description=long_description,
|
||||
author='Dmitry Shachnev',
|
||||
author_email='mitya57@gmail.com',
|
||||
version='0.2',
|
||||
url='https://github.com/mitya57/python-markdown-math',
|
||||
py_modules=['mdx_math'],
|
||||
license='BSD')
|
Reference in New Issue
Block a user