Skip to content

Commit

Permalink
Allow to pass escaped double quote characters to string arguments (#102)
Browse files Browse the repository at this point in the history
* Allow to pass escaped double quotecharacters to string arguments

* Recursive globs with '**'

* Double quotes not allowed in path under Windows
  • Loading branch information
mondeja committed May 23, 2022
1 parent 04ae977 commit 1859d00
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 112 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ start or end trigger.
%}
```

```jinja
{% include-markdown "/escap\"ed/double-quotes/in/file\"/name.md" %}
```

<!-- mdpo-disable-next-line -->
#### **`include`**

Expand Down
4 changes: 4 additions & 0 deletions locale/es/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ hacer coincidir en un disparador de inicio o fin de varias líneas.
%}
```

```jinja
{% include-markdown "/escap\"ed/double-quotes/in/file\"/name.md" %}
```

#### **`include`**

Incluye el contenido de un archivo o un grupo de archivos.
Expand Down
4 changes: 4 additions & 0 deletions locale/fr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ si vous devez faire correspondre un déclencheur de début ou de fin multiligne.
%}
```

```jinja
{% include-markdown "/escap\"ed/double-quotes/in/file\"/name.md" %}
```

#### **`include`**

Inclus le contenu d'un fichier ou d'un groupe de fichiers.
Expand Down
79 changes: 44 additions & 35 deletions mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,49 @@
False: 'false',
}

BOOL_ARGUMENT_PATTERN = r'\w+'
STR_ARGUMENT_PATTERN = r'([^"]|(?<=\\)")+'

INCLUDE_TAG_REGEX = re.compile(
r'''
(?P<_includer_indent>[^\S\r\n]*){%
rf'''
(?P<_includer_indent>[^\S\r\n]*){{%
\s*
include
\s+
"(?P<filename>[^"]+)"
"(?P<filename>{STR_ARGUMENT_PATTERN})"
(?P<arguments>.*?)
\s*
%}
%}}
''',
flags=re.VERBOSE | re.DOTALL,
)

INCLUDE_MARKDOWN_TAG_REGEX = re.compile(
r'''
(?P<_includer_indent>[^\S\r\n]*){%
\s*
include\-markdown
\s+
"(?P<filename>[^"]+)"
(?P<arguments>.*?)
\s*
%}
''',
flags=re.VERBOSE | re.DOTALL,
INCLUDE_TAG_REGEX.pattern.replace(' include', ' include-markdown'),
flags=INCLUDE_TAG_REGEX.flags,
)

ARGUMENT_REGEXES = {
'start': re.compile(r'start="([^"]+)"'),
'end': re.compile(r'end="([^"]+)"'),
'rewrite-relative-urls': re.compile(r'rewrite-relative-urls=(\w*)'),
'comments': re.compile(r'comments=(\w*)'),
'preserve-includer-indent': re.compile(r'preserve-includer-indent=(\w*)'),
'dedent': re.compile(r'dedent=(\w*)'),
# str
'start': re.compile(rf'start="({STR_ARGUMENT_PATTERN})"'),
'end': re.compile(rf'end="({STR_ARGUMENT_PATTERN})"'),
'exclude': re.compile(rf'exclude="({STR_ARGUMENT_PATTERN})"'),

# bool
'rewrite-relative-urls': re.compile(
rf'rewrite-relative-urls=({BOOL_ARGUMENT_PATTERN})',
),
'comments': re.compile(rf'comments=({BOOL_ARGUMENT_PATTERN})'),
'preserve-includer-indent': re.compile(
rf'preserve-includer-indent=({BOOL_ARGUMENT_PATTERN})',
),
'dedent': re.compile(rf'dedent=({BOOL_ARGUMENT_PATTERN})'),
'trailing-newlines': re.compile(
rf'trailing-newlines=({BOOL_ARGUMENT_PATTERN})',
),

# int
'heading-offset': re.compile(r'heading-offset=(-?\d+)'),
'exclude': re.compile(r'exclude="([^"]+)"'),
'trailing-newlines': re.compile(r'trailing-newlines=(\w*)'),
}

logger = logging.getLogger('mkdocs.plugins.mkdocs_include_markdown_plugin')
Expand All @@ -73,7 +78,7 @@ def get_file_content(

def found_include_tag(match):
_includer_indent = match.group('_includer_indent')
filename = match.group('filename')
filename = match.group('filename').replace('\\"', '"')
arguments_string = match.group('arguments')

if os.path.isabs(filename):
Expand All @@ -91,7 +96,7 @@ def found_include_tag(match):
if exclude_match is None:
ignore_paths = []
else:
exclude_string = exclude_match.group(1)
exclude_string = exclude_match.group(1).replace('\\"', '"')
if os.path.isabs(exclude_string):
exclude_globstr = exclude_string
else:
Expand All @@ -104,7 +109,7 @@ def found_include_tag(match):
ignore_paths = glob.glob(exclude_globstr)

file_paths_to_include = process.filter_paths(
glob.glob(file_path_glob),
glob.iglob(file_path_glob, recursive=True),
ignore_paths=ignore_paths,
)

Expand Down Expand Up @@ -150,8 +155,10 @@ def found_include_tag(match):
start_match = re.search(ARGUMENT_REGEXES['start'], arguments_string)
end_match = re.search(ARGUMENT_REGEXES['end'], arguments_string)

start = None if not start_match else start_match.group(1)
end = None if not end_match else end_match.group(1)
start = None if not start_match else (
start_match.group(1).replace('\\"', '"')
)
end = None if not end_match else end_match.group(1).replace('\\"', '"')

text_to_include = ''
expected_but_any_found = [start is not None, end is not None]
Expand All @@ -176,7 +183,7 @@ def found_include_tag(match):
new_text_to_include,
file_path,
docs_dir,
file_path,
page_src_path,
)

# trailing newlines right stripping
Expand Down Expand Up @@ -219,7 +226,7 @@ def found_include_tag(match):

def found_include_markdown_tag(match):
_includer_indent = match.group('_includer_indent')
filename = match.group('filename')
filename = match.group('filename').replace('\\"', '"')
arguments_string = match.group('arguments')

if os.path.isabs(filename):
Expand All @@ -237,7 +244,7 @@ def found_include_markdown_tag(match):
if exclude_match is None:
ignore_paths = []
else:
exclude_string = exclude_match.group(1)
exclude_string = exclude_match.group(1).replace('\\"', '"')
if os.path.isabs(exclude_string):
exclude_globstr = exclude_string
else:
Expand All @@ -250,7 +257,7 @@ def found_include_markdown_tag(match):
ignore_paths = glob.glob(exclude_globstr)

file_paths_to_include = process.filter_paths(
glob.glob(file_path_glob),
glob.iglob(file_path_glob, recursive=True),
ignore_paths=ignore_paths,
)

Expand Down Expand Up @@ -303,8 +310,10 @@ def found_include_markdown_tag(match):
start_match = re.search(ARGUMENT_REGEXES['start'], arguments_string)
end_match = re.search(ARGUMENT_REGEXES['end'], arguments_string)

start = None if not start_match else start_match.group(1)
end = None if not end_match else end_match.group(1)
start = None if not start_match else (
start_match.group(1).replace('\\"', '"')
)
end = None if not end_match else end_match.group(1).replace('\\"', '"')

# heading offset
offset = 0
Expand Down Expand Up @@ -344,7 +353,7 @@ def found_include_markdown_tag(match):
new_text_to_include,
file_path,
docs_dir,
file_path,
page_src_path,
)

# trailing newlines right stripping
Expand Down
6 changes: 5 additions & 1 deletion mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,13 @@ def filter_paths(filepaths: list, ignore_paths: list = []):
# ignore by filepath
if filepath in ignore_paths:
continue

# ignore by dirpath (relative or absolute)
if (os.sep).join(filepath.split(os.sep)[:-1]) in ignore_paths:
continue
response.append(filepath)

# ignore if is a directory
if not os.path.isdir(filepath):
response.append(filepath)
response.sort()
return response
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ line_length = 79
use_parentheses = True
combine_as_imports = True
include_trailing_comma = True
known_tests = tests
known_tests = testing_utils
sections = STDLIB,THIRDPARTY,FIRSTPARTY,TESTS,LOCALFOLDER
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import os
import sys

import pytest


TESTS_DIR = os.path.abspath(os.path.dirname(__file__))
if TESTS_DIR not in sys.path:
sys.path.append(TESTS_DIR)


@pytest.fixture
def page():
'''Fake mkdocs page object.'''
Expand Down
Loading

0 comments on commit 1859d00

Please sign in to comment.