2006-10-06

dp.SyntaxHighlighter代码高亮

UPDATE:由于Blogger的一些限制,导致本文中的一些内容不正确,请查看此篇补遗
有好几个朋友问我,我用的什么语法高亮的组件,还以为我的Blogger设置有什么特别的,其实我倒没有作过什么设置,我一般是使用生成好了的Html代码,然后贴到日志中的,比如,Vim可以生成高亮的HTML代码,Eclipse,Visual Studio可以将带有样式的代码复制为RTF格式,不少编辑器都有这个功能,另外可以使用在线的代码高亮工具,如 http://www.geshi.org/
另外一个在Blogger中可以使用的高亮组件是基于JavaScript的一个dp.SyntaxHighlighter,其实不少地方都可以看到它的。可以先看看它的Demo。也支持不少常见的代码,不过和GeSHi比要少很多,但也够用了。
具体用法是这样的:先下载该组件,将Scripts中shCore.js和所需高亮的语言shXXX.js以及Styles下的SyntaxHighlighter.css上传到某个地方,如果需要自定义,可以根据TestPages.css进行修改。然后在Blogger的模版的body元素末尾加入以下代码:
〈script src="shCore.js" type="text/javascript"〉〈/script〉
〈script src="shBrush[所要加载的高亮语法].js" type="text/javascript"〉〈/script〉
…………
〈script type="text/javascript"〉dp.SyntaxHighlighter.HighlightAll('code');〈/script〉
然后,在代码中添加<textarea class="[语言]" name="code"></textarea>,其中code属性也对应前面HighlightAll('code')中的code。
由于受Blogger程序的限制,代码中部分符号使用全角表示。
测试一下:

"""
Comment\"
string
"""

cache = {}

string s = "## comments inside a string"
string str = "helllo \"world\", how things?"

def arrange(plans, lines, totalMinutes):
"""arrangements of plans taken lines times with an heuristic that the sum
of values in one arrangement is less then totalMinutes
"""
#if in cache we are done otherwise start calculating and save them to cache
if (plans, lines, totalMinutes) in cache:
return cache[(plans, lines, totalMinutes)]
if lines==1:
r = [[plan] for plan in plans]
cache[(plans, lines, totalMinutes)] = r
return r
solutions = []
for plan in plans:
for ar in sort(list(arrange(plans, lines-1, totalMinutes))):
try:
one_solution = tuple(sort([plan] + list(ar)))
if sum(one_solution) <= totalMinutes and one_solution not in solutions:
solutions.append(one_solution)
except Exception, e:
print "Error:", str(e)
cache[(plans, lines, totalMinutes)] = solutions
return solutions

if __name__ == "__main__":
import sys
lines, totalMinutes = int(sys.argv[1]), int(sys.argv[2])
plans = tuple([int(p) for p in sys.argv[3:]])
print "for", lines, totalMinutes, plans
for sol in arrange(plans, lines, totalMinutes):
print sol

2 条评论:

billconan 说...

中秋快乐!

frelyx 说...

你的测试并没有成功啊