Beispiel #1
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,
    )
Beispiel #2
0
    def __init__(self, settings: MarkdownSettings):
        self.settings = settings
        self.extensions = []

        if self.settings.enable_checklist:
            self.extensions.append(ChecklistExtension())

        if self.settings.enable_codehilite:
            self.extensions.append(
                CodeHiliteExtension(**self.settings.codehilite_options)
            )

        if self.settings.enable_fenced_code:
            self.extensions.append(FencedCodeExtension())

        if self.settings.enable_footnotes:
            self.extensions.append(FootnoteExtension(**self.settings.footnotes_options))

        if self.settings.enable_smartypants:
            self.extensions.append(SmartyExtension(**self.settings.smartypants_options))

        if self.settings.enable_toc:
            self.extensions.append(TocExtension(**self.settings.toc_options))

        if self.settings.enable_truly_sane_lists:
            self.extensions.append(
                TrulySaneListExtension(**self.settings.truly_sane_lists_options)
            )

        super().__init__(
            output_format=self.settings.output_format,
            tab_length=self.settings.tab_length,
            extensions=self.extensions,
        )
Beispiel #3
0
 def parse_markdown(timestamp: str, message: str) -> str:
     # FIXME: review, pentest
     bleach = BleachExtension(
         strip=True,
         tags=[
             'h2',
             'h3',
             'h4',
             'h5',
             'h6',
             'blockquote',
             'ul',
             'ol',
             'dl',
             'dt',
             'dd',
             'li',
             'code',
             'sup',
             'pre',
             'br',
             'a',
             'p',
             'em',
             'strong',
         ],
         attributes={
             '*': [],
             'h2': ['id'],
             'h3': ['id'],
             'h4': ['id'],
             'h5': ['id'],
             'h6': ['id'],
             'li': ['id'],
             'sup': ['id'],
             'a': ['href'],  # FIXME: can people be deceptive with this?
         },
         styles={},
         protocols=['http', 'https'],
     )
     slug_timestamp = str(timestamp).replace(' ', '').replace(':',
                                                              '').replace(
                                                                  '.', '')
     FootnoteExtension.get_separator = lambda x: slug_timestamp + '-'
     md = markdown.Markdown(extensions=[
         bleach,
         SmartyExtension(
             smart_dashes=True,
             smart_quotes=True,
             smart_ellipses=True,
             substitutions={},
         ),
         'markdown.extensions.nl2br',
         'markdown.extensions.footnotes',
         'markdown.extensions.toc',
         'markdown.extensions.def_list',
         'markdown.extensions.abbr',
         'markdown.extensions.fenced_code',
     ], )
     return md.convert(message)
Beispiel #4
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
Beispiel #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)
Beispiel #6
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',
     )
Beispiel #7
0
def render_markdown(text):
    return markdown.markdown(text, extensions=[SmartyExtension()])
Beispiel #8
0
def markdown2html(markdown_text):
    return markdown.markdown(
        markdown_text,
        extensions=[CodeHiliteExtension(),
                    SmartyExtension()],
        output_format='html5')
def render_markdown(text):
    import markdown
    from markdown.extensions.smarty import SmartyExtension

    return markdown.markdown(text, extensions=[SmartyExtension()])
Beispiel #10
0
    # set the number of chapters to the number of correctly formatted chapters in the working directory if not specified
    args.nChapters = len(
        glob(source_prefix + chapter_filename_format.format('*') +
             source_suffix))

if args.nLinked is None or args.nLinked > args.nChapters:
    # default to linking all chapters
    args.nLinked = args.nChapters

md = markdown.Markdown(extensions=[
    SmartyExtension(
        substitutions={
            'left-single-quote': '‘',
            'right-single-quote': '’',
            'left-double-quote': '“',
            'right-double-quote': '”',
            'left-angle-quote': '«',
            'right-angle-quote': '»',
            'ellipsis': '…',
            'ndash': '–',
            'mdash': '—'
        })
],
                       output_format='html5')

jinja = Environment(
    loader=FileSystemLoader('.')).get_template('chapter_template.html')


class Chapter:
    def __init__(self, index, title=None, source_file=None):
        self.index = index
Beispiel #11
0
class BootstrapifyExtension(Extension):
    """
    Add bootstrap classes to result HTML elements
    """
    def extendMarkdown(self, md, md_globals):
        md.registerExtension(self)

        md.treeprocessors.add(
            'bootstrapify',
            BoostrapifyProcessor(md),
            "_end",
        )


markdown_extensions = [
    SmartyExtension(),
    ExtraExtension(),
    FencedCodeExtension(),
    CodeSampleExtension(),
    CommentsExtension(),
    BootstrapifyExtension(),
]
markdown_no_toc = markdown.Markdown(extensions=markdown_extensions)
markdown_toc = markdown.Markdown(extensions=markdown_extensions + [
    TocExtension(marker='[table-of-contents]', anchorlink=True),
])


@register.filter()
def markdownify(text):
    try:
Beispiel #12
0
import markdown
from markdown.extensions.smarty import SmartyExtension

mk = markdown.Markdown([SmartyExtension()])
Beispiel #13
0
import bleach
from bleach.callbacks import nofollow, target_blank
from django import template
from django.utils.safestring import mark_safe
import markdown
from markdown.extensions.smarty import SmartyExtension

register = template.Library()


class SkipImagesExtension(markdown.extensions.Extension):
    def extendMarkdown(self, md, md_globals):
        del md.inlinePatterns['image_link']
        del md.inlinePatterns['image_reference']


md = markdown.Markdown(extensions=[
    'markdown.extensions.nl2br', 'markdown.extensions.sane_lists',
    SmartyExtension(smart_angled_quotes=True),
    SkipImagesExtension()
],
                       output_format='html5')


@register.filter(name='markdown')
def markdown_filter(text):
    text = bleach.clean(text, strip=True)
    text = md.reset().convert(text)
    return mark_safe(bleach.linkify(text, [nofollow, target_blank]))