用google prettify code给blogspot代码着色
Wordpress有许多代码语法高亮插件可以便捷地展示源代码,但是Blogspot我还发现此类的应用。不过我们可以让google prettify code为Blogspot着色代码。
google prettify code是一个轻量级的Javascript模块通过CSS文件对代码进行上色处理,支持C、Java、PHP、Python、HTMLl和Javascript等十几种语言。让我们动手吧。
1.进入Blogspot控制台 –>布局 –>修改HTML
在head区调用google prettify code的Javascript和CSS文件:
<link href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css" rel="stylesheet" type="text/css"/> <script src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js" type="text/javascript"/>
2.将“<body>”改成“<body onload=’prettyPrint()’>”
3.修改pre标签的CSS以适合您的使用。
pre { margin: 5px 20px; border: 1px dashed #666; padding: 5px; background: #f8f8f8; white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ }
现在可以写一篇博客试一下了,将您的代码放在pre标签内,给pre加上class为“prettyprint”,即:
<pre class=”prettyprint”>
your code
</pre>
您可以访问我在blogspot上的博客查看效果。
如果代码是HTML,建议将它们转换成postable后再写入文章,不过如果用ScribeFire写作,它会将代码自动转换成postable,ScribeFire真得不错。下面两个网站可以在线转换:
http://www.elliotswan.com/postable/
http://www.khurshid.com/i-make-postable/
如果您对google prettify code感兴趣,请访问它们的网站,阅读Readme获得更多信息。

#include #include #include #define _TABSIZE 4 using namespace std; int tabsize = _TABSIZE; class token { public: token() : _what(code) {} protected: enum type {code, comment, pp, keyword}; string _str; type _what; friend istream& operator>>(istream&, token&); friend ostream& operator<<(ostream&, const token&); }; bool iskeyword(const string& s) { static const char* keywords[] = { "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "class", "compl", "const", "const_cast", "continue", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", "new", "not", "not_eq", "operator", "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", "return", "short", "signed", "sizeof", "static", "static_cast", "struct", "switch", "template", "this", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq" }; for (int i = 0; i < sizeof(keywords) / sizeof(char*); i++) if (string(keywords[i]) == s) return true; return false; } bool containspp(const string& s) { static const char* pptokens[] = { "define", "elif", "else", "endif", "error", "if", "ifdef", "ifndef", "include", "line", "pragma", "undef" }; for (int i = 0; i >(istream& is, token& t) { t._str = "", t._what = token::code; int c = is.get(); switch (c) { case '/': c = is.get(); if (c == '*') { t._str = "/*"; t._what = token::comment; while (1) { c = is.get(); if (c == EOF) return is.unget(), is.clear(), is; if (c == '/') { if (t._str.length() > 2 && t._str[t._str.length() - 1] == '*') { return t._str += '/', is; } } t._str += (char)c; } } else if (c == '/') { t._str = "//"; t._what = token::comment; c = is.get(); while (c != '\n' && c != EOF) { t._str += (char)c; c = is.get(); } if (c == '\n') { t._str += '\n'; } return is; } t._str = '/'; return is.unget(), is.clear(), is; case '#': t._str = '#'; c = is.get(); while (strchr(" \r\n\t", c)) { t._str += (char)c; c = is.get(); } if (c == EOF) return is.unget(), is.clear(), is; while (strchr("abcdefghijklmnopqrstuvwxyz", c)) { t._str += (char)c; c = is.get(); } is.unget(), is.clear(); if (containspp(t._str)) t._what = token::pp; return is; case '\'': case '"': { char q = (char)c; t._str = q; while (1) { c = is.get(); if (c == EOF) return is.unget(), is.clear(), is; if (c == q) { if (t._str.length() >= 2) { if (!(t._str[t._str.length() - 1] == '\\' && t._str[t._str.length() - 2] != '\\')) return t._str += q, is; } else { return t._str += q, is; } } t._str += (char)c; } } case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'i': case 'l': case 'm': case 'n': case 'o': case 'p': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': t._str += (char)c; c = is.get(); while (isalpha(c) || isdigit(c) || c == '_') { t._str += (char)c; c = is.get(); } is.unget(), is.clear(); if (iskeyword(t._str)) t._what = token::keyword; return is; case EOF: return is; default: t._str += (char)c; c = is.get(); while (c != '/' && c != '#' && !strchr("abcdefgilmnoprstuvwx", c) && c != '\'' && c != '"' && c != EOF) { t._str += (char)c; c = is.get(); } is.unget(), is.clear(); return is; } } string html(const string& s) { string s1; string::size_type i; for (i = 0; i < s.length(); i++) { switch (s[i]) { case '&': s1 += "&"; break; case '': s1 += ">"; break; case '"': s1 += """; break; case '\t': s1.append(tabsize, ' '); break; default: s1 += s[i]; } } return s1; } ostream& operator<<(ostream& os, const token& t) { if (t._what == token::code) cout << html(t._str); else if (t._what == token::comment) cout << "" << html(t._str) << ""; else if (t._what == token::keyword) cout << "" << html(t._str) << ""; else if (t._what == token::pp) cout << "" << html(t._str) << ""; else cout << html(t._str); return os; } int main(int argc, char **argv) { if (argc != 2 && argc != 3) { cout << "usage: cpphtml file [tab size]" << endl; return 0; } ifstream is(argv[1]); if (!is.good()) { cerr << "bad input file" << endl; return -1; } if (argc == 3) { tabsize = atoi(argv[2]); if (tabsize <= 0) tabsize = _TABSIZE; } cout << "" << endl << "" << endl << "" << endl; cout << ".keyword{color:rgb(0,0,255);}" << endl; cout << ".comment{color:rgb(0,128,0);}" << endl; cout << ".pp{color:rgb(0,0,255);}" << endl; cout << "" << endl << "" << endl; cout << ""; token t; while (is >> t) { cout << t; } cout << "" << "" << endl << "" << endl; return 0; }You should put <pre class=”prettyprint”> at the beginning, and put </pre> at the end. I modified this for you.