diff --git a/README.rst b/README.rst index 059a68f..1388f74 100644 --- a/README.rst +++ b/README.rst @@ -144,6 +144,14 @@ table_infer_header or ````). 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``. diff --git a/markdownify/__init__.py b/markdownify/__init__.py index d946d2c..fe8cbe8 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -196,6 +196,7 @@ class DefaultOptions: sub_symbol = '' sup_symbol = '' table_infer_header = False + task_lists = False wrap = False wrap_width = 80 @@ -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']): diff --git a/markdownify/main.py b/markdownify/main.py index ba70671..d33168d 100755 --- a/markdownify/main.py +++ b/markdownify/main.py @@ -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 " diff --git a/tests/test_task_lists.py b/tests/test_task_lists.py new file mode 100644 index 0000000..4e68b42 --- /dev/null +++ b/tests/test_task_lists.py @@ -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 = ('') + assert markdownify(html, task_lists=True) == ( + '* [x] Pay rent\n* [ ] Renew passport' + ) + + +def test_task_checkbox_with_label_markup_and_nested_list(): + html = ('
') + assert markdownify(html, task_lists=True) == ( + '1. [x] **Done**\n * [ ] Child' + ) + + +def test_standalone_empty_task_keeps_state(): + assert markdownify('', task_lists=True) == '* [ ]' + + +def test_task_option_defaults_to_disabled_and_respects_strip(): + html = '' + 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('

Text

', task_lists=True) == 'Text' + assert markdownify('', task_lists=True) == '* Text' + + +def test_label_boundary_and_code_context(): + html = '' + assert markdownify(html, task_lists=True) == '* [x] Done' + html = '' + assert markdownify(html, task_lists=True) == '* `Code`' + + +def test_task_lists_cli(monkeypatch, capsys): + monkeypatch.setattr('sys.stdin', io.StringIO('')) + main(['--task-lists']) + assert capsys.readouterr().out == '* [ ] Task\n'