Skip to content

Commit

Permalink
Merge pull request #43 from cmacmackin/fix-inherit-heading-depth
Browse files Browse the repository at this point in the history
Fix `inheritHeadingDepth` adding extra new lines
  • Loading branch information
ZedThree authored Feb 7, 2023
2 parents fd3c00a + 5135d01 commit 8da79f0
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 36 deletions.
176 changes: 172 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,174 @@
*~
markdown_include.egg-info/

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
*.pyc
test.py
test.md
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
19 changes: 8 additions & 11 deletions markdown_include/include.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# include.py
Expand Down Expand Up @@ -166,19 +165,17 @@ def run(self, lines):
text.append("")
for i in range(len(text)):
# Strip the newline, and optionally increase header depth
if self.inheritHeadingDepth or self.headingOffset:
if HEADING_SYNTAX.search(text[i]):
text[i] = text[i].rstrip("\r\n")
if self.inheritHeadingDepth:
text[i] = bonusHeading + text[i]
if self.headingOffset:
text[i] = "#" * self.headingOffset + text[i]
else:
text[i] = text[i].rstrip("\r\n")
if HEADING_SYNTAX.search(text[i]):
if self.inheritHeadingDepth:
text[i] = bonusHeading + text[i]
if self.headingOffset:
text[i] = "#" * self.headingOffset + text[i]

text[i] = text[i].rstrip("\r\n")
text_to_insert = "\r\n".join(text)
line = line[: m.start()] + text_to_insert.strip() + line[m.end() :]
del lines[loc]
lines[loc:loc] = line.split("\r\n")
lines[loc:loc] = line.splitlines()
m = INC_SYNTAX.search(line)

else:
Expand Down
5 changes: 5 additions & 0 deletions tests/resources/table_inner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Inner title

| Aaaaa | aaaaa | sdknjhdjklfhd |
| ----- | ----- | ------------- |
| aaa | aaa | aaaa |
2 changes: 1 addition & 1 deletion tests/resources/template_inside.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{!resources/simple.md!}
{!simple.md!}

This is a template with a template.
49 changes: 29 additions & 20 deletions tests/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest


RESOURCE_DIR = pathlib.Path(__file__).parent.absolute()
RESOURCE_DIR = pathlib.Path(__file__).parent.absolute() / "resources"


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


def test_single_include(markdown_include):
source = "{!resources/simple.md!}"
source = "{!simple.md!}"
html = markdown.markdown(source, extensions=[markdown_include])

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


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

assert (
Expand All @@ -42,9 +42,9 @@ def test_headers(markdown_include):
source = (
"Source file\n"
"# Heading Level 1 of main file\n"
"{!resources/header.md!}\n"
"{!header.md!}\n"
"## Heading Level 2 of main file\n"
"{!resources/header.md!}"
"{!header.md!}"
)

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


def test_embedded_template(markdown_include):
source = "{!resources/template_inside.md!}"
source = "{!template_inside.md!}"
html = markdown.markdown(source, extensions=[markdown_include])

assert (
Expand All @@ -74,7 +74,7 @@ def test_embedded_template(markdown_include):


def test_single_include_inherit_heading_depth(markdown_include_inherit_heading_depth):
source = "{!resources/simple.md!}"
source = "{!simple.md!}"
html = markdown.markdown(
source, extensions=[markdown_include_inherit_heading_depth]
)
Expand All @@ -83,7 +83,7 @@ def test_single_include_inherit_heading_depth(markdown_include_inherit_heading_d


def test_double_include_inherit_heading_depth(markdown_include_inherit_heading_depth):
source = "{!resources/simple.md!} and {!resources/simple_2.md!}"
source = "{!simple.md!} and {!simple_2.md!}"
html = markdown.markdown(
source, extensions=[markdown_include_inherit_heading_depth]
)
Expand All @@ -97,9 +97,9 @@ def test_headers_inherit_heading_depth(markdown_include_inherit_heading_depth):
source = (
"Source file\n"
"# Heading Level 1 of main file\n"
"{!resources/header.md!}\n"
"{!header.md!}\n"
"## Heading Level 2 of main file\n"
"{!resources/header.md!}"
"{!header.md!}"
)

html = markdown.markdown(
Expand All @@ -111,12 +111,12 @@ def test_headers_inherit_heading_depth(markdown_include_inherit_heading_depth):
<p>Source file</p>
<h1>Heading Level 1 of main file</h1>
<h2>This heading will be one level deeper from the previous heading</h2>
<p>More included file content.</p>
<p>End of included content.</p>
<p>More included file content.
End of included content.</p>
<h2>Heading Level 2 of main file</h2>
<h3>This heading will be one level deeper from the previous heading</h3>
<p>More included file content.</p>
<p>End of included content.</p>"""
<p>More included file content.
End of included content.</p>"""
)


Expand All @@ -135,17 +135,17 @@ def test_processor_lines():
source = [
"Source file",
"# Heading Level 1 of main file",
"{!resources/header.md!}",
"{!header.md!}",
"## Heading Level 2 of main file",
"{!resources/header.md!}",
"{!header.md!}",
]
result_lines = processor.run(source)

assert len(result_lines) == 9


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

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


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

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


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

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


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

assert html == dedent(
"""\
<p>This is line 3
This is line 1</p>"""
)


def test_nested_table(markdown_include_inherit_heading_depth):
source = "{!table_inner.md!}"
html = markdown.markdown(
source, extensions=[markdown_include_inherit_heading_depth, "tables"]
)

assert "<table>" in html

0 comments on commit 8da79f0

Please sign in to comment.