public class Prettify extends Object
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:
<script type="text/javascript" src="/path/to/prettify.js"></script>
<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.
<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")
| Modifier and Type | Class and Description |
|---|---|
class |
Prettify.CreateSimpleLexer |
| Modifier and Type | Field and Description |
|---|---|
static String |
ALL_KEYWORDS |
static String |
C_KEYWORDS |
static Pattern |
C_TYPES |
static String |
COFFEE_KEYWORDS |
static String |
COMMON_KEYWORDS |
static String |
CPP_KEYWORDS |
static String |
CSHARP_KEYWORDS |
static String |
FLOW_CONTROL_KEYWORDS |
static String |
JAVA_KEYWORDS |
static String |
JSCRIPT_KEYWORDS |
protected Map<String,Object> |
langHandlerRegistry
Maps language-specific file extensions to handlers.
|
static String |
PERL_KEYWORDS |
static String |
PR_ATTRIB_NAME
token style for an sgml attribute name.
|
static String |
PR_ATTRIB_VALUE
token style for an sgml attribute value.
|
static String |
PR_COMMENT
token style for a comment
|
static String |
PR_DECLARATION
token style for a markup declaration such as a DOCTYPE.
|
static String |
PR_KEYWORD
token style for a keyword
|
static String |
PR_LITERAL
token style for a literal value.
|
static String |
PR_NOCODE
A class that indicates a section of markup that is not code, e.g.
|
static String |
PR_PLAIN
token style for a plain text.
|
static String |
PR_PUNCTUATION
token style for a punctuation string.
|
static String |
PR_SOURCE
token style for embedded source.
|
static String |
PR_STRING
token style for a string literal
|
static String |
PR_TAG
token style for an sgml tag.
|
static String |
PR_TYPE
token style for a type
|
static String |
PYTHON_KEYWORDS |
static String |
RUBY_KEYWORDS |
static String |
RUST_KEYWORDS |
static String |
SH_KEYWORDS |
| Constructor and Description |
|---|
Prettify() |
| Modifier and Type | Method and Description |
|---|---|
protected static void |
appendDecorations(int basePos,
String sourceCode,
Prettify.CreateSimpleLexer langHandler,
List<Object> out)
Apply the given language handler to sourceCode and add the resulting
decorations to out.
|
protected List<String> |
getFileExtensionsFromClass(Class<? extends Lang> clazz) |
Prettify.CreateSimpleLexer |
langHandlerForExtension(String extension,
String source)
Get the parser for the extension specified.
|
void |
register(Class<? extends Lang> clazz)
Register language handler.
|
protected void |
registerLangHandler(Prettify.CreateSimpleLexer handler,
List<String> fileExtensions)
Register a language handler for the given file extensions.
|
protected Prettify.CreateSimpleLexer |
sourceDecorator(Map<String,Object> options)
returns a function that produces a list of decorations from source text.
|
public static final String FLOW_CONTROL_KEYWORDS
public static final String C_KEYWORDS
public static final String COMMON_KEYWORDS
public static final String CPP_KEYWORDS
public static final String JAVA_KEYWORDS
public static final String RUST_KEYWORDS
public static final String CSHARP_KEYWORDS
public static final String COFFEE_KEYWORDS
public static final String JSCRIPT_KEYWORDS
public static final String PERL_KEYWORDS
public static final String PYTHON_KEYWORDS
public static final String RUBY_KEYWORDS
public static final String SH_KEYWORDS
public static final String ALL_KEYWORDS
public static final Pattern C_TYPES
public static final String PR_STRING
public static final String PR_KEYWORD
public static final String PR_COMMENT
public static final String PR_TYPE
public static final String PR_LITERAL
public static final String PR_PUNCTUATION
public static final String PR_PLAIN
public static final String PR_TAG
public static final String PR_DECLARATION
public static final String PR_SOURCE
public static final String PR_ATTRIB_NAME
public static final String PR_ATTRIB_VALUE
public static final String PR_NOCODE
protected static void appendDecorations(int basePos,
String sourceCode,
Prettify.CreateSimpleLexer langHandler,
List<Object> out)
basePos - the index of sourceCode within the chunk of source
whose decorations are already present on out.protected Prettify.CreateSimpleLexer sourceDecorator(Map<String,Object> options) throws Exception
options - a set of optional parameters.Exceptionprotected void registerLangHandler(Prettify.CreateSimpleLexer handler, List<String> fileExtensions) throws Exception
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 - Exceptionpublic void register(Class<? extends Lang> clazz) throws Exception
clazz - the class of the languageException - cannot instantiate the object using the class,
or language handler with specified extension exist alreadyprotected List<String> getFileExtensionsFromClass(Class<? extends Lang> clazz) throws Exception
Exceptionpublic Prettify.CreateSimpleLexer langHandlerForExtension(String extension, String source)
extension - the file extension, if null, default parser will be returnedsource - the source codeCopyright © 2015. All rights reserved.