Skip to content

Commit

Permalink
Add new shortcode support for bbcode plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed Mar 8, 2018
1 parent 1eca74e commit af7d4f4
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions v7/bbcode/bbcode.py
Expand Up @@ -26,17 +26,17 @@

"""Implementation of compile_html based on bbcode."""

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 bbcode
except ImportError:
bbcode = None # NOQA

from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing, write_metadata
try:
from collections import OrderedDict
except ImportError:
Expand All @@ -54,26 +54,37 @@ def __init__(self):
self.parser = bbcode.Parser()
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."""
if not is_two_file:
m_data, 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)
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 bbcode is None:
req_missing(['bbcode'], 'build this site (compile BBCode)')
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.format(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.list:
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 @@ -95,9 +106,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, "wb+", encoding="utf8") as fd:
if onefile:
fd.write('[note]<!--\n')
fd.write('[note]\n')
fd.write(write_metadata(metadata))
fd.write('-->[/note]\n\n')
fd.write('[/note]\n\n')
fd.write(content)

0 comments on commit af7d4f4

Please sign in to comment.