Skip to content

Commit

Permalink
add support for metadata formats to asciidoc plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed Feb 16, 2018
1 parent 3f995d5 commit b8323aa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions v7/asciidoc/asciidoc.plugin
Expand Up @@ -3,10 +3,10 @@ Name = asciidoc
Module = asciidoc

[Nikola]
PluginCategory = Compiler
PluginCategory = PageCompiler

[Documentation]
Author = Roberto Alsina
Version = 0.4
Version = 0.5
Website = http://plugins.getnikola.com/#asciidoc
Description = Compile ASCIIDoc into HTML
66 changes: 35 additions & 31 deletions v7/asciidoc/asciidoc.py
Expand Up @@ -30,12 +30,12 @@
"""

import codecs
import io
import os
import subprocess

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

try:
from collections import OrderedDict
Expand All @@ -49,49 +49,53 @@ class CompileAsciiDoc(PageCompiler):
name = "asciidoc"
demote_headers = True

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile asciidoc into HTML strings."""
binary = self.site.config.get('ASCIIDOC_BINARY', 'asciidoc')
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)
p = subprocess.Popen((binary, '-b', 'html5', '-s', '-'), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output, err = p.communicate(input=new_data.encode('utf8'))
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})

return output.decode('utf8'), p.returncode, [], 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))
binary = self.site.config.get('ASCIIDOC_BINARY', 'asciidoc')
try:
subprocess.check_call((binary, '-b', 'html5', '-s', '-o', dest, source))
with open(dest, 'r', encoding='utf-8') as inf:
output, shortcode_deps = self.site.apply_shortcodes(inf.read(), with_dependencies=True)
with open(dest, 'w', encoding='utf-8') as outf:
outf.write(output)
with io.open(dest, "w+", encoding="utf8") as out_file:
with io.open(source, "r", encoding="utf8") as in_file:
data = in_file.read()
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 shortcode_deps:
if deps.list:
self.logger.error(
"Cannot save dependencies for post {0} (post unknown)",
source)
else:
post._depfile[dest] += shortcode_deps
except OSError as e:
print(e)
req_missing(['asciidoc'], 'build this site (compile with asciidoc)', python=False)

def compile_html(self, source, dest, is_two_file=True):
"""Compile the post into HTML (deprecated API)."""
try:
post = self.site.post_per_input_file[source]
except KeyError:
post = None

return compile(source, dest, is_two_file, post, None)
if error_level == 0:
return True
else:
return False

def create_post(self, path, **kw):
content = kw.pop('content', 'Write your post here.')
one_file = kw.pop('onefile', False) # NOQA
is_page = kw.pop('is_page', False) # NOQA
metadata = OrderedDict()
"""Create a new post."""
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
# is_page is not used by create_post as of now.
kw.pop('is_page', False)
metadata = {}
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
if not content.endswith('\n'):
content += '\n'
with codecs.open(path, "wb+", "utf8") as fd:
if one_file:
fd.write("////\n")
fd.write(write_metadata(metadata))
fd.write("////\n")
with io.open(path, "w+", encoding="utf8") as fd:
if onefile:
fd.write(write_metadata(metadata, comment_wrap=('///', '///'), site=self.site, compiler=self))
fd.write(content)

0 comments on commit b8323aa

Please sign in to comment.