miyuchina / mistletoe

A fast, extensible and spec-compliant Markdown parser in pure Python.
MIT License
841 stars 119 forks source link

Update render inline code #227

Open choeppler opened 1 month ago

choeppler commented 1 month ago

Currently \verb is used to render inline code. However, that has some downsides as it does not work in moving arguments or within macros like \multicolumn, and it doesn't break long inline code lines. This PR changes the implementation to use \texttt, which fixes those issues.

The PR provides three patches which can be integration tested with the following script to create test cases as described below:

#!/bin/env python3

import mistletoe
from mistletoe.latex_renderer import LaTeXRenderer

def render(f):
   with open(f, 'r') as fp:
     return mistletoe.markdown(fp, LaTeXRenderer)

# test case that fails for main but is fixed by 866bc2f35a95f18536a1626ec3f4d42b1209b939
test_0 = """
~~strikethrough~~
"""
# test case that fails for main but is fixed by 1fba4806a682d3edd0eb0dbe9e103784ac7e94f4
test_1 = """
this is the set of LaTeX escape chars: $ # { } & _ % \\ ~ ^
"""
# test case to check whether 4a97c287f0192f968c107d3cf428e78d4804e1d7 still renders inline code as expected
test_2 = """
line `inline code` line

line `inline  code with two spaces` line

line `inline\tcode with tab` line

line `inline\t\tcode with two tabs` line

line `inline  code with\nnewline` line 

line without inline code which would require a linebreak because it is simply way too long to fit.

line `inline code which would require a linebreak because it is simply way too long to fit`.

this is the set of LaTeX escape chars in inline code: `$ # { } & _ % \\ ~ ^`

"""
for test, name in zip([test_0, test_1, test_2], ['test_0', 'test_1', 'test_2']):
  open('%s.md'%name, 'w').write(test)
  open('%s.tex'%name, 'w').write(render('%s.md'%name))
coveralls commented 1 month ago

Coverage Status

coverage: 94.201% (-0.006%) from 94.207% when pulling 4a97c287f0192f968c107d3cf428e78d4804e1d7 on boschresearch:fix-render-inline-code into a40cd2fceb4c4f8193aa9b227e4906b01891439a on miyuchina:master.