Skip to content

Commit 6a7dd98

Browse files
authored
Allow to include from different parts of the same page using the same 'start' and 'end' arguments (#51)
1 parent bec5916 commit 6a7dd98

File tree

7 files changed

+122
-19
lines changed

7 files changed

+122
-19
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 3.0.1
2+
current_version = 3.1.0
33

44
[bumpversion:file:mkdocs_include_markdown_plugin/__init__.py]
55

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__title__ = 'mkdocs_include_markdown_plugin'
2-
__version__ = '3.0.1'
2+
__version__ = '3.1.0'

mkdocs_include_markdown_plugin/event.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,13 @@ def found_include_tag(match):
100100

101101
# string options
102102
start_match = re.search(ARGUMENT_REGEXES['start'], arguments_string)
103-
if start_match is not None:
104-
start = process.interpret_escapes(start_match.group(1))
105-
_, _, text_to_include = text_to_include.partition(start)
106-
107103
end_match = re.search(ARGUMENT_REGEXES['end'], arguments_string)
108-
if end_match is not None:
109-
end = process.interpret_escapes(end_match.group(1))
110-
text_to_include, _, _ = text_to_include.partition(end)
104+
105+
text_to_include, _, _ = process.filter_inclusions(
106+
start_match,
107+
end_match,
108+
text_to_include,
109+
)
111110

112111
# nested includes
113112
new_text_to_include = get_file_content(text_to_include, file_path_abs)
@@ -185,16 +184,13 @@ def found_include_markdown_tag(match):
185184

186185
# string options
187186
start_match = re.search(ARGUMENT_REGEXES['start'], arguments_string)
188-
start = None
189-
if start_match is not None:
190-
start = process.interpret_escapes(start_match.group(1))
191-
_, _, text_to_include = text_to_include.partition(start)
192-
193187
end_match = re.search(ARGUMENT_REGEXES['end'], arguments_string)
194-
end = None
195-
if end_match is not None:
196-
end = process.interpret_escapes(end_match.group(1))
197-
text_to_include, _, _ = text_to_include.partition(end)
188+
189+
text_to_include, start, end = process.filter_inclusions(
190+
start_match,
191+
end_match,
192+
text_to_include,
193+
)
198194

199195
# relative URLs rewriting
200196
if bool_options['rewrite-relative-urls']['value']:

mkdocs_include_markdown_plugin/process.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,33 @@ def interpret_escapes(value: str) -> str:
157157
return value.encode('latin-1', 'backslashreplace').decode('unicode_escape')
158158

159159

160+
def filter_inclusions(start_match, end_match, text_to_include):
161+
"""Manages inclusions from files using ``start`` and ``end`` diective
162+
arguments.
163+
"""
164+
start = None
165+
end = None
166+
if start_match is not None:
167+
start = interpret_escapes(start_match.group(1))
168+
if end_match is not None:
169+
end = interpret_escapes(end_match.group(1))
170+
new_text_to_include = ''
171+
if end_match is not None:
172+
for start_text in text_to_include.split(start)[1:]:
173+
for i, end_text in enumerate(start_text.split(end)):
174+
if not i % 2:
175+
new_text_to_include += end_text
176+
else:
177+
new_text_to_include = text_to_include.split(start)[1]
178+
if new_text_to_include:
179+
text_to_include = new_text_to_include
180+
elif end_match is not None:
181+
end = interpret_escapes(end_match.group(1))
182+
text_to_include, _, _ = text_to_include.partition(end)
183+
184+
return (text_to_include, start, end)
185+
186+
160187
def increase_headings_offset(markdown, offset=0):
161188
'''Increases the headings depth of a snippet of Makdown content.'''
162189
_inside_fenced_codeblock = False

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = mkdocs_include_markdown_plugin
3-
version = 3.0.1
3+
version = 3.1.0
44
description = Mkdocs Markdown includer plugin.
55
long_description = file: README.md
66
long_description_content_type = text/markdown

tests/test_include.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,46 @@
9292
id='start/end (unescaped special characters)',
9393
),
9494
95+
# Multiples start and end matchs
96+
pytest.param(
97+
'''{%
98+
include-markdown "{filepath}"
99+
start="<!--start-tag-->"
100+
end="<!--end-tag-->"
101+
comments=false
102+
%}''',
103+
'''Some text
104+
105+
<!--start-tag-->
106+
This should be included.
107+
<!--end-tag-->
108+
109+
This shouldn't be included.
110+
111+
<!--start-tag-->
112+
This should be included also.
113+
<!--end-tag-->
114+
115+
Here some text
116+
that should be ignored.
117+
118+
<!--start-->
119+
<!--end-->
120+
121+
Etc
122+
<!--start-tag-->
123+
This should be included even if hasn't defined after end tag.
124+
''',
125+
'''
126+
This should be included.
127+
128+
This should be included also.
129+
130+
This should be included even if hasn't defined after end tag.
131+
''',
132+
id='multiple-start-end-matchs',
133+
),
134+
95135
# Preserve included indent
96136
pytest.param(
97137
'''1. Ordered list item

tests/test_include_markdown.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,46 @@
160160
id='comments=false',
161161
),
162162
163+
# Multiples start and end matchs
164+
pytest.param(
165+
'''{%
166+
include-markdown "{filepath}"
167+
start="<!--start-tag-->"
168+
end="<!--end-tag-->"
169+
comments=false
170+
%}''',
171+
'''Some text
172+
173+
<!--start-tag-->
174+
This should be included.
175+
<!--end-tag-->
176+
177+
This shouldn't be included.
178+
179+
<!--start-tag-->
180+
This should be included also.
181+
<!--end-tag-->
182+
183+
Here some text
184+
that should be ignored.
185+
186+
<!--start-->
187+
<!--end-->
188+
189+
Etc
190+
<!--start-tag-->
191+
This should be included even if hasn't defined after end tag.
192+
''',
193+
'''
194+
This should be included.
195+
196+
This should be included also.
197+
198+
This should be included even if hasn't defined after end tag.
199+
''',
200+
id='multiple-start-end-matchs',
201+
),
202+
163203
# Don't preserve included indent
164204
pytest.param(
165205
'''1. Ordered list item

0 commit comments

Comments
 (0)