Example #1
0
def _test_pandoc_ast(s):
    """Check pandoc -> podoc AST -> pandoc round-trip."""
    if not has_pandoc():  # pragma: no cover
        raise ImportError("pypandoc is not available")
    ast_dict = json.loads(pandoc(s, 'json', format=PANDOC_MARKDOWN_FORMAT))
    ast = ast_from_pandoc(ast_dict)
    ast.show()
    assert ast.to_pandoc() == ast_dict
Example #2
0
File: _ast.py Project: podoc/podoc
    def attach(self, podoc):
        if not has_pandoc():  # pragma: no cover
            logger.debug("pandoc is not available.")
            return

        source_langs, target_langs = get_pandoc_formats()

        # From pandoc source formats to AST.
        def _make_source_func(lang):
            def conv(doc, context=None):
                """Convert a document from `lang` to the podoc AST, via
                pandoc."""
                d = pandoc(doc, 'json', format=lang)
                # Convert the
                ast = ast_from_pandoc(json.loads(d))
                return ast
            return conv

        # podoc_langs = podoc.languages
        for source in source_langs:
            # if source in podoc_langs:
            #     continue
            func = _make_source_func(source)
            podoc.register_lang(source, pandoc=True,
                                file_ext=PANDOC_FILE_EXTENSIONS.get(source, None),
                                )
            podoc.register_func(source=source, target='ast', func=func)

        # From AST to pandoc target formats.
        def _make_target_func(lang):
            output_file_required = lang in PANDOC_OUTPUT_FILE_REQUIRED

            def conv(ast, context=None):
                """Convert a document from the podoc AST to `lang`, via pandoc."""
                d = json.dumps(ast.to_pandoc())
                context = context or {}
                kwargs = {}
                if output_file_required:
                    output = context.get('output', None)
                    if not output:
                        raise ValueError("The target language %s requires an output file.", lang)
                    kwargs = {'outputfile': output}
                    context['output_file_required'] = True
                out = pandoc(d, lang, format='json', **kwargs)
                return out
            return conv

        # podoc_langs = podoc.languages
        for target in target_langs:
            # if target in podoc_langs:
            #     continue
            func = _make_target_func(target)
            podoc.register_lang(target, pandoc=True,
                                file_ext=PANDOC_FILE_EXTENSIONS.get(source, None),
                                )
            podoc.register_func(source='ast', target=target, func=func)
Example #3
0
def _test_pandoc_ast(s):
    """Check the compatibility of the podoc AST with pandoc.

    * Perform Markdown -> pandoc AST dict -> podoc AST -> podoc AST dict
    * Check that pandoc AST dict = podoc AST dict

    """
    if not has_pandoc():  # pragma: no cover
        raise ImportError("pypandoc is not available")
    ast_dict = json.loads(pandoc(s, 'json', format=PANDOC_MARKDOWN_FORMAT))
    ast = ast_from_pandoc(ast_dict)
    ast.show()
    assert ast.to_pandoc() == ast_dict
Example #4
0
def _test_pandoc_ast(s):
    """Check the compatibility of the podoc AST with pandoc.

    * Perform Markdown -> pandoc AST dict -> podoc AST -> podoc AST dict
    * Check that pandoc AST dict = podoc AST dict

    """
    if not has_pandoc():  # pragma: no cover
        raise ImportError("pypandoc is not available")
    # NOTE: we disable pandoc Markdown extensions.
    ast_dict = json.loads(pandoc(s, 'json', format=PANDOC_MARKDOWN_FORMAT))
    ast = ast_from_pandoc(ast_dict)
    # ast.show()
    assert ast.to_pandoc() == ast_dict
Example #5
0
    def attach(self, podoc):
        if not has_pandoc():  # pragma: no cover
            logger.debug("pandoc is not available.")
            return

        source_langs, target_langs = get_pandoc_formats()

        # From pandoc source formats to AST.
        def _make_source_func(lang):
            def conv(doc):
                """Convert a document from `lang` to the podoc AST, via
                pandoc."""
                d = pandoc(doc, 'json', format=lang)
                # Convert the
                ast = ast_from_pandoc(json.loads(d))
                return ast

            return conv

        # podoc_langs = podoc.languages
        for source in source_langs:
            # if source in podoc_langs:
            #     continue
            func = _make_source_func(source)
            podoc.register_lang(source, pandoc=True)
            podoc.register_func(source=source, target='ast', func=func)

        # From AST to pandoc target formats.
        def _make_target_func(lang):
            def conv(ast):
                """Convert a document from the podoc AST to `lang`, via
                pandoc."""
                d = json.dumps(ast.to_pandoc())
                out = pandoc(d, lang, format='json')
                return out

            return conv

        # podoc_langs = podoc.languages
        for target in target_langs:
            # if target in podoc_langs:
            #     continue
            func = _make_target_func(target)
            podoc.register_lang(target, pandoc=True)
            podoc.register_func(source='ast', target=target, func=func)
Example #6
0
    def attach(self, podoc):
        if not has_pandoc():  # pragma: no cover
            logger.debug("pandoc is not available.")
            return

        source_langs, target_langs = get_pandoc_formats()

        # From pandoc source formats to AST.
        def _make_source_func(lang):
            def conv(doc):
                """Convert a document from `lang` to the podoc AST, via
                pandoc."""
                d = pandoc(doc, 'json', format=lang)
                # Convert the
                ast = ast_from_pandoc(json.loads(d))
                return ast
            return conv

        # podoc_langs = podoc.languages
        for source in source_langs:
            # if source in podoc_langs:
            #     continue
            func = _make_source_func(source)
            podoc.register_lang(source, pandoc=True)
            podoc.register_func(source=source, target='ast', func=func)

        # From AST to pandoc target formats.
        def _make_target_func(lang):
            def conv(ast):
                """Convert a document from the podoc AST to `lang`, via
                pandoc."""
                d = json.dumps(ast.to_pandoc())
                out = pandoc(d, lang, format='json')
                return out
            return conv

        # podoc_langs = podoc.languages
        for target in target_langs:
            # if target in podoc_langs:
            #     continue
            func = _make_target_func(target)
            podoc.register_lang(target, pandoc=True)
            podoc.register_func(source='ast', target=target, func=func)