Skip to content
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
2 changes: 1 addition & 1 deletion pandoc_mustache/pandoc_mustache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def prepare(doc):
def action(elem, doc):
""" Apply combined mustache template to all strings in document.
"""
if type(elem) == Str and doc.mhash is not None:
if type(elem) in (Str, CodeBlock, Code) and doc.mhash is not None:
elem.text = doc.mrenderer.render(elem.text, doc.mhash)
return elem

Expand Down
45 changes: 45 additions & 0 deletions tests/test_mustache_in_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Test that a mustache variable in the code or codeblock will be replaced.
"""
import os, subprocess

def test_mustache_code(tmpdir):

# Define empty dictionaries
doc = {}
template = {}

# Prepare file names
doc['path'] = tmpdir.join("document.md")
template['path'] = tmpdir.join("template.yaml")

# Prepare file contents
doc['metadata'] = '''---
mustache: {mustachefile}
---
''' # quadruple curly brace will be printed as double curly brace, doubling is escaping from .format
doc['mfiles'] = { "mustachefile": template['path'] }
doc['code'] = """`echo 'Hello {{who}}!'`
```
printf 'Hello {{who}}!
```
"""
template['content'] = "who: 'world'"

# Write contents to files
with open(doc['path'].strpath, "a") as myfile:
myfile.write(doc['metadata'].format(**doc['mfiles']))
myfile.write(doc['code'])
template['path'].write(template['content'])

# Run pandoc
output = subprocess.check_output(["pandoc", doc['path'].strpath, "--filter", "pandoc-mustache", "--to=markdown", "--standalone"], universal_newlines=True)
# Remove header
tested_output = output.split('\n', 4)[4]

# Test output
print (tested_output)
assert tested_output == """`echo 'Hello world!'`

printf 'Hello world!
"""