Skip to content

Commit

Permalink
Warn when passing invalid arguments to directives (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Jun 2, 2024
1 parent 6889f54 commit 42981e4
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 243 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,42 +177,42 @@ content to include.

```jinja
{%
include-markdown "../README.md"
start="<!--intro-start-->"
end="<!--intro-end-->"
include-markdown "../README.md"
start="<!--intro-start-->"
end="<!--intro-end-->"
%}
```

```jinja
{%
include-markdown 'includes/header.md'
start='<!--\n\ttable-start\n-->'
end='<!--\n\ttable-end\n-->'
rewrite-relative-urls=false
comments=false
include-markdown 'includes/header.md'
start='<!--\n\ttable-start\n-->'
end='<!--\n\ttable-end\n-->'
rewrite-relative-urls=false
comments=false
%}
```

```jinja
{%
include-markdown "includes/header.md"
heading-offset=1
include-markdown "includes/header.md"
heading-offset=1
%}
```

```jinja
{%
include-markdown "../LICENSE*"
start="<!--license \"start\" -->"
end='<!--license "end" -->'
exclude="../*.rst"
include-markdown "../LICENSE*"
start="<!--license \"start\" -->"
end='<!--license "end" -->'
exclude="../*.rst"
%}
```

```jinja
{%
include-markdown "**"
exclude="./{index,LICENSE}.md"
include-markdown "**"
exclude="./{index,LICENSE}.md"
%}
```

Expand Down Expand Up @@ -260,16 +260,16 @@ Includes the content of a file or a group of files.

```jinja
{%
include "../examples.md"
start="~~~yaml"
end="~~~\n"
include "../examples.md"
start="~~~yaml"
end="~~~\n"
%}
```

```jinja
{%
include '**'
exclude='./*.md'
include '**'
exclude='./*.md'
%}
```

Expand Down
42 changes: 21 additions & 21 deletions locale/es/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,42 @@ especificado. Sólo soporta la sintaxis de encabezado de caracteres de hash

```jinja
{%
include-markdown "../README.md"
start="<!--intro-start-->"
end="<!--intro-end-->"
include-markdown "../README.md"
start="<!--intro-start-->"
end="<!--intro-end-->"
%}
```

```jinja
{%
include-markdown 'includes/header.md'
start='<!--\n\ttable-start\n-->'
end='<!--\n\ttable-end\n-->'
rewrite-relative-urls=false
comments=false
include-markdown 'includes/header.md'
start='<!--\n\ttable-start\n-->'
end='<!--\n\ttable-end\n-->'
rewrite-relative-urls=false
comments=false
%}
```

```jinja
{%
include-markdown "includes/header.md"
heading-offset=1
include-markdown "includes/header.md"
heading-offset=1
%}
```

```jinja
{%
include-markdown "../LICENSE*"
start="<!--license \"start\" -->"
end='<!--license "end" -->'
exclude="../*.rst"
include-markdown "../LICENSE*"
start="<!--license \"start\" -->"
end='<!--license "end" -->'
exclude="../*.rst"
%}
```

```jinja
{%
include-markdown "**"
exclude="./{index,LICENSE}.md"
include-markdown "**"
exclude="./{index,LICENSE}.md"
%}
```

Expand Down Expand Up @@ -252,16 +252,16 @@ procesados para incluir de forma recursiva. Los valores posibles son `true` y

```jinja
{%
include "../examples.md"
start="~~~yaml"
end="~~~\n"
include "../examples.md"
start="~~~yaml"
end="~~~\n"
%}
```

```jinja
{%
include '**'
exclude='./*.md'
include '**'
exclude='./*.md'
%}
```

Expand Down
42 changes: 21 additions & 21 deletions locale/fr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,42 @@ négatives pour supprimer les caractères `#` de tête.

```jinja
{%
include-markdown "../README.md"
start="<!--intro-start-->"
end="<!--intro-end-->"
include-markdown "../README.md"
start="<!--intro-start-->"
end="<!--intro-end-->"
%}
```

```jinja
{%
include-markdown 'includes/header.md'
start='<!--\n\ttable-start\n-->'
end='<!--\n\ttable-end\n-->'
rewrite-relative-urls=false
comments=false
include-markdown 'includes/header.md'
start='<!--\n\ttable-start\n-->'
end='<!--\n\ttable-end\n-->'
rewrite-relative-urls=false
comments=false
%}
```

```jinja
{%
include-markdown "includes/header.md"
heading-offset=1
include-markdown "includes/header.md"
heading-offset=1
%}
```

```jinja
{%
include-markdown "../LICENSE*"
start="<!--license \"start\" -->"
end='<!--license "end" -->'
exclude="../*.rst"
include-markdown "../LICENSE*"
start="<!--license \"start\" -->"
end='<!--license "end" -->'
exclude="../*.rst"
%}
```

```jinja
{%
include-markdown "**"
exclude="./{index,LICENSE}.md"
include-markdown "**"
exclude="./{index,LICENSE}.md"
%}
```

Expand Down Expand Up @@ -250,16 +250,16 @@ pour des inclusions récursives. Les valeurs possibles sont `true` et `false`.

```jinja
{%
include "../examples.md"
start="~~~yaml"
end="~~~\n"
include "../examples.md"
start="~~~yaml"
end="~~~\n"
%}
```

```jinja
{%
include '**'
exclude='./*.md'
include '**'
exclude='./*.md'
%}
```

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.1.1"
version = "6.1.2"
description = "Mkdocs Markdown includer plugin."
readme = "README.md"
license = "Apache-2.0"
Expand Down
41 changes: 38 additions & 3 deletions src/mkdocs_include_markdown_plugin/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from wcmatch import glob

from mkdocs_include_markdown_plugin import process
from mkdocs_include_markdown_plugin.logger import logger


@dataclass
Expand All @@ -21,7 +22,7 @@ class DirectiveBoolArgument: # noqa: D101


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

DirectiveBoolArgumentsDict = dict[str, DirectiveBoolArgument]

Expand All @@ -44,8 +45,8 @@ class DirectiveBoolArgument: # noqa: D101
GLOB_FLAGS = glob.NEGATE | glob.EXTGLOB | glob.GLOBSTAR | glob.BRACE
RE_ESCAPED_PUNCTUATION = re.escape(string.punctuation)

DOUBLE_QUOTED_STR_RE = r'([^"]|(?<=\\)["])+'
SINGLE_QUOTED_STR_RE = r"([^']|(?<=\\)['])+"
DOUBLE_QUOTED_STR_RE = r'([^"]|(?<=\\)")+'
SINGLE_QUOTED_STR_RE = r"([^']|(?<=\\)')+"

# In the following regular expression, the substrings "$OPENING_TAG"
# and "$CLOSING_TAG" will be replaced by the effective opening and
Expand Down Expand Up @@ -103,6 +104,40 @@ def str_arg(arg: str) -> re.Pattern[str]:
'heading-offset': arg('heading-offset'),
}

INCLUDE_DIRECTIVE_ARGS = {
key for key in ARGUMENT_REGEXES if key not in (
'rewrite-relative-urls', 'heading-offset', 'comments',
)
}

INCLUDE_MARKDOWN_DIRECTIVE_ARGS = {
key for key in ARGUMENT_REGEXES if key != 'recursive'
}


def warn_invalid_directive_arguments(
arguments_string: str,
directive_lineno: int,
directive: Literal['include', 'include-markdown'],
page_src_path: str | None,
docs_dir: str,
) -> None:
"""Warns about the invalid arguments passed to a directive."""
arg_re = re.compile(rf'[\w-]*=[{RE_ESCAPED_PUNCTUATION}\w]*')
valid_args = (
INCLUDE_DIRECTIVE_ARGS if directive == 'include'
else INCLUDE_MARKDOWN_DIRECTIVE_ARGS
)
for arg_value in re.findall(arg_re, arguments_string):
if arg_value.split('=', 1)[0] not in valid_args:
location = process.file_lineno_message(
page_src_path, docs_dir, directive_lineno,
)
logger.warning(
f"Invalid argument '{arg_value}' in"
f" '{directive}' directive at {location}. Ignoring...",
)


def parse_filename_argument(
match: re.Match[str],
Expand Down
Loading

0 comments on commit 42981e4

Please sign in to comment.