Skip to content

Commit aa17cc0

Browse files
authored
Don't compile argument regexes if not required (#262)
1 parent bc8c281 commit aa17cc0

File tree

4 files changed

+137
-120
lines changed

4 files changed

+137
-120
lines changed

examples/basic/docs/index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
```yaml
2-
{ % include '../mkdocs.yml'% }
3-
```
4-
51
{% include-markdown './included.md' start='<--start-->' %}

src/mkdocs_include_markdown_plugin/directive.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@dataclass
2121
class DirectiveBoolArgument: # noqa: D101
2222
value: bool
23-
regex: re.Pattern[str]
23+
regex: Callable[[], re.Pattern[str]]
2424

2525

2626
if TYPE_CHECKING: # pragma: no cover
@@ -168,8 +168,9 @@ def warn_invalid_directive_arguments(
168168
directive: Literal['include', 'include-markdown'],
169169
page_src_path: str | None,
170170
docs_dir: str,
171-
) -> None:
171+
) -> list[str]:
172172
"""Warns about the invalid arguments passed to a directive."""
173+
used_arguments = []
173174
valid_args = (
174175
INCLUDE_DIRECTIVE_ARGS
175176
if directive == 'include'
@@ -184,6 +185,9 @@ def warn_invalid_directive_arguments(
184185
f"Invalid argument '{maybe_arg}' in"
185186
f" '{directive}' directive at {location}. Ignoring...",
186187
)
188+
else:
189+
used_arguments.append(maybe_arg)
190+
return used_arguments
187191

188192

189193
def parse_filename_argument(
@@ -202,8 +206,10 @@ def parse_filename_argument(
202206
return filename, raw_filename
203207

204208

205-
def parse_string_argument(match: re.Match[str]) -> str | None:
209+
def parse_string_argument(match: re.Match[str] | None) -> str | None:
206210
"""Return the string argument matched by ``match``."""
211+
if match is None:
212+
return None
207213
value = match[1]
208214
if value is None:
209215
value = match[3]
@@ -246,6 +252,7 @@ def parse_bool_options(
246252
option_names: list[str],
247253
defaults: DefaultValues,
248254
arguments_string: str,
255+
used_arguments: list[str],
249256
) -> tuple[DirectiveBoolArgumentsDict, list[str]]:
250257
"""Parse boolean options from arguments string."""
251258
invalid_args: list[str] = []
@@ -254,16 +261,17 @@ def parse_bool_options(
254261
for option_name in option_names:
255262
bool_options[option_name] = DirectiveBoolArgument(
256263
value=defaults[option_name], # type: ignore
257-
regex=ARGUMENT_REGEXES[option_name](),
264+
regex=ARGUMENT_REGEXES[option_name],
258265
)
259266

260267
for arg_name, arg in bool_options.items():
261-
bool_arg_match = arg.regex.search(arguments_string)
262-
if bool_arg_match is None:
268+
if arg_name not in used_arguments:
263269
continue
270+
bool_arg_match = arg.regex().search(arguments_string)
264271
try:
265272
bool_options[arg_name].value = TRUE_FALSE_STR_BOOL[
266-
bool_arg_match[1] or TRUE_FALSE_BOOL_STR[arg.value]
273+
(bool_arg_match and bool_arg_match[1])
274+
or TRUE_FALSE_BOOL_STR[arg.value]
267275
]
268276
except KeyError:
269277
invalid_args.append(arg_name)

0 commit comments

Comments
 (0)