diff --git a/.bumpversion.cfg b/.bumpversion.cfg index b722d5d..c422629 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 3.8.0 +current_version = 3.8.1 [bumpversion:file:mkdocs_include_markdown_plugin/__init__.py] diff --git a/examples/include-to-list-item/docs/index.md b/examples/include-to-list-item/docs/index.md new file mode 100644 index 0000000..242ff3b --- /dev/null +++ b/examples/include-to-list-item/docs/index.md @@ -0,0 +1,5 @@ +1. This is the first number line + +1. {% include-markdown "../included.md" %} + +1. If everything works as expected this should be number 3 diff --git a/examples/include-to-list-item/included.md b/examples/include-to-list-item/included.md new file mode 100644 index 0000000..8dce42b --- /dev/null +++ b/examples/include-to-list-item/included.md @@ -0,0 +1,8 @@ +This content chunk contains code + +``` +This is my example +It is a code block +``` + +With some text after it diff --git a/examples/include-to-list-item/mkdocs.yml b/examples/include-to-list-item/mkdocs.yml new file mode 100644 index 0000000..0f97266 --- /dev/null +++ b/examples/include-to-list-item/mkdocs.yml @@ -0,0 +1,3 @@ +site_name: Foo +plugins: + - include-markdown diff --git a/mkdocs_include_markdown_plugin/__init__.py b/mkdocs_include_markdown_plugin/__init__.py index 345f426..9b44e07 100644 --- a/mkdocs_include_markdown_plugin/__init__.py +++ b/mkdocs_include_markdown_plugin/__init__.py @@ -1,2 +1,2 @@ __title__ = 'mkdocs_include_markdown_plugin' -__version__ = '3.8.0' +__version__ = '3.8.1' diff --git a/mkdocs_include_markdown_plugin/event.py b/mkdocs_include_markdown_plugin/event.py index 11b06fa..a127215 100644 --- a/mkdocs_include_markdown_plugin/event.py +++ b/mkdocs_include_markdown_plugin/event.py @@ -5,6 +5,7 @@ import logging import os import re +import string import textwrap from mkdocs_include_markdown_plugin import process @@ -32,7 +33,7 @@ # `on_page_markdown` method below. INCLUDE_TAG_REGEX = re.compile( rf''' - (?P<_includer_indent>[^\S\r\n]*)$OPENING_TAG + (?P<_includer_indent>[ \t\f\v\w{re.escape(string.punctuation)}]*?)$OPENING_TAG \s* include \s+ @@ -40,7 +41,7 @@ (?P.*?) \s* $CLOSING_TAG - ''', + ''', # noqa: E501 flags=re.VERBOSE | re.DOTALL, ) @@ -319,14 +320,17 @@ def found_include_tag(match): new_text_to_include, ) - if bool_options['dedent']: + if bool_options['dedent']['value']: new_text_to_include = textwrap.dedent(new_text_to_include) # includer indentation preservation if bool_options['preserve-includer-indent']['value']: new_text_to_include = ''.join( _includer_indent + line - for line in new_text_to_include.splitlines(keepends=True) + for line in ( + new_text_to_include.splitlines(keepends=True) + or [''] + ) ) else: new_text_to_include = _includer_indent + new_text_to_include @@ -359,6 +363,7 @@ def found_include_markdown_tag(match): directive_match_start = match.start() _includer_indent = match.group('_includer_indent') + _empty_includer_indent = ' ' * len(_includer_indent) filename, raw_filename = parse_filename_argument(match) if filename is None: @@ -536,7 +541,12 @@ def found_include_markdown_tag(match): if offset_match: offset += int(offset_match.group(1)) - text_to_include = '' + separator = '\n' if bool_options['trailing-newlines']['value'] else '' + if not start and not end: + start_end_part = '' + else: + start_end_part = f"'{html.escape(start)}' " if start else "'' " + start_end_part += f"'{html.escape(end)}' " if end else "'' " # if any start or end strings are found in the included content # but the arguments are specified, we must raise a warning @@ -544,6 +554,8 @@ def found_include_markdown_tag(match): # `True` means that no start/end strings have been found in content # but they have been specified, so the warning(s) must be raised expected_but_any_found = [start is not None, end is not None] + + text_to_include = '' for file_path in file_paths_to_include: new_text_to_include = read_file(file_path, encoding) @@ -587,18 +599,32 @@ def found_include_markdown_tag(match): destination_path=page_src_path, ) + # comments + if bool_options['comments']['value']: + new_text_to_include = ( + f'{_includer_indent}' + f'{separator}{new_text_to_include}' + f'{separator}' + ) + else: + new_text_to_include = ( + f'{_includer_indent}{new_text_to_include}' + ) + # dedent - if bool_options['dedent']: + if bool_options['dedent']['value']: new_text_to_include = textwrap.dedent(new_text_to_include) # includer indentation preservation if bool_options['preserve-includer-indent']['value']: new_text_to_include = ''.join( - _includer_indent + line - for line in new_text_to_include.splitlines(keepends=True) + (_empty_includer_indent if i > 0 else '') + line + for i, line in enumerate( + new_text_to_include.splitlines(keepends=True) + or [''], + ) ) - else: - new_text_to_include = _includer_indent + new_text_to_include if offset_match: new_text_to_include = process.increase_headings_offset( @@ -628,32 +654,19 @@ def found_include_markdown_tag(match): f' {readable_files_to_include}', ) - if not bool_options['comments']['value']: - return text_to_include - - separator = '\n' if bool_options['trailing-newlines']['value'] else '' - if not start and not end: - start_end_part = '' - else: - start_end_part = f"'{html.escape(start)}' " if start else "'' " - start_end_part += f"'{html.escape(end)}' " if end else "'' " - - return ( - f'{_includer_indent}{separator}{text_to_include}' - f'{separator}{_includer_indent}' - ) + return text_to_include markdown = re.sub( include_tag_regex, found_include_tag, markdown, ) - return re.sub( + markdown = re.sub( include_markdown_tag_regex, found_include_markdown_tag, markdown, ) + return markdown def on_page_markdown( diff --git a/setup.cfg b/setup.cfg index 88aded5..ddcacf9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = mkdocs_include_markdown_plugin -version = 3.8.0 +version = 3.8.1 description = Mkdocs Markdown includer plugin. long_description = file: README.md long_description_content_type = text/markdown diff --git a/tests/test_include_markdown.py b/tests/test_include_markdown.py index 4a04bbe..83f562a 100644 --- a/tests/test_include_markdown.py +++ b/tests/test_include_markdown.py @@ -678,7 +678,7 @@ ''', 'Content to include\n', '''1. List item number 1 -1. Content to include +1. Content to include 1. List item number 3 ''', [], @@ -695,7 +695,7 @@ ''', 'Content to include\n', '''1. List item number 1 -1. Content to include +1. Content to include 1. List item number 3 ''', [], @@ -720,6 +720,44 @@ [], id='escape-comments', ), + + pytest.param( + '''1. This is the first number line + +1. {% + include-markdown "{filepath}" + %} + +1. If everything works as expected this should be number 3 +''', + '''This content chunk contains code + +``` +This is my example +It is a code block +``` + +With some text after it +''', + '''1. This is the first number line + +1. + This content chunk contains code +''' + ' ' + ''' + ``` + This is my example + It is a code block + ``` +''' + ' ' + ''' + With some text after it +''' + ' ' + ''' + + +1. If everything works as expected this should be number 3 +''', # noqa: ISC003 + [], + id='include-code-block-to-list-item (#123)', + ), ), ) def test_include_markdown(