Skip to content

Commit

Permalink
Apply substitutions from all directives at once (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Jun 10, 2024
1 parent 1dd2835 commit f7ebed6
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions src/mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


if TYPE_CHECKING:
from typing import TypedDict
from typing import Literal, TypedDict

from mkdocs.structure.pages import Page

Expand All @@ -44,6 +44,23 @@
)


# Placeholders (taken from Python-Markdown)
STX = '\u0002'
''' "Start of Text" marker for placeholder templates. '''
ETX = '\u0003'
''' "End of Text" marker for placeholder templates. '''
INLINE_PLACEHOLDER_PREFIX = f'{STX}klzzwxh:'


def build_placeholder(
num: int,
directive: Literal['include', 'include-markdown'],
) -> str:
"""Return a placeholder."""
directive_prefix = 'im' if directive == 'include-markdown' else 'i'
return f'{INLINE_PLACEHOLDER_PREFIX}{directive_prefix}{num}{ETX}'


@dataclass
class Settings: # noqa: D101
exclude: list[str] | None
Expand Down Expand Up @@ -80,6 +97,9 @@ def get_file_content( # noqa: PLR0913, PLR0915
if page_src_path in settings_ignore_paths:
return markdown

new_found_include_contents: list[tuple[str, str]] = []
new_found_include_markdown_contents: list[tuple[str, str]] = []

def found_include_tag( # noqa: PLR0912, PLR0915
match: re.Match[str],
) -> str:
Expand Down Expand Up @@ -281,7 +301,11 @@ def found_include_tag( # noqa: PLR0912, PLR0915
f' {readable_files_to_include}',
)

return text_to_include
nonlocal new_found_include_contents
include_index = len(new_found_include_contents)
placeholder = build_placeholder(include_index, 'include')
new_found_include_contents.append((placeholder, text_to_include))
return placeholder

def found_include_markdown_tag( # noqa: PLR0912, PLR0915
match: re.Match[str],
Expand Down Expand Up @@ -559,17 +583,31 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
f' {readable_files_to_include}',
)

return text_to_include
nonlocal new_found_include_markdown_contents
markdown_include_index = len(new_found_include_markdown_contents)
placeholder = build_placeholder(
markdown_include_index, 'include-markdown')
new_found_include_markdown_contents.append(
(placeholder, text_to_include))
return placeholder

# Replace contents by placeholders
markdown = tags['include-markdown'].sub(
found_include_markdown_tag,
markdown,
)
return tags['include'].sub(
markdown = tags['include'].sub(
found_include_tag,
markdown,
)

# Replace placeholders by contents
for placeholder, text in new_found_include_contents:
markdown = markdown.replace(placeholder, text, 1)
for placeholder, text in new_found_include_markdown_contents:
markdown = markdown.replace(placeholder, text, 1)
return markdown


def on_page_markdown(
markdown: str,
Expand Down

0 comments on commit f7ebed6

Please sign in to comment.