Skip to content

Commit

Permalink
New shortcode support for misaka
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed Mar 8, 2018
1 parent c2bab5c commit 9b643fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
4 changes: 2 additions & 2 deletions v7/bbcode/bbcode.py
Expand Up @@ -55,9 +55,9 @@ def __init__(self):
self.parser.add_simple_formatter("note", "")

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile asciidoc into HTML strings."""
"""Compile bbcode into HTML strings."""
if not is_two_file:
m_data, data = self.split_metadata(data, post, lang)
_, data = self.split_metadata(data, post, lang)
from nikola import shortcodes as sc
new_data, shortcodes = sc.extract_shortcodes(data)
output = self.parser.format(new_data)
Expand Down
2 changes: 1 addition & 1 deletion v7/misaka/misaka.plugin
Expand Up @@ -7,6 +7,6 @@ PluginCategory = Compiler

[Documentation]
Author = Chris Lee
Version = 0.2.2
Version = 0.3.2
Website = http://c133.org/
Description = Compile Markdown into HTML with Misaka instead of python-markdown
55 changes: 32 additions & 23 deletions v7/misaka/misaka.py
Expand Up @@ -28,9 +28,11 @@

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 misaka
Expand All @@ -45,13 +47,9 @@
gist_extension = None
podcast_extension = None

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


class CompileMisaka(PageCompiler):
"""Compile Misaka into HTML."""

name = "misaka"
demote_headers = True

Expand All @@ -66,21 +64,32 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
if misaka is None:
req_missing(['misaka'], 'build this site (compile with misaka)')
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 = misaka.html(data, extensions=self.ext)
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_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 = misaka.html(new_data, extensions=self.ext)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
return output, 0, [], shortcode_deps

def compile_html(self, source, dest, is_two_file=True):
"""Compile the post into HTML (deprecated API)."""
Expand All @@ -101,9 +110,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 9b643fd

Please sign in to comment.