diff options
Diffstat (limited to 'plugins/asciidoc/asciidoc.py')
| -rw-r--r-- | plugins/asciidoc/asciidoc.py | 64 |
1 files changed, 46 insertions, 18 deletions
diff --git a/plugins/asciidoc/asciidoc.py b/plugins/asciidoc/asciidoc.py index 1a35c00..b7be3ef 100644 --- a/plugins/asciidoc/asciidoc.py +++ b/plugins/asciidoc/asciidoc.py @@ -30,12 +30,13 @@ You will need, of course, to install asciidoc """ -import codecs +import io import os +import shlex 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 @@ -49,28 +50,55 @@ class CompileAsciiDoc(PageCompiler): name = "asciidoc" demote_headers = True - def compile_html(self, source, dest, is_two_file=True): - makedirs(os.path.dirname(dest)) + 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') - try: - subprocess.check_call((binary, '-b', 'html5', '-s', '-o', dest, source)) - except OSError as e: - if e.strreror == 'No such file or directory': - req_missing(['asciidoc'], 'build this site (compile with asciidoc)', python=False) + options = self.site.config.get('ASCIIDOC_OPTIONS', '') + options = shlex.split(options) + command = [binary, '-b', 'html5', '-s'] + options + ['-'] + 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(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + output = p.communicate(input=new_data.encode('utf8'))[0].decode('utf8') + output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post}) + return output, 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)) + 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 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: + 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) |
