Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,6 @@ venv.bak/

# mypy
.mypy_cache/

# emacs backup files
*~
12 changes: 11 additions & 1 deletion MarkdownPP/Modules/Include.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Include(Module):
# matches unescaped formatting characters such as ` or _
formatre = re.compile(r"[^\\]?[_*`]")

# matches code block start and stop
# code blocks can also be indented by 4 spaces or a tab
# in which case we don't car because it does not match titlere
codeblockre = re.compile(r"```")

# includes should happen before anything else
priority = 0

Expand All @@ -55,12 +60,17 @@ def include_file(self, filename, pwd="", shift=0):
f = open(filename, "r", encoding = self.encoding)
data = f.readlines()
f.close()
in_codeblock = False

# line by line, apply shift and recursively include file data
linenum = 0
includednum = 0
for line in data:
match = self.includere.search(line)

if self.codeblockre.search(line):
in_codeblock = not in_codeblock

if match:
dirname = path.dirname(filename)
data[linenum:linenum+1] = self.include(match, dirname)
Expand All @@ -72,7 +82,7 @@ def include_file(self, filename, pwd="", shift=0):
if shift:

titlematch = self.titlere.search(line)
if titlematch:
if titlematch and not in_codeblock:
to_del = []
for _ in range(shift):
# Skip underlines with empty above text
Expand Down
2 changes: 1 addition & 1 deletion MarkdownPP/Modules/LaTeXRender.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def render(self, formula):
formula = formula.replace("$", "")
encoded_formula = formula.replace("%", "[comment]").replace("+", "%2B")
display_formula = formula.replace("\n", "")
print('Rendering: %s ...' % display_formula)
# print('Rendering: %s ...' % display_formula)

# Prepare POST request to QuickLaTeX via ProblemSetMarmoset
# (for added processing)
Expand Down
4 changes: 4 additions & 0 deletions test/datafiles/test_include_code.mdpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```
# uname -a
Linux mymachine 4.18.0-0.bpo.1-amd64 #1 SMP Debian 4.18.6-1~bpo9+1 (2018-09-13) x86_64 GNU/Linux
```
14 changes: 14 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ def test_include_url(self):
output.seek(0)
self.assertEqual(output.read(), result)


def test_include_issue66(self):
input = StringIO('foobar\n!INCLUDE "datafiles/test_include_code.mdpp", 2')
result = """foobar
```
# uname -a
Linux mymachine 4.18.0-0.bpo.1-amd64 #1 SMP Debian 4.18.6-1~bpo9+1 (2018-09-13) x86_64 GNU/Linux
```
"""
output = StringIO()
MarkdownPP(input=input, modules=['include'], output=output)
output.seek(0)
self.assertEqual(output.read(), result)

def test_youtube(self):
input = StringIO('foobar\n!VIDEO '
'"http://www.youtube.com/embed/7aEYoP5-duY"\n')
Expand Down