1 Star 0 Fork 0

前面那方/MarkdonwBaseEditor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
textbrowser.py 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
前面那方 提交于 2018-09-18 22:34 . 1) Improve the preview function
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWebEngineWidgets import QWebEngineView
from globalvalue import *
import houdini
from misaka import Markdown, HtmlRenderer
from pygments import highlight
from pygments.formatters import HtmlFormatter, ClassNotFound
from pygments.lexers import get_lexer_by_name
class HighlighterRenderer(HtmlRenderer):
def blockcode(self, text, lang):
try:
lexer = get_lexer_by_name(lang, stripall=False)
except ClassNotFound:
lexer = None
if lexer:
formatter = HtmlFormatter(linenos=True)
return highlight(text, lexer, formatter)
# default
return '\n<pre><code>{}</code></pre>\n'.format(houdini.escape_html(text.strip()))
class TextBrowser(QTextEdit):
def __init__(self, parent = None):
super().__init__(parent)
self.initUI(parent)
def initUI(self, parent = None):
self.setReadOnly(True)
pass
#self.setZoomFactor(1.25)
def setText(self, text):
htmlrd = HighlighterRenderer()
mdToHtml = Markdown(htmlrd, extensions=('fenced-code', 'tables', 'footnotes',
'autolink', 'highlight','strikethrough', 'underline', 'quote', 'superscript',
'math', 'no-intra-emphasis', 'space-headers', 'math-explicit'))
html = mdToHtml(text)
html = html.replace('<table>', '<table border="1">')
#css head
cssStyle = '<style type = "text/css">\n'
#css body
with open('./resource/typora.style', 'r') as file:
typoraStyle = file.read()
cssStyle = cssStyle + typoraStyle
cssStyle = cssStyle + HtmlFormatter().get_style_defs('.highlight')
#css end
cssStyle = cssStyle + '</style>\n'
self.setHtml(cssStyle + html)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/zhongbin/MarkdonwBaseEditor.git
git@gitee.com:zhongbin/MarkdonwBaseEditor.git
zhongbin
MarkdonwBaseEditor
MarkdonwBaseEditor
master

搜索帮助