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
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
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
def _test_renderer(s, *contains_nodes): """Test the renderer on a string.""" # Parse the string with CommonMark-py. ast = Markdown().read_markdown(s) print() print('****** PODOC AST ******') ast.show() # Check that the tree contains a node. if contains_nodes: assert _tree_contains_nodes(ast, contains_nodes) # Check markdown =(podoc)=> AST =(podoc)=> markdown. contents = ASTToMarkdown().transform(ast) assert contents.strip() == s # Check markdown =(podoc)=> AST =(pandoc)=> markdown. pandoc_json = json.dumps(ast.to_pandoc(), indent=2) markdown_pandoc = pandoc(pandoc_json, PANDOC_MARKDOWN_FORMAT, format='json') # NOTE: the pandoc-converted Markdown is not guaranteed to # be equal to the original Markdown document. For example * List # is transformed into - List, same with ATXHeader etc. # However, we do test that the generated JSON is compatible with pandoc. assert markdown_pandoc # Check markdown_pandoc =(podoc)=> (AST_pandoc == AST_podoc). ast_0 = Markdown().read_markdown(markdown_pandoc) print('****** PODOC AST (from markdown_pandoc) ******') ast_0.show() assert ast_0 == ast # Check markdown =(pandoc)=> (AST_pandoc == AST_podoc). p = pandoc(s, 'json', format=PANDOC_MARKDOWN_FORMAT) # NOTE: we use the '-' bullet char in the tests, for better compatibility # with pandoc. ast_1 = ast_from_pandoc(json.loads(p), bullet_char='-') print('****** PODOC AST (via pandoc) ******') ast_1.show() assert ast_1 == ast
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
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
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
def _test_renderer(s, *contains_nodes): """Test the renderer on a string.""" # Parse the string with CommonMark-py. ast = Markdown().read_markdown(s) ast.show() # Check that the tree contains a node. if contains_nodes: assert _tree_contains_nodes(ast, contains_nodes) # Render the AST to Markdown. contents = ASTToMarkdown().transform(ast) assert contents.strip() == s # markdown =(podoc)=> AST =(pandoc)=> markdown pandoc_json = json.dumps(ast.to_pandoc(), indent=2) markdown_pandoc = pandoc(pandoc_json, PANDOC_MARKDOWN_FORMAT, format='json') # NOTE: the pandoc-converted Markdown is not guaranteed to # be equal to the original Markdown document. For example * List # is transformed into - List, same with ATXHeader etc. # However, we do test that the generated JSON is compatible with pandoc. assert markdown_pandoc
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