Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error computing includer indentation #124

Merged
merged 3 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .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]

Expand Down
5 changes: 5 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions examples/include-to-list-item/mkdocs.yml
@@ -0,0 +1,3 @@
site_name: Foo
plugins:
- include-markdown
2 changes: 1 addition & 1 deletion mkdocs_include_markdown_plugin/__init__.py
@@ -1,2 +1,2 @@
__title__ = 'mkdocs_include_markdown_plugin'
__version__ = '3.8.0'
__version__ = '3.8.1'
65 changes: 39 additions & 26 deletions mkdocs_include_markdown_plugin/event.py
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import re
import string
import textwrap

from mkdocs_include_markdown_plugin import process
Expand Down Expand Up @@ -32,15 +33,15 @@
# `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+
(?:"(?P<double_quoted_filename>{DOUBLE_QUOTED_STR_ARGUMENT_PATTERN})")?(?:'(?P<single_quoted_filename>{SINGLE_QUOTED_STR_ARGUMENT_PATTERN})')?
(?P<arguments>.*?)
\s*
$CLOSING_TAG
''',
''', # noqa: E501
flags=re.VERBOSE | re.DOTALL,
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -536,14 +541,21 @@ 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
#
# `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)

Expand Down Expand Up @@ -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'<!-- BEGIN INCLUDE {html.escape(filename)}'
f' {start_end_part}-->{separator}{new_text_to_include}'
f'{separator}<!-- END INCLUDE -->'
)
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(
Expand Down Expand Up @@ -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}<!-- BEGIN INCLUDE {html.escape(filename)}'
f' {start_end_part}-->{separator}{text_to_include}'
f'{separator}{_includer_indent}<!-- END INCLUDE -->'
)
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(
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
42 changes: 40 additions & 2 deletions tests/test_include_markdown.py
Expand Up @@ -678,7 +678,7 @@
''',
'Content to include\n',
'''1. List item number 1
1. <!-- BEGIN INCLUDE {filepath} --> Content to include <!-- END INCLUDE -->
1. <!-- BEGIN INCLUDE {filepath} -->Content to include<!-- END INCLUDE -->
1. List item number 3
''',
[],
Expand All @@ -695,7 +695,7 @@
''',
'Content to include\n',
'''1. List item number 1
1. <!-- BEGIN INCLUDE {filepath} --> Content to include <!-- END INCLUDE -->
1. <!-- BEGIN INCLUDE {filepath} -->Content to include<!-- END INCLUDE -->
1. List item number 3
''',
[],
Expand All @@ -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. <!-- BEGIN INCLUDE {filepath} -->
This content chunk contains code
''' + ' ' + '''
```
This is my example
It is a code block
```
''' + ' ' + '''
With some text after it
''' + ' ' + '''
<!-- END INCLUDE -->

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(
Expand Down