2010年9月8日 星期三

SyntaxHighter: 在 blogspot 上顯示漂亮的 code 格式

SyntaxHighlighter 目前出到 3.0.83 版
以下是簡單的如何使用在 blogspot 上的流程,官網有詳細的說明



1. 安裝 SyntaxHighlighter的 core 和 theme
在 <header> ... </header> 之間加上
<link href='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shCore.css' rel='stylesheet' type='text/css'/>  
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shCore.js' type='text/javascript'/>
<link href='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>

官方提供了一些 theme 的選擇

2. 安裝你想要的語言的 brush
ex: Python
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushPython.js' type='text/javascript'/> 

官方提供的 brush 列表

3. 啟動 SyntaxHighlighter
<script type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all()
</script>

注意:blogspot 一定要加上 SyntaxHighlighter.config.bloggerMode = true;

4. 使用
語法:
<pre class="brush: <code 的語言>">
... code ...
</pre>


例子: Python code
<pre class="brush: py">
class X(object):
def __init__(self, x=set([])):
self.x = x
</pre>

顯示:
class X(object):
def __init__(self, x=set([])):
self.x = x


使用實例: Python: default value to a constructor's parameter

沒有留言:

張貼留言