Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add pygments support

  • Loading branch information...
commit ed6c158ed614715c558e0642e89d0c6b8f9f8da0 1 parent 078a659
David authored
Showing with 24 additions and 2 deletions.
  1. +1 −1  models.py
  2. +22 −1 templatefilters.py
  3. +1 −0  templates/base.html
View
2  models.py
@@ -5,7 +5,7 @@
from sqlalchemy.orm import relation, backref
from sqlalchemy.ext.declarative import declarative_base
-engine = create_engine('mysql://root:root@localhost/davidblog_new?charset=utf8', echo=False)
+engine = create_engine('mysql://root:root@localhost/davidblog?charset=utf8', echo=False)
Base = declarative_base()
metadata = Base.metadata
View
23 templatefilters.py
@@ -1,6 +1,10 @@
#-*-coding:utf-8-*-
import hashlib
+import re
from markdown import markdown
+from pygments import highlight
+from pygments.lexers import get_lexer_by_name
+from pygments.formatters import HtmlFormatter
def avatar(value):
return hashlib.md5(value.lower()).hexdigest()
@@ -17,4 +21,21 @@ def formnote(value):
return ''
def content(value):
- return markdown(value)
+ content = markdown(value)
+ codes = re.findall('<code>.*?</code>', content, re.U | re.S)
+ for i in codes:
+ syntax = re.search('#syntax#\w+#/syntax#', i, re.U | re.S)
+ if syntax:
+ code = i[i.index('#/syntax#') + 9:-7]
+ else:
+ code = i[6:-7]
+ try:
+ lexer = get_lexer_by_name(syntax.group()[8:-9], stripall=True)
+ except:
+ lexer = get_lexer_by_name('c', stripall=True)
+ code = highlight(
+ code,
+ lexer,
+ HtmlFormatter(linenos=False, cssclass="syntax"))
+ content = content.replace(i, code)
+ return content
View
1  templates/base.html
@@ -7,6 +7,7 @@
<meta name="author" content="davidx, 灰太狼大王" />
<meta name="keywords" content="davidx, 灰太狼大王, 泥泞的沼泽, python, linux, webpy, web, php, yii, gentoo" />
<link rel="stylesheet" type="text/css" href="/static/css/styles.css" />
+ <link rel="stylesheet" type="text/css" href="/static/css/syntax.css" />
<link rel="SHORTCUT ICON" href="/static/favicon.ico" />
<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
</head>
Please sign in to comment.
Something went wrong with that request. Please try again.