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 warnings not shown sometimes with missing end and start delimiters #177

Merged
merged 3 commits into from
Aug 31, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
python-version: "3.11"
py: py311
- platform: ubuntu-latest
python-version: 3.12.0-beta.4
python-version: 3.12.0-rc.1
py: py312
- platform: macos-latest
python-version: 3.8
Expand All @@ -53,7 +53,7 @@ jobs:
python-version: "3.11"
py: py311
- platform: macos-latest
python-version: 3.12.0-beta.4
python-version: 3.12.0-rc.1
py: py312
- platform: windows-latest
python-version: 3.8
Expand All @@ -62,7 +62,7 @@ jobs:
python-version: "3.11"
py: py311
- platform: windows-latest
python-version: 3.12.0-beta.4
python-version: 3.12.0-rc.1
py: py312
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mkdocs-include-markdown-plugin"
version = "6.0.0"
version = "6.0.1"
description = "Mkdocs Markdown includer plugin."
readme = "README.md"
license = "Apache-2.0"
Expand Down
72 changes: 36 additions & 36 deletions src/mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ def interpret_escapes(value: str) -> str:


def filter_inclusions(
new_start: str | None,
new_end: str | None,
start: str | None,
end: str | None,
text_to_include: str,
) -> tuple[str, bool, bool]:
"""Filter inclusions in a text.
Expand All @@ -294,48 +294,48 @@ def filter_inclusions(
arguments.
"""
expected_start_not_found, expected_end_not_found = (False, False)
new_text_to_include = ''

if new_start is not None:
start = interpret_escapes(new_start)
end = interpret_escapes(new_end) if new_end is not None else None

new_text_to_include = ''

if end is not None:
end_found = False
start_split = text_to_include.split(start)[1:]
if not start_split:
expected_start_not_found = True
else:
for start_text in start_split:
for i, end_text in enumerate(start_text.split(end)):
if not i % 2:
new_text_to_include += end_text
end_found = True
if not end_found:
expected_end_not_found = True
if start is not None and end is None:
start = interpret_escapes(start)
if start not in text_to_include:
expected_start_not_found = True
else:
if start in text_to_include:
new_text_to_include = text_to_include.split(
start,
maxsplit=1,
)[1]
else:
expected_start_not_found = True
text_to_include = new_text_to_include

elif new_end is not None: # pragma: no branch
end = interpret_escapes(new_end)
if end in text_to_include:
text_to_include = text_to_include.split(
new_text_to_include = text_to_include.split(
start,
maxsplit=1,
)[1]
elif start is None and end is not None:
end = interpret_escapes(end)
if end not in text_to_include:
expected_end_not_found = True
new_text_to_include = text_to_include
else:
new_text_to_include = text_to_include.split(
end,
maxsplit=1,
)[0]
else:
elif start is not None and end is not None:
start, end = interpret_escapes(start), interpret_escapes(end)
if start not in text_to_include:
expected_start_not_found = True
if end not in text_to_include:
expected_end_not_found = True

text_parts = (
text_to_include.split(start)[1:]
if start in text_to_include else [text_to_include]
)

for start_text in text_parts:
for i, end_text in enumerate(start_text.split(end)):
if not i % 2:
new_text_to_include += end_text
else: # pragma: no cover
new_text_to_include = text_to_include

return (
text_to_include,
new_text_to_include,
expected_start_not_found,
expected_end_not_found,
)
Expand Down
18 changes: 9 additions & 9 deletions tests/test_unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ def _run_test(
caplog,
tmp_path,
):
included_filepath = tmp_path / 'included.md'
includer_filepath = tmp_path / 'includer.md'
included_file = tmp_path / 'included.md'
includer_file = tmp_path / 'includer.md'

included_filepath.write_text(content_to_include)
includer_filepath.write_text(
content_to_include.replace('{filepath}', included_filepath.as_posix()),
included_file.write_text(content_to_include)
includer_file.write_text(
content_to_include.replace('{filepath}', included_file.as_posix()),
)

# assert content
page_content = includer_schema.replace(
'{filepath}',
included_filepath.as_posix(),
included_file.as_posix(),
)
includer_filepath.write_text(page_content)
includer_file.write_text(page_content)

expected_result = expected_result.replace(
'{filepath}',
included_filepath.as_posix(),
included_file.as_posix(),
)

assert (
on_page_markdown(
page_content,
page(includer_filepath),
page(includer_file),
tmp_path,
config,
)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_unit/test_exclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ def test_exclude(
}

exclude_prefix = f'{tmp_path}{os.sep}' if exclude_prefix else ''
includer_filepath_content = f'''{{%
includer_file_content = f'''{{%
{directive} "{tmp_path}{os.sep}content/*"
exclude='{exclude_prefix}{exclude}'
comments=false
%}}'''
for basename, file in files.items():
file.write_text(f'{basename}\n')

includer_file.write_text(includer_filepath_content)
includer_file.write_text(includer_file_content)

func = functools.partial(
on_page_markdown,
includer_filepath_content,
includer_file_content,
page(includer_file),
includer_folder,
)
Expand Down
45 changes: 8 additions & 37 deletions tests/test_unit/test_glob_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_glob_include_absolute(page, tmp_path):
included_01_file = tmp_path / 'included_01.txt'
included_02_file = tmp_path / 'included_02.txt'

includer_filepath_content = f'''foo
includer_file_content = f'''foo

{{%
include "./included*.txt"
Expand All @@ -29,7 +29,7 @@ def test_glob_include_absolute(page, tmp_path):
included_01_content = 'bar'
included_02_content = 'baz'

includer_file.write_text(includer_filepath_content)
includer_file.write_text(includer_file_content)
included_01_file.write_text(included_01_content)
included_02_file.write_text(included_02_content)

Expand All @@ -42,15 +42,14 @@ def test_glob_include_absolute(page, tmp_path):
'''

assert on_page_markdown(
includer_filepath_content, page(includer_file), tmp_path,
includer_file_content, page(includer_file), tmp_path,
) == expected_result


@parametrize_directives
@pytest.mark.parametrize(
(
'includer_content',
'expected_result',
'expected_warnings_schemas',
),
(
Expand All @@ -68,14 +67,6 @@ def test_glob_include_absolute(page, tmp_path):
end="<!-- end-1 -->"
comments=false
%}
''',
'''
baz



bar

''',
[],
id='start-end',
Expand All @@ -86,18 +77,6 @@ def test_glob_include_absolute(page, tmp_path):
end="<!-- end-2 -->"
comments=false
%}
''',
# if aren't both``end`` and ``start`` specified, produces
# a strange but expected output
'''This 01 must appear only without specifying start.
<!-- start-1 -->
bar
<!-- end-1 -->
This 01 must appear only without specifying end.
This 02 must appear only without specifying start.
<!-- start-2 -->
baz

''',
[],
id='end',
Expand All @@ -119,7 +98,6 @@ def test_glob_include_absolute(page, tmp_path):
comments=false
%}
''',
'\n\n\n',
[
(
"Delimiter end '<!-- end-not-found-1 -->'"
Expand Down Expand Up @@ -153,7 +131,6 @@ def test_glob_include_absolute(page, tmp_path):
def test_glob_include(
includer_content,
directive,
expected_result,
expected_warnings_schemas,
page,
caplog,
Expand All @@ -163,7 +140,7 @@ def test_glob_include(
included_01_file = tmp_path / 'included_01.txt'
included_02_file = tmp_path / 'included_02.txt'

includer_filepath_content = f'''foo
includer_file_content = f'''foo

{includer_content.replace('{directive}', directive)}
'''
Expand All @@ -181,19 +158,13 @@ def test_glob_include(
This 02 must appear only without specifying end.
'''

includer_file.write_text(includer_filepath_content)
includer_file.write_text(includer_file_content)
included_01_file.write_text(included_01_content)
included_02_file.write_text(included_02_content)

# assert content
expected_result = f'''foo

{expected_result}
'''

assert on_page_markdown(
includer_filepath_content, page(includer_file), tmp_path,
) == expected_result
on_page_markdown(
includer_file_content, page(includer_file), tmp_path,
)

# assert warnings
expected_warnings_schemas = expected_warnings_schemas or []
Expand Down
26 changes: 13 additions & 13 deletions tests/test_unit/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
(
"Delimiter start '<!--start-->' of 'include'"
' directive at {filepath}:3'
' not detected in the file {included_filepath}'
' not detected in the file {included_file}'
),
],
id='start=foo (not found)-end=None',
Expand All @@ -220,7 +220,7 @@
(
"Delimiter end '<!--end-->' of 'include'"
' directive at {filepath}:2'
' not detected in the file {included_filepath}'
' not detected in the file {included_file}'
),
],
id='start=None-end=foo (not found)',
Expand Down Expand Up @@ -368,33 +368,33 @@ def test_include(
caplog,
tmp_path,
):
included_filepath = tmp_path / 'included.md'
includer_filepath = tmp_path / 'includer.md'
included_file = tmp_path / 'included.md'
includer_file = tmp_path / 'includer.md'

included_filepath.write_text(content_to_include)
includer_filepath.write_text(
content_to_include.replace('{filepath}', included_filepath.as_posix()),
included_file.write_text(content_to_include)
includer_file.write_text(
content_to_include.replace('{filepath}', included_file.as_posix()),
)

# assert content
page_content = includer_schema.replace(
'{filepath}', included_filepath.as_posix(),
'{filepath}', included_file.as_posix(),
)
includer_filepath.write_text(page_content)
includer_file.write_text(page_content)

assert on_page_markdown(
page_content, page(includer_filepath), tmp_path,
page_content, page(includer_file), tmp_path,
) == expected_result

# assert warnings
expected_warnings_schemas = expected_warnings_schemas or []
expected_warnings = [
msg_schema.replace(
'{filepath}',
str(includer_filepath.relative_to(tmp_path)),
str(includer_file.relative_to(tmp_path)),
).replace(
'{included_filepath}',
str(included_filepath.relative_to(tmp_path)),
'{included_file}',
str(included_file.relative_to(tmp_path)),
) for msg_schema in expected_warnings_schemas
]

Expand Down
Loading