def test_extension_object(self): app = Flask(__name__) from markdown.extensions.codehilite import CodeHiliteExtension codehilite = CodeHiliteExtension() app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [codehilite] pages = FlatPages(app) self.check_default_codehilite_page(pages) codehilite = CodeHiliteExtension( linenums='True') #Check config applies app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [codehilite] pages.reload() pages._file_cache = {} self.check_codehilite_with_linenums(pages)
def convert(txt, gv_odir, gv_bdir='media/draft', gv_namepre=""): codehilite = CodeHiliteExtension(linenums=False, guess_lang=False) toc = TocExtension(anchorlink=False, permalink=True) md = markdown.Markdown(extensions=[ 'wpcmd.mde.metadata', toc, 'markdown.extensions.tables', codehilite, 'wpcmd.mde.fenced_code_extra', ], extension_configs={ 'wpcmd.mde.fenced_code_extra': { 'graphviz': { 'OUTPUT_DIR': gv_odir, 'BASE_URL': gv_bdir, 'NAME_PRE': gv_namepre }, } }) html = md.convert(txt) graphviz = _get_extra_output(md, 'graphviz') if graphviz: # The text will inclued a content with all converted image from graphviz codes. # We need replace original text use this text to avoid the repeted conversion from graphviz codes. txt = graphviz['text'] return html, md, txt
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, )
def prepare_to_save(self): self.slug = re.sub('[^\\w]+', '-', self.title.lower()) hilite = CodeHiliteExtension(linenums=False, css_class='highlight') markdown_text = markdown(self.markdown_body, extensions=[hilite, 'fenced_code']) self.html_body = Markup(markdown_text)
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(' ', ' ') return html_ish
def render_markdown(self, text): return markdown.markdown(text, extensions=[ CodeHiliteExtension(linenums=False, noclasses=True), FencedCodeExtension() ])
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
def parse_markdown(content): """ Small helper for parsing markdown files. """ html = '' default_meta = { 'title': ['',], 'subtitle': ['',], 'author': ['',], 'tags': ['',], 'published_at': ['',], 'thumb': ['',] } meta = default_meta try: md = Markdown(extensions=[ 'markdown.extensions.meta', CodeHiliteExtension(linenums=False), 'markdown.extensions.fenced_code', 'markdown.extensions.admonition', TocExtension(baselevel=2) ]) html = md.convert(content) meta = md.Meta except: meta = default_meta return html, meta
def detail(slug): r = db_posts.scan(FilterExpression='published = :b AND slug = :s', ExpressionAttributeValues={ ":b": True, ":s": slug }) if len(r) > 0: post = r['Items'][0] # convert post content to HTML hilite = CodeHiliteExtension(linenums=False, css_class='highlight') extras = ExtraExtension() post['content'] = markdown(post['content'], extensions=[hilite, extras]) post['content'] = parse_html(post['content'], oembed_providers, urlize_all=True, maxwidth=app.config['SITE_WIDTH']) post['content'] = Markup(post['content']) return render_template('detail.html', post=post) else: flash('Post not found.', 'danger') return redirect(url_for('home'))
def __init__(self): self.source_path = './content' self.template_path = './templates' self.output_path = './output' self.static_path = './static' self.md = markdown.Markdown( extensions=['meta', 'extra', CodeHiliteExtension()], output_format='html5', tab_length=2) self.jinja_env = Environment( loader=FileSystemLoader(self.template_path), autoescape=select_autoescape(['html', 'xml'])) self.source_files = os.listdir(self.source_path) self.rendered_posts = [] if os.path.exists(self.output_path): shutil.rmtree(self.output_path) if os.path.exists(self.static_path): os.makedirs(os.path.join(self.output_path, self.static_path)) for f in os.listdir(self.static_path): shutil.copy2( os.path.join(self.static_path, f), os.path.normpath( os.path.join(self.output_path, self.static_path, f)))
def html_content(self): # This function will be used to turn our biography into html code hilite = CodeHiliteExtension(linenums=False, css_class='highlight') extras = ExtraExtension() content = self.converter() markdown_content = markdown(content, extensions=[hilite, extras]) return Markup(markdown_content)
def testMultipleBlocksSameStyle(self): if has_pygments: # See also: https://github.com/Python-Markdown/markdown/issues/1240 expected = ( '<div class="codehilite" style="background: #202020"><pre style="line-height: 125%; margin: 0;">' '<span></span><code><span style="color: #999999; font-style: italic"># First Code Block</span>\n' '</code></pre></div>\n\n' '<p>Normal paragraph</p>\n' '<div class="codehilite" style="background: #202020"><pre style="line-height: 125%; margin: 0;">' '<span></span><code><span style="color: #999999; font-style: italic"># Second Code Block</span>\n' '</code></pre></div>' ) else: expected = ( '<pre class="codehilite"><code class="language-python"># First Code Block\n' '</code></pre>\n\n' '<p>Normal paragraph</p>\n' '<pre class="codehilite"><code class="language-python"># Second Code Block\n' '</code></pre>' ) self.assertMarkdownRenders( ( '\t:::Python\n' '\t# First Code Block\n\n' 'Normal paragraph\n\n' '\t:::Python\n' '\t# Second Code Block' ), expected, extensions=[CodeHiliteExtension(pygments_style="native", noclasses=True)] )
def _get_article_content(self, afile, istxt=False): txt = None if istxt: txt = afile else: if not os.path.exists(afile): slog.error('The file "%s" is inexistance!' % afile) return None, None, None, None txt = read_file(afile) FencedBlockPreprocessor.FENCED_BLOCK_RE = re.compile( r''' (?P<fence>^(?:~{3,}|`{3,}))[ ]* # Opening ``` or ~~~ # Optional {, lang="lang" or lang (\{?\.?(?:lang=")?(?P<lang>[a-zA-Z0-9_+-]*)"?)?[ ]* # Optional highlight lines, single- or double-quote-delimited (hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot))?[ ]* }?[ ]*\n # Optional closing } (?P<code>.*?)(?<=\n) (?P=fence)[ ]*$''', re.MULTILINE | re.DOTALL | re.VERBOSE) fencedcode = FencedCodeExtension() codehilite = CodeHiliteExtension(linenums=False, guess_lang=False) md = markdown.Markdown(extensions=[ 'markdown.extensions.meta', 'markdown.extensions.tables', fencedcode, codehilite, ]) html = md.convert(txt) meta = md.Meta adict = self._get_article_metadata(meta) return html, adict, txt, self._get_medias(txt)
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, )
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
def get(name=""): if not name: raise ErrInvalidName("Please provide a post name") # Parse the entry file, the file should follow the following template: # key: value with open(f"{CONTENT_DIR}/{name}.md", "r", encoding="utf-8") as input_file: text = input_file.read() hilite = CodeHiliteExtension(linenums=False, css_class="highlight") extras = ExtraExtension() md = markdown.Markdown(extensions=["meta", "fenced_code", hilite, extras], output_format="html5") html = md.convert(text) # Clean up the meta format meta = {key.lower(): value[0] for key, value in md.Meta.items()} summary = app.util.get_summary(html) if "direciton" not in meta: try: meta["direciton"] = "rtl" if langdetect.detect( summary) == "ar" else "ltr" except: meta["direciton"] = "ltr" if "cover" not in meta: meta["cover"] = app.util.get_cover(html) body = {"content": html, "summary": summary} return {**meta, **body}
def html_content(self): hilite = CodeHiliteExtension(linenums=False, css_class='highlight') extras = ExtraExtension() markdown_content = markdown(self.content, extensions=[hilite, extras]) oembed_content = parse_html(markdown_content, urlize_all=True, maxwidth=app.config['SITE_WIDTH']) return Markup(oembed_content)
def html_content(self): hilite = CodeHiliteExtension(linenums=False, css_class='highlight') extras = ExtraExtension() markdown_content = markdown(self.content, extensions=[hilite, extras]) oembed_content = parse_html(markdown_content, oembed_providers, urlize_all=True) return Markup(oembed_content)
def testLangPrefixEmpty(self): self.assertMarkdownRenders( ('\t:::Python\n' '\t# A Code Comment'), ('<pre class="codehilite"><code class="python"># A Code Comment\n' '</code></pre>'), extensions=[ CodeHiliteExtension(use_pygments=False, lang_prefix='') ])
def testUsePygmentsFalse(self): self.assertMarkdownRenders(( '\t:::Python\n' '\t# A Code Comment' ), ('<pre class="codehilite"><code class="language-python"># A Code Comment\n' '</code></pre>'), extensions=[ CodeHiliteExtension(use_pygments=False) ])
def markdown_to_html(plain): """Convert Markdown to HTML""" import re import base64 from bs4 import BeautifulSoup import markdown from markdown.extensions.abbr import AbbrExtension from markdown.extensions.codehilite import CodeHiliteExtension from markdown.extensions.def_list import DefListExtension from markdown.extensions.fenced_code import FencedCodeExtension from markdown.extensions.footnotes import FootnoteExtension # Don't convert if plain text is really plain if re.match(r"[a-zA-Z0-9æøåÆØÅ ,.?+-]*$", plain): return plain # Fix whitespaces in input plain = plain.replace("\xc2\xa0", " ").replace("\xa0", " ") # For convenience: Fix mathjax escaping plain = plain.replace(r"\[", r"\\[") plain = plain.replace(r"\]", r"\\]") plain = plain.replace(r"\(", r"\\(") plain = plain.replace(r"\)", r"\\)") html = markdown.markdown(plain, extensions=[ AbbrExtension(), CodeHiliteExtension( noclasses=True, linenums=False, pygments_style='friendly', guess_lang=False, ), DefListExtension(), FencedCodeExtension(), FootnoteExtension(), ], output_format="html5") html_tree = BeautifulSoup(html, 'html.parser') tag = _get_first_tag(html_tree) if not tag: if not html: # Add space to prevent input field from shrinking in UI html = " " html_tree = BeautifulSoup(f"<div>{html}</div>", "html.parser") tag = _get_first_tag(html_tree) # Store original text as data-attribute on tree root # Note: convert newlines to <br> to make text readable in the Anki viewer original_html = base64.b64encode( plain.replace("\n", "<br />").encode('utf-8')).decode() tag['data-original-markdown'] = original_html return str(html_tree)
def markdown(value): generated_html = md.markdown(value, extensions=[ 'fenced_code', CodeHiliteExtension( pygments_style='native', linenos=False), 'extra', "footnotes" ]) return mark_safe(f"<div class='markdown'>{generated_html}</div>")
def markdown_to_html(text: str) -> str: """Convert the input text from markdown to html""" return markdown.markdown(text, extensions=[ CodeHiliteExtension(linenums=True), 'markdown.extensions.fenced_code', 'markdown.extensions.footnotes', 'markdown.extensions.tables', 'markdown.extensions.toc', ])
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)
def html_content(self): """ Generate HTML representation of the markdown-formatted note, and also convert any media URLs into rich media objects such as video players or images. """ hilite = CodeHiliteExtension(linenums=False, css_class='highlight') extras = ExtraExtension() markdown_content = markdown(self.content, extensions=[hilite, extras]) return Markup(markdown_content)
def convert(text): md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.meta', LinkExtension(), ListExtension(), RedirectExtension(), CodeHiliteExtension(guess_lang=False) ]) return md.convert(text)
def html_content(self): # todo: research what all these things do hilite = CodeHiliteExtension(linenums=False, css_class="highlight") extras = ExtraExtension() markdown_content = markdown(self.content, extensions=[hilite, extras]) oembed_content = parse_html(markdown_content, oembed_providers, urlize_all=True, maxwidth=app.config["SITE_WIDTH"]) return Markup(oembed_content)
def html_content(self): """ parses markdown content """ hil = CodeHiliteExtension(linenums=True, css_class='highlight') extra = ExtraExtension() mrkdwn_content = markdown(self.text, extensions=[hil, extra]) oembed_content = parse_html( mrkdwn_content, OEMBED_PROVIDERS, urlize_all=True) return Markup(oembed_content)
def html_content(self): """Parse article bosy for markup and features.""" hilite = CodeHiliteExtension(linenums=False, css_class='highlight') extras = ExtraExtension() markdown_content = markdown(self.body, extensions=[hilite, extras]) oembed_providers = bootstrap_basic(OEmbedCache) oembed_content = parse_html( markdown_content, oembed_providers, urlize_all=True) return Markup(oembed_content)
def load(self, func=None): with codecs.open(self.absolute_path(), encoding='utf-8') as f: content = f.read() html = markdown.markdown( content, extensions=[CodeHiliteExtension(guess_lang=True)]) tree = BeautifulSoup(html, 'lxml') root = self._find_root(tree) self._content_hash = (hashlib.md5(content).hexdigest()) self._registry = {} self._root = Chunk(root, None, self._registry, func=func) self.loaded = True
#***************************************************************************** # Copyright (C) 2015 Vincent Delecroix <*****@*****.**> # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** import codecs import markdown import os from markdown.extensions.codehilite import CodeHiliteExtension from mdx_math import MathExtension code_hilite = CodeHiliteExtension([]) code_hilite.setConfig('guess_lang', False) code_hilite.setConfig('css_class', 'code-highlight') math = MathExtension() def process_article(text): return markdown.markdown( text, encoding="utf-8", extensions=[code_hilite, math, 'markdown.extensions.tables'], )