|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import re |
| 6 | + |
5 | 7 | from mkdocs.config.config_options import Type as MkType |
6 | 8 |
|
| 9 | +from mkdocs_include_markdown_plugin.regexes import INCLUDE_TAG_RE |
| 10 | + |
7 | 11 |
|
8 | | -CONFIG_DEFAULT_COMMENTS = True |
| 12 | +DEFAULT_COMMENTS = True |
| 13 | +DEFAULT_OPENING_TAG = '{%' |
| 14 | +DEFAULT_CLOSING_TAG = '%}' |
9 | 15 |
|
10 | 16 | CONFIG_DEFAULTS = { |
11 | | - 'opening_tag': '{%', |
12 | | - 'closing_tag': '%}', |
| 17 | + 'opening_tag': DEFAULT_OPENING_TAG, |
| 18 | + 'closing_tag': DEFAULT_CLOSING_TAG, |
13 | 19 | 'encoding': 'utf-8', |
14 | 20 | 'preserve_includer_indent': True, |
15 | 21 | 'dedent': False, |
16 | 22 | 'trailing_newlines': True, |
17 | | - 'comments': CONFIG_DEFAULT_COMMENTS, |
| 23 | + 'comments': DEFAULT_COMMENTS, |
18 | 24 | } |
19 | 25 |
|
20 | 26 | CONFIG_SCHEME = ( |
21 | 27 | ( |
22 | 28 | 'opening_tag', |
23 | | - MkType(str, default=CONFIG_DEFAULTS['opening_tag']), |
| 29 | + MkType(str, default=DEFAULT_OPENING_TAG), |
24 | 30 | ), |
25 | 31 | ( |
26 | 32 | 'closing_tag', |
27 | | - MkType(str, default=CONFIG_DEFAULTS['closing_tag']), |
| 33 | + MkType(str, default=DEFAULT_CLOSING_TAG), |
28 | 34 | ), |
29 | 35 | ( |
30 | 36 | 'encoding', |
|
44 | 50 | ), |
45 | 51 | ( |
46 | 52 | 'comments', |
47 | | - MkType(bool, default=CONFIG_DEFAULTS['comments']), |
| 53 | + MkType(bool, default=DEFAULT_COMMENTS), |
48 | 54 | ), |
49 | 55 | ) |
| 56 | + |
| 57 | + |
| 58 | +def create_include_tag( |
| 59 | + opening_tag: str, closing_tag: str, tag: str = 'include', |
| 60 | +) -> re.Pattern[str]: |
| 61 | + """Create a regex pattern to match an inclusion tag directive. |
| 62 | +
|
| 63 | + Replaces the substrings '$OPENING_TAG' and '$CLOSING_TAG' from |
| 64 | + INCLUDE_TAG_REGEX by the effective tag. |
| 65 | + """ |
| 66 | + return re.compile( |
| 67 | + INCLUDE_TAG_RE.replace(' include', f' {tag}').replace( |
| 68 | + '$OPENING_TAG', re.escape(opening_tag), |
| 69 | + ).replace('$CLOSING_TAG', re.escape(closing_tag)), |
| 70 | + flags=re.VERBOSE | re.DOTALL, |
| 71 | + ) |
0 commit comments