Skip to content

Commit

Permalink
Don't increase '#' characters of comments in indented codeblocks usin…
Browse files Browse the repository at this point in the history
…g heading-offset
  • Loading branch information
mondeja committed Jun 11, 2021
1 parent 263e480 commit 027cc02
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.1.2
current_version = 3.1.3

[bumpversion:file:mkdocs_include_markdown_plugin/__init__.py]

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion mkdocs_include_markdown_plugin/__init__.py
@@ -1,2 +1,2 @@
__title__ = 'mkdocs_include_markdown_plugin'
__version__ = '3.1.2'
__version__ = '3.1.3'
37 changes: 23 additions & 14 deletions mkdocs_include_markdown_plugin/process.py
Expand Up @@ -186,24 +186,33 @@ def filter_inclusions(start_match, end_match, text_to_include):

def increase_headings_offset(markdown, offset=0):
'''Increases the headings depth of a snippet of Makdown content.'''
_inside_fenced_codeblock = False
_current_fenced_codeblock_delimiter = None
_inside_fcodeblock = False # inside fenced codeblock
_current_fcodeblock_delimiter = None # current fenced codeblock delimiter
_inside_icodeblock = False # inside indented codeblcok

lines = []
for line in markdown.splitlines(keepends=True):
if not _inside_fenced_codeblock:
if line.startswith('```') or line.startswith('~~~'):
_inside_fenced_codeblock = True
_current_fenced_codeblock_delimiter = line[:3]
lines.append(line)
lstripped_line = line.lstrip()

if not _inside_fcodeblock and not _inside_icodeblock:
if any([
lstripped_line.startswith('```'),
lstripped_line.startswith('~~~'),
]):
_inside_fcodeblock = True
_current_fcodeblock_delimiter = line[:3]
elif line.startswith(' '):
_inside_icodeblock = True
elif line.startswith('#'):
lines.append('#' * offset + line)
else:
lines.append(line)
line = '#' * offset + line
else:
lines.append(line)
if line.startswith(_current_fenced_codeblock_delimiter):
_inside_fenced_codeblock = False
_current_fenced_codeblock_delimiter = None
if _current_fcodeblock_delimiter:
if lstripped_line.startswith(_current_fcodeblock_delimiter):
_inside_fcodeblock = False
_current_fcodeblock_delimiter = None
else:
if not line.startswith(' '):
_inside_icodeblock = False
lines.append(line)

return ''.join(lines)
2 changes: 1 addition & 1 deletion scripts/update_translations.py
Expand Up @@ -21,7 +21,7 @@ def main():
if not os.path.isdir(language_dir):
os.mkdir(language_dir)

po_filepath = os.path.join(language_dir, 'README.po')
po_filepath = os.path.join(language_dir, 'readme.po')
po = markdown_to_pofile(
'README.md', location=False, po_filepath=po_filepath,
)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
@@ -1,6 +1,6 @@
[metadata]
name = mkdocs_include_markdown_plugin
version = 3.1.2
version = 3.1.3
description = Mkdocs Markdown includer plugin.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
26 changes: 26 additions & 0 deletions tests/test_process.py
Expand Up @@ -218,6 +218,32 @@ def test_rewrite_relative_urls(
''',
id='```,~~~',
),
pytest.param(
'''# Foo
# this is a comment
hello = "world"
# Bar
# another comment
## Qux
''',
1,
'''## Foo
# this is a comment
hello = "world"
## Bar
# another comment
### Qux
''',
id='indented-codeblocks',
),
),
)
def test_dont_increase_heading_offset_inside_fenced_codeblocks(
Expand Down

0 comments on commit 027cc02

Please sign in to comment.