Skip to content

Commit

Permalink
New shortcode support for mistune
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed Mar 8, 2018
1 parent 9b643fd commit 8765db1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion v7/mistune/mistune.plugin
Expand Up @@ -7,6 +7,6 @@ PluginCategory = Compiler

[Documentation]
Author = Roberto Alsina
Version = 0.2
Version = 0.3
Website = http://plugins.getnikola.com/#mistune
Description = Compile Markdown into HTML with Mistune instead of python-markdown
56 changes: 32 additions & 24 deletions v7/mistune/mistune.py
Expand Up @@ -26,11 +26,11 @@

"""Implementation of compile_html based on Mistune."""

from __future__ import unicode_literals

import codecs
import io
import os
import re

from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing, write_metadata

try:
import mistune
Expand All @@ -41,9 +41,6 @@
except ImportError:
OrderedDict = dict # NOQA

from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing, write_metadata


class CompileMistune(PageCompiler):
"""Compile Markdown into HTML using Mistune."""
Expand All @@ -56,26 +53,37 @@ def __init__(self, *args, **kwargs):
if mistune is not None:
self.parser = mistune.Markdown()

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)
output = self.parser(new_data)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
return output, 0, [], 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."""
if mistune is None:
req_missing(['mistune'], 'build this site (compile with mistune)')
makedirs(os.path.dirname(dest))
with codecs.open(dest, "w+", "utf8") as out_file:
with codecs.open(source, "r", "utf8") as in_file:
with io.open(dest, "w+", encoding="utf8") as out_file:
with io.open(source, "r", encoding="utf8") as in_file:
data = in_file.read()
if not is_two_file:
data = re.split('(\n\n|\r\n\r\n)', data, maxsplit=1)[-1]
output = self.parser(data)
output, shortcode_deps = self.site.apply_shortcodes(output, filename=source, with_dependencies=True, extra_context=dict(post=post))
out_file.write(output)
if post is None:
if shortcode_deps:
self.logger.error(
"Cannot save dependencies for post {0} (post unknown)",
source)
output, error_level, deps, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
if post is None:
if deps:
self.logger.error(
"Cannot save dependencies for post {0} (post unknown)",
source)
else:
post._depfile[dest] += shortcode_deps
if error_level == 0:
return True
else:
post._depfile[dest] += shortcode_deps
return False

def compile_html(self, source, dest, is_two_file=True):
"""Compile the post into HTML (deprecated API)."""
Expand All @@ -96,9 +104,9 @@ def create_post(self, path, **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('<!--\n')
fd.write(write_metadata(metadata).strip())
fd.write('\n-->\n\n')
fd.write(content)

0 comments on commit 8765db1

Please sign in to comment.