Skip to content

Commit

Permalink
new-feature: support using "$" as math quote
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Feb 5, 2022
1 parent 6246062 commit e92fdfe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
26 changes: 14 additions & 12 deletions md2zhihu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def join_math_block(nodes):
join_math_text(nodes)


def parse_math(nodes):
def parse_math(conf, nodes):
"""
Extract all math segment such as ``$$ ... $$`` from a text and build a
math_block or math_inline node.
Expand All @@ -466,10 +466,10 @@ def parse_math(nodes):
for n in nodes:

if 'children' in n:
n['children'] = parse_math(n['children'])
n['children'] = parse_math(conf, n['children'])

if n['type'] == 'text':
new_children = extract_math(n)
new_children = extract_math(conf, n)
children.extend(new_children)
else:
children.append(n)
Expand Down Expand Up @@ -502,22 +502,24 @@ def join_math_text(nodes):
i += 1


inline_math = r'\$\$(.*?)\$\$'


def extract_math(n):
def extract_math(conf, n):
"""
Extract ``$$ ... $$`` from a text node and build a new node.
Extract ``$$ ... $$`` or ``$ .. $` from a text node and build a new node.
The original text node is split into multiple segments.
The math is a block if it is a paragraph.
Otherwise, it is an inline math.
"""
children = []

math_regex = r'([$]|[$][$])([^$].*?)\1'

t = n['text']
while True:
match = re.search(inline_math, t, flags=re.DOTALL)
match = re.search(math_regex, t, flags=re.DOTALL)
if match:
children.append({'type': 'text', 'text': t[:match.start()]})
children.append({'type': 'math_inline', 'text': match.groups()[0]})
children.append({'type': 'math_inline', 'text': match.groups()[1]})
t = t[match.end():]

left = children[-2]['text']
Expand Down Expand Up @@ -1148,14 +1150,14 @@ def convert_md(conf, handler=None):
used_refs = replace_ref_with_def(ast, refs)

# extract already inlined math
ast = parse_math(ast)
ast = parse_math(conf, ast)

# with open('after-math-1', 'w') as f:
# f.write(pprint.pformat(ast))

# join cross paragraph math
join_math_block(ast)
ast = parse_math(ast)
ast = parse_math(conf, ast)

# with open('after-math-2', 'w') as f:
# f.write(pprint.pformat(ast))
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ python setup.py install
)

# PYTHONPATH="$(cd ..; pwd)" pytest -x -v
PYTHONPATH="$(cd ..; pwd)" pytest -x -v -k test_minimal_mistake
PYTHONPATH="$(cd ..; pwd)" pytest -x -v -k test_zhihu_math_inline
16 changes: 16 additions & 0 deletions test/data/zhihu-math-inline/src/simple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
inline math: $ x $ 哈哈

inline math: $$
y
$$
inline math: $
y
$
block math:
$$
z
$$
14 changes: 14 additions & 0 deletions test/data/zhihu-math-inline/want/simple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
inline math: <img src="https://www.zhihu.com/equation?tex=%20x%20" alt=" x " class="ee_img tr_noresize" eeimg="1"> 哈哈

inline math: <img src="https://www.zhihu.com/equation?tex=y" alt="y" class="ee_img tr_noresize" eeimg="1">

inline math: <img src="https://www.zhihu.com/equation?tex=y" alt="y" class="ee_img tr_noresize" eeimg="1">

block math:

<img src="https://www.zhihu.com/equation?tex=z%5C%5C" alt="z\\" class="ee_img tr_noresize" eeimg="1">



Reference:

3 changes: 3 additions & 0 deletions test/test_md2zhihu.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ def test_zhihu_extrefs(self):
def test_zhihu_rewrite(self):
self._test_platform_no_push('zhihu-rewrite', 'simple.md', ['--rewrite', '^s', '/foo/'])

def test_zhihu_math_inline(self):
self._test_platform_no_push('zhihu-math-inline', 'simple.md', [])

def test_zhihu_jekyll(self):
self._test_platform_no_push('zhihu-jekyll', '2021-06-11-simple.md', ['--jekyll'])

Expand Down

0 comments on commit e92fdfe

Please sign in to comment.