1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
<title>Javascript code prettifier</title>
8
<link href="src/prettify.css" type="text/css" rel="stylesheet" />
10
<script src="src/prettify.js" type="text/javascript"></script>
12
<style type="text/css">
13
body { margin-left: .5in }
14
h1, h2, h3, h4, .footer { margin-left: -.4in; }
18
<body onload="prettyPrint()" bgcolor="white">
19
<small style="float: right">Languages : <a href="README-zh-Hans.html">CH</a></small>
20
<h1>Javascript code prettifier</h1>
24
<li><a href="http://code.google.com/p/google-code-prettify/downloads/list">Download</a> a distribution
25
<li>Include the script and stylesheets in your document
26
(you will need to make sure the css and js file are on your server, and
27
adjust the paths in the <tt>script</tt> and <tt>link</tt> tag)
28
<pre class="prettyprint">
29
<link href="prettify.css" type="text/css" rel="stylesheet" />
30
<script type="text/javascript" src="prettify.js"></script></pre>
31
<li>Add <code class="prettyprint lang-in.tag">onload="prettyPrint()"</code> to your
33
<li>Modify the stylesheet to get the coloring you prefer</li>
37
<p>Put code snippets in
38
<tt><pre class="prettyprint">...</pre></tt>
39
or <tt><code class="prettyprint">...</code></tt>
40
and it will automatically be pretty printed.
42
<table summary="code examples">
47
<td><pre style="border: 1px solid #888;padding: 2px"
48
><a name="voila1"></a>class Voila {
51
static const string VOILA = "Voila";
53
// will not interfere with embedded <a href="#voila1">tags</a>.
56
<td><pre class="prettyprint"><a name="voila2"></a>class Voila {
59
static const string VOILA = "Voila";
61
// will not interfere with embedded <a href="#voila2">tags</a>.
66
<h3 id="langs">For which languages does it work?</h3>
67
<p>The comments in <tt>prettify.js</tt> are authoritative but the lexer
68
should work on a number of languages including C and friends,
69
Java, Python, Bash, SQL, HTML, XML, CSS, Javascript, and Makefiles.
70
It works passably on Ruby, PHP, VB, and Awk and a decent subset of Perl
71
and Ruby, but, because of commenting conventions, doesn't work on
72
Smalltalk, or CAML-like languages.</p>
74
<p>LISPy languages are supported via an extension:
75
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lisp.js"
76
><code>lang-lisp.js</code></a>.</p>
78
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-clj.js"
79
><code>Clojure</code></a>,
80
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-css.js"
81
><code>CSS</code></a>,
82
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-go.js"
84
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-hs.js"
85
><code>Haskell</code></a>,
86
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lua.js"
87
><code>Lua</code></a>,
88
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-ml.js"
89
><code>OCAML, SML, F#</code></a>,
90
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-n.js"
91
><code>Nemerle</code></a>,
92
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-proto.js"
93
><code>Protocol Buffers</code></a>,
94
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-scala.js"
95
><code>Scala</code></a>,
96
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-sql.js"
97
><code>SQL</code></a>,
98
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-tex.js"
99
><code>TeX, LaTeX</code></a>,
100
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vhdl.js"
101
><code>VHDL</code></a>,
102
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vb.js"
103
><code>Visual Basic</code></a>,
104
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-wiki.js"
105
><code>WikiText</code></a>,
106
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-xq.js"
107
><code>XQuery</code></a>, and
108
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-yaml.js"
109
><code>YAML</code></a>.
111
<p>If you'd like to add an extension for your favorite language, please
112
look at <tt>src/lang-lisp.js</tt> and file an
113
<a href="http://code.google.com/p/google-code-prettify/issues/list"
114
>issue</a> including your language extension, and a testcase.</p>
116
<h3>How do I specify the language of my code?</h3>
117
<p>You don't need to specify the language since <code>prettyprint()</code>
118
will guess. You can specify a language by specifying the language extension
119
along with the <code>prettyprint</code> class like so:</p>
120
<pre class="prettyprint lang-html"
121
><pre class="prettyprint <b>lang-html</b>">
122
The lang-* class specifies the language file extensions.
123
File extensions supported by default include
124
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html",
125
"java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh",
126
"xhtml", "xml", "xsl".
129
<p>You may also use the
130
<a href="http://dev.w3.org/html5/spec-author-view/the-code-element.html#the-code-element"
131
>HTML 5</a> convention of embedding a <tt>code</tt> element inside the
132
<code>PRE</code> and using <code>language-java</code> style classes.
133
E.g. <xmp class="prettyprint"><pre class="prettyprint"><code class="language-java">...</code></pre></xmp>
135
<h3>It doesn't work on <tt><obfuscated code sample></tt>?</h3>
136
<p>Yes. Prettifying obfuscated code is like putting lipstick on a pig
137
— i.e. outside the scope of this tool.</p>
139
<h3>Which browsers does it work with?</h3>
140
<p>It's been tested with IE 6, Firefox 1.5 & 2, and Safari 2.0.4.
141
Look at <a href="tests/prettify_test.html">the test page</a> to see if it
142
works in your browser.</p>
144
<h3>What's changed?</h3>
145
<p>See the <a href="CHANGES.html">change log</a></p>
147
<h3>Why doesn't Prettyprinting of strings work on WordPress?</h3>
148
<p>Apparently wordpress does "smart quoting" which changes close quotes.
149
This causes end quotes to not match up with open quotes.
150
<p>This breaks prettifying as well as copying and pasting of code samples.
152
<a href="http://wordpress.org/support/topic/125038"
153
>WordPress's help center</a> for info on how to stop smart quoting of code
156
<h3 id="linenums">How do I put line numbers in my code?</h3>
157
<p>You can use the <code>linenums</code> class to turn on line
158
numbering. If your code doesn't start at line number 1, you can
159
add a colon and a line number to the end of that class as in
160
<code>linenums:52</code>.
163
<pre class="prettyprint"><pre class="prettyprint linenums:<b>4</b>"
164
>// This is line 4.
173
<pre class="prettyprint linenums:4"
183
<h3>How do I prevent a portion of markup from being marked as code?</h3>
184
<p>You can use the <code>nocode</code> class to identify a span of markup
186
<pre class="prettyprint"><pre class=prettyprint>
187
int x = foo(); /* This is a comment <span class="nocode">This is not code</span>
188
Continuation of comment */
192
<pre class="prettyprint">
193
int x = foo(); /* This is a comment <span class="nocode">This is not code</span>
194
Continuation of comment */
198
<p>For a more complete example see the issue22
199
<a href="tests/prettify_test.html#issue22">testcase</a>.</p>
201
<h3>I get an error message "a is not a function" or "opt_whenDone is not a function"</h3>
202
<p>If you are calling <code>prettyPrint</code> via an event handler, wrap it in a function.
205
<code class="prettyprint lang-js"
206
>addEventListener('load', prettyPrint, false);</code>
208
wrap it in a closure like
210
<code class="prettyprint lang-js"
211
>addEventListener('load', function (event) { prettyPrint() }, false);</code>
213
so that the browser does not pass an event object to <code>prettyPrint</code> which
216
<h3>How can I customize the colors and styles of my code?</h3>
218
Prettify adds <code><span></code> with <code>class</code>es describing
219
the kind of code. You can create CSS styles to matches these
222
<a href="http://google-code-prettify.googlecode.com/svn/trunk/styles/index.html">
223
theme gallery</a> for examples.
229
<!-- Created: Tue Oct 3 17:51:56 PDT 2006 -->
230
<!-- hhmts start -->Last modified: Fri May 27 20:23:23 PDT 2011 <!-- hhmts end -->