|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright © 2012-2014 Roberto Alsina and others. Has conversations. Original line has conversations. |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any |
| 6 | +# person obtaining a copy of this software and associated |
| 7 | +# documentation files (the "Software"), to deal in the |
| 8 | +# Software without restriction, including without limitation |
| 9 | +# the rights to use, copy, modify, merge, publish, |
| 10 | +# distribute, sublicense, and/or sell copies of the |
| 11 | +# Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice |
| 15 | +# shall be included in all copies or substantial portions of |
| 16 | +# the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 19 | +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 20 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 21 | +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
| 22 | +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 23 | +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 24 | +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +"""Implementation of compile_html based on asciidoc. |
| 28 | +
|
| 29 | +You will need, of course, to install asciidoc |
| 30 | +
|
| 31 | +""" |
| 32 | + |
| 33 | +from __future__ import unicode_literals |
| 34 | + |
| 35 | +import codecs |
| 36 | +import io |
| 37 | +import os |
| 38 | +import re |
| 39 | +import subprocess |
| 40 | + |
| 41 | +from lxml import etree |
| 42 | +try: |
| 43 | + import smc.mw as mw |
| 44 | +except: |
| 45 | + mw = None |
| 46 | + |
| 47 | +from nikola.plugin_categories import PageCompiler |
| 48 | +from nikola.utils import makedirs, req_missing, write_metadata |
| 49 | + |
| 50 | +try: |
| 51 | + from collections import OrderedDict |
| 52 | +except ImportError: |
| 53 | + OrderedDict = dict # NOQA |
| 54 | + |
| 55 | + |
| 56 | +class CompileMediaWiki(PageCompiler): |
| 57 | + """Compile mediawiki into HTML.""" |
| 58 | + |
| 59 | + name = "mediawiki" |
| 60 | + demote_headers = True |
| 61 | + |
| 62 | + def compile_html(self, source, dest, is_two_file=True): |
| 63 | + makedirs(os.path.dirname(dest)) |
| 64 | + if mw is None: |
| 65 | + req_missing(['smc.mw'], 'build this site (compile with MediaWiki)', python=True) |
| 66 | + with io.open(dest, "w+", encoding="utf8") as out_file: |
| 67 | + with io.open(source, "r", encoding="utf8") as in_file: |
| 68 | + data = in_file.read() |
| 69 | + if not is_two_file: |
| 70 | + data = re.split('(\n\n|\r\n\r\n)', data, maxsplit=1)[-1] |
| 71 | + parser = mw.Parser(parseinfo=False, whitespace='', nameguard=False) |
| 72 | + ast = parser.parse(data, 'document', semantics=mw.Semantics(parser)) |
| 73 | + output = etree.tostring(ast, encoding='utf8').decode('utf8') |
| 74 | + out_file.write(output) |
| 75 | + |
| 76 | + def create_post(self, path, **kw): |
| 77 | + content = kw.pop('content', None) |
| 78 | + onefile = kw.pop('onefile', False) |
| 79 | + # is_page is not used by create_post as of now. |
| 80 | + kw.pop('is_page', False) |
| 81 | + |
| 82 | + metadata = {} |
| 83 | + metadata.update(self.default_metadata) |
| 84 | + metadata.update(kw) |
| 85 | + makedirs(os.path.dirname(path)) |
| 86 | + if not content.endswith('\n'): |
| 87 | + content += '\n' |
| 88 | + with io.open(path, "w+", encoding="utf8") as fd: |
| 89 | + if onefile: |
| 90 | + fd.write(write_metadata(metadata)) |
| 91 | + fd.write('\n\n') |
| 92 | + fd.write(content) |