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 af7d4f4

Browse files
author
Roberto Alsina
committedMar 8, 2018
Add new shortcode support for bbcode plugin
1 parent 1eca74e commit af7d4f4

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed
 

‎v7/bbcode/bbcode.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626

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

29-
import codecs
29+
import io
3030
import os
31-
import re
31+
32+
from nikola.plugin_categories import PageCompiler
33+
from nikola.utils import makedirs, req_missing, write_metadata
3234

3335
try:
3436
import bbcode
3537
except ImportError:
3638
bbcode = None # NOQA
3739

38-
from nikola.plugin_categories import PageCompiler
39-
from nikola.utils import makedirs, req_missing, write_metadata
4040
try:
4141
from collections import OrderedDict
4242
except ImportError:
@@ -54,26 +54,37 @@ def __init__(self):
5454
self.parser = bbcode.Parser()
5555
self.parser.add_simple_formatter("note", "")
5656

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

7889
def compile_html(self, source, dest, is_two_file=True):
7990
"""Compile the post into HTML (deprecated API)."""
@@ -95,9 +106,9 @@ def create_post(self, path, **kw):
95106
makedirs(os.path.dirname(path))
96107
if not content.endswith('\n'):
97108
content += '\n'
98-
with codecs.open(path, "wb+", "utf8") as fd:
109+
with io.open(path, "wb+", encoding="utf8") as fd:
99110
if onefile:
100-
fd.write('[note]<!--\n')
111+
fd.write('[note]\n')
101112
fd.write(write_metadata(metadata))
102-
fd.write('-->[/note]\n\n')
113+
fd.write('[/note]\n\n')
103114
fd.write(content)

0 commit comments

Comments
 (0)
Please sign in to comment.