Skip to content

Commit 1783995

Browse files
authored
Merge pull request #173 from chrispy-snps/chrispy/support-definition-lists
support HTML definition lists (`<dl>`, `<dt>`, and `<dd>`)
2 parents f73a435 + 0fb8556 commit 1783995

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Diff for: markdownify/__init__.py

+32
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,38 @@ def convert_code(self, el, text, convert_as_inline):
319319

320320
convert_kbd = convert_code
321321

322+
def convert_dd(self, el, text, convert_as_inline):
323+
text = (text or '').strip()
324+
if convert_as_inline:
325+
return ' ' + text + ' '
326+
if not text:
327+
return '\n'
328+
329+
# indent definition content lines by four spaces
330+
def _indent_for_dd(match):
331+
line_content = match.group(1)
332+
return ' ' + line_content if line_content else ''
333+
text = line_with_content_re.sub(_indent_for_dd, text)
334+
335+
# insert definition marker into first-line indent whitespace
336+
text = ':' + text[1:]
337+
338+
return '%s\n' % text
339+
340+
def convert_dt(self, el, text, convert_as_inline):
341+
# remove newlines from term text
342+
text = (text or '').strip()
343+
text = all_whitespace_re.sub(' ', text)
344+
if convert_as_inline:
345+
return ' ' + text + ' '
346+
if not text:
347+
return '\n'
348+
349+
# TODO - format consecutive <dt> elements as directly adjacent lines):
350+
# https://michelf.ca/projects/php-markdown/extra/#def-list
351+
352+
return '\n%s\n' % text
353+
322354
def _convert_hn(self, n, el, text, convert_as_inline):
323355
""" Method name prefixed with _ to prevent <hn> to call this """
324356
if convert_as_inline:

Diff for: tests/test_conversions.py

+10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ def test_code():
104104
assert md('<code>foo<sub>bar</sub>baz</code>', sub_symbol='^') == '`foobarbaz`'
105105

106106

107+
def test_dl():
108+
assert md('<dl><dt>term</dt><dd>definition</dd></dl>') == '\nterm\n: definition\n'
109+
assert md('<dl><dt><p>te</p><p>rm</p></dt><dd>definition</dd></dl>') == '\nte rm\n: definition\n'
110+
assert md('<dl><dt>term</dt><dd><p>definition-p1</p><p>definition-p2</p></dd></dl>') == '\nterm\n: definition-p1\n\n definition-p2\n'
111+
assert md('<dl><dt>term</dt><dd><p>definition 1</p></dd><dd><p>definition 2</p></dd></dl>') == '\nterm\n: definition 1\n: definition 2\n'
112+
assert md('<dl><dt>term 1</dt><dd>definition 1</dd><dt>term 2</dt><dd>definition 2</dd></dl>') == '\nterm 1\n: definition 1\nterm 2\n: definition 2\n'
113+
assert md('<dl><dt>term</dt><dd><blockquote><p>line 1</p><p>line 2</p></blockquote></dd></dl>') == '\nterm\n: > line 1\n >\n > line 2\n'
114+
assert md('<dl><dt>term</dt><dd><ol><li><p>1</p><ul><li>2a</li><li>2b</li></ul></li><li><p>3</p></li></ol></dd></dl>') == '\nterm\n: 1. 1\n\n * 2a\n * 2b\n 2. 3\n'
115+
116+
107117
def test_del():
108118
inline_tests('del', '~~')
109119

0 commit comments

Comments
 (0)