Special-case use of HTML tags for converting <sub> / <sup> (#119)

Allow different strings before / after `<sub>` / `<sup>` content

In particular, this allows setting `sub_symbol='<sub>'`,
`sup_symbol='<sup>'`, to use raw HTML in the output when
converting subscripts and superscripts.
This commit is contained in:
Joseph Myers
2024-06-23 11:28:05 +00:00
committed by GitHub
parent 2ec33384de
commit 7861b330cd
3 changed files with 14 additions and 3 deletions

View File

@@ -87,7 +87,11 @@ strong_em_symbol
sub_symbol, sup_symbol sub_symbol, sup_symbol
Define the chars that surround ``<sub>`` and ``<sup>`` text. Defaults to an Define the chars that surround ``<sub>`` and ``<sup>`` text. Defaults to an
empty string, because this is non-standard behavior. Could be something like empty string, because this is non-standard behavior. Could be something like
``~`` and ``^`` to result in ``~sub~`` and ``^sup^``. ``~`` and ``^`` to result in ``~sub~`` and ``^sup^``. If the value starts
with ``<`` and ends with ``>``, it is treated as an HTML tag and a ``/`` is
inserted after the ``<`` in the string used after the text; this allows
specifying ``<sub>`` to use raw HTML in the output for subscripts, for
example.
newline_style newline_style
Defines the style of marking linebreaks (``<br>``) in markdown. The default Defines the style of marking linebreaks (``<br>``) in markdown. The default

View File

@@ -43,17 +43,22 @@ def abstract_inline_conversion(markup_fn):
""" """
This abstracts all simple inline tags like b, em, del, ... This abstracts all simple inline tags like b, em, del, ...
Returns a function that wraps the chomped text in a pair of the string Returns a function that wraps the chomped text in a pair of the string
that is returned by markup_fn. markup_fn is necessary to allow for that is returned by markup_fn, with '/' inserted in the string used after
the text if it looks like an HTML tag. markup_fn is necessary to allow for
references to self.strong_em_symbol etc. references to self.strong_em_symbol etc.
""" """
def implementation(self, el, text, convert_as_inline): def implementation(self, el, text, convert_as_inline):
markup = markup_fn(self) markup = markup_fn(self)
if markup.startswith('<') and markup.endswith('>'):
markup_after = '</' + markup[1:]
else:
markup_after = markup
if el.find_parent(['pre', 'code', 'kbd', 'samp']): if el.find_parent(['pre', 'code', 'kbd', 'samp']):
return text return text
prefix, suffix, text = chomp(text) prefix, suffix, text = chomp(text)
if not text: if not text:
return '' return ''
return '%s%s%s%s%s' % (prefix, markup, text, markup, suffix) return '%s%s%s%s%s' % (prefix, markup, text, markup_after, suffix)
return implementation return implementation

View File

@@ -268,11 +268,13 @@ def test_strong_em_symbol():
def test_sub(): def test_sub():
assert md('<sub>foo</sub>') == 'foo' assert md('<sub>foo</sub>') == 'foo'
assert md('<sub>foo</sub>', sub_symbol='~') == '~foo~' assert md('<sub>foo</sub>', sub_symbol='~') == '~foo~'
assert md('<sub>foo</sub>', sub_symbol='<sub>') == '<sub>foo</sub>'
def test_sup(): def test_sup():
assert md('<sup>foo</sup>') == 'foo' assert md('<sup>foo</sup>') == 'foo'
assert md('<sup>foo</sup>', sup_symbol='^') == '^foo^' assert md('<sup>foo</sup>', sup_symbol='^') == '^foo^'
assert md('<sup>foo</sup>', sup_symbol='<sup>') == '<sup>foo</sup>'
def test_lang(): def test_lang():