prettify.parser
Class Prettify

java.lang.Object
  extended by prettify.parser.Prettify

public class Prettify
extends java.lang.Object

This is similar to the prettify.js in JavaScript Prettify. All comments are adapted from the JavaScript Prettify.

Some functions for browser-side pretty printing of code contained in html.

For a fairly comprehensive set of languages see the README file that came with this source. At a minimum, the lexer should work on a number of languages including C and friends, Java, Python, Bash, SQL, HTML, XML, CSS, Javascript, and Makefiles. It works passably on Ruby, PHP and Awk and a subset of Perl, but, because of commenting conventions, doesn't work on Smalltalk, Lisp-like, or CAML-like languages without an explicit lang class.

Usage:

  1. include this source file in an html page via <script type="text/javascript" src="/path/to/prettify.js"></script>
  2. define style rules. See the example page for examples.
  3. mark the <pre> and <code> tags in your source with class=prettyprint. You can also use the (html deprecated) <xmp> tag, but the pretty printer needs to do more substantial DOM manipulations to support that, so some css styles may not be preserved.
That's it. I wanted to keep the API as simple as possible, so there's no need to specify which language the code is in, but if you wish, you can add another class to the <pre> or <code> element to specify the language, as in <pre class="prettyprint lang-java">. Any class that starts with "lang-" followed by a file extension, specifies the file type. See the "lang-*.js" files in this directory for code that implements per-language file handlers.

Change log:
cbeust, 2006/08/22

Java annotations (start with "@") are now captured as literals ("lit")


Nested Class Summary
 class Prettify.CreateSimpleLexer
           
 
Field Summary
static java.lang.String ALL_KEYWORDS
           
static java.lang.String C_KEYWORDS
           
static java.util.regex.Pattern C_TYPES
           
static java.lang.String COFFEE_KEYWORDS
           
static java.lang.String COMMON_KEYWORDS
           
static java.lang.String CPP_KEYWORDS
           
static java.lang.String CSHARP_KEYWORDS
           
static java.lang.String FLOW_CONTROL_KEYWORDS
           
static java.lang.String JAVA_KEYWORDS
           
static java.lang.String JSCRIPT_KEYWORDS
           
protected  java.util.Map<java.lang.String,java.lang.Object> langHandlerRegistry
          Maps language-specific file extensions to handlers.
static java.lang.String PERL_KEYWORDS
           
static java.lang.String PR_ATTRIB_NAME
          token style for an sgml attribute name.
static java.lang.String PR_ATTRIB_VALUE
          token style for an sgml attribute value.
static java.lang.String PR_COMMENT
          token style for a comment
static java.lang.String PR_DECLARATION
          token style for a markup declaration such as a DOCTYPE.
static java.lang.String PR_KEYWORD
          token style for a keyword
static java.lang.String PR_LITERAL
          token style for a literal value.
static java.lang.String PR_NOCODE
          A class that indicates a section of markup that is not code, e.g.
static java.lang.String PR_PLAIN
          token style for a plain text.
static java.lang.String PR_PUNCTUATION
          token style for a punctuation string.
static java.lang.String PR_SOURCE
          token style for embedded source.
static java.lang.String PR_STRING
          token style for a string literal
static java.lang.String PR_TAG
          token style for an sgml tag.
static java.lang.String PR_TYPE
          token style for a type
static java.lang.String PYTHON_KEYWORDS
           
static java.lang.String RUBY_KEYWORDS
           
static java.lang.String RUST_KEYWORDS
           
static java.lang.String SH_KEYWORDS
           
 
Constructor Summary
Prettify()
           
 
Method Summary
protected static void appendDecorations(int basePos, java.lang.String sourceCode, Prettify.CreateSimpleLexer langHandler, java.util.List<java.lang.Object> out)
          Apply the given language handler to sourceCode and add the resulting decorations to out.
protected  java.util.List<java.lang.String> getFileExtensionsFromClass(java.lang.Class<? extends Lang> clazz)
           
 Prettify.CreateSimpleLexer langHandlerForExtension(java.lang.String extension, java.lang.String source)
          Get the parser for the extension specified.
 void register(java.lang.Class<? extends Lang> clazz)
          Register language handler.
protected  void registerLangHandler(Prettify.CreateSimpleLexer handler, java.util.List<java.lang.String> fileExtensions)
          Register a language handler for the given file extensions.
protected  Prettify.CreateSimpleLexer sourceDecorator(java.util.Map<java.lang.String,java.lang.Object> options)
          returns a function that produces a list of decorations from source text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FLOW_CONTROL_KEYWORDS

public static final java.lang.String FLOW_CONTROL_KEYWORDS
See Also:
Constant Field Values

C_KEYWORDS

public static final java.lang.String C_KEYWORDS
See Also:
Constant Field Values

COMMON_KEYWORDS

public static final java.lang.String COMMON_KEYWORDS
See Also:
Constant Field Values

CPP_KEYWORDS

public static final java.lang.String CPP_KEYWORDS
See Also:
Constant Field Values

JAVA_KEYWORDS

public static final java.lang.String JAVA_KEYWORDS
See Also:
Constant Field Values

RUST_KEYWORDS

public static final java.lang.String RUST_KEYWORDS
See Also:
Constant Field Values

CSHARP_KEYWORDS

public static final java.lang.String CSHARP_KEYWORDS
See Also:
Constant Field Values

COFFEE_KEYWORDS

public static final java.lang.String COFFEE_KEYWORDS
See Also:
Constant Field Values

JSCRIPT_KEYWORDS

public static final java.lang.String JSCRIPT_KEYWORDS
See Also:
Constant Field Values

PERL_KEYWORDS

public static final java.lang.String PERL_KEYWORDS
See Also:
Constant Field Values

PYTHON_KEYWORDS

public static final java.lang.String PYTHON_KEYWORDS
See Also:
Constant Field Values

RUBY_KEYWORDS

public static final java.lang.String RUBY_KEYWORDS
See Also:
Constant Field Values

SH_KEYWORDS

public static final java.lang.String SH_KEYWORDS
See Also:
Constant Field Values

ALL_KEYWORDS

public static final java.lang.String ALL_KEYWORDS
See Also:
Constant Field Values

C_TYPES

public static final java.util.regex.Pattern C_TYPES

PR_STRING

public static final java.lang.String PR_STRING
token style for a string literal

See Also:
Constant Field Values

PR_KEYWORD

public static final java.lang.String PR_KEYWORD
token style for a keyword

See Also:
Constant Field Values

PR_COMMENT

public static final java.lang.String PR_COMMENT
token style for a comment

See Also:
Constant Field Values

PR_TYPE

public static final java.lang.String PR_TYPE
token style for a type

See Also:
Constant Field Values

PR_LITERAL

public static final java.lang.String PR_LITERAL
token style for a literal value. e.g. 1, null, true.

See Also:
Constant Field Values

PR_PUNCTUATION

public static final java.lang.String PR_PUNCTUATION
token style for a punctuation string.

See Also:
Constant Field Values

PR_PLAIN

public static final java.lang.String PR_PLAIN
token style for a plain text.

See Also:
Constant Field Values

PR_TAG

public static final java.lang.String PR_TAG
token style for an sgml tag.

See Also:
Constant Field Values

PR_DECLARATION

public static final java.lang.String PR_DECLARATION
token style for a markup declaration such as a DOCTYPE.

See Also:
Constant Field Values

PR_SOURCE

public static final java.lang.String PR_SOURCE
token style for embedded source.

See Also:
Constant Field Values

PR_ATTRIB_NAME

public static final java.lang.String PR_ATTRIB_NAME
token style for an sgml attribute name.

See Also:
Constant Field Values

PR_ATTRIB_VALUE

public static final java.lang.String PR_ATTRIB_VALUE
token style for an sgml attribute value.

See Also:
Constant Field Values

PR_NOCODE

public static final java.lang.String PR_NOCODE
A class that indicates a section of markup that is not code, e.g. to allow embedding of line numbers within code listings.

See Also:
Constant Field Values

langHandlerRegistry

protected java.util.Map<java.lang.String,java.lang.Object> langHandlerRegistry
Maps language-specific file extensions to handlers.

Constructor Detail

Prettify

public Prettify()
Method Detail

appendDecorations

protected static void appendDecorations(int basePos,
                                        java.lang.String sourceCode,
                                        Prettify.CreateSimpleLexer langHandler,
                                        java.util.List<java.lang.Object> out)
Apply the given language handler to sourceCode and add the resulting decorations to out.

Parameters:
basePos - the index of sourceCode within the chunk of source whose decorations are already present on out.

sourceDecorator

protected Prettify.CreateSimpleLexer sourceDecorator(java.util.Map<java.lang.String,java.lang.Object> options)
                                              throws java.lang.Exception
returns a function that produces a list of decorations from source text. This code treats ", ', and ` as string delimiters, and \ as a string escape. It does not recognize perl's qq() style strings. It has no special handling for double delimiter escapes as in basic, or the tripled delimiters used in python, but should work on those regardless although in those cases a single string literal may be broken up into multiple adjacent string literals. It recognizes C, C++, and shell style comments.

Parameters:
options - a set of optional parameters.
Returns:
a function that examines the source code in the input job and builds the decoration list.
Throws:
java.lang.Exception

registerLangHandler

protected void registerLangHandler(Prettify.CreateSimpleLexer handler,
                                   java.util.List<java.lang.String> fileExtensions)
                            throws java.lang.Exception
Register a language handler for the given file extensions.

Parameters:
handler - a function from source code to a list of decorations. Takes a single argument job which describes the state of the computation. The single parameter has the form { sourceCode: {string} as plain text. decorations: {Array.<number|string>} an array of style classes preceded by the position at which they start in job.sourceCode in order. The language handler should assigned this field. basePos: {int} the position of source in the larger source chunk. All positions in the output decorations array are relative to the larger source chunk. }
fileExtensions -
Throws:
java.lang.Exception

register

public void register(java.lang.Class<? extends Lang> clazz)
              throws java.lang.Exception
Register language handler. The clazz will not be instantiated

Parameters:
clazz - the class of the language
Throws:
java.lang.Exception - cannot instantiate the object using the class, or language handler with specified extension exist already

getFileExtensionsFromClass

protected java.util.List<java.lang.String> getFileExtensionsFromClass(java.lang.Class<? extends Lang> clazz)
                                                               throws java.lang.Exception
Throws:
java.lang.Exception

langHandlerForExtension

public Prettify.CreateSimpleLexer langHandlerForExtension(java.lang.String extension,
                                                          java.lang.String source)
Get the parser for the extension specified.

Parameters:
extension - the file extension, if null, default parser will be returned
source - the source code
Returns:
the parser