Package detail

code-prettify

google42.6kApache-2.00.1.0

Google Code Prettify

syntax, highlight, highlighting, source

readme

JavaScript code prettifier

An embeddable script that makes source-code snippets in HTML prettier.

  • Works on HTML pages.
  • Works even if code contains embedded links, line numbers, etc.
  • Simple API: include some JS & CSS and add an onload handler.
  • Lightweights: small download and does not block page from loading while running.
  • Customizable styles via CSS. See the themes gallery.
  • Supports all C-like, Bash-like, and XML-like languages. No need to specify the language.
  • Extensible language handlers for other languages. You can specify the language.
  • Widely used with good cross-browser support. Powers https://code.google.com/ and http://stackoverflow.com/

See an example.

Setup

  • Include the script tag below in your document:
    <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
    
  • See Getting Started to configure that URL with options you need.
  • Look at the skin gallery and pick styles that suit you.

Usage

Put code snippets in <pre class="prettyprint">...</pre> or <code class="prettyprint">...</code> and it will automatically be pretty-printed.

<pre class="prettyprint">class Voila {
public:
  // Voila
  static const string VOILA = "Voila";

  // will not interfere with embedded <a href="#voila2">tags</a>.
}</pre>

FAQ

For which languages does it work?

The comments in prettify.js are authoritative but the lexer should work on a number of languages including C and friends, Java, Python, Bash, SQL, HTML, XML, CSS, JavaScript, Makefile, and Rust.

It works passably on Ruby, PHP, VB, and Awk and a decent subset of Perl and Ruby, but because of commenting conventions, doesn't work on Smalltalk, OCaml, etc. without a language extension.

Other languages are supported via extensions:

Apollo; Basic; Clojure; CSS; Dart; Erlang; Go; Haskell; Lasso; Lisp, Scheme; LLVM; Logtalk; Lua; MATLAB; MLs: F#, Ocaml,SML; Mumps; Nemerle; Pascal; Protocol buffers; R, S; RD; Rust; Scala; SQL; Swift; TCL; LaTeX; Visual Basic; VHDL; Wiki; XQ; YAML

If you'd like to add an extension for your favorite language, please look at src/lang-lisp.js and submit a pull request.

How do I specify the language of my code?

You don't need to specify the language since PR.prettyPrint() will guess. You can specify a language by specifying the language extension along with the prettyprint class:

<pre class="prettyprint lang-html">
  The lang-* class specifies the language file extensions.
  File extensions supported by default include:
    "bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java",
    "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml",
    "xsl".
</pre>

You may also use the HTML 5 convention of embedding a <code> element inside the <pre> and using language-java style classes:

<pre class="prettyprint"><code class="language-java">...</code></pre>

It doesn't work on "obfuscated code sample"?

Yes. Prettifying obfuscated code is like putting lipstick on a pig — i.e. outside the scope of this tool.

Which browsers does it work with?

It's been tested with IE 6, Firefox 1.5 & 2, and Safari 2.0.4. Look at the tests to see if it works in your browser.

What's changed?

See the changelog.

Why doesn't Prettyprinting of strings work on WordPress?

Apparently wordpress does "smart quoting" which changes close quotes. This causes end quotes to not match up with open quotes.

This breaks prettifying as well as copying and pasting of code samples. See WordPress's help center for info on how to stop smart quoting of code snippets.

How do I put line numbers in my code?

You can use the linenums class to turn on line numbering. If your code doesn't start at line number 1, you can add a colon and a line number to the end of that class as in linenums:52. For example:

<pre class="prettyprint linenums:4"
>// This is line 4.
foo();
bar();
baz();
boo();
far();
faz();
</pre>

How do I prevent a portion of markup from being marked as code?

You can use the nocode class to identify a span of markup that is not code:

<pre class="prettyprint">
int x = foo();  /* This is a comment  <span class="nocode">This is not code</span>
  Continuation of comment */
int y = bar();
</pre>

For a more complete example see the issue #22 testcase.

I get an error message "a is not a function" or "opt_whenDone is not a function"

If you are calling prettyPrint via an event handler, wrap it in a function. Instead of doing:

addEventListener('load', PR.prettyPrint, false);

wrap it in a closure like:

addEventListener('load', function(event) { PR.prettyPrint(); }, false);

so that the browser does not pass an event object to PR.prettyPrint which will confuse it.

How can I customize the colors and styles of my code?

Prettify adds <span> with classes describing the kind of code. You can create CSS styles to matches these classes.

See the theme gallery for examples.

I can't add classes to my code (because it comes from Markdown, etc.)

Instead of <pre class="prettyprint ..."> you can use a comment or processing instructions that survives processing instructions: <?prettify ...?> works as explained in Getting Started.

How can I put line numbers on every line instead of just every fifth line?

Prettify puts lines into an HTML list element so that line numbers aren't caught by copy/paste, and the line numbering is controlled by CSS in the default stylesheet, prettify.css.

The following should turn line numbering back on for the other lines:

<style>
li.L0, li.L1, li.L2, li.L3,
li.L5, li.L6, li.L7, li.L8 {
  list-style-type: decimal !important;
}
</style>

Discussion

Please use the official support group for discussions, suggestions, and general feedback.

License

Apache License 2.0

changelog

Known Issues

  • Perl formatting is really crappy. Partly because the author is lazy and partly because Perl is hard to parse.
  • On some browsers, <code> elements with newlines in the text which use CSS to specify white-space:pre will have the newlines improperly stripped if the element is not attached to the document at the time the stripping is done. Also, on IE6, all newlines will be stripped from <code> elements because of the way IE6 produces innerHTML. Workaround: use <pre> for code with newlines.

Change Log

29 March 2007

  • Added tests for PHP support to address issue #3.
  • Fixed bug #6: prettyPrintOne was not halting. This was not reachable through the normal entry point.
  • Fixed bug #4: recursing into a script block or PHP tag that was not properly closed would not silently drop the content. (test)
  • Fixed bug #8: was eating tabs (test)
  • Fixed entity handling so that the caveat

    Caveats: please properly escape less-thans. x&lt;y instead of x<y, and use " instead of &quot; for string delimiters.

    is no longer applicable.

  • Added noisefree's C# patch #7
  • Added a distribution that has comments and whitespace removed to reduce download size from 45.5kB to 12.8kB.

4 Jul 2008

  • Added #17 language specific formatters that are triggered by the presence of a lang-<language-file-extension>
  • Fixed bug #29: python handling of '''string'''
  • Fixed bug: / in regex [charsets] should not end regex

5 Jul 2008

  • Defined language extensions for Lisp and Lua

14 Jul 2008

  • Language handlers for F#, OCAML, SQL
  • Support for nocode spans to allow embedding of line numbers and code annotations which should not be styled or otherwise affect the tokenization of prettified code. See the issue #22 testcase.

6 Jan 2009

  • Language handlers for Visual Basic, Haskell, CSS, and WikiText
  • Added .mxml extension to the markup style handler for Flex MXML files. See issue #37.
  • Added .m extension to the C style handler so that Objective C source files properly highlight. See issue #58.
  • Changed HTML lexer to use the same embedded source mechanism as the wiki language handler, and changed to use the registered CSS handler for STYLE element content.

21 May 2009

  • Rewrote to improve performance on large files. See benchmarks.
  • Fixed bugs with highlighting of Haskell line comments, Lisp number literals, Lua strings, C preprocessor directives, newlines in Wiki code on Windows, and newlines in IE6.

14 August 2009

  • Fixed prettifying of <code> blocks with embedded newlines.

3 October 2009

  • Fixed prettifying of XML/HTML tags that contain uppercase letters.

19 July 2010

  • Added support for line numbers. Bug #22
  • Added YAML support. Bug #123
  • Added VHDL support courtesy Le Poussin.
  • IE performance improvements. Bug #102 courtesy jacobly.
  • A variety of markup formatting fixes courtesy smain and thezbyg.
  • Fixed copy and paste in IE 6, 7, 8.
  • Changed output to use &#160; instead of &nbsp; so that the output works when embedded in XML. Bug #108.

7 September 2010

  • Added support for coffeescript courtesy Cezary Bartoszuk.

4 March 2011

  • Added a themes gallery to showcase contributed styles.
  • Added support for XQuery courtesy Patrick Wied, Nemerle courtesy Zimin A.V., and Latex support courtesy Martin S.

29 March 2011

  • Fixed IE newline issues, and copying/pasting of prettified source code from IE. This required significant internal changes but involves no API changes. Caveat: prettyPrintOne injects the HTML passed to it into a <pre> element. If the HTML comes from a trusted source, this may allow XSS. Do not do this. This should not be a problem for existing apps since the standard usage is to rewrite the HTML and then inject it, so anyone doing that with untrusted HTML already has an XSS vulnerability. If you sanitize and prettify HTML from an untrusted source, sanitize first.

4 February 2013

  • Language handlers for Dart, Erlang, Mumps, TCL, R, S., and others
  • Bug fix: VB REM style comments.
  • Bug fix: CSS color literals / ID selector confusion.
  • Bug fix: IE8 line breaks.

24 February 2013

  • Added a one script autoload&run mechanism and a way to embed hints in processing instructions/comments. See example.

4 March 2013

  • Matlab language handler courtesy Amro³

28 Apr 2015

  • Migrated to Github