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 9b643fd

Browse files
author
Roberto Alsina
committedMar 8, 2018
New shortcode support for misaka
1 parent c2bab5c commit 9b643fd

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed
 

‎v7/bbcode/bbcode.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def __init__(self):
5555
self.parser.add_simple_formatter("note", "")
5656

5757
def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
58-
"""Compile asciidoc into HTML strings."""
58+
"""Compile bbcode into HTML strings."""
5959
if not is_two_file:
60-
m_data, data = self.split_metadata(data, post, lang)
60+
_, data = self.split_metadata(data, post, lang)
6161
from nikola import shortcodes as sc
6262
new_data, shortcodes = sc.extract_shortcodes(data)
6363
output = self.parser.format(new_data)

‎v7/misaka/misaka.plugin

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

88
[Documentation]
99
Author = Chris Lee
10-
Version = 0.2.2
10+
Version = 0.3.2
1111
Website = http://c133.org/
1212
Description = Compile Markdown into HTML with Misaka instead of python-markdown

‎v7/misaka/misaka.py

+32-23
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
from __future__ import unicode_literals
3030

31-
import codecs
31+
import io
3232
import os
33-
import re
33+
34+
from nikola.plugin_categories import PageCompiler
35+
from nikola.utils import makedirs, req_missing, write_metadata
3436

3537
try:
3638
import misaka
@@ -45,13 +47,9 @@
4547
gist_extension = None
4648
podcast_extension = None
4749

48-
from nikola.plugin_categories import PageCompiler
49-
from nikola.utils import makedirs, req_missing, write_metadata
50-
5150

5251
class CompileMisaka(PageCompiler):
5352
"""Compile Misaka into HTML."""
54-
5553
name = "misaka"
5654
demote_headers = True
5755

@@ -66,21 +64,32 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
6664
if misaka is None:
6765
req_missing(['misaka'], 'build this site (compile with misaka)')
6866
makedirs(os.path.dirname(dest))
69-
with codecs.open(dest, "w+", "utf8") as out_file:
70-
with codecs.open(source, "r", "utf8") as in_file:
67+
with io.open(dest, "w+", encoding="utf8") as out_file:
68+
with io.open(source, "r", encoding="utf8") as in_file:
7169
data = in_file.read()
72-
if not is_two_file:
73-
data = re.split('(\n\n|\r\n\r\n)', data, maxsplit=1)[-1]
74-
output = misaka.html(data, extensions=self.ext)
75-
output, shortcode_deps = self.site.apply_shortcodes(output, filename=source, with_dependencies=True, extra_context=dict(post=post))
76-
out_file.write(output)
77-
if post is None:
78-
if shortcode_deps:
79-
self.logger.error(
80-
"Cannot save dependencies for post {0} (post unknown)",
81-
source)
70+
output, error_level, deps, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
71+
out_file.write(output)
72+
if post is None:
73+
if deps:
74+
self.logger.error(
75+
"Cannot save dependencies for post {0} (post unknown)",
76+
source)
77+
else:
78+
post._depfile[dest] += shortcode_deps
79+
if error_level == 0:
80+
return True
8281
else:
83-
post._depfile[dest] += shortcode_deps
82+
return False
83+
84+
def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
85+
"""Compile markdown into HTML strings."""
86+
if not is_two_file:
87+
_, data = self.split_metadata(data, post, lang)
88+
from nikola import shortcodes as sc
89+
new_data, shortcodes = sc.extract_shortcodes(data)
90+
output = misaka.html(new_data, extensions=self.ext)
91+
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
92+
return output, 0, [], shortcode_deps
8493

8594
def compile_html(self, source, dest, is_two_file=True):
8695
"""Compile the post into HTML (deprecated API)."""
@@ -101,9 +110,9 @@ def create_post(self, path, **kw):
101110
makedirs(os.path.dirname(path))
102111
if not content.endswith('\n'):
103112
content += '\n'
104-
with codecs.open(path, "wb+", "utf8") as fd:
113+
with io.open(path, "w+", encoding="utf8") as fd:
105114
if onefile:
106-
fd.write('<!-- \n')
107-
fd.write(write_metadata(metadata))
108-
fd.write('-->\n\n')
115+
fd.write('<!--\n')
116+
fd.write(write_metadata(metadata).strip())
117+
fd.write('\n-->\n\n')
109118
fd.write(content)

0 commit comments

Comments
 (0)
Please sign in to comment.