Пример #1
0
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))
Пример #2
0
 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
Пример #3
0
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))
Пример #4
0
 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')])
Пример #5
0
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
Пример #6
0
        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)
Пример #7
0
 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]
Пример #8
0
 def render_text(self, text):
     return Text(paragraphs=[
         Paragraph(
             content=self.markdown_renderer.render(Document(par.content)))
         for par in text.paragraphs
     ])
Пример #9
0
        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())
Пример #10
0
 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')])
Пример #11
0
 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')])
Пример #12
0
 def markdownResultTest(self, markdown, expected):
     output = self.renderer.render(Document(markdown))
     self.assertEqual(expected, output)
Пример #13
0
def parse_document(raw: str) -> Document:
    """Parse a ZFM document."""
    set_zfm_tokens()
    doc = Document(raw)
    strip_comments(doc)
    return doc
Пример #14
0
def convert(markdown, link_resolver=(lambda path: path)):
    with NotionRenderer(link_resolver=link_resolver) as renderer:
        return renderer.render(Document(markdown))
Пример #15
0
def markdown(iterable, renderer=SanitizedHTMLRenderer):
    with renderer() as renderer:
        return renderer.render(Document(iterable))