Пример #1
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
Пример #2
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,
    )
Пример #3
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
Пример #4
0
def mdToHtml(md, _extensions, configs):
    html = markdown.markdown(md,
                             extensions=[
                                 SaneListExtension(),
                                 TableExtension(),
                                 FencedCodeExtension(),
                                 DeleteSubExtension(**configs)
                             ] + strToClassEXt(_extensions))
    return html
Пример #5
0
def markdown_filter(text):
    """ Convert markdown to html """
    md2html = markdown(text, extensions=[CodeHiliteExtension(linenums=False, css_class='highlight'),
                                          ExtraExtension(),
                                         SaneListExtension(),
                                         SmartyExtension(smart_dashes=True,
                                                          smart_quotes=False,
                                                         smart_angled_quotes=False,
                                                         smart_ellipses=False)])
    safe_html = sanitize_html(md2html)
    return Markup(safe_html)
Пример #6
0
 def set_note(self, payload):
     html = markdown.markdown(
         payload,
         tab_length=2,
         extensions=[GithubExtension(), SaneListExtension(), TocExtension()],
         output_format='html5',
     )
     # self.webview.load_uri('file:///D:/Users/.../test.html')
     self.web_view.load_html(
         html,
         # base_uri='file:///D:/Users/.../Python/test/'
     )
     self.web_view.show()
Пример #7
0
 def __init__(self, app):
     self.app = app
     self.markdown = markdown.Markdown(
         extensions=[
             ClassifyExtension(),
             WikiLinkExtension(
                 build_url=self.build_url,
                 html_class=self.html_class,
             ),
             TocExtension(
                 baselevel=2,
             ),
             SaneListExtension(),
             SmartyExtension(),
             SmartSymbolsExtension(),
         ],
         output_format='html5',
     )
Пример #8
0
def convert_markdown(filename, stylename=None, linenos=False):
    """ Convert a markdown file to an HTML div, and return the result. """
    displayname, content = get_file_content(filename)
    stylename = stylename.lower() if stylename else DEFAULT_STYLE
    debug('Converting MD: {}'.format(displayname))
    hilighter = CodeHiliteExtension(
        pygments_style=stylename,
        linenums=linenos,
        noclasses=True,
        css_class='hilight',
    )
    return '\n'.join(('<div class="markdown">',
                      markdown(content,
                               output_format='html5',
                               extensions=[
                                   hilighter,
                                   FencedCodeExtension(),
                                   SaneListExtension(),
                               ]), '</div>'))
Пример #9
0
def pydici_simple_format(value, arg=None):
    """Very simple markup formating based on markdown and custom links"""
    dealIds = [
        i[0] for i in Lead.objects.exclude(deal_id="").values_list("deal_id")
    ]
    trigrammes = [i[0] for i in Consultant.objects.values_list("trigramme")]

    #TODO: this may not scale with thousands of leads. It may be splitted in shunk on day.
    for dealId in re.findall(r"%s" % "|".join(dealIds), value):
        value = value.replace(
            dealId, u"<a href='%s'>%s</a>" %
            (Lead.objects.get(deal_id=dealId).get_absolute_url(), dealId))

    for trigramme in re.findall(r"%s" % "|".join(trigrammes), value):
        value = value.replace(
            trigramme, u"<a href='%s'>%s</a>" %
            (Consultant.objects.get(trigramme=trigramme).get_absolute_url(),
             trigramme))

    # Authorized tags for markdown (thanks https://github.com/yourcelf/bleach-whitelist/blob/master/bleach_whitelist/bleach_whitelist.py)
    markdown_tags = [
        "h1", "h2", "h3", "h4", "h5", "h6", "b", "i", "strong", "em", "tt",
        "p", "br", "span", "div", "blockquote", "code", "hr", "ul", "ol", "li",
        "dd", "dt", "img", "a"
    ]
    markdown_attrs = {
        "img": ["src", "alt", "title"],
        "a": ["href", "alt", "title"]
    }
    value = bleach.clean(markdown.markdown(value,
                                           tab_length=2,
                                           extensions=[
                                               SaneListExtension(),
                                           ]),
                         tags=markdown_tags,
                         attributes=markdown_attrs)

    return mark_safe(value)
Пример #10
0
        self.result = MarkdownData(
            blocks,
            links,
        )

        # StripTopLevelTags expects doc_tag in the output,
        # so give it an empty one.
        return '<{0}></{0}>'.format(self.md.doc_tag)


# Reuse one instance for the conversions.
_converter = TKConverter()
_MD = markdown.Markdown(extensions=[
    _converter,
    SaneListExtension(),
])


def convert(text: str) -> MarkdownData:
    """Convert markdown syntax into data ready to be passed to richTextBox."""
    _MD.reset()
    _MD.convert(text)
    return _converter.result


def join(*args: MarkdownData) -> MarkdownData:
    """Join several text blocks together.

    This merges together blocks, reassigning link callbacks as needed.
    """