Skip to content

Commit 7861b33

Browse files
authored
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.
1 parent 2ec3338 commit 7861b33

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ strong_em_symbol
8787
sub_symbol, sup_symbol
8888
Define the chars that surround ``<sub>`` and ``<sup>`` text. Defaults to an
8989
empty string, because this is non-standard behavior. Could be something like
90-
``~`` and ``^`` to result in ``~sub~`` and ``^sup^``.
90+
``~`` and ``^`` to result in ``~sub~`` and ``^sup^``. If the value starts
91+
with ``<`` and ends with ``>``, it is treated as an HTML tag and a ``/`` is
92+
inserted after the ``<`` in the string used after the text; this allows
93+
specifying ``<sub>`` to use raw HTML in the output for subscripts, for
94+
example.
9195

9296
newline_style
9397
Defines the style of marking linebreaks (``<br>``) in markdown. The default

markdownify/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,22 @@ def abstract_inline_conversion(markup_fn):
4343
"""
4444
This abstracts all simple inline tags like b, em, del, ...
4545
Returns a function that wraps the chomped text in a pair of the string
46-
that is returned by markup_fn. markup_fn is necessary to allow for
46+
that is returned by markup_fn, with '/' inserted in the string used after
47+
the text if it looks like an HTML tag. markup_fn is necessary to allow for
4748
references to self.strong_em_symbol etc.
4849
"""
4950
def implementation(self, el, text, convert_as_inline):
5051
markup = markup_fn(self)
52+
if markup.startswith('<') and markup.endswith('>'):
53+
markup_after = '</' + markup[1:]
54+
else:
55+
markup_after = markup
5156
if el.find_parent(['pre', 'code', 'kbd', 'samp']):
5257
return text
5358
prefix, suffix, text = chomp(text)
5459
if not text:
5560
return ''
56-
return '%s%s%s%s%s' % (prefix, markup, text, markup, suffix)
61+
return '%s%s%s%s%s' % (prefix, markup, text, markup_after, suffix)
5762
return implementation
5863

5964

tests/test_conversions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,13 @@ def test_strong_em_symbol():
268268
def test_sub():
269269
assert md('<sub>foo</sub>') == 'foo'
270270
assert md('<sub>foo</sub>', sub_symbol='~') == '~foo~'
271+
assert md('<sub>foo</sub>', sub_symbol='<sub>') == '<sub>foo</sub>'
271272

272273

273274
def test_sup():
274275
assert md('<sup>foo</sup>') == 'foo'
275276
assert md('<sup>foo</sup>', sup_symbol='^') == '^foo^'
277+
assert md('<sup>foo</sup>', sup_symbol='<sup>') == '<sup>foo</sup>'
276278

277279

278280
def test_lang():

0 commit comments

Comments
 (0)