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 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 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 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 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
def strToClassEXt(extensions): e_idx = [] if ("nl2br" in extensions): e_idx.append(Nl2BrExtension()) if ("footnotes" in extensions): e_idx.append(FootnoteExtension()) if ("def_list" in extensions): e_idx.append(DefListExtension()) if ("md_in_html" in extensions): e_idx.append(MarkdownInHtmlExtension()) if ("pymdownx.caret" in extensions): e_idx.append(InsertSupExtension(smart_insert=False, insert=False)) if ("pymdownx.magiclink" in extensions): e_idx.append(MagiclinkExtension()) if ("pymdownx.smartsymbols" in extensions): e_idx.append(SmartSymbolsExtension()) return e_idx
if users.is_current_user_admin(): return True try: if oauth.is_current_user_admin(): return True except oauth.OAuthRequestError: pass return False md = markdown.Markdown( extensions=[ md_wikilink.WikiLinkExtension(), md_itemprop.ItemPropExtension(), md_url.URLExtension(), md_mathjax.MathJaxExtension(), md_strikethrough.StrikethroughExtension(), md_partials.PartialsExtension(), md_tables.TableExtension(), md_section.SectionExtension(), md_embed.EmbedExtension(), DefListExtension(), AttrListExtension(), ], safe_mode=False, smart_emphasis=False, )