Skip to content

Commit

Permalink
Implement compile_string in v8 (non-subprocess) compilers
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Sep 3, 2017
1 parent 3ab366b commit 93ea09a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 37 deletions.
20 changes: 15 additions & 5 deletions v8/bbcode/bbcode.py
Expand Up @@ -54,6 +54,20 @@ 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 the source file into HTML strings (with shortcode support).
Returns a tuple of at least two elements: HTML string [0] and shortcode dependencies [last].
"""
if bbcode is None:
req_missing(['bbcode'], 'build this site (compile BBCode)')
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
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, 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:
Expand All @@ -62,11 +76,7 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
with codecs.open(dest, "w+", "utf8") as out_file:
with codecs.open(source, "r", "utf8") as in_file:
data = in_file.read()
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
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, extra_context={'post': post})
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
if post is None:
if shortcode_deps:
Expand Down
20 changes: 15 additions & 5 deletions v8/commonmark/commonmark.py
Expand Up @@ -57,6 +57,20 @@ def __init__(self, *args, **kwargs):
self.parser = CommonMark.Parser()
self.renderer = CommonMark.HtmlRenderer()

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML strings (with shortcode support).
Returns a tuple of at least two elements: HTML string [0] and shortcode dependencies [last].
"""
if CommonMark is None:
req_missing(['commonmark'], 'build this site (compile with CommonMark)')
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = self.renderer.render(self.parser.parse(new_data))
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
return output, 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 CommonMark is None:
Expand All @@ -65,11 +79,7 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
with codecs.open(dest, "w+", "utf8") as out_file:
with codecs.open(source, "r", "utf8") as in_file:
data = in_file.read()
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = self.renderer.render(self.parser.parse(new_data))
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source, extra_context={'post': post})
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
if post is None:
if shortcode_deps:
Expand Down
18 changes: 13 additions & 5 deletions v8/markmin/markmin/__init__.py
Expand Up @@ -46,17 +46,25 @@ class CompileMarkmin(PageCompiler):
name = "markmin"
demote_headers = True

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML strings (with shortcode support).
Returns a tuple of at least two elements: HTML string [0] and shortcode dependencies [last].
"""
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = m2h.markmin2html(new_data, pretty_print=True)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
return output, 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."""
makedirs(os.path.dirname(dest))
with codecs.open(source, "rb+", "utf8") as in_f:
with codecs.open(dest, "wb+", "utf8") as out_f:
data = in_f.read()
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = m2h.markmin2html(new_data, pretty_print=True)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source, extra_context={'post': post})
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_f.write(output)
if post is None:
if shortcode_deps:
Expand Down
24 changes: 17 additions & 7 deletions v8/mediawiki/mediawiki.py
Expand Up @@ -57,6 +57,22 @@ class CompileMediaWiki(PageCompiler):
name = "mediawiki"
demote_headers = True

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML strings (with shortcode support).
Returns a tuple of at least two elements: HTML string [0] and shortcode dependencies [last].
"""
if mw is None:
req_missing(['smc.mw'], 'build this site (compile with MediaWiki)', python=True)
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
parser = mw.Parser(parseinfo=False, whitespace='', nameguard=False)
ast = parser.parse(new_data, 'document', semantics=mw.Semantics(parser))
output = etree.tostring(ast, encoding='utf8').decode('utf8')
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
return output, 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."""
makedirs(os.path.dirname(dest))
Expand All @@ -65,13 +81,7 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
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 = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
parser = mw.Parser(parseinfo=False, whitespace='', nameguard=False)
ast = parser.parse(new_data, 'document', semantics=mw.Semantics(parser))
output = etree.tostring(ast, encoding='utf8').decode('utf8')
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source, extra_context={'post': post})
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
if post is None:
if shortcode_deps:
Expand Down
20 changes: 15 additions & 5 deletions v8/misaka/misaka.py
Expand Up @@ -61,6 +61,20 @@ def __init__(self, *args, **kwargs):
self.ext = misaka.EXT_FENCED_CODE | misaka.EXT_STRIKETHROUGH | \
misaka.EXT_AUTOLINK | misaka.EXT_NO_INTRA_EMPHASIS

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML strings (with shortcode support).
Returns a tuple of at least two elements: HTML string [0] and shortcode dependencies [last].
"""
if misaka is None:
req_missing(['misaka'], 'build this site (compile with misaka)')
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
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, 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 misaka is None:
Expand All @@ -69,11 +83,7 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
with codecs.open(dest, "w+", "utf8") as out_file:
with codecs.open(source, "r", "utf8") as in_file:
data = in_file.read()
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
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, extra_context={'post': post})
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
if post is None:
if shortcode_deps:
Expand Down
20 changes: 15 additions & 5 deletions v8/mistune/mistune.py
Expand Up @@ -56,6 +56,20 @@ def __init__(self, *args, **kwargs):
if mistune is not None:
self.parser = mistune.Markdown()

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML strings (with shortcode support).
Returns a tuple of at least two elements: HTML string [0] and shortcode dependencies [last].
"""
if mistune is None:
req_missing(['mistune'], 'build this site (compile with mistune)')
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = self.parser(new_data)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
return output, 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 mistune is None:
Expand All @@ -64,11 +78,7 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
with codecs.open(dest, "w+", "utf8") as out_file:
with codecs.open(source, "r", "utf8") as in_file:
data = in_file.read()
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = self.parser(new_data)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source, extra_context={'post': post})
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
if post is None:
if shortcode_deps:
Expand Down
20 changes: 15 additions & 5 deletions v8/textile/textile.py
Expand Up @@ -50,6 +50,20 @@ class CompileTextile(PageCompiler):
name = "textile"
demote_headers = True

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML strings (with shortcode support).
Returns a tuple of at least two elements: HTML string [0] and shortcode dependencies [last].
"""
if textile is None:
req_missing(['textile'], 'build this site (compile Textile)')
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = textile(new_data, html_type='html5')
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
return output, 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 textile is None:
Expand All @@ -58,11 +72,7 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
with codecs.open(dest, "w+", "utf8") as out_file:
with codecs.open(source, "r", "utf8") as in_file:
data = in_file.read()
if not is_two_file:
_, data = self.split_metadata(data, post, lang)
new_data, shortcodes = sc.extract_shortcodes(data)
output = textile(new_data, html_type='html5')
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source, extra_context={'post': post})
output, shortcode_deps = self.compile_string(data, source, is_two_file, post, lang)
out_file.write(output)
if post is None:
if shortcode_deps:
Expand Down

0 comments on commit 93ea09a

Please sign in to comment.