Google Code Prettify - 輕量級的語法上色工具
[img]http://jsgears.com/_icons/famfamfam/folder.gif[/img] Google Code Prettify (Javascript code prettifier)[img]http://jsgears.com/_icons/famfamfam/icon_home.gif[/img] [url]http://code.google.com/p/google-code-prettify/[/url]
[img]http://jsgears.com/_icons/famfamfam/comment.gif[/img] 輕量級的語法上色工具
這是一套由 Google 所推出並且大量運用於 Google Code 網站的輕量級的語法上色工具,如果您曾經用過 Google 的 API,對於本篇文章的程式碼色彩必定不陌生,因為這是同一套程式所產生的。
用法也相當簡單,下載解壓縮後,引用他的 CSS 和 JavaScript:
[jsg.example]
<pre class="prettyprint">
<link href="prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify.js"></script></pre>
[/jsg.example]
把要上色的程式碼用 class="prettyprint" 的 div 標籤包起來:
[jsg.example]
<pre class="prettyprint">
<pre class="prettyprint">
你的 code 放這邊!
</pre>
</pre>
[/jsg.example]
最後在 body 標籤內加上 onload="prettyPrint()" 即可!
[jsg.example]
<pre class="prettyprint">
<body onload="prettyPrint()">
</pre>
[/jsg.example]
Google Code Prettify 的使用方式就是這麼簡單,會自動判斷要上色的關鍵字,支援多種語言如 C 語言家族、Java、Python、Bash、SQL、HTML、XML、CSS、Javascript、Makefiles 等,對於 Perl、Ruby、PHP、VB、Awk 也部份支援。底下是一些其他範例:
[jsg.example]
<style type="text/css">
pre.prettyprint {
border: 1px solid #BBB;
background-color: #FAFAFA;
}
</style>
<link href="http://jsgears.googlecode.com/svn/trunk/prettify.min/prettify.css" type="text/css" rel="stylesheet" />
這是 Google Maps API 的第一個範例:
<pre class="prettyprint">
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=abcdefg&sensor=true_or_false"
type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 500px; height: 300px"></div>
</body>
</html>
</pre>
C++
<pre class="prettyprint">
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
</pre>
Java
<pre class="prettyprint">
public class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello, world!\n");
}
}
</pre>
<script type="text/javascript" src="http://jsgears.googlecode.com/svn/trunk/prettify.min/prettify.js"></script>
<script type="text/javascript">
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = prettyPrint;
}
else {
window.onload = function() {
oldonload();
prettyPrint();
}
}
</script>
[/jsg.example] 這也太神奇了吧= =
頁:
[1]