• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..30-Sep-2019-

src/H30-Sep-2019-8,0664,868

styles/H30-Sep-2019-482443

CHANGES.mdH A D28-Sep-20195.9 KiB138101

COPYINGH A D28-Sep-201910.5 KiB192160

README.mdH A D28-Sep-20197.2 KiB241181

README.md

1# JavaScript code prettifier
2
3Announcement: [**Action required** *rawgit.com* is going away](https://groups.google.com/forum/#!topic/js-code-prettifier/IFHev_kLm30).
4
5An embeddable script that makes source-code snippets in HTML prettier.
6
7* Works on HTML pages.
8* Works even if code contains embedded links, line numbers, etc.
9* Simple API: include some JS & CSS and add an onload handler.
10* Lightweights: small download and does not block page from loading while
11  running.
12* Customizable styles via CSS. See the [themes gallery][1].
13* Supports all C-like, Bash-like, and XML-like languages. No need to specify
14  the language.
15* Extensible language handlers for other languages. You can specify the
16  language.
17* Widely used with good cross-browser support. Powers https://code.google.com/
18  and http://stackoverflow.com/
19
20[See an example][2].
21
22## Setup
23
24 * Include the script tag below in your document:
25```HTML
26<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
27```
28 * See [Getting Started](docs/getting_started.md) to configure that URL with
29   options you need.
30 * Look at the [skin gallery][1] and pick styles that suit you.
31
32## Usage
33
34Put code snippets in `<pre class="prettyprint">...</pre>` or
35`<code class="prettyprint">...</code>` and it will automatically be
36pretty-printed.
37
38```HTML
39<pre class="prettyprint">class Voila {
40public:
41  // Voila
42  static const string VOILA = "Voila";
43
44  // will not interfere with embedded <a href="#voila2">tags</a>.
45}</pre>
46```
47
48## FAQ
49
50### For which languages does it work?
51
52The comments in `prettify.js` are authoritative but the lexer should work on a
53number of languages including C and friends, Java, Python, Bash, SQL, HTML,
54XML, CSS, JavaScript, Makefile, and Rust.
55
56It works passably on Ruby, PHP, VB, and Awk and a decent subset of Perl and
57Ruby, but because of commenting conventions, doesn't work on Smalltalk, OCaml,
58etc. without a language extension.
59
60Other languages are supported via extensions:
61
62[Apollo](src/lang-apollo.js);
63[Basic](src/lang-basic.js);
64[Clojure](src/lang-clj.js);
65[CSS](src/lang-css.js);
66[Dart](src/lang-dart.js);
67[Erlang](src/lang-erlang.js);
68[Go](src/lang-go.js);
69[Haskell](src/lang-hs.js);
70[Lasso](src/lang-lasso.js);
71[Lisp, Scheme](src/lang-lisp.js);
72[LLVM](src/lang-llvm.js);
73[Logtalk](src/lang-logtalk.js);
74[Lua](src/lang-lua.js);
75[MATLAB](src/lang-matlab.js);
76[MLs: F#, Ocaml,SML](src/lang-ml.js);
77[Mumps](src/lang-mumps.js);
78[Nemerle](src/lang-n.js);
79[Pascal](src/lang-pascal.js);
80[Protocol buffers](src/lang-proto.js);
81[R, S](src/lang-r.js);
82[RD](src/lang-rd.js);
83[Rust](src/lang-rust.js);
84[Scala](src/lang-scala.js);
85[SQL](src/lang-sql.js);
86[Swift](src/lang-swift.js);
87[TCL](src/lang-tcl.js);
88[LaTeX](src/lang-tex.js);
89[Visual Basic](src/lang-vb.js);
90[VHDL](src/lang-vhdl.js);
91[Wiki](src/lang-wiki.js);
92[XQ](src/lang-xq.js);
93[YAML](src/lang-yaml.js)
94
95If you'd like to add an extension for your favorite language, please look at
96`src/lang-lisp.js` and submit a pull request.
97
98### How do I specify the language of my code?
99
100You don't need to specify the language since `PR.prettyPrint()` will guess.
101You can specify a language by specifying the language extension along with the
102`prettyprint` class:
103
104```HTML
105<pre class="prettyprint lang-html">
106  The lang-* class specifies the language file extensions.
107  File extensions supported by default include:
108    "bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java",
109    "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml",
110    "xsl".
111</pre>
112```
113
114You may also use the [HTML 5][3] convention of embedding a `<code>` element
115inside the `<pre>` and using `language-java` style classes:
116
117```HTML
118<pre class="prettyprint"><code class="language-java">...</code></pre>
119```
120
121### It doesn't work on "obfuscated code sample"?
122
123Yes. Prettifying obfuscated code is like putting lipstick on a pig &mdash;
124i.e. outside the scope of this tool.
125
126### Which browsers does it work with?
127
128It's been tested with IE 6, Firefox 1.5 & 2, and Safari 2.0.4. Look at
129[the tests][4] to see if it works in your browser.
130
131### What's changed?
132
133See the [changelog](CHANGES.md).
134
135### Why doesn't Prettyprinting of strings work on WordPress?
136
137Apparently wordpress does "smart quoting" which changes close quotes.  This
138causes end quotes to not match up with open quotes.
139
140This breaks prettifying as well as copying and pasting of code samples.  See
141[WordPress's help center][5] for info on how to stop smart quoting of code
142snippets.
143
144### How do I put line numbers in my code?
145
146You can use the `linenums` class to turn on line numbering.  If your code
147doesn't start at line number `1`, you can add a colon and a line number to the
148end of that class as in `linenums:52`. For example:
149
150```HTML
151<pre class="prettyprint linenums:4"
152>// This is line 4.
153foo();
154bar();
155baz();
156boo();
157far();
158faz();
159</pre>
160```
161
162### How do I prevent a portion of markup from being marked as code?
163
164You can use the `nocode` class to identify a span of markup that is not code:
165
166```HTML
167<pre class="prettyprint">
168int x = foo();  /* This is a comment  <span class="nocode">This is not code</span>
169  Continuation of comment */
170int y = bar();
171</pre>
172```
173
174For a more complete example see the [issue #22 testcase][6].
175
176### I get an error message "a is not a function" or "opt_whenDone is not a function"
177
178If you are calling `prettyPrint` via an event handler, wrap it in a function.
179Instead of doing:
180
181```JavaScript
182addEventListener('load', PR.prettyPrint, false);
183```
184
185wrap it in a closure like:
186
187```JavaScript
188addEventListener('load', function(event) { PR.prettyPrint(); }, false);
189```
190
191so that the browser does not pass an event object to `PR.prettyPrint`
192which will confuse it.
193
194### How can I customize the colors and styles of my code?
195
196Prettify adds `<span>` with `class`es describing the kind of code.  You can
197create CSS styles to matches these classes.
198
199See the [theme gallery][1] for examples.
200
201### I can't add classes to my code (because it comes from Markdown, etc.)
202
203Instead of `<pre class="prettyprint ...">` you can use a comment or processing
204instructions that survives processing instructions: `<?prettify ...?>` works
205as explained in [Getting Started](docs/getting_started.md).
206
207### How can I put line numbers on every line instead of just every fifth line?
208
209Prettify puts lines into an HTML list element so that line numbers aren't
210caught by copy/paste, and the line numbering is controlled by CSS in the
211default stylesheet, `prettify.css`.
212
213The following should turn line numbering back on for the other lines:
214
215```HTML
216<style>
217li.L0, li.L1, li.L2, li.L3,
218li.L5, li.L6, li.L7, li.L8 {
219  list-style-type: decimal !important;
220}
221</style>
222```
223
224## Discussion
225
226Please use the official [support group][7] for discussions, suggestions, and
227general feedback.
228
229## License
230
231[Apache License 2.0](COPYING)
232
233
234[1]: https://raw.githack.com/google/code-prettify/master/styles/index.html
235[2]: https://raw.githack.com/google/code-prettify/master/examples/quine.html
236[3]: http://dev.w3.org/html5/spec-author-view/the-code-element.html#the-code-element
237[4]: https://raw.githack.com/google/code-prettify/master/tests/prettify_test.html
238[5]: http://wordpress.org/support/topic/125038
239[6]: https://raw.githack.com/google/code-prettify/master/tests/prettify_test.html#issue22
240[7]: http://groups.google.com/group/js-code-prettifier
241