Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ table_infer_header
or ``<th>``). When set to ``True``, the first body row is used as the header row.
Defaults to ``False``, which leaves the header row empty.

task_lists
Preserve checkbox inputs inside list items as GFM task markers, ``[x]`` for
checked and ``[ ]`` for unchecked. Defaults to ``False``. Enable with
``markdownify(html, task_lists=True)`` or ``markdownify --task-lists``.
As in HTML, any present ``checked`` attribute means checked, including
``checked="false"``. Other input types and checkboxes outside list items
are unaffected. GFM renderers recognize task markers at the start of an item.

wrap, wrap_width
If ``wrap`` is set to ``True``, all text paragraphs are wrapped at
``wrap_width`` characters. Defaults to ``False`` and ``80``.
Expand Down
17 changes: 17 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class DefaultOptions:
sub_symbol = ''
sup_symbol = ''
table_infer_header = False
task_lists = False
wrap = False
wrap_width = 80

Expand Down Expand Up @@ -591,6 +592,22 @@ def convert_img(self, el, text, parent_tags):

return '![%s](%s%s)' % (alt, src, title_part)

def convert_input(self, el, text, parent_tags):
if (not self.options['task_lists']
or (el.get('type') or '').lower() != 'checkbox'
or 'li' not in parent_tags
or '_noformat' in parent_tags):
return text

marker = '[x]' if el.has_attr('checked') else '[ ]'
# Reuse existing whitespace, including after a wrapping label.
following = el
while following.next_sibling is None and following.parent.name != 'li':
following = following.parent
following = following.next_sibling
separator = '' if isinstance(following, NavigableString) and str(following)[:1].isspace() else ' '
return marker + separator

def convert_video(self, el, text, parent_tags):
if ('_inline' in parent_tags
and el.parent.name not in self.options['keep_inline_images_in']):
Expand Down
2 changes: 2 additions & 0 deletions markdownify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def main(argv=sys.argv[1:]):
parser.add_argument('-w', '--wrap', action='store_true',
help="Wrap all text paragraphs at --wrap-width characters.")
parser.add_argument('--wrap-width', type=int, default=80)
parser.add_argument('--task-lists', action='store_true',
help="Preserve checkbox states in list items using GFM task markers.")
parser.add_argument('--bs4-options',
default='html.parser',
help="Specifies the parser that BeautifulSoup should use to parse "
Expand Down
52 changes: 52 additions & 0 deletions tests/test_task_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import io

import pytest

from markdownify import markdownify
from markdownify.main import main


@pytest.mark.parametrize('space', ['', ' '])
def test_checked_and_unchecked_tasks(space):
html = ('<ul><li><input type="checkbox" checked>' + space + 'Pay rent</li>'
'<li><input type="checkbox">' + space + 'Renew passport</li></ul>')
assert markdownify(html, task_lists=True) == (
'* [x] Pay rent\n* [ ] Renew passport'
)


def test_task_checkbox_with_label_markup_and_nested_list():
html = ('<ol><li><label><input type="CHECKBOX" checked="false">'
'<strong>Done</strong></label><ul><li><input type="checkbox">'
'Child</li></ul></li></ol>')
assert markdownify(html, task_lists=True) == (
'1. [x] **Done**\n * [ ] Child'
)


def test_standalone_empty_task_keeps_state():
assert markdownify('<ul><li><input type=checkbox></li></ul>', task_lists=True) == '* [ ]'


def test_task_option_defaults_to_disabled_and_respects_strip():
html = '<ul><li><input type=checkbox checked> Done</li></ul>'
assert markdownify(html) == '* Done'
assert markdownify(html, task_lists=True, strip=['input']) == '* Done'


def test_other_inputs_and_non_list_checkboxes_are_unchanged():
assert markdownify('<p><input type=checkbox checked>Text</p>', task_lists=True) == 'Text'
assert markdownify('<ul><li><input type=text value=test>Text</li></ul>', task_lists=True) == '* Text'


def test_label_boundary_and_code_context():
html = '<ul><li><label><input type=checkbox checked></label> Done</li></ul>'
assert markdownify(html, task_lists=True) == '* [x] Done'
html = '<ul><li><code><input type=checkbox>Code</code></li></ul>'
assert markdownify(html, task_lists=True) == '* `Code`'


def test_task_lists_cli(monkeypatch, capsys):
monkeypatch.setattr('sys.stdin', io.StringIO('<ul><li><input type=checkbox>Task</li></ul>'))
main(['--task-lists'])
assert capsys.readouterr().out == '* [ ] Task\n'