def test_codeblock(self): inp = '''```python print("hello") ```''' exp = '<pre class="codeblock"><codeblock class="python">print("hello")</codeblock></pre>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def export(): if request.json is None: abort(400) exporter_config = request.json['exporterConfig'] entity_decorators = {} block_map = dict(BLOCK_MAP, **exporter_config.get('block_map', {})) style_map = dict(STYLE_MAP, **exporter_config.get('style_map', {})) entity_decorators[ENTITY_TYPES.FALLBACK] = import_decorator( 'missing_inline') block_map[BLOCK_TYPES.FALLBACK] = import_decorator('missing_block') style_map[INLINE_STYLES.FALLBACK] = import_decorator('missing_inline') for type_, value in exporter_config.get('entity_decorators', {}).iteritems(): entity_decorators[type_] = import_decorator(value) exporter = HTML({ 'entity_decorators': entity_decorators, 'block_map': block_map, 'style_map': style_map, }) html = exporter.render(request.json['contentState']) markdown = render_markdown(request.json['contentState']) return json.dumps({ 'html': html, 'markdown': markdown, 'prettified': prettify(html), })
def export(): if request.json is None: abort(400) exporter_config = request.json["exporterConfig"] entity_decorators = {} block_map = dict(BLOCK_MAP, **exporter_config.get("block_map", {})) style_map = dict(STYLE_MAP, **exporter_config.get("style_map", {})) entity_decorators[ENTITY_TYPES.FALLBACK] = import_decorator( "missing_inline" ) block_map[BLOCK_TYPES.FALLBACK] = import_decorator("missing_block") style_map[INLINE_STYLES.FALLBACK] = import_decorator("missing_inline") for type_, value in exporter_config.get("entity_decorators", {}).items(): entity_decorators[type_] = import_decorator(value) exporter = HTML( { "entity_decorators": entity_decorators, "block_map": block_map, "style_map": style_map, } ) html = exporter.render(request.json["contentState"]) markdown = render_markdown(request.json["contentState"]) return json.dumps( {"html": html, "markdown": markdown, "prettified": prettify(html)} )
def test_table(self): inp = '''|first|second|third| |:---:|:---|---:| |1.1|1.2|1.3| |2.1|2.2|2.3|''' exp = '<table class="responsive-table highlight striped"><tr><td class="center" >first</td><td class="left" >second</td><td class="right" >third</td></tr><tr><td class="center" >1.1</td><td class="left" >1.2</td><td class="right" >1.3</td></tr><tr><td class="center" >2.1</td><td class="left" >2.2</td><td class="right" >2.3</td></tr></table>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_li(self): inp = '''- 1 - 2 - 2.1 - 2.2 2 string - 3 ''' exp = '<ul class="browser-default"><li>1</li><li>2<br><ul class="browser-default"><li>2.1</li><li>2.2</li>2 string<br></ul></li><li>3</li></ul>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def md2html(markdown: Union[str, None], link_blank_target=False): """Convert markdown to clean html""" extensions = settings.MARKDOWNX_MARKDOWN_EXTENSIONS if link_blank_target: extensions.append(LinkBlankTargetExtension()) html = render_markdown( text=markdown or "", extensions=extensions, extension_configs=settings.MARKDOWNX_MARKDOWN_EXTENSION_CONFIGS, ) return clean(html)
def __init__(self, dct, post): # render markdown desc = dct.get('desc', None) if desc: dct['desc'] = improve_typography(render_markdown(desc)) # improve typography in title dct['title'] = improve_typography(dct['title']) self._dct = dct self.tags = map(Tag, dct['tags']) # automatically add "video" tag if 'video' not in dct['tags'] and \ any(x in dct['link'] for x in ['youtube.com', 'vimeo.com']): self.tags.append(Tag('video')) self.tags.append(Tag('_post', post))
def test_code(self): inp = '`code`' exp = '<code>code</code>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_mix_inline(self): inp = "`**code**` **[OhYee's Blog](https://www.oyohyee.com/)**" exp = '<code>**code**</code> <strong><a href="https://www.oyohyee.com/">OhYee\'s Blog</a></strong>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_strong(self): inp = '**strong**' exp = '<strong>strong</strong>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_escape(self): inp = r'\q \`code`' exp = 'q `code`' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_link(self): inp = "[OhYee's Blog](https://www.oyohyee.com/)" exp = '<a href="https://www.oyohyee.com/">OhYee\'s Blog</a>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_image(self): inp = "![OhYee's Blog](https://www.oyohyee.com/logo.svg)" exp = '<a href="https://www.oyohyee.com/logo.svg" alt="OhYee\'s Blog" data-lightbox="OhYee\'s Blog-https://www.oyohyee.com/logo.svg" data-title="OhYee\'s Blog"><img class="img-responsive" src="https://www.oyohyee.com/logo.svg" alt="OhYee\'s Blog"></a>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def render_markdown(self, **kwargs): value = self.raw_value return value and render_markdown(value) or ''
def markdown(s): return mark_safe(render_markdown(force_text(s)))
def test_blockquote(self): inp = '''> blockquote''' exp = '<blockquote>blockquote</blockquote>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_autolink(self): inp = 'https://www.oyohyee.com/' exp = '<a href="https://www.oyohyee.com/">https://www.oyohyee.com/</a>' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_math(self): inp = r'$\int$' exp = r'$\int$' oup = render_markdown(inp) self.assertEqual(exp, oup)
def test_em(self): inp = '*em*' exp = '<em>em</em>' oup = render_markdown(inp) self.assertEqual(exp, oup)