def markdown(iterable, renderer=HTMLRenderer): """ Output HTML with default settings. Enables inline and block-level HTML tags. """ with renderer() as renderer: return renderer.render(Document(iterable))
def compile(self, markdown, title, page_name, page_id): lines = self.render(Document(markdown)) page = Page( title=title, title_id=page_id, page_name=page_name, page_id=page_id, lines=lines, ) self.zhp.pages[page_id] = page
def markdown(iterable, renderer=HTMLRenderer): """ Converts markdown input to the output supported by the given renderer. If no renderer is supplied, ``HTMLRenderer`` is used. Note that extra token types supported by the given renderer are automatically (and temporarily) added to the parsing process. """ with renderer() as renderer: return renderer.render(Document(iterable))
def test_filter_conditions(self): import re filter_conds = [ lambda x: re.match(r'heading', x), lambda x: re.match(r'foo', x) ] renderer = TOCRenderer(filter_conds=filter_conds) token = Document( ['# title\n', '\n', '## heading\n', '\n', '#### not heading\n']) renderer.render(token) self.assertEqual(renderer._headings, [(4, 'not heading')])
def gen_preview(model: 'Model', content: str) -> str: with NotementumRenderer(model=model) as renderer: render = renderer.render(Document(content)) highlight_style = renderer.formatter.get_style_defs() with open(resource_filename('notementum', 'res/preview.html')) as f: preview = f.read() preview = preview.replace('{{{HIGHLIGHT_STYLE}}}', highlight_style) preview = preview.replace('{{{CONTENT}}}', render) return preview
def wrapper(self): # take the string after the last '__' in function name filename = func.__name__ filename = filename.split('__', 1)[1] # parse input markdown, call render on it and check the output with open('test/samples/{}.md'.format(filename), 'r') as fin: actual = self.renderer.render(Document(fin)) with open( 'test/samples/{}.{}'.format( filename, self.sampleOutputExtension), 'r') as expectedFin: expected = ''.join(expectedFin) self.assertEqual(expected, actual)
def render_markdown(self, text, par=True): # par=True mean we strip <p> and </p> rendered = self.markdown_renderer.render(Document(prepare(text))) return rendered if par else rendered[3:-5]
def render_text(self, text): return Text(paragraphs=[ Paragraph( content=self.markdown_renderer.render(Document(par.content))) for par in text.paragraphs ])
page_name = title_to_page_name(title) with open(filename) as f: m = f.read() filename_map[filename] = page_name pages.append((m, title, page_name, page_id)) zhp.links.links.append(Link(link=page_name, id=page_id)) zhp.toc.toc.append(TOCEntry(title=title, id=page_id)) page_id += 1 image_id = max(zhp.images) + 1 image_map = {} images = set() for m, _, _, _ in pages: with Gatherer() as renderer: images |= renderer.render(Document(m)) for i in images: print(f"adding image {i}") with open(i, "rb") as f: zhp.images[image_id] = Image(data=f.read()) image_map[i] = image_id image_id += 1 for m, title, page_name, page_id in pages: with PageRenderer(zhp, image_map, filename_map) as renderer: renderer.compile(m, title, page_name, page_id) with open(args.o, "wb") as f: f.write(zhp.serialize())
def test_omit_title(self): renderer = TOCRenderer(omit_title=True) token = Document(['# title\n', '\n', '## heading\n']) renderer.render(token) self.assertEqual(renderer._headings, [(2, 'heading')])
def test_depth(self): renderer = TOCRenderer(depth=3) token = Document(['# title\n', '## heading\n', '#### heading\n']) renderer.render(token) self.assertEqual(renderer._headings, [(2, 'heading')])
def markdownResultTest(self, markdown, expected): output = self.renderer.render(Document(markdown)) self.assertEqual(expected, output)
def parse_document(raw: str) -> Document: """Parse a ZFM document.""" set_zfm_tokens() doc = Document(raw) strip_comments(doc) return doc
def convert(markdown, link_resolver=(lambda path: path)): with NotionRenderer(link_resolver=link_resolver) as renderer: return renderer.render(Document(markdown))
def markdown(iterable, renderer=SanitizedHTMLRenderer): with renderer() as renderer: return renderer.render(Document(iterable))