add inline math

This commit is contained in:
tqchen 2015-08-23 17:10:48 -07:00
parent bc2e426b47
commit a0d5059846
3 changed files with 56 additions and 2 deletions

View file

@ -28,6 +28,7 @@ All the features are by default enabled
* auto_toc_tree_section: when setted, [Auto Toc Tree](#auto-toc-tree) will only be enabled on section that matches the title.
* enable_auto_doc_ref: whether enable [Auto Doc Ref](#auto-doc-ref) feature.
* enable_math: whether eanble [Math Formula](#math-formula)
* enable_inline_math: whether eanble [Inline Math](#inline-math)
* enable_eval_rst: whether [Embed reStructuredText](#embed-restructuredtext) is enabled.
* url_resolver: a function that maps a existing relative position in the document to a http link
@ -107,7 +108,7 @@ def function():
```
### Math Formula
You can normally write latex math formula with ```math``` codeblock.
You can normally write latex math formula with ```math``` codeblock. See also [Inline Math](#inline-math).
Example
```
@ -137,3 +138,20 @@ will be rendered as
:show-inheritance:
```
This example used to use sphinx autodoc to insert document of AutoStructify class definition into the document.
Inline Math
-----------
Besides the [Math Formula](#math-formula), you can also write latex math in inline codeblock text. You can do so by inserting ```$```
in the beginning and end of inline codeblock.
Example
```
This formula ``$ y=\sum_{i=1}^n g(x_i) $``
```
will be rendered as:
This formula ``$ y=\sum_{i=1}^n g(x_i) $``

View file

@ -13,7 +13,9 @@ Contents
Getting Started
---------------
This document is actually generated by recommonmark, and its source code can be used as a good reference.
To see what you can get beyond the normal markdown syntax, checkout [AutoStructify Component](auto_structify.md).
[AutoStructify Component](auto_structify.md) provides a lot of additional features beyond markdown,
including reference generation, syntax hightlight and math formula.
Why a bridge?
-------------

View file

@ -20,6 +20,7 @@ class AutoStructify(transforms.Transform):
'enable_auto_toc_tree': True,
'enable_eval_rst': True,
'enable_math': True,
'enable_inline_math': True,
'url_resolver': lambda x: x,
}
@ -175,6 +176,37 @@ class AutoStructify(transforms.Transform):
node['refuri'] = uri
return None
def auto_inline_code(self, node):
"""Try to automatically generate nodes for inline literals.
Parameters
----------
node : nodes.literal
Original codeblock node
Returns
-------
tocnode: docutils node
The converted toc tree node, None if conversion is not possible.
"""
assert isinstance(node, nodes.literal)
if len(node.children) != 1:
return None
content = node.children[0]
if not isinstance(content, nodes.Text):
return None
content = content.astext().strip()
if content.startswith('$') and content.endswith('$'):
if not self.config['enable_inline_math']:
return None
content = content[1:-1]
self.state_machine.reset(self.document,
node.parent,
self.current_level)
return self.state_machine.run_role('math', content=content)
else:
return None
def auto_code_block(self, node):
"""Try to automatically generate nodes for codeblock syntax.
@ -234,6 +266,8 @@ class AutoStructify(transforms.Transform):
newnode = self.auto_doc_ref(node)
elif isinstance(node, nodes.literal_block):
newnode = self.auto_code_block(node)
elif isinstance(node, nodes.literal):
newnode = self.auto_inline_code(node)
return newnode
def traverse(self, node):