Skip to content

Commit e9b2d4c

Browse files
author
Roberto Alsina
committedMar 8, 2018
new shortcode support for v8/kramdown
1 parent 6428468 commit e9b2d4c

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed
 

‎v8/kramdown/kramdown.py

+35-24
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
3535
"""
3636

37-
import codecs
37+
import io
3838
import os
39-
from os.path import abspath
4039
import subprocess
40+
import tempfile
4141

4242
from nikola.plugin_categories import PageCompiler
4343
from nikola.utils import makedirs, write_metadata
@@ -49,28 +49,39 @@ class CompileKramdown(PageCompiler):
4949
name = "kramdown"
5050
demote_headers = True
5151

52+
def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
53+
"""Compile markdown into HTML strings."""
54+
if not is_two_file:
55+
_, data = self.split_metadata(data, post, lang)
56+
57+
from nikola import shortcodes as sc
58+
new_data, shortcodes = sc.extract_shortcodes(data)
59+
60+
# Kramdown takes a file as argument and prints to stdout
61+
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as source:
62+
source.write(new_data)
63+
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as dest:
64+
command = ['kramdown', '-o', 'html', '--no-auto-ids', source.name]
65+
subprocess.check_call(command, stdout=dest)
66+
with open(dest.name, 'r') as inf:
67+
output = inf.read()
68+
69+
os.unlink(source.name)
70+
os.unlink(dest.name)
71+
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
72+
73+
return output, shortcode_deps
74+
5275
def compile(self, source, dest, is_two_file=True, post=None, lang=None):
5376
"""Compile the source file into HTML and save as dest."""
5477
makedirs(os.path.dirname(dest))
55-
command = ['kramdown', '-o', 'html', '--no-auto-ids', abspath(source)]
56-
57-
# Windows kludge
58-
if os.name == 'nt':
59-
command[4] = command[4].replace("\\", "\\\\")
60-
61-
with open(abspath(dest), 'wt') as f:
62-
subprocess.check_call(command, stdout=f)
63-
with open(dest, 'r', encoding='utf-8') as inf:
64-
output, shortcode_deps = self.site.apply_shortcodes(inf.read())
65-
with open(dest, 'w', encoding='utf-8') as outf:
66-
outf.write(output)
67-
if post is None:
68-
if shortcode_deps:
69-
self.logger.error(
70-
"Cannot save dependencies for post {0} (post unknown)",
71-
source)
72-
else:
73-
post._depfile[dest] += shortcode_deps
78+
with io.open(dest, "w+", encoding="utf8") as out_file:
79+
with io.open(source, "r", encoding="utf8") as in_file:
80+
data = in_file.read()
81+
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
82+
out_file.write(output)
83+
post._depfile[dest] += shortcode_deps
84+
return True
7485

7586
def create_post(self, path, content=None, onefile=False, is_page=False, **kw):
7687
"""Create post file with optional metadata."""
@@ -80,9 +91,9 @@ def create_post(self, path, content=None, onefile=False, is_page=False, **kw):
8091
makedirs(os.path.dirname(path))
8192
if not content.endswith('\n'):
8293
content += '\n'
83-
with codecs.open(path, "wb+", "utf8") as fd:
94+
with io.open(path, "w+", encoding="utf8") as fd:
8495
if onefile:
8596
fd.write("<!--\n")
86-
fd.write(write_metadata(metadata))
87-
fd.write("-->\n\n")
97+
fd.write(write_metadata(metadata).strip())
98+
fd.write("\n-->\n\n")
8899
fd.write(content)

0 commit comments

Comments
 (0)
Please sign in to comment.