Skip to content

ECMAScript2/es2-code-prettify

 
 

Repository files navigation

Code Prettify for ES2

JavaScript code prettifier based on Google Code Prettify.

Language files are fully compatible with Google Code Prettify.

  • Works on HTML pages.
  • Works even if code contains embedded links, line numbers, etc.
  • Customizable styles via CSS. See the themes gallery.

See an example.

Comparison

Google Code PrettifyES2 Code Prettify
Lower limit of supported browsersIE6, Gecko 1.8, Opera 8🌸IE5, 🌸Gecko 0.6, 🌸Opera 7.0
LanguagesAdd dynamically except built-inSelect the language to be used on the site or page at build time
Dynamic addition of theme
File size🌸14.551 bytes50,042 bytes(+24KB: RegExp ponyfill), Build with "web,c,cs,java,bash,python,perl,ruby,coffee".
Dependency🌸noneweb-doc-base, what-browser-am-i, ES2 RegExpCompat
Line Numbering✔ (You can disable the function at build time.)
Set by comment node✔ (You can disable the feature at build time. Some browsers do not support comment nodes.)
Execution by time divisionManual. Measure and schedule the elapsed time at the time to finish highlighting the code block.Always. Elapsed time is measured and scheduled at each timing of regular expression instance creation, token creation, and decoration.

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>

How to build

Please see gulpfile.js or web-doc-base / gulpfile.js.

gulp all
gulp web

FAQ

For which languages does it work?

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.

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

How do I specify the language of my code?

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.
</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 5, Gecko 0.6, Opera 7.0. Look at the tests to see if it works in your browser.

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.

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.

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>

Charts

Flowchart of Google Code Prettify

Edit in draw.io

SimpleLexerRegistry

Edit in draw.io

License

ES2 Code Prettify is licensed under Apache License 2.0

(C) 2022-2023 itozyun(outcloud.blogspot.com)

About

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

Resources

Stars

Watchers

Forks

Languages

  • JavaScript 100.0%