def test_extension_use(self): markdown = marko.Markdown(extensions=["footnote", "toc"]) assert len(markdown._extra_elements) == 3 assert len(markdown._renderer_mixins) == 2 assert hasattr(markdown._renderer_mixins[1], "render_footnote_def")
def compile_readme_markdown(path: Path) -> Tuple[str, str]: """ Read and compile a markdown 'README' document, stripping the <h1> heading and returning the title and remainder of the document separately. Returns ======= title: str The title (free from HTML escape sequences) description: str The remainder of the compiled markdown HTML source. """ with path.open() as f: html = marko.Markdown()(f.read()) lines = html.splitlines(keepends=True) first_line = (lines[0] if lines else "").strip() if not (first_line.startswith("<h1>") and first_line.endswith("</h1>")): raise ReadmeMissingTitleError( f"{path} must start with a h1-level title.") title = first_line[len("<h1>"):-len("</h1>")] if "<" in title: raise ReadmeMalformedTitleError( f"{path} must have only simple text in its h1 title") title = unescape(title) description = "".join(lines[1:]) return title, description
def test_extension_use(self): markdown = marko.Markdown(extensions=['footnote', 'toc']) assert len(markdown._extra_elements) == 3 assert len(markdown._renderer_mixins) == 2 assert hasattr(markdown._renderer_mixins[1], 'render_footnote_def')
def load_file(doc: DocFile): if doc.file_data_path is None: doc.file_data_path = doc.file_path with doc.file_data_path.open('rb') as f: doc.file_bytes = f.read() doc.file_stat = os.stat(f.fileno()) doc.file_read_datetime = datetime.datetime.utcnow() text = doc.file_bytes.decode('UTF-8') m = marko.Markdown(parser=TrackingParser) md_doc = m.parse(text) # type: Any headings = md_doc.stramp_headings # type: List[Tuple[int, int]] raw_positions = get_raw_byte_offsets((pos for pos, level in headings), text) sections = [DocSection(0, '(ROOT)', 0, len(doc.file_bytes))] for (pos, level), raw_pos in zip(headings, raw_positions): sections.append(DocSection(level, '', start_offset=raw_pos)) doc.root_heading = link_sections(sections)
def test_ast_renderer_unescape_raw_text(self): markdown = marko.Markdown(renderer=ASTRenderer) res = markdown("<*") assert res["children"][0]["children"][0]["children"] == "<*" res = markdown(" <*") assert res["children"][0]["children"][0]["children"] == "<*\n"
def test_ast_renderer(self): text = "[Overview](#overview)\n\n* * *" markdown = marko.Markdown(renderer=ASTRenderer) res = markdown(text) assert isinstance(res, dict) assert res["element"] == "document" assert res["children"][0]["element"] == "paragraph"
def test_extension_with_illegal_element(self): class MyExtension: elements = [object] markdown = marko.Markdown(extensions=[MyExtension]) with pytest.raises(TypeError, match="The element should be a subclass of"): markdown.convert("hello world\n")
def test_markdown_renderer(self): with open("tests/samples/syntax.md", encoding="utf-8") as f: text = f.read() markdown = marko.Markdown(renderer=MarkdownRenderer) rerendered = markdown(text) assert normalize_html(marko.convert(rerendered)) == normalize_html( marko.convert(text))
def test_xml_renderer(self): from marko.ast_renderer import XMLRenderer text = "[Overview](#overview)" markdown = marko.Markdown(renderer=XMLRenderer) res = markdown(text) self.assertIn('<?xml version="1.0" encoding="UTF-8"?>', res) self.assertIn('dest="#overview"', res)
def test_ast_renderer(self): from marko.ast_renderer import ASTRenderer text = "[Overview](#overview)" markdown = marko.Markdown(renderer=ASTRenderer) res = markdown(text) assert isinstance(res, dict) assert res['element'] == 'document' assert res['children'][0]['element'] == 'paragraph'
def test_ast_renderer(self): from marko.ast_renderer import ASTRenderer text = "[Overview](#overview)" markdown = marko.Markdown(renderer=ASTRenderer) res = markdown(text) self.assertIsInstance(res, dict) self.assertEqual(res['element'], 'document') self.assertEqual(res['children'][0]['element'], 'paragraph')
def test_extension_override_non_base_element(self): class MyHeading(block.BlockElement): override = True class MyExtension: elements = [MyHeading] markdown = marko.Markdown(extensions=[MyExtension]) markdown._setup_extensions() assert markdown.parser.block_elements["MyHeading"] is MyHeading
def test_markdown_renderer(self): from marko.md_renderer import MarkdownRenderer with open('tests/samples/syntax.md') as f: text = f.read() markdown = marko.Markdown(renderer=MarkdownRenderer) rerendered = markdown(text) assert (normalize_html(marko.convert(rerendered)) == normalize_html( marko.convert(text)))
def test_extension_setup(self): markdown = marko.Markdown() markdown.use('footnote') markdown.convert('abc') with pytest.raises(marko.SetupDone): markdown.use('toc') assert hasattr(markdown.renderer, 'render_footnote_def')
def test_extension_override(self): class MyRendererMixin: def render_paragraph(self, element): return 'foo bar' class MyExtension: renderer_mixins = [MyRendererMixin] markdown = marko.Markdown(extensions=['gfm', MyExtension]) out = markdown.convert('hello world\n') assert out == 'foo bar'
def test_extension_override(self): class MyRendererMixin: def render_paragraph(self, element): return "foo bar" class MyExtension: renderer_mixins = [MyRendererMixin] markdown = marko.Markdown(extensions=["gfm", MyExtension]) out = markdown.convert("hello world\n") assert out == "foo bar"
def test_extension_setup(self): markdown = marko.Markdown() markdown.use("footnote") markdown.convert("abc") with pytest.raises(marko.SetupDone, match="Unable to register more extensions"): markdown.use("toc") assert hasattr(markdown.renderer, "render_footnote_def")
def test_extension_override_element(self): class MyHeading(block.Heading): override = True class MyExtension: elements = [MyHeading] markdown = marko.Markdown(extensions=[MyExtension]) markdown._setup_extensions() assert markdown.parser.block_elements["Heading"] is MyHeading assert markdown.parser.block_elements["Heading"].get_type( ) == "Heading"
def test_no_delegate_render_methods(self): class RendererMixin: def render_paragraph(self, element): return "ohohohohohoh" class Extension: renderer_mixins = [RendererMixin] markdown = marko.Markdown(renderer=ASTRenderer, extensions=[Extension]) res = markdown.convert("Hello world") paragraph = res["children"][0] assert isinstance(paragraph, dict) assert paragraph["element"] == "paragraph" raw_text = paragraph["children"][0] assert raw_text["children"] == "Hello world"
def main(args): if args.date: args.date = datetime.datetime.strptime(args.date, DATE_FMT) else: args.date = datetime.date.today() args.bump = Bump[args.bump] md = marko.Markdown(renderer=Renderer) for repo in find_repos(args.root, exclude=args.exclude): print(f"processing {repo}") wf_id = get_wf_id(repo) version = update_workflow(repo / wf_id, version=args.version, bump=args.bump) msg = f"Update for version {version}." if args.msg is None else args.msg update_changelog(repo / "CHANGELOG.md", md, version, msg, date=args.date, entry_type=args.entry_type)
def main(): namespace = parse(sys.argv[1:]) if namespace.document: with codecs.open(namespace.document, encoding="utf-8") as f: content = f.read() else: keystroke = "Ctrl+Z" if sys.platform.startswith("win") else "Ctrl+D" print( "Type in the markdown content to be converted. End with {}".format( keystroke), file=sys.stderr) content = sys.stdin.read() markdown = marko.Markdown(namespace.parser, namespace.renderer, extensions=namespace.extension) result = markdown(content) if namespace.output: with codecs.open(namespace.output, "w", encoding="utf-8") as f: f.write(result) else: print(result)
def get_md_parser(): "Get the Markdown parser for HTML. Allows for future extensions." return marko.Markdown()
def markdown2html(value): "Process the value from Markdown to HTML." return marko.Markdown(renderer=HtmlRenderer).convert(value or "")
def main(): markdown = marko.Markdown(renderer=MarkdownRenderer, extensions=[Extension]) text = stdin.read() output = markdown.convert(text) print(output)
assert False, element def render_literal(self, element): return self.render_raw_text(element) def render_raw_text(self, element): return element.children def render_line_break(self, element): return ' ' def render_code_span(self, element): return element.children markdown = marko.Markdown(renderer=RawStringRenderer) def convert_text(s: str) -> str: return markdown(s) def extract_type(d): if isinstance(d, str): return d type_name = d['type'] if type_name == 'reference': return extract_type(d['reference'])
def markdown(value): "Template filter: Convert Markdown to HMTL." return jinja2.utils.Markup(marko.Markdown().convert(value or ""))
def __init__(self, *args, **kwargs): super(CompileMarko, self).__init__(*args, **kwargs) if marko is not None: self.ext = ["toc", "footnote", "pangu", "codehilite"] self.markdown = marko.Markdown(extensions=self.ext)
def test_xml_renderer(self): text = "[Overview](#overview)\n\n* * *" markdown = marko.Markdown(renderer=XMLRenderer) res = markdown(text) assert '<?xml version="1.0" encoding="UTF-8"?>' in res assert 'dest="#overview"' in res
def render(self, content: str) -> str: gemini = marko.Markdown(renderer=GeminiRenderer) return str(gemini.convert(content).strip())
def render(content: str) -> str: markdown = marko.Markdown(extensions=["codehilite"]) return str(markdown.convert(content))