Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8765db1

Browse files
author
Roberto Alsina
committedMar 8, 2018
New shortcode support for mistune
1 parent 9b643fd commit 8765db1

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed
 

‎v7/mistune/mistune.plugin

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ PluginCategory = Compiler
77

88
[Documentation]
99
Author = Roberto Alsina
10-
Version = 0.2
10+
Version = 0.3
1111
Website = http://plugins.getnikola.com/#mistune
1212
Description = Compile Markdown into HTML with Mistune instead of python-markdown

‎v7/mistune/mistune.py

+32-24
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

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

29-
from __future__ import unicode_literals
30-
31-
import codecs
29+
import io
3230
import os
33-
import re
31+
32+
from nikola.plugin_categories import PageCompiler
33+
from nikola.utils import makedirs, req_missing, write_metadata
3434

3535
try:
3636
import mistune
@@ -41,9 +41,6 @@
4141
except ImportError:
4242
OrderedDict = dict # NOQA
4343

44-
from nikola.plugin_categories import PageCompiler
45-
from nikola.utils import makedirs, req_missing, write_metadata
46-
4744

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

56+
def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
57+
"""Compile markdown into HTML strings."""
58+
if not is_two_file:
59+
_, data = self.split_metadata(data, post, lang)
60+
from nikola import shortcodes as sc
61+
new_data, shortcodes = sc.extract_shortcodes(data)
62+
output = self.parser(new_data)
63+
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
64+
return output, 0, [], shortcode_deps
65+
5966
def compile(self, source, dest, is_two_file=True, post=None, lang=None):
6067
"""Compile the source file into HTML and save as dest."""
6168
if mistune is None:
6269
req_missing(['mistune'], 'build this site (compile with mistune)')
6370
makedirs(os.path.dirname(dest))
64-
with codecs.open(dest, "w+", "utf8") as out_file:
65-
with codecs.open(source, "r", "utf8") as in_file:
71+
with io.open(dest, "w+", encoding="utf8") as out_file:
72+
with io.open(source, "r", encoding="utf8") as in_file:
6673
data = in_file.read()
67-
if not is_two_file:
68-
data = re.split('(\n\n|\r\n\r\n)', data, maxsplit=1)[-1]
69-
output = self.parser(data)
70-
output, shortcode_deps = self.site.apply_shortcodes(output, filename=source, with_dependencies=True, extra_context=dict(post=post))
71-
out_file.write(output)
72-
if post is None:
73-
if shortcode_deps:
74-
self.logger.error(
75-
"Cannot save dependencies for post {0} (post unknown)",
76-
source)
74+
output, error_level, deps, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
75+
out_file.write(output)
76+
if post is None:
77+
if deps:
78+
self.logger.error(
79+
"Cannot save dependencies for post {0} (post unknown)",
80+
source)
81+
else:
82+
post._depfile[dest] += shortcode_deps
83+
if error_level == 0:
84+
return True
7785
else:
78-
post._depfile[dest] += shortcode_deps
86+
return False
7987

8088
def compile_html(self, source, dest, is_two_file=True):
8189
"""Compile the post into HTML (deprecated API)."""
@@ -96,9 +104,9 @@ def create_post(self, path, **kw):
96104
makedirs(os.path.dirname(path))
97105
if not content.endswith('\n'):
98106
content += '\n'
99-
with codecs.open(path, "wb+", "utf8") as fd:
107+
with io.open(path, "w+", encoding="utf8") as fd:
100108
if onefile:
101-
fd.write('<!-- \n')
102-
fd.write(write_metadata(metadata))
103-
fd.write('-->\n\n')
109+
fd.write('<!--\n')
110+
fd.write(write_metadata(metadata).strip())
111+
fd.write('\n-->\n\n')
104112
fd.write(content)

0 commit comments

Comments
 (0)
Please sign in to comment.