Exemplo n.º 1
0
def format_text(
    text,
    useMarkdown,
    markdownStyle,
    markdownLineNums,
    markdownTabLength,
):
    if useMarkdown:
        noclasses = markdownStyle != 'default'
        html_ish = markdown.markdown(
            text,
            output_format="xhtml1",
            extensions=[
                SmartEmphasisExtension(),
                FencedCodeExtension(),
                FootnoteExtension(),
                AttrListExtension(),
                DefListExtension(),
                TableExtension(),
                AbbrExtension(),
                Nl2BrExtension(),
                CodeHiliteExtension(noclasses=noclasses,
                                    pygments_style=markdownStyle,
                                    linenums=markdownLineNums),
                SaneListExtension(),
                SmartyExtension()
            ],
            lazy_ol=False,
            tab_length=markdownTabLength,
        )
    else:
        # Preserve whitespace.
        html_ish = text.replace('\n', '<br>').replace(' ', '&nbsp;')
    return html_ish
Exemplo n.º 2
0
def parse(text):
    """
    https://github.com/Python-Markdown/markdown/wiki/Third-Party-Extensions
    https://facelessuser.github.io/pymdown-extensions
    """
    text = markdown.markdown(text,
                             extensions=[
                                 FootnoteExtension(),
                                 TableExtension(),
                                 FencedCodeExtension(),
                                 CodeHiliteExtension(),
                                 Nl2BrExtension(),
                                 TocExtension(slugify=slugs.uslugify,
                                              permalink=False),
                                 TrulySaneExt(),
                                 tilde.makeExtension(),
                                 caret.makeExtension(),
                             ])
    tags = 'a,h1,h2,h3,h4,h5,h6,p,div,pre,code,span,img,br,' \
           'ul,ol,li,table,tr,th,td,thead,tbody,blockquote,' \
           'del,em,strong,sub,sup'
    attrs = {'*': ['class'], 'a': ['href', 'rel'], 'img': ['alt']}
    attrs.update({f'h{n}': ['id']
                  for n in range(1, 7)})  # h1..h6 support TOC anchor
    text = bleach.clean(
        text,
        tags=tags.split(','),
        attributes=attrs,
    )  # HTML sanitizer
    return text
Exemplo n.º 3
0
def convert_markdown_to_html(clean_md):
    """
    Take a string `clean_md` and return a string where the Markdown syntax is
    converted to HTML.
    """

    assert isinstance(clean_md, unicode), "Input `clean_md` is not Unicode"

    new_html = markdown.markdown(clean_md,
                                 output_format="xhtml1",
                                 extensions=[
                                     SmartEmphasisExtension(),
                                     FencedCodeExtension(),
                                     FootnoteExtension(),
                                     AttrListExtension(),
                                     DefListExtension(),
                                     TableExtension(),
                                     AbbrExtension(),
                                     Nl2BrExtension(),
                                     CodeHiliteExtension(
                                         noclasses=True,
                                         pygments_style=preferences.PREFS.get(
                                             const.MARKDOWN_SYNTAX_STYLE),
                                         linenums=preferences.PREFS.get(
                                             const.MARKDOWN_LINE_NUMS)),
                                     SaneListExtension()
                                 ],
                                 lazy_ol=False)

    assert isinstance(new_html, unicode)

    return new_html
Exemplo n.º 4
0
    def extendMarkdown(self, md, md_globals):
        # Built-in extensions
        FencedCodeExtension().extendMarkdown(md, md_globals)
        SmartEmphasisExtension().extendMarkdown(md, md_globals)
        TableExtension().extendMarkdown(md, md_globals)

        #gfm.AutolinkExtension().extendMarkdown(md, md_globals)
        #gfm.AutomailExtension().extendMarkdown(md, md_globals)
        #gfm.HiddenHiliteExtension([
        #    ('guess_lang', 'False'),
        #    ('css_class', 'highlight')
        #]).extendMarkdown(md, md_globals)
        #gfm.SemiSaneListExtension().extendMarkdown(md, md_globals)
        #gfm.SpacedLinkExtension().extendMarkdown(md, md_globals)
        #gfm.StrikethroughExtension().extendMarkdown(md, md_globals)
        # Custom extensions
        AutolinkExtension().extendMarkdown(md, md_globals)
        AutomailExtension().extendMarkdown(md, md_globals)
        HiddenHiliteExtension([
            ('guess_lang', 'False'),
            ('css_class', 'highlight')
        ]).extendMarkdown(md, md_globals)
        SemiSaneListExtension().extendMarkdown(md, md_globals)
        SpacedLinkExtension().extendMarkdown(md, md_globals)
        StrikethroughExtension().extendMarkdown(md, md_globals)
Exemplo n.º 5
0
    def extendMarkdown(self, md, md_globals):
        # Built-in extensions
        gfmFencedCodeExtension().extendMarkdown(md)
        #FencedCodeExtension().extendMarkdown(md)
        #FencedCodeExtension().extendMarkdown(md, md_globals)
        #SmartEmphasisExtension().extendMarkdown(md, md_globals)
        #TableExtension().extendMarkdown(md, md_globals)
        TableExtension().extendMarkdown(md)

        # Custom extensions
        gfm.AutolinkExtension().extendMarkdown(md, md_globals)
        gfm.AutomailExtension().extendMarkdown(md, md_globals)
        # gfm.HiddenHiliteExtension([
        #     ('guess_lang', 'False'),
        #     ('css_class', 'highlight')
        # ]).extendMarkdown(md, md_globals)
        # print(md_globals)
        # print(self.config)
        # print(dir(md))
        # raise KeyboardInterrupt()
        #print(self.config)
        #gfm.HiddenHiliteExtension().extendMarkdown(md, md_globals)
        gfm.HiddenHiliteExtension().extendMarkdown(md, self.config)
        gfm.SemiSaneListExtension().extendMarkdown(md, md_globals)
        gfm.SpacedLinkExtension().extendMarkdown(md, md_globals)
        gfm.StrikethroughExtension().extendMarkdown(md, md_globals)
        gfm.TaskListExtension().extendMarkdown(md, md_globals)
Exemplo n.º 6
0
def render_instructions(step):  # TODO deduplicate markdown cleanup code
    instructions = step.instruction

    tags = markdown_tags + [
        'pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead', 'img'
    ]
    parsed_md = md.markdown(instructions,
                            extensions=[
                                'markdown.extensions.fenced_code',
                                TableExtension(),
                                UrlizeExtension(),
                                MarkdownFormatExtension()
                            ])
    markdown_attrs['*'] = markdown_attrs['*'] + ['class', 'width', 'height']

    instructions = bleach.clean(parsed_md, tags, markdown_attrs)

    ingredients = []

    for i in step.ingredients.all():
        ingredients.append(IngredientObject(i))

    try:
        template = Template(instructions)
        instructions = template.render(ingredients=ingredients)
    except TemplateSyntaxError:
        return _('Could not parse template code.'
                 ) + ' Error: Template Syntax broken'
    except UndefinedError:
        return _('Could not parse template code.') + ' Error: Undefined Error'

    return instructions
Exemplo n.º 7
0
def parse_markdown(text, noclasses, style, line_nums, tab_len, mathext):
    extensions = []
    if mathext: extensions.append(MathExtension())
    extensions.extend([
        FencedCodeExtension(),
        FootnoteExtension(),
        AttrListExtension(),
        DefListExtension(),
        TableExtension(),
        AbbrExtension(),
        Nl2BrExtension(),
        CodeHiliteExtension(
            noclasses=noclasses,
            pygments_style=style,
            linenums=line_nums,
        ),
        SaneListExtension(),
        SmartyExtension()
    ])
    return markdown.markdown(
        text,
        output_format="xhtml1",
        extensions=extensions,
        lazy_ol=False,
        tab_length=tab_len,
    )
Exemplo n.º 8
0
 def save(self, *args, **kwargs):
     self.html = markdown(
         self.data,
         extensions=[TableExtension(),
                     GithubFlavoredMarkdownExtension()],
         safe_mode=True,
         enable_attributes=False)
     super(NoteMixin, self).save(*args, **kwargs)
Exemplo n.º 9
0
 def _render_documentation(self, path, detail):
     with open(path, "r") as f:
         content = markdown(f.read(),
                            extensions=[
                                TableExtension(),
                                FSMExtension(),
                                BlockdiagExtension(format="svg")
                            ])
         return content
Exemplo n.º 10
0
    def save(self, *args, **kwargs):
        self.html = markdown(self.data,
        extensions=[TableExtension(), GithubFlavoredMarkdownExtension()], safe_mode=True,  
        enable_attributes=False)

        # Define the new card with the greatest priority.
        if not self.pk and self.ancestor:
            self.set_priority()
        super(CardMixin, self).save(*args, **kwargs)
Exemplo n.º 11
0
def mdToHtml(md, _extensions, configs):
    html = markdown.markdown(md,
                             extensions=[
                                 SaneListExtension(),
                                 TableExtension(),
                                 FencedCodeExtension(),
                                 DeleteSubExtension(**configs)
                             ] + strToClassEXt(_extensions))
    return html
Exemplo n.º 12
0
def markdown(value, header_level=1):
    html = md(value,
              output_format='html5', safe_mode='escape',
              extensions=[
                  TocExtension(baselevel=header_level),
                  CodeHiliteExtension(),
                  FencedCodeExtension(),
                  TableExtension(),
              ])
    return mark_safe(html)
Exemplo n.º 13
0
 def render(self, gs, *, baselevel=2, hyphenate=False, paragraphs=None):
     md = Markdown(extensions=[
             MetaExtension(),
             TableExtension(),
             WikiLinkExtension(),
             CMS7Extension(gs, path=self.source, baselevel=baselevel, hyphenate=hyphenate, paragraphs=paragraphs),
             TocExtension(anchorlink=True)
         ],
         output_format='html5')
     return Markup(md.convert(self.text))
Exemplo n.º 14
0
 def render(self, gs):
     md = Markdown(extensions=[
         MetaExtension(),
         TableExtension(),
         WikiLinkExtension(),
         CMS7Extension(gs, baselevel=2),
         TocExtension()
     ],
                   output_format='html5')
     return Markup(md.convert(self.text))
Exemplo n.º 15
0
    def extendMarkdown(self, md):
        # Built-in extensions
        TableExtension().extendMarkdown(md)

        # Custom extensions
        gfm.AutolinkExtension().extendMarkdown(md)
        gfm.AutomailExtension().extendMarkdown(md)
        gfm.SemiSaneListExtension().extendMarkdown(md)
        gfm.StandaloneFencedCodeExtension().extendMarkdown(md)
        gfm.StrikethroughExtension().extendMarkdown(md)
        gfm.TaskListExtension().extendMarkdown(md)
Exemplo n.º 16
0
def help():
    current_dir = os.path.join(os.path.dirname(__file__))
    help_content = open(os.path.join(current_dir, "../templates/help.md"), "r").read()

    output_text = re.sub("<table>", '<table class="table">', markdown.markdown(help_content,
        extensions=[TocExtension(baselevel=1,
            title="Overview", toc_depth="2-3"), TableExtension()]))
    return render_template("help.html",
                markdown_text = Markup(output_text),
                loggedinuser=current_user
        )
Exemplo n.º 17
0
    def save(self, *args, **kwargs):
        self.html = markdown(
            self.data,
            extensions=[TableExtension(),
                        GithubFlavoredMarkdownExtension()],
            safe_mode=True,
            enable_attributes=False)

        if not self.pk and self.ancestor:
            self.set_priority()

        super(PostMixin, self).save(*args, **kwargs)
Exemplo n.º 18
0
    def generateHtml(self):
        try:
            extensions = [
                TableExtension(),
                WikiLinkExtension(html_class='internal', build_url=urlBuilder)
            ]
            source = self.blob.data.decode('utf-8')
            html = markdown.markdown(source, extensions=extensions)

            self.metadata['as'] = {'html': html}
        except Exception, e:
            print "markdown processing failed on", self.name, str(e)
Exemplo n.º 19
0
def markdown(value):
    tags = markdown_tags + [
        'pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead'
    ]
    parsed_md = md.markdown(
        value,
        extensions=[
            'markdown.extensions.fenced_code', TableExtension(),
            UrlizeExtension(), MarkdownFormatExtension()
        ]
    )
    markdown_attrs['*'] = markdown_attrs['*'] + ['class']
    return bleach.clean(parsed_md, tags, markdown_attrs)
Exemplo n.º 20
0
    def extendMarkdown(self, md, md_globals):
        # Built-in extensions
        FencedCodeExtension().extendMarkdown(md)
        TableExtension().extendMarkdown(md)

        # Custom extensions
        gfm.AutolinkExtension().extendMarkdown(md, md_globals)
        gfm.AutomailExtension().extendMarkdown(md, md_globals)

        gfm.HiddenHiliteExtension().extendMarkdown(md, self.config)
        gfm.SemiSaneListExtension().extendMarkdown(md, md_globals)
        gfm.SpacedLinkExtension().extendMarkdown(md, md_globals)
        gfm.StrikethroughExtension().extendMarkdown(md, md_globals)
        gfm.TaskListExtension().extendMarkdown(md, md_globals)
Exemplo n.º 21
0
    def renderContent(self) -> None:
        self.checkModified()

        if self.__renderedStr is not None:
            # Previous render is still valid.
            return
        if self.errors & DocErrors.CONTENT:
            # Loading failed; nothing to render.
            return
        if self.errors & DocErrors.RENDERING:
            # Rendering attempted and failed.
            return

        # Load content.
        contentPath = self.contentPath
        if contentPath is None:
            # If the init module fails to import, resources in the package
            # are inaccessible. Don't try to load them, to avoid error spam.
            return
        packageName = self.resource.packageName
        try:
            content = importlib_resources.read_text(packageName,
                                                    contentPath.name)
        except Exception:
            logging.exception('Error loading documentation content "%s"',
                              contentPath.name)
            self.errors |= DocErrors.CONTENT
            return

        # Create a private Markdown converter.
        # Rendering a the table of content will trigger Markdown conversion
        # of child pages, so we can't use a single shared instance.
        extractor = ExtractionExtension()
        md = Markdown(extensions=[
            extractor,
            FixupExtension(),
            DefListExtension(),
            FencedCodeExtension(),
            CodeHiliteExtension(guess_lang=False),
            TableExtension()
        ])

        # Do the actual rendering.
        try:
            self.__renderedStr = md.convert(content)
        except Exception:
            logging.exception('Error rendering Markdown for %s', packageName)
            self.errors |= DocErrors.RENDERING
        else:
            self.__extractedInfo = extractor.extracted
Exemplo n.º 22
0
def documentation_formats():
    """
    Documentation formats page
    """
    with open(os.path.join(app_folder, "md", "doc_formats.md"),
              "r",
              encoding='utf-8') as install_instr:
        content = install_instr.read()
    md = Markdown(extensions=[TocExtension(baselevel=1), TableExtension()])
    content = Markup(md.convert(content))
    toc = Markup(md.toc)
    return render_template("documentation.html",
                           menu="documentation",
                           content=content,
                           toc=toc)
Exemplo n.º 23
0
def md2html(md_text):
    css = '''
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style scoped="scoped">
                div {float: left;margin-bottom: 2%}
                table {
                    border-collapse: collapse;
                    border-spacing: 0;
                    empty-cells: show;
                    border: 1px solid #cbcbcb;
                    {#font-size: 3px;#}
                    margin: 1% auto;
                    table-layout: fixed;
                    {#word-break: break-all;#}
                    word-wrap: break-word;
                }
                table td, th {
                    border-left: 1px solid #cbcbcb;
                    border-width: 0 0 0 1px;
                    margin: 0;
                    padding: 0.3em 0.5em;
                    width:100px
                }
                table td:first-child, table th:first-child {
                    border-left-width: 0;
                }
                table thead, table tfoot {
                    color: #000;
                    text-align: left;
                    vertical-align: bottom;
                }
                table thead {
                    background: #e0e0e0;
                }
                table tfoot {
                    background: #ededed;
                }
                table tr:nth-child(2n-1) td {
                    background-color: #f2f2f2;
                }
                </style>
    '''
    html = markdown.markdown(
        md_text, extensions=[FencedCodeExtension(),
                             TableExtension()])
    return css + html
Exemplo n.º 24
0
    def generateHTML(md_filename=None, source=None, css=None):
        if md_filename is None:
            if source is None:
                raise ValueError('You must supply md_filename or source')
            html_filename = tempfile.mktemp('html', 'md-edit')
        else:
            with open(md_filename, 'r') as f:
                source = f.read()
            html_filename = os.path.splitext(md_filename)[0] + '.html'

        extras = [
            'fenced-code-blocks', 'toc', 'footnotes', 'wiki-tables',
            'code-friendly'
        ]
        extensions = [
            TocExtension(baselevel=3),
            FencedCodeExtension(),
            TableExtension(),
            AdmonitionExtension(),
            CodeHiliteExtension()
        ]

        html = Markdown(extras=extras, extensions=extensions).convert(source)

        # custom postprocess

        # strike
        strike_pathern = re.compile(r'~{2}(.*?)~{2}')
        html = re.sub(strike_pathern, r'<del>\1</del>', html)

        # quoted
        quoted_pathern = re.compile(r'\:\"(.*?)\"\:')
        html = re.sub(quoted_pathern, r'&ldquo;\1&rdquo;', html)

        with open(html_filename, 'w') as f:
            f.write(html)

        if not css is None:
            html = '<style>' + css + '</style>' + html

        return html
Exemplo n.º 25
0
    def __init__(self, status: str):
        """
        Initialize.

        Arguments:
            status: the content of the SECURITY.md file.
        """

        self.headers = []
        self.data = []

        markdown_instance = markdown.Markdown(extensions=[TableExtension()])

        elem = markdown_instance.parser.parseDocument(
            [s for s in status.split("\n") if s != "" and s[0] != "#" and s[0] != "["]
        )
        self._pe(elem.getroot())

        self.data = [r for r in self.data if len([c for c in r if c is not None]) > 0]
        for row in self.data:
            row.append("")
Exemplo n.º 26
0
 def test_align_columns_legacy(self):
     self.assertMarkdownRenders(
         self.dedent(
             """
             | Item      | Value |
             | :-------- | -----:|
             | Computer  | $1600 |
             | Phone     |   $12 |
             | Pipe      |    $1 |
             """
         ),
         self.dedent(
             """
             <table>
             <thead>
             <tr>
             <th align="left">Item</th>
             <th align="right">Value</th>
             </tr>
             </thead>
             <tbody>
             <tr>
             <td align="left">Computer</td>
             <td align="right">$1600</td>
             </tr>
             <tr>
             <td align="left">Phone</td>
             <td align="right">$12</td>
             </tr>
             <tr>
             <td align="left">Pipe</td>
             <td align="right">$1</td>
             </tr>
             </tbody>
             </table>
             """
         ),
         extensions=[TableExtension(use_align_attribute=True)]
     )
Exemplo n.º 27
0
    def extendMarkdown(self, md, md_globals):
        # Nl2BrExtension().extendMarkdown(md, md_globals)
        FencedCodeExtension().extendMarkdown(md, md_globals)
        SmartEmphasisExtension().extendMarkdown(md, md_globals)
        TableExtension().extendMarkdown(md, md_globals)
        AdmonitionExtension().extendMarkdown(md, md_globals)

        CodeHiliteExtension(use_pygments=True,
                            css_class='roca_css').extendMarkdown(
                                md, md_globals)

        TocExtension(anchorlink=False,
                     permalink=True).extendMarkdown(md, md_globals)

        gfm.AutomailExtension().extendMarkdown(md, md_globals)
        gfm.SemiSaneListExtension().extendMarkdown(md, md_globals)
        gfm.SpacedLinkExtension().extendMarkdown(md, md_globals)
        gfm.StrikethroughExtension().extendMarkdown(md, md_globals)
        gfm.AutolinkExtension().extendMarkdown(md, md_globals)
        gfm.TaskListExtension().extendMarkdown(md, md_globals)

        SubstituteExtension().extendMarkdown(md, md_globals)
Exemplo n.º 28
0
def help(request):

    path = os.path.join(os.path.dirname(__file__), "pages", "index.md")

    if request.path[:7] == "/about/":
        dir = request.path[7:]

        unsafe_path = os.path.join(os.path.dirname(__file__), "pages", dir)

        if unsafe_path.startswith(
                os.path.join(os.path.dirname(__file__), "pages")):
            safe_path = unsafe_path
            if os.path.isfile(safe_path):
                path = safe_path

    input_file = codecs.open(path, mode="r", encoding="utf-8")
    text = input_file.read()

    html = markdown.markdown(text, extensions=[TableExtension()])

    return render(request,
                  "about/help.html",
                  context={"file": mark_safe(html)})
Exemplo n.º 29
0
 def test_align_three_legacy(self):
     self.assertMarkdownRenders(
         self.dedent(
             """
             |foo|bar|baz|
             |:--|:-:|--:|
             |   | Q |   |
             |W  |   |  W|
             """
         ),
         self.dedent(
             """
             <table>
             <thead>
             <tr>
             <th align="left">foo</th>
             <th align="center">bar</th>
             <th align="right">baz</th>
             </tr>
             </thead>
             <tbody>
             <tr>
             <td align="left"></td>
             <td align="center">Q</td>
             <td align="right"></td>
             </tr>
             <tr>
             <td align="left">W</td>
             <td align="center"></td>
             <td align="right">W</td>
             </tr>
             </tbody>
             </table>
             """
         ),
         extensions=[TableExtension(use_align_attribute=True)]
     )
Exemplo n.º 30
0
def compile():
    """Process a markdown file according to command-line arguments"""
    parser = argparse.ArgumentParser(
        description='Create the documentation file')
    parser.add_argument('-l', help='Give language translations',
                        default=False, action='store_true')
    parser.add_argument('-c', help='Check validity',
                        default=False, action='store_true')
    parser.add_argument('-b', help='Add buttons for each example',
                        default=False, action='store_true')
    parser.add_argument('--hide', help='Hide examples by default',
                        default=False, action='store_true')
    parser.add_argument('--delete', help='Delete examples',
                        default=False, action='store_true')
    parser.add_argument('--output', help='Output file name', default=None)
    parser.add_argument('infile')
    args = parser.parse_args()
    extensions = [TableExtension(),
                  CodeHiliteExtension({}),
                  EmbedExtension(args.delete, args.l)
                  ]
    if args.l:
        from .translate import TranslateMlrExtension
        extensions.insert(0, TranslateMlrExtension())
        if not args.c:
            from .embed_code import EmbedCodeExtension
            extensions.insert(0, EmbedCodeExtension(args.b, args.hide, args.delete))
    if args.c:
        from .test_mlr import TestExtension
        extensions.insert(0, TestExtension(args.b, args.hide, args.delete))
    output = args.output or (
        os.path.basename(args.infile).rsplit('.', 1)[0] + '.html')
    markdown.markdownFromFile(
        input=args.infile,
        output=output,
        encoding='utf-8',
        extensions=extensions)