Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple templates on one line #27

Merged
merged 3 commits into from Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -11,6 +11,11 @@ This module can now be installed using ``pip``.

pip install markdown-include

## Tests
Use the unittest module
```bash
python -m unittest discover unittests/
```

## Usage
This module can be used in a program in the following way:
Expand Down
17 changes: 8 additions & 9 deletions markdown_include/include.py
Expand Up @@ -82,7 +82,7 @@ def run(self, lines):
for loc, line in enumerate(lines):
m = INC_SYNTAX.search(line)

if m:
while m:
filename = m.group(1)
filename = os.path.expanduser(filename)
if not os.path.isabs(filename):
Expand All @@ -91,7 +91,7 @@ def run(self, lines):
)
try:
with open(filename, 'r', encoding=self.encoding) as r:
text = r.readlines()
text = self.run(r.readlines())

except Exception as e:
if not self.throwException:
Expand All @@ -102,7 +102,6 @@ def run(self, lines):
else:
raise e

line_split = INC_SYNTAX.split(line)
if len(text) == 0:
text.append('')
for i in range(len(text)):
Expand All @@ -116,12 +115,12 @@ def run(self, lines):
text[i] = '#' * self.headingOffset + text[i]
else:
text[i] = text[i].rstrip('\r\n')
text[0] = line_split[0] + text[0]
text[-1] = text[-1] + line_split[2]
lines = lines[:loc] + text + lines[loc+1:]
break
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')
m = INC_SYNTAX.search(line)

else:
h = HEADING_SYNTAX.search(line)
if h:
Expand Down
3 changes: 3 additions & 0 deletions unittests/resources/header.md
@@ -0,0 +1,3 @@
# This heading will be one level deeper from the previous heading
More included file content.
End of included content.
1 change: 1 addition & 0 deletions unittests/resources/simple.md
@@ -0,0 +1 @@
This is a simple template
1 change: 1 addition & 0 deletions unittests/resources/simple_2.md
@@ -0,0 +1 @@
This is another simple template
3 changes: 3 additions & 0 deletions unittests/resources/template_inside.md
@@ -0,0 +1,3 @@
{!resources/simple.md!}

This is a template with a template.
20 changes: 20 additions & 0 deletions unittests/test_embedded.py
@@ -0,0 +1,20 @@
import os
from unittest import TestCase

import markdown

from markdown_include.include import IncludePreprocessor, MarkdownInclude


class TestEmbedded(TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.markdown_include = MarkdownInclude(
configs={'base_path': os.path.dirname(os.path.realpath(__file__))}
)

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

self.assertEqual(html, "<p>This is a simple template</p>\n<p>This is a template with a template.</p>")
45 changes: 45 additions & 0 deletions unittests/test_include.py
@@ -0,0 +1,45 @@
import os
from unittest import TestCase

import markdown

from markdown_include.include import MarkdownInclude


class TestInclude(TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.markdown_include = MarkdownInclude(
configs={'base_path': os.path.dirname(os.path.realpath(__file__))}
)

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

self.assertEqual(html, '<p>This is a simple template</p>')

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

self.assertEqual(html, '<p>This is a simple template and This is another simple template</p>')

def test_headers(self):
source = "Source file\n" \
"# Heading Level 1 of main file\n" \
"{!resources/header.md!}\n" \
"## Heading Level 2 of main file\n" \
"{!resources/header.md!}"

html = markdown.markdown(source, extensions=[self.markdown_include])

self.assertEqual(html, "<p>Source file</p>\n"
"<h1>Heading Level 1 of main file</h1>\n"
"<h1>This heading will be one level deeper from the previous heading</h1>\n"
"<p>More included file content.\n"
"End of included content.</p>\n"
"<h2>Heading Level 2 of main file</h2>\n"
"<h1>This heading will be one level deeper from the previous heading</h1>\n"
"<p>More included file content.\n"
"End of included content.</p>")
45 changes: 45 additions & 0 deletions unittests/test_include_header_indent.py
@@ -0,0 +1,45 @@
import os
from unittest import TestCase

import markdown

from markdown_include.include import MarkdownInclude


class TestInclude(TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.markdown_include = MarkdownInclude(
configs={'base_path': os.path.dirname(os.path.realpath(__file__)), 'inheritHeadingDepth': True}
)

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

self.assertEqual(html, '<p>This is a simple template</p>')

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

self.assertEqual(html, '<p>This is a simple template and This is another simple template</p>')

def test_headers(self):
source = "Source file\n" \
"# Heading Level 1 of main file\n" \
"{!resources/header.md!}\n" \
"## Heading Level 2 of main file\n" \
"{!resources/header.md!}"

html = markdown.markdown(source, extensions=[self.markdown_include])

self.assertEqual(html, "<p>Source file</p>\n"
"<h1>Heading Level 1 of main file</h1>\n"
"<h2>This heading will be one level deeper from the previous heading</h2>\n"
"<p>More included file content.</p>\n"
"<p>End of included content.</p>\n"
"<h2>Heading Level 2 of main file</h2>\n"
"<h3>This heading will be one level deeper from the previous heading</h3>\n"
"<p>More included file content.</p>\n"
"<p>End of included content.</p>")
26 changes: 26 additions & 0 deletions unittests/test_lines.py
@@ -0,0 +1,26 @@
import os
from unittest import TestCase

from markdown_include.include import IncludePreprocessor


class TestProcessor(TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.processor = IncludePreprocessor(None, {
"base_path": os.path.dirname(os.path.realpath(__file__)),
"encoding": "utf-8",
"inheritHeadingDepth": False,
"headingOffset": 0,
"throwException": False,
})

def test_lines(self):
source = ["Source file",
"# Heading Level 1 of main file",
"{!resources/header.md!}",
"## Heading Level 2 of main file",
"{!resources/header.md!}"]
result_lines = self.processor.run(source)

self.assertEqual(9, len(result_lines))