README.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <title>Javascript code prettifier</title>
  6. <link href="src/prettify.css" type="text/css" rel="stylesheet" />
  7. <script src="src/prettify.js" type="text/javascript"></script>
  8. <style type="text/css">
  9. body { margin-left: .5in }
  10. h1, h2, h3, h4, .footer { margin-left: -.4in; }
  11. a.Extension { display: inline-block; width: 5em; height:2.5em; border: 1px solid black; vertical-align: top; text-align: center }
  12. </style>
  13. </head>
  14. <body onload="prettyPrint()" bgcolor="white">
  15. <small style="float: right">Languages : <a href="README-zh-Hans.html">CH</a></small>
  16. <h1>Javascript code prettifier</h1>
  17. <h2>Setup</h2>
  18. <ol>
  19. <li><a href="http://code.google.com/p/google-code-prettify/downloads/list">Download</a> a distribution
  20. <li>Include the script tag below in your document
  21. <pre class="prettyprint">
  22. &gt;script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js&gt;&lt;/script&gt;</pre>
  23. <li>See <a href="http://code.google.com/p/google-code-prettify/wiki/GettingStarted">Getting Started</a> to configure that URL with options you need.</a>
  24. <li>Look at the <a href="http://google-code-prettify.googlecode.com/svn/trunk/styles/index.html">skin gallery</a> and pick styles that suit you.</li>
  25. </ol>
  26. <h2>Usage</h2>
  27. <p>Put code snippets in
  28. <tt>&lt;pre class="prettyprint"&gt;...&lt;/pre&gt;</tt>
  29. or <tt>&lt;code class="prettyprint"&gt;...&lt;/code&gt;</tt>
  30. and it will automatically be pretty printed.
  31. <table summary="code examples">
  32. <tr>
  33. <th>The original
  34. <th>Prettier
  35. <tr>
  36. <td><pre style="border: 1px solid #888;padding: 2px"
  37. ><a name="voila1"></a>class Voila {
  38. public:
  39. // Voila
  40. static const string VOILA = "Voila";
  41. // will not interfere with embedded <a href="#voila1">tags</a>.
  42. }</pre>
  43. <td><pre class="prettyprint"><a name="voila2"></a>class Voila {
  44. public:
  45. // Voila
  46. static const string VOILA = "Voila";
  47. // will not interfere with embedded <a href="#voila2">tags</a>.
  48. }</pre>
  49. </table>
  50. <h2>FAQ</h2>
  51. <h3 id="langs">For which languages does it work?</h3>
  52. <p>The comments in <tt>prettify.js</tt> are authoritative but the lexer
  53. should work on a number of languages including C and friends,
  54. Java, Python, Bash, SQL, HTML, XML, CSS, Javascript, Makefiles,
  55. and Rust.
  56. It works passably on Ruby, PHP, VB, and Awk and a decent subset of Perl
  57. and Ruby, but, because of commenting conventions, but doesn't work on
  58. Smalltalk.</p>
  59. <p>Other languages are supported via extensions:
  60. <div>
  61. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-apollo.js">Apollo</a>
  62. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-basic.js">Basic</a>
  63. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-clj.js">Clojure</a>
  64. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-css.js">CSS</a>
  65. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-dart.js">Dart</a>
  66. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-erlang.js">Erlang</a>
  67. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-go.js">Go</a>
  68. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-hs.js">Haskell</a>
  69. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lisp.js">Lisp, Scheme</a>
  70. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-llvm.js">Llvm</a>
  71. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lua.js">Lua</a>
  72. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-matlab.js">Matlab</a>
  73. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-ml.js">MLs:F#, Ocaml,SML</a>
  74. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-mumps.js">Mumps</a>
  75. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-n.js">Nemerle</a>
  76. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-pascal.js">Pascal</a>
  77. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-proto.js">Protocol buffers</a>
  78. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-r.js">R, S</a>
  79. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-rd.js">RD</a>
  80. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-scala.js">Scala</a>
  81. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-sql.js">SQL</a>
  82. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-tcl.js">TCL</a>
  83. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-tex.js">Latek</a>
  84. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vb.js">Visual Basic</a>
  85. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vhdl.js">CHDL</a>
  86. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-wiki.js">Wiki</a>
  87. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-xq.js">XQ</a>
  88. <a class="Extension" href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-yaml.js">YAML</a>
  89. </div>
  90. <p>If you'd like to add an extension for your favorite language, please
  91. look at <tt>src/lang-lisp.js</tt> and file an
  92. <a href="http://code.google.com/p/google-code-prettify/issues/list"
  93. >issue</a> including your language extension, and a testcase.</p>
  94. <h3>How do I specify the language of my code?</h3>
  95. <p>You don't need to specify the language since <code>prettyprint()</code>
  96. will guess. You can specify a language by specifying the language extension
  97. along with the <code>prettyprint</code> class like so:</p>
  98. <pre class="prettyprint lang-html"
  99. >&lt;pre class=&quot;prettyprint <b>lang-html</b>&quot;&gt;
  100. The lang-* class specifies the language file extensions.
  101. File extensions supported by default include
  102. "bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html",
  103. "java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh",
  104. "xhtml", "xml", "xsl".
  105. &lt;/pre&gt;</pre>
  106. <p>You may also use the
  107. <a href="http://dev.w3.org/html5/spec-author-view/the-code-element.html#the-code-element"
  108. >HTML 5</a> convention of embedding a <tt>code</tt> element inside the
  109. <code>PRE</code> and using <code>language-java</code> style classes.
  110. E.g. <xmp class="prettyprint"><pre class="prettyprint"><code class="language-java">...</code></pre></xmp>
  111. <h3>It doesn't work on <tt>&lt;obfuscated code sample&gt;</tt>?</h3>
  112. <p>Yes. Prettifying obfuscated code is like putting lipstick on a pig
  113. &mdash; i.e. outside the scope of this tool.</p>
  114. <h3>Which browsers does it work with?</h3>
  115. <p>It's been tested with IE 6, Firefox 1.5 &amp; 2, and Safari 2.0.4.
  116. Look at <a href="tests/prettify_test.html">the test page</a> to see if it
  117. works in your browser.</p>
  118. <h3>What's changed?</h3>
  119. <p>See the <a href="CHANGES.html">change log</a></p>
  120. <h3>Why doesn't Prettyprinting of strings work on WordPress?</h3>
  121. <p>Apparently wordpress does "smart quoting" which changes close quotes.
  122. This causes end quotes to not match up with open quotes.
  123. <p>This breaks prettifying as well as copying and pasting of code samples.
  124. See
  125. <a href="http://wordpress.org/support/topic/125038"
  126. >WordPress's help center</a> for info on how to stop smart quoting of code
  127. snippets.</p>
  128. <h3 id="linenums">How do I put line numbers in my code?</h3>
  129. <p>You can use the <code>linenums</code> class to turn on line
  130. numbering. If your code doesn't start at line number 1, you can
  131. add a colon and a line number to the end of that class as in
  132. <code>linenums:52</code>.
  133. <p>For example
  134. <pre class="prettyprint">&lt;pre class="prettyprint linenums:<b>4</b>"
  135. &gt;// This is line 4.
  136. foo();
  137. bar();
  138. baz();
  139. boo();
  140. far();
  141. faz();
  142. &lt;pre&gt;</pre>
  143. produces
  144. <pre class="prettyprint linenums:4"
  145. >// This is line 4.
  146. foo();
  147. bar();
  148. baz();
  149. boo();
  150. far();
  151. faz();
  152. </pre>
  153. <h3>How do I prevent a portion of markup from being marked as code?</h3>
  154. <p>You can use the <code>nocode</code> class to identify a span of markup
  155. that is not code.
  156. <pre class="prettyprint">&lt;pre class=prettyprint&gt;
  157. int x = foo(); /* This is a comment &lt;span class="nocode"&gt;This is not code&lt;/span&gt;
  158. Continuation of comment */
  159. int y = bar();
  160. &lt;/pre&gt;</pre>
  161. produces
  162. <pre class="prettyprint">
  163. int x = foo(); /* This is a comment <span class="nocode">This is not code</span>
  164. Continuation of comment */
  165. int y = bar();
  166. </pre>
  167. <p>For a more complete example see the issue22
  168. <a href="tests/prettify_test.html#issue22">testcase</a>.</p>
  169. <h3>I get an error message "a is not a function" or "opt_whenDone is not a function"</h3>
  170. <p>If you are calling <code>prettyPrint</code> via an event handler, wrap it in a function.
  171. Instead of doing
  172. <blockquote>
  173. <code class="prettyprint lang-js"
  174. >addEventListener('load', prettyPrint, false);</code>
  175. </blockquote>
  176. wrap it in a closure like
  177. <blockquote>
  178. <code class="prettyprint lang-js"
  179. >addEventListener('load', function (event) { prettyPrint() }, false);</code>
  180. </blockquote>
  181. so that the browser does not pass an event object to <code>prettyPrint</code> which
  182. will confuse it.
  183. <h3>How can I customize the colors and styles of my code?</h3>
  184. <p>
  185. Prettify adds <code>&lt;span&gt;</code> with <code>class</code>es describing
  186. the kind of code. You can create CSS styles to matches these
  187. classes.
  188. See the
  189. <a href="http://google-code-prettify.googlecode.com/svn/trunk/styles/index.html">
  190. theme gallery</a> for examples.
  191. </p>
  192. <h3>I can't add classes to my code (because it comes from Markdown, etc.)</h3>
  193. <p>
  194. Instead of <code class="prettyprint">&lt;pre class="prettyprint ..."&gt;</code> you can use a
  195. comment or processing instructions that survives processing instructions :
  196. <code>&lt;?prettify ...?&gt;</code> works as explained in
  197. <a href="http://code.google.com/p/google-code-prettify/wiki/GettingStarted">Getting Started</a></p>
  198. <br><br><br>
  199. <div class="footer">
  200. <!-- Created: Tue Oct 3 17:51:56 PDT 2006 -->
  201. <!-- hhmts start -->Last modified: Mon Mar 4 14:16:04 EST 2013 <!-- hhmts end -->
  202. </div>
  203. </body>
  204. </html>