使用Misaka 自定义块渲染器
使用pygments生成样式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-
import misaka
from misaka import HtmlRenderer, SmartyPants
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters.html import HtmlFormatter
class HighlighterRenderer(HtmlRenderer, SmartyPants):
def block_code(self, text, lang):
has_syntax_highlite = False
if not lang:
lang = 'text'
try:
lexer = get_lexer_by_name(lang, stripall=True)
if lang != 'text':
has_syntax_highlite = True
except:
lexer = get_lexer_by_name('text', stripall=True)
formatter = HtmlFormatter()
return "{open_block}{formatted}{close_block}".format(
open_block="<div class='code-highlight'>" if has_syntax_highlite else '',
formatted=highlight(text, lexer, formatter),
close_block="</div>" if has_syntax_highlite else ''
)
def table(self, header, body):
return "<table class='table table-bordered table-hover'>" + header + body + "</table>"
markdown_renderer = misaka.Markdown(
HighlighterRenderer(flags=misaka.HTML_ESCAPE | misaka.HTML_HARD_WRAP | misaka.HTML_SAFELINK),
extensions=misaka.EXT_FENCED_CODE | misaka.EXT_NO_INTRA_EMPHASIS | misaka.EXT_TABLES | misaka.EXT_AUTOLINK | misaka.EXT_SPACE_HEADERS | misaka.EXT_STRIKETHROUGH | misaka.EXT_SUPERSCRIPT
)
本文网址: https://pylist.com/topic/148.html 转摘请注明来源