Skip to content

Commit 8da79f0

Browse files
authored
Merge pull request #43 from cmacmackin/fix-inherit-heading-depth
Fix `inheritHeadingDepth` adding extra new lines
2 parents fd3c00a + 5135d01 commit 8da79f0

File tree

5 files changed

+215
-36
lines changed

5 files changed

+215
-36
lines changed

.gitignore

Lines changed: 172 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,174 @@
11
*~
2-
markdown_include.egg-info/
2+
3+
# temporary files which can be created if a process still has a handle open of a deleted file
4+
.fuse_hidden*
5+
6+
# KDE directory preferences
7+
.directory
8+
9+
# Linux trash folder which might appear on any partition or disk
10+
.Trash-*
11+
12+
# .nfs files are created when an open file is removed but is still being accessed
13+
.nfs*
14+
15+
# Byte-compiled / optimized / DLL files
16+
__pycache__/
17+
*.py[cod]
18+
*$py.class
19+
20+
# C extensions
21+
*.so
22+
23+
# Distribution / packaging
24+
.Python
25+
build/
26+
develop-eggs/
327
dist/
4-
*.pyc
5-
test.py
6-
test.md
28+
downloads/
29+
eggs/
30+
.eggs/
31+
lib/
32+
lib64/
33+
parts/
34+
sdist/
35+
var/
36+
wheels/
37+
share/python-wheels/
38+
*.egg-info/
39+
.installed.cfg
40+
*.egg
41+
MANIFEST
42+
43+
# PyInstaller
44+
# Usually these files are written by a python script from a template
45+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
46+
*.manifest
47+
*.spec
48+
49+
# Installer logs
50+
pip-log.txt
51+
pip-delete-this-directory.txt
52+
53+
# Unit test / coverage reports
54+
htmlcov/
55+
.tox/
56+
.nox/
57+
.coverage
58+
.coverage.*
59+
.cache
60+
nosetests.xml
61+
coverage.xml
62+
*.cover
63+
*.py,cover
64+
.hypothesis/
65+
.pytest_cache/
66+
cover/
67+
68+
# Translations
69+
*.mo
70+
*.pot
71+
72+
# Django stuff:
73+
*.log
74+
local_settings.py
75+
db.sqlite3
76+
db.sqlite3-journal
77+
78+
# Flask stuff:
79+
instance/
80+
.webassets-cache
81+
82+
# Scrapy stuff:
83+
.scrapy
84+
85+
# Sphinx documentation
86+
docs/_build/
87+
88+
# PyBuilder
89+
.pybuilder/
90+
target/
91+
92+
# Jupyter Notebook
93+
.ipynb_checkpoints
94+
95+
# IPython
96+
profile_default/
97+
ipython_config.py
98+
99+
# pyenv
100+
# For a library or package, you might want to ignore these files since the code is
101+
# intended to run in multiple environments; otherwise, check them in:
102+
# .python-version
103+
104+
# pipenv
105+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
106+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
107+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
108+
# install all needed dependencies.
109+
#Pipfile.lock
110+
111+
# poetry
112+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
113+
# This is especially recommended for binary packages to ensure reproducibility, and is more
114+
# commonly ignored for libraries.
115+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
116+
#poetry.lock
117+
118+
# pdm
119+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
120+
#pdm.lock
121+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
122+
# in version control.
123+
# https://pdm.fming.dev/#use-with-ide
124+
.pdm.toml
125+
126+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
127+
__pypackages__/
128+
129+
# Celery stuff
130+
celerybeat-schedule
131+
celerybeat.pid
132+
133+
# SageMath parsed files
134+
*.sage.py
135+
136+
# Environments
137+
.env
138+
.venv
139+
env/
140+
venv/
141+
ENV/
142+
env.bak/
143+
venv.bak/
144+
145+
# Spyder project settings
146+
.spyderproject
147+
.spyproject
148+
149+
# Rope project settings
150+
.ropeproject
151+
152+
# mkdocs documentation
153+
/site
154+
155+
# mypy
156+
.mypy_cache/
157+
.dmypy.json
158+
dmypy.json
159+
160+
# Pyre type checker
161+
.pyre/
162+
163+
# pytype static type analyzer
164+
.pytype/
165+
166+
# Cython debug symbols
167+
cython_debug/
168+
169+
# PyCharm
170+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
171+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
172+
# and can be added to the global gitignore or merged into this file. For a more nuclear
173+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
174+
#.idea/

markdown_include/include.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
# -*- coding: utf-8 -*-
32
#
43
# include.py
@@ -166,19 +165,17 @@ def run(self, lines):
166165
text.append("")
167166
for i in range(len(text)):
168167
# Strip the newline, and optionally increase header depth
169-
if self.inheritHeadingDepth or self.headingOffset:
170-
if HEADING_SYNTAX.search(text[i]):
171-
text[i] = text[i].rstrip("\r\n")
172-
if self.inheritHeadingDepth:
173-
text[i] = bonusHeading + text[i]
174-
if self.headingOffset:
175-
text[i] = "#" * self.headingOffset + text[i]
176-
else:
177-
text[i] = text[i].rstrip("\r\n")
168+
if HEADING_SYNTAX.search(text[i]):
169+
if self.inheritHeadingDepth:
170+
text[i] = bonusHeading + text[i]
171+
if self.headingOffset:
172+
text[i] = "#" * self.headingOffset + text[i]
173+
174+
text[i] = text[i].rstrip("\r\n")
178175
text_to_insert = "\r\n".join(text)
179176
line = line[: m.start()] + text_to_insert.strip() + line[m.end() :]
180177
del lines[loc]
181-
lines[loc:loc] = line.split("\r\n")
178+
lines[loc:loc] = line.splitlines()
182179
m = INC_SYNTAX.search(line)
183180

184181
else:

tests/resources/table_inner.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Inner title
2+
3+
| Aaaaa | aaaaa | sdknjhdjklfhd |
4+
| ----- | ----- | ------------- |
5+
| aaa | aaa | aaaa |

tests/resources/template_inside.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{!resources/simple.md!}
1+
{!simple.md!}
22

33
This is a template with a template.

tests/test_include.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99

10-
RESOURCE_DIR = pathlib.Path(__file__).parent.absolute()
10+
RESOURCE_DIR = pathlib.Path(__file__).parent.absolute() / "resources"
1111

1212

1313
@pytest.fixture(scope="module")
@@ -23,14 +23,14 @@ def markdown_include_inherit_heading_depth():
2323

2424

2525
def test_single_include(markdown_include):
26-
source = "{!resources/simple.md!}"
26+
source = "{!simple.md!}"
2727
html = markdown.markdown(source, extensions=[markdown_include])
2828

2929
assert html == "<p>This is a simple template</p>"
3030

3131

3232
def test_double_include(markdown_include):
33-
source = "{!resources/simple.md!} and {!resources/simple_2.md!}"
33+
source = "{!simple.md!} and {!simple_2.md!}"
3434
html = markdown.markdown(source, extensions=[markdown_include])
3535

3636
assert (
@@ -42,9 +42,9 @@ def test_headers(markdown_include):
4242
source = (
4343
"Source file\n"
4444
"# Heading Level 1 of main file\n"
45-
"{!resources/header.md!}\n"
45+
"{!header.md!}\n"
4646
"## Heading Level 2 of main file\n"
47-
"{!resources/header.md!}"
47+
"{!header.md!}"
4848
)
4949

5050
html = markdown.markdown(source, extensions=[markdown_include])
@@ -64,7 +64,7 @@ def test_headers(markdown_include):
6464

6565

6666
def test_embedded_template(markdown_include):
67-
source = "{!resources/template_inside.md!}"
67+
source = "{!template_inside.md!}"
6868
html = markdown.markdown(source, extensions=[markdown_include])
6969

7070
assert (
@@ -74,7 +74,7 @@ def test_embedded_template(markdown_include):
7474

7575

7676
def test_single_include_inherit_heading_depth(markdown_include_inherit_heading_depth):
77-
source = "{!resources/simple.md!}"
77+
source = "{!simple.md!}"
7878
html = markdown.markdown(
7979
source, extensions=[markdown_include_inherit_heading_depth]
8080
)
@@ -83,7 +83,7 @@ def test_single_include_inherit_heading_depth(markdown_include_inherit_heading_d
8383

8484

8585
def test_double_include_inherit_heading_depth(markdown_include_inherit_heading_depth):
86-
source = "{!resources/simple.md!} and {!resources/simple_2.md!}"
86+
source = "{!simple.md!} and {!simple_2.md!}"
8787
html = markdown.markdown(
8888
source, extensions=[markdown_include_inherit_heading_depth]
8989
)
@@ -97,9 +97,9 @@ def test_headers_inherit_heading_depth(markdown_include_inherit_heading_depth):
9797
source = (
9898
"Source file\n"
9999
"# Heading Level 1 of main file\n"
100-
"{!resources/header.md!}\n"
100+
"{!header.md!}\n"
101101
"## Heading Level 2 of main file\n"
102-
"{!resources/header.md!}"
102+
"{!header.md!}"
103103
)
104104

105105
html = markdown.markdown(
@@ -111,12 +111,12 @@ def test_headers_inherit_heading_depth(markdown_include_inherit_heading_depth):
111111
<p>Source file</p>
112112
<h1>Heading Level 1 of main file</h1>
113113
<h2>This heading will be one level deeper from the previous heading</h2>
114-
<p>More included file content.</p>
115-
<p>End of included content.</p>
114+
<p>More included file content.
115+
End of included content.</p>
116116
<h2>Heading Level 2 of main file</h2>
117117
<h3>This heading will be one level deeper from the previous heading</h3>
118-
<p>More included file content.</p>
119-
<p>End of included content.</p>"""
118+
<p>More included file content.
119+
End of included content.</p>"""
120120
)
121121

122122

@@ -135,17 +135,17 @@ def test_processor_lines():
135135
source = [
136136
"Source file",
137137
"# Heading Level 1 of main file",
138-
"{!resources/header.md!}",
138+
"{!header.md!}",
139139
"## Heading Level 2 of main file",
140-
"{!resources/header.md!}",
140+
"{!header.md!}",
141141
]
142142
result_lines = processor.run(source)
143143

144144
assert len(result_lines) == 9
145145

146146

147147
def test_include_lines(markdown_include):
148-
source = "{!resources/longer.md!lines=1 3}"
148+
source = "{!longer.md!lines=1 3}"
149149
html = markdown.markdown(source, extensions=[markdown_include])
150150

151151
assert html == dedent(
@@ -156,7 +156,7 @@ def test_include_lines(markdown_include):
156156

157157

158158
def test_include_line_range(markdown_include):
159-
source = "{!resources/longer.md!lines=3-5}"
159+
source = "{!longer.md!lines=3-5}"
160160
html = markdown.markdown(source, extensions=[markdown_include])
161161

162162
assert html == dedent(
@@ -168,7 +168,7 @@ def test_include_line_range(markdown_include):
168168

169169

170170
def test_include_lines_and_line_range(markdown_include):
171-
source = "{!resources/longer.md!lines=1 3-5 8}"
171+
source = "{!longer.md!lines=1 3-5 8}"
172172
html = markdown.markdown(source, extensions=[markdown_include])
173173

174174
assert html == dedent(
@@ -182,11 +182,20 @@ def test_include_lines_and_line_range(markdown_include):
182182

183183

184184
def test_include_lines_out_of_order(markdown_include):
185-
source = "{!resources/longer.md!lines=3 1}"
185+
source = "{!longer.md!lines=3 1}"
186186
html = markdown.markdown(source, extensions=[markdown_include])
187187

188188
assert html == dedent(
189189
"""\
190190
<p>This is line 3
191191
This is line 1</p>"""
192192
)
193+
194+
195+
def test_nested_table(markdown_include_inherit_heading_depth):
196+
source = "{!table_inner.md!}"
197+
html = markdown.markdown(
198+
source, extensions=[markdown_include_inherit_heading_depth, "tables"]
199+
)
200+
201+
assert "<table>" in html

0 commit comments

Comments
 (0)