Пример #1
0
    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
Пример #2
0
    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')
        options = self.site.config.get('ASCIIDOC_OPTIONS', '')
        options = shlex.split(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([binary, '-b', 'html5', '-s', '-'] + options,
                             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
Пример #3
0
    def compile_string(self,
                       data,
                       source_path=None,
                       is_two_file=True,
                       post=None,
                       lang=None):
        """Compile markdown into HTML strings."""
        if not is_two_file:
            _, data = self.split_metadata(data, post, lang)

        from nikola import shortcodes as sc
        new_data, shortcodes = sc.extract_shortcodes(data)

        # Kramdown takes a file as argument and prints to stdout
        with tempfile.NamedTemporaryFile(mode='w+', delete=False) as source:
            source.write(new_data)
        with tempfile.NamedTemporaryFile(mode='w+', delete=False) as dest:
            command = ['kramdown', '-o', 'html', '--no-auto-ids', source.name]
            subprocess.check_call(command, stdout=dest)
        with open(dest.name, 'r') as inf:
            output = inf.read()

        os.unlink(source.name)
        os.unlink(dest.name)
        output, shortcode_deps = self.site.apply_shortcodes_uuid(
            output,
            shortcodes,
            filename=source_path,
            extra_context={'post': post})

        return output, shortcode_deps
Пример #4
0
 def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
     """Compile Markdown into HTML strings."""
     if Markdown is None:
         req_missing(['markdown'], 'build this site (compile Markdown)')
     if not is_two_file:
         _, data = self.split_metadata(data)
     new_data, shortcodes = sc.extract_shortcodes(data)
     output, _ = self.converter.convert(new_data)
     output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
     return output, shortcode_deps
Пример #5
0
 def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
     """Compile Markdown into HTML strings."""
     if Markdown is None:
         req_missing(['markdown'], 'build this site (compile Markdown)')
     if not is_two_file:
         _, data = self.split_metadata(data, post, lang)
     new_data, shortcodes = sc.extract_shortcodes(data)
     output, _ = self.converter.convert(new_data)
     output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
     return output, shortcode_deps
Пример #6
0
    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
Пример #7
0
 def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
     """Compile docstrings into HTML strings, with shortcode support."""
     if not is_two_file:
         _, data = self.split_metadata(data, None, lang)
     new_data, shortcodes = sc.extract_shortcodes(data)
     # The way pdoc generates output is a bit inflexible
     with tempfile.TemporaryDirectory() as tmpdir:
         subprocess.check_call(['pdoc', '--html', '--html-dir', tmpdir] + shlex.split(new_data.strip()))
         fname = os.listdir(tmpdir)[0]
         with open(os.path.join(tmpdir, fname), 'r', encoding='utf8') as inf:
             output = inf.read()
     return self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
Пример #8
0
    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
Пример #9
0
    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
Пример #10
0
    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')
        options = self.site.config.get('ASCIIDOC_OPTIONS', '')
        options = shlex.split(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([binary, '-b', 'html5', '-s', '-'] + options, 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
Пример #11
0
 def compile_string(self,
                    data,
                    source_path=None,
                    is_two_file=True,
                    post=None,
                    lang=None):
     """Compile notebooks into HTML strings."""
     new_data, shortcodes = sc.extract_shortcodes(data)
     output = self._compile_string(
         nbformat.reads(new_data, current_nbformat))
     return self.site.apply_shortcodes_uuid(output,
                                            shortcodes,
                                            filename=source_path,
                                            extra_context={'post': post})
Пример #12
0
 def compile_string(self,
                    data,
                    source_path=None,
                    is_two_file=True,
                    post=None,
                    lang=None):
     """Compile HTML into HTML strings, with shortcode support."""
     if not is_two_file:
         _, data = self.split_metadata(data, post, lang)
     new_data, shortcodes = sc.extract_shortcodes(data)
     return self.site.apply_shortcodes_uuid(new_data,
                                            shortcodes,
                                            filename=source_path,
                                            extra_context={'post': post})
Пример #13
0
 def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
     """Compile docstrings into HTML strings, with shortcode support."""
     if not is_two_file:
         _, data = self.split_metadata(data, None, lang)
     new_data, shortcodes = sc.extract_shortcodes(data)
     # The way pdoc generates output is a bit inflexible
     path_templates = os.path.join(self.plugin_path, "tempaltes")
     LOGGER.info(f"set path tempaltes to {path_templates}")
     with tempfile.TemporaryDirectory() as tmpdir:
         subprocess.check_call(['pdoc', '--html', '--html-no-source', '--html-dir', tmpdir, "--template-dir", path_templates] + shlex.split(new_data.strip()))
         fname = os.listdir(tmpdir)[0]
         tmd_subdir = os.path.join(tmpdir, fname)
         fname = os.listdir(tmd_subdir)[0]
         LOGGER.info(f"tmpdir = {tmd_subdir}, fname = {fname}")
         with open(os.path.join(tmd_subdir, fname), 'r', encoding='utf8') as inf:
             output = inf.read()
     return self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
Пример #14
0
    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
Пример #15
0
    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 marko is None:
            req_missing(["marko"], "build this site (compile with marko)")
        if not is_two_file:
            _, data = self.split_metadata(data, post, lang)
        new_data, shortcodes = sc.extract_shortcodes(data)
        output = self.markdown.convert(new_data)
        output, shortcode_deps = self.site.apply_shortcodes_uuid(
            output,
            shortcodes,
            filename=source_path,
            extra_context={"post": post})
        return output, shortcode_deps
Пример #16
0
    def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
        """Compile reST into HTML strings."""
        # If errors occur, this will be added to the line number reported by
        # docutils so the line number matches the actual line number (off by
        # 7 with default metadata, could be more or less depending on the post).
        add_ln = 0
        if not is_two_file:
            m_data, data = self.split_metadata(data, post, lang)
            add_ln = len(m_data.splitlines()) + 1

        default_template_path = os.path.join(os.path.dirname(__file__), 'template.txt')
        settings_overrides = {
            'initial_header_level': 1,
            'record_dependencies': True,
            'stylesheet_path': None,
            'link_stylesheet': True,
            'syntax_highlight': 'short',
            # This path is not used by Nikola, but we need something to silence
            # warnings about it from reST.
            'math_output': 'mathjax /assets/js/mathjax.js',
            'template': default_template_path,
            'language_code': LEGAL_VALUES['DOCUTILS_LOCALES'].get(LocaleBorg().current_lang, 'en'),
            'doctitle_xform': self.site.config.get('USE_REST_DOCINFO_METADATA'),
            'file_insertion_enabled': self.site.config.get('REST_FILE_INSERTION_ENABLED'),
        }

        from nikola import shortcodes as sc
        new_data, shortcodes = sc.extract_shortcodes(data)
        if self.site.config.get('HIDE_REST_DOCINFO', False):
            self.site.rst_transforms.append(RemoveDocinfo)
        output, error_level, deps, _ = rst2html(
            new_data, settings_overrides=settings_overrides, logger=self.logger, source_path=source_path, l_add_ln=add_ln, transforms=self.site.rst_transforms)
        if not isinstance(output, str):
            # To prevent some weird bugs here or there.
            # Original issue: empty files.  `output` became a bytestring.
            output = output.decode('utf-8')

        output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
        return output, error_level, deps, shortcode_deps
Пример #17
0
    def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
        """Compile reST into HTML strings."""
        # If errors occur, this will be added to the line number reported by
        # docutils so the line number matches the actual line number (off by
        # 7 with default metadata, could be more or less depending on the post).
        add_ln = 0
        if not is_two_file:
            m_data, data = self.split_metadata(data, post, lang)
            add_ln = len(m_data.splitlines()) + 1

        default_template_path = os.path.join(os.path.dirname(__file__), 'template.txt')
        settings_overrides = {
            'initial_header_level': 1,
            'record_dependencies': True,
            'stylesheet_path': None,
            'link_stylesheet': True,
            'syntax_highlight': 'short',
            # This path is not used by Nikola, but we need something to silence
            # warnings about it from reST.
            'math_output': 'mathjax /assets/js/mathjax.js',
            'template': default_template_path,
            'language_code': LEGAL_VALUES['DOCUTILS_LOCALES'].get(LocaleBorg().current_lang, 'en')
        }

        from nikola import shortcodes as sc
        new_data, shortcodes = sc.extract_shortcodes(data)
        if self.site.config.get('HIDE_REST_DOCINFO', False):
            self.site.rst_transforms.append(RemoveDocinfo)
        output, error_level, deps, _ = rst2html(
            new_data, settings_overrides=settings_overrides, logger=self.logger, source_path=source_path, l_add_ln=add_ln, transforms=self.site.rst_transforms,
            no_title_transform=self.site.config.get('NO_DOCUTILS_TITLE_TRANSFORM', False))
        if not isinstance(output, unicode_str):
            # To prevent some weird bugs here or there.
            # Original issue: empty files.  `output` became a bytestring.
            output = output.decode('utf-8')

        output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
        return output, error_level, deps, shortcode_deps
Пример #18
0
 def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
     """Compile notebooks into HTML strings."""
     new_data, shortcodes = sc.extract_shortcodes(data)
     output = self._compile_string(nbformat.reads(new_data, current_nbformat))
     return self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, extra_context={'post': post})
Пример #19
0
def test_extract_shortcodes(input, expected, monkeypatch):
    i = iter('SC%d' % i for i in range(1, 100))
    monkeypatch.setattr(shortcodes, '_new_sc_id', i.__next__)
    extracted = shortcodes.extract_shortcodes(input)
    assert extracted == expected
Пример #20
0
def test_extract_shortcodes(input, expected, monkeypatch):
    i = iter('SC%d' % i for i in range(1, 100))
    monkeypatch.setattr(shortcodes, '_new_sc_id', i.__next__)
    extracted = shortcodes.extract_shortcodes(input)
    assert extracted == expected
Пример #21
0
 def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
     """Compile HTML into HTML strings, with shortcode support."""
     if not is_two_file:
         _, data = self.split_metadata(data, post, lang)
     new_data, shortcodes = sc.extract_shortcodes(data)
     return self.site.apply_shortcodes_uuid(new_data, shortcodes, filename=source_path, extra_context={'post': post})