highlight
This commit is contained in:
parent
0e1c2f0199
commit
6d02bd6c92
5 changed files with 118 additions and 3 deletions
55
makesite.py
55
makesite.py
|
|
@ -40,10 +40,61 @@ import unicodedata
|
|||
import locale
|
||||
import requests
|
||||
import mistune
|
||||
from pygments import highlight
|
||||
from pygments.lexers import get_lexer_by_name
|
||||
from pygments.formatters import html
|
||||
|
||||
# set user locale
|
||||
locale.setlocale(locale.LC_ALL, "")
|
||||
|
||||
# initialize markdown
|
||||
def block_code(text, lang, inlinestyles=False, linenos=False):
|
||||
if not lang:
|
||||
text = text.strip()
|
||||
return u'<pre><code>%s</code></pre>\n' % mistune.escape(text)
|
||||
|
||||
try:
|
||||
lexer = get_lexer_by_name(lang, stripall=True)
|
||||
formatter = HtmlFormatter(
|
||||
noclasses=inlinestyles, linenos=linenos
|
||||
)
|
||||
code = highlight(text, lexer, formatter)
|
||||
if linenos:
|
||||
return '<div class="highlight-wrapper">%s</div>\n' % code
|
||||
return code
|
||||
except:
|
||||
return '<pre class="%s"><code>%s</code></pre>\n' % (
|
||||
lang, mistune.escape(text)
|
||||
)
|
||||
|
||||
|
||||
class HighlightMixin(object):
|
||||
|
||||
options = {'escape': False, 'hard_wrap':True}
|
||||
|
||||
def block_code(self, text, lang):
|
||||
# renderer has an options
|
||||
inlinestyles = self.options.get('inlinestyles', False)
|
||||
linenos = self.options.get('linenos', False)
|
||||
return block_code(text, lang, inlinestyles, linenos)
|
||||
|
||||
|
||||
class HighlightRenderer(mistune.Renderer):
|
||||
|
||||
options = {'escape': False, 'hard_wrap':True}
|
||||
|
||||
def block_code(self, code, lang):
|
||||
if not lang:
|
||||
return '\n<pre><code>%s</code></pre>\n' % \
|
||||
mistune.escape(code)
|
||||
lexer = get_lexer_by_name(lang, stripall=True)
|
||||
formatter = html.HtmlFormatter()
|
||||
return highlight(code, lexer, formatter)
|
||||
|
||||
#markdown_renderer = mistune.Renderer(escape=False, hard_wrap=True)
|
||||
#markdown = mistune.Markdown(renderer=HighlightMixin)
|
||||
renderer = HighlightRenderer()
|
||||
markdown = mistune.Markdown(renderer=renderer)
|
||||
|
||||
def fread(filename):
|
||||
"""Read file and close the file."""
|
||||
|
|
@ -124,7 +175,7 @@ def read_content(filename):
|
|||
# Convert Markdown content to HTML.
|
||||
if filename.endswith((".md", ".mkd", ".mkdn", ".mdown", ".markdown")):
|
||||
clean_text = text.replace('<!-- more -->', '')
|
||||
text = mistune.markdown(clean_text)
|
||||
text = markdown(clean_text)
|
||||
|
||||
# Update the dictionary with content and RFC 2822 date.
|
||||
content.update({"content": text, "rfc_2822_date": rfc_2822_format(content["date"])})
|
||||
|
|
@ -235,7 +286,7 @@ def make_posts(
|
|||
avatar=comment.get("avatar", ""),
|
||||
site=comment.get("site", ""),
|
||||
date=comment["date"],
|
||||
content=mistune.markdown(comment["content"]),
|
||||
content=markdown(comment["content"]),
|
||||
)
|
||||
out_comments.append(out_comment)
|
||||
page_params["comments"] = "".join(out_comments)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue