Skip to content

Commit 08f1db5

Browse files
authored
Don't increase '#' characters of comments in indented codeblocks using heading-offset (#54)
1 parent 263e480 commit 08f1db5

File tree

7 files changed

+53
-18
lines changed

7 files changed

+53
-18
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.1.2
2+
current_version = 3.1.3
33

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

File renamed without changes.
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.1.2'
2+
__version__ = '3.1.3'

mkdocs_include_markdown_plugin/process.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,33 @@ def filter_inclusions(start_match, end_match, text_to_include):
186186

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

192193
lines = []
193194
for line in markdown.splitlines(keepends=True):
194-
if not _inside_fenced_codeblock:
195-
if line.startswith('```') or line.startswith('~~~'):
196-
_inside_fenced_codeblock = True
197-
_current_fenced_codeblock_delimiter = line[:3]
198-
lines.append(line)
195+
lstripped_line = line.lstrip()
196+
197+
if not _inside_fcodeblock and not _inside_icodeblock:
198+
if any([
199+
lstripped_line.startswith('```'),
200+
lstripped_line.startswith('~~~'),
201+
]):
202+
_inside_fcodeblock = True
203+
_current_fcodeblock_delimiter = line[:3]
204+
elif line.startswith(' '):
205+
_inside_icodeblock = True
199206
elif line.startswith('#'):
200-
lines.append('#' * offset + line)
201-
else:
202-
lines.append(line)
207+
line = '#' * offset + line
203208
else:
204-
lines.append(line)
205-
if line.startswith(_current_fenced_codeblock_delimiter):
206-
_inside_fenced_codeblock = False
207-
_current_fenced_codeblock_delimiter = None
209+
if _current_fcodeblock_delimiter:
210+
if lstripped_line.startswith(_current_fcodeblock_delimiter):
211+
_inside_fcodeblock = False
212+
_current_fcodeblock_delimiter = None
213+
else:
214+
if not line.startswith(' '):
215+
_inside_icodeblock = False
216+
lines.append(line)
208217

209218
return ''.join(lines)

scripts/update_translations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def main():
2121
if not os.path.isdir(language_dir):
2222
os.mkdir(language_dir)
2323

24-
po_filepath = os.path.join(language_dir, 'README.po')
24+
po_filepath = os.path.join(language_dir, 'readme.po')
2525
po = markdown_to_pofile(
2626
'README.md', location=False, po_filepath=po_filepath,
2727
)

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.1.2
3+
version = 3.1.3
44
description = Mkdocs Markdown includer plugin.
55
long_description = file: README.md
66
long_description_content_type = text/markdown

tests/test_process.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,32 @@ def test_rewrite_relative_urls(
218218
''',
219219
id='```,~~~',
220220
),
221+
pytest.param(
222+
'''# Foo
223+
224+
# this is a comment
225+
hello = "world"
226+
227+
# Bar
228+
229+
# another comment
230+
231+
## Qux
232+
''',
233+
1,
234+
'''## Foo
235+
236+
# this is a comment
237+
hello = "world"
238+
239+
## Bar
240+
241+
# another comment
242+
243+
### Qux
244+
''',
245+
id='indented-codeblocks',
246+
),
221247
),
222248
)
223249
def test_dont_increase_heading_offset_inside_fenced_codeblocks(

0 commit comments

Comments
 (0)