Skip to content

Commit

Permalink
new shortcode support for v8/kramdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed Mar 8, 2018
1 parent 6428468 commit e9b2d4c
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions v8/kramdown/kramdown.py
Expand Up @@ -34,10 +34,10 @@
"""

import codecs
import io
import os
from os.path import abspath
import subprocess
import tempfile

from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, write_metadata
Expand All @@ -49,28 +49,39 @@ class CompileKramdown(PageCompiler):
name = "kramdown"
demote_headers = True

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile markdown into HTML strings."""
if not is_two_file:
_, data = self.split_metadata(data, post, lang)

from nikola import shortcodes as sc
new_data, shortcodes = sc.extract_shortcodes(data)

# Kramdown takes a file as argument and prints to stdout
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as source:
source.write(new_data)
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as dest:
command = ['kramdown', '-o', 'html', '--no-auto-ids', source.name]
subprocess.check_call(command, stdout=dest)
with open(dest.name, 'r') as inf:
output = inf.read()

os.unlink(source.name)
os.unlink(dest.name)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})

return output, shortcode_deps

def compile(self, source, dest, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML and save as dest."""
makedirs(os.path.dirname(dest))
command = ['kramdown', '-o', 'html', '--no-auto-ids', abspath(source)]

# Windows kludge
if os.name == 'nt':
command[4] = command[4].replace("\\", "\\\\")

with open(abspath(dest), 'wt') as f:
subprocess.check_call(command, stdout=f)
with open(dest, 'r', encoding='utf-8') as inf:
output, shortcode_deps = self.site.apply_shortcodes(inf.read())
with open(dest, 'w', encoding='utf-8') as outf:
outf.write(output)
if post is None:
if shortcode_deps:
self.logger.error(
"Cannot save dependencies for post {0} (post unknown)",
source)
else:
post._depfile[dest] += shortcode_deps
with io.open(dest, "w+", encoding="utf8") as out_file:
with io.open(source, "r", encoding="utf8") as in_file:
data = in_file.read()
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
post._depfile[dest] += shortcode_deps
return True

def create_post(self, path, content=None, onefile=False, is_page=False, **kw):
"""Create post file with optional metadata."""
Expand All @@ -80,9 +91,9 @@ def create_post(self, path, content=None, onefile=False, is_page=False, **kw):
makedirs(os.path.dirname(path))
if not content.endswith('\n'):
content += '\n'
with codecs.open(path, "wb+", "utf8") as fd:
with io.open(path, "w+", encoding="utf8") as fd:
if onefile:
fd.write("<!--\n")
fd.write(write_metadata(metadata))
fd.write("-->\n\n")
fd.write(write_metadata(metadata).strip())
fd.write("\n-->\n\n")
fd.write(content)

0 comments on commit e9b2d4c

Please sign in to comment.