Skip to content

Commit

Permalink
Added option to preserve includer directive indentation in included c…
Browse files Browse the repository at this point in the history
…ontent
  • Loading branch information
mondeja committed Dec 24, 2020
1 parent f2a6a75 commit 00151ff
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 32 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ content to include.
include is wrapped by `<!-- BEGIN INCLUDE -->` and `<!-- END INCLUDE -->`
comments which help to identify that the content has been included. Possible
values are `true` and `false`.
- **preserve_includer_indent** (*false*): When this option is enabled, all
lines of the content to include is indented with the same number of spaces
used to indent the includer `{% %}` template. Possible values are `true` and
`false`.

> Note that the **start** and **end** strings may contain usual (Python-style)
escape sequences like `\n`, which is handy if you need to match on a multi-line
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_include_markdown_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__title__ = 'mkdocs_include_markdown_plugin'
__version__ = '2.2.0'
__version__ = '2.3.0'
__description__ = 'Mkdocs Markdown includer plugin.'
63 changes: 42 additions & 21 deletions mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@
'false': False
}

TRUE_FALSE_BOOL_STR = {
True: 'true',
False: 'false'
}

INCLUDE_TAG_REGEX = re.compile(
r'''
{% # opening tag
{%
\s*
include # directive name
include
\s+
"(?P<filename>[^"]+)" # "filename"
"(?P<filename>[^"]+)"
\s*
%} # closing tag
%}
''',
flags=re.VERBOSE,
)

INCLUDE_MARKDOWN_TAG_REGEX = re.compile(
r'''
{% # opening tag
(?P<includer_indent>[^\S\r\n]*){%
\s*
include\-markdown # directive name
\s+
Expand All @@ -34,8 +39,9 @@
(?:\s+end="(?P<end>[^"]+)")?
(?:\s+rewrite_relative_urls=(?P<rewrite_relative_urls>\w*))?
(?:\s+comments=(?P<comments>\w*))?
(?:\s+preserve_includer_indent=(?P<preserve_includer_indent>\w*))?
\s*
%} # closing tag
%}
''',
flags=re.VERBOSE,
)
Expand All @@ -61,34 +67,41 @@ def found_include_tag(match):
return text_to_include

def found_include_markdown_tag(match):
# handle filename parameter and read content
filename = match.group('filename')

file_path_abs = page_src_path.parent / filename

if not file_path_abs.exists():
raise FileNotFoundError('File \'%s\' not found' % filename)

text_to_include = file_path_abs.read_text(encoding='utf8')

# handle options
start = match.group('start')
end = match.group('end')
includer_indent = match.group('includer_indent')

if start is not None:
start = process.interpret_escapes(start)
if end is not None:
end = process.interpret_escapes(end)

# boolean options
bool_options = {
'rewrite_relative_urls': True,
'comments': True
'comments': True,
'preserve_includer_indent': False
}

for opt_name in bool_options:
for opt_name, default_value in bool_options.items():
try:
bool_options[opt_name] = TRUE_FALSE_STR_BOOL[
match.group(opt_name) or 'true']
match.group(opt_name) or TRUE_FALSE_BOOL_STR[default_value]
]
except KeyError:
raise ValueError(('Unknown value for \'%s\'. Possible values '
'are: true, false') % opt_name)

file_path_abs = page_src_path.parent / filename

if not file_path_abs.exists():
raise FileNotFoundError('File \'%s\' not found' % filename)

text_to_include = file_path_abs.read_text(encoding='utf8')
if start is not None:
start = process.interpret_escapes(start)
if end is not None:
end = process.interpret_escapes(end)

if start is not None:
_, _, text_to_include = text_to_include.partition(start)
Expand All @@ -102,11 +115,19 @@ def found_include_markdown_tag(match):
destination_path=page_src_path,
)

if bool_options['preserve_includer_indent']:
text_to_include = ''.join(
includer_indent + line
for line in text_to_include.splitlines(keepends=True))
else:
text_to_include = includer_indent + text_to_include

if not bool_options['comments']:
return text_to_include

return (
'<!-- BEGIN INCLUDE %s %s %s -->\n' % (
includer_indent
+ '<!-- BEGIN INCLUDE %s %s %s -->\n' % (
filename, html.escape(start or ''), html.escape(end or '')
)
+ text_to_include
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[bumpversion]
current_version = 2.2.0
current_version = 2.3.0

[bumpversion:file:mkdocs_include_markdown_plugin/__init__.py]

[coverage:run]
omit = mkdocs_include_markdown_plugin/plugin.py

[coverage:report]
exclude_lines =
exclude_lines =
pragma: no cover

[flake8]
Expand Down
40 changes: 32 additions & 8 deletions tests/test_include_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
'''Foo''',
),
# Preserve includer/included indent
# Preserve included indent
(
'''1. Ordered list item
{%
Expand All @@ -166,6 +166,23 @@
- Unordered sublist item
- Other unordered sublist item''',
),
# Preserve includer indent
(
'''1. Ordered list item
{%
include-markdown "{filepath}"
comments=false
preserve_includer_indent=true
%}''',
'''- First unordered sublist item
- Second unordered sublist item
- Third unordered sublist item''',
'''1. Ordered list item
- First unordered sublist item
- Second unordered sublist item
- Third unordered sublist item''',
)
),
)
def test_include_markdown(includer_schema, content_to_include,
Expand Down Expand Up @@ -276,16 +293,23 @@ def test_include_markdown_relative_rewrite(page, tmp_path,
''') # noqa: E501


@pytest.mark.parametrize('opt_name', ('rewrite_relative_urls', 'comments'))
@pytest.mark.parametrize(
'opt_name',
(
'rewrite_relative_urls',
'comments',
'preserve_includer_indent'
)
)
def test_include_markdown_invalid_bool_option(opt_name, page):
page_content = textwrap.dedent('''{%
include-markdown "subfile.md"
{opt_name}=invalidoption
%}''').replace('{opt_name}', opt_name)
with tempfile.NamedTemporaryFile(suffix='.md') as f:
page_content = textwrap.dedent('''{%
include-markdown "{filepath}"
{opt_name}=invalidoption
%}''').replace('{opt_name}', opt_name).replace('{filepath}', f.name)

with tempfile.NamedTemporaryFile(suffix='.md') as f_includer:
with pytest.raises(ValueError) as excinfo:
_on_page_markdown(page_content, page(f_includer.name))
_on_page_markdown(page_content, page(f.name))

expected_exc_message = (
'Unknown value for \'%(opt_name)s\'.'
Expand Down

0 comments on commit 00151ff

Please sign in to comment.