Prettify
Looking for WET v3.1?
As of September 23, 2014, version 3.1 of the Web Experience Toolkit is no longer supported. The source code and documentation have been moved to the wet-boew-legacy repository.
Syntax highlighting of source code snippets in an html page using google-code-prettify.
How to use
- Apply
class="wb-prettify"
to an element on the page to load the component (once per page). - Apply
class="prettyprint"
to apre
orcode
element to apply syntax highlighting. Alternatively useclass="wb-prettify all-pre"
to apply syntax highlighting to allpre
elements on the page. - Apply
class="linenums"
to apre
orcode
element to add line numbers. Alternatively useclass="wb-prettify linenums"
to apply line numbers to all applicablepre
elements on the page. Specify the starting number by addinglinenums:#
beforelinenums
. - Add extra language support by using
class="wb-prettify lang-*"
(e.g.,class="wb-prettify lang-css"
) and applyingclass="lang-*"
to each applicablepre
orcode
element. Supported formats are below:- lang-apollo
- lang-clj
- lang-css
- lang-dart
- lang-go
- lang-hs
- lang-lisp
- lang-lua
- lang-ml
- lang-n
- lang-proto
- lang-scala
- lang-sql
- lang-tex
- lang-vb
- lang-vhdl
- lang-wiki
- lang-xq
- lang-yaml
Examples
HTML
<form action="#" method="post">
<div>
<label for="data1">Form input default appearance:</label>
<input name="data1" type="text" id="data1" />
</div>
<div>
<label for="data2">Form text area default appearance:</label>
<textarea name="data2" cols="15" rows="3" id="data2"></textarea>
</div>
<div>
<label for="data4">Form <code>select</code> default appearance:</label>
<select id="data4" name="data4">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>
</div>
<fieldset>
<legend>Form <code>legend</code>, <code>fieldset</code> and <code>checkbox</code> default appearance</legend>
<div>
<input name="checkbox" type="checkbox" id="data5" value="checkbox" /> <label for="data5">Option 1</label>
<input name="checkbox" type="checkbox" id="data6" value="checkbox" /> <label for="data6">Option 2</label>
<input name="checkbox" type="checkbox" id="data7" value="checkbox" /> <label for="data7">Option 3</label>
<input name="checkbox" type="checkbox" id="data8" value="checkbox" /> <label for="data8">Option 4</label>
</div>
</fieldset>
<div>
<input name="submit" type="submit" id="submit" value="Submit default appearance" />
<input name="reset" type="reset" id="reset" value="Reset default appearance" />
</div>
</form>
CSS (uses linenums)
#gcwu-date-mod dt,#gcwu-date-mod dd{
float:left;
}
#gcwu-date-mod dd{
margin:0 0 0 10px;
}
#gcwu-content h2,#gcwu-priorities h3{
color:#333;
font-weight:400;
}
#gcwu-content h2,#gcwu-priorities h3{
border:none;
margin-bottom:5px;
font-size:1.6em;
}
div#gcwu-headlines ul li,div#gcwu-headlines ul{
list-style:none !important;
margin:0 !important;
padding:0 !important;
}
JavaScript (uses linenums:50 and linenums)
/**
* Initialize the plugin. This only runs once on the DOM.
* @function init
*/
init = function() {
var cssClass, cssClasses, i, len, $pre,
$elm = $( this ),
modeJS = wb.getMode() + ".min.js",
deps = [ "site!deps/prettify" + modeJS ],
settings = $.extend( {}, defaults, $elm.data() );
wb.remove( selector );
// Check the element for `lang-*` syntax CSS classes
cssClasses = typeof $elm[0].className === "string" ? $elm[0].className.split( " " ) : [];
for ( i = 0, len = cssClasses.length; i < len; i++ ) {
cssClass = cssClasses[ i ];
if ( cssClass.indexOf( "lang-" ) === 0 ) {
deps.push( "site!deps/" + cssClass + modeJS );
}
}
// CSS class overides of settings
settings.allpre = settings.allpre || $elm.hasClass( "all-pre" );
settings.linenums = settings.linenums || $elm.hasClass( "linenums" );
// Apply global settings
if ( settings.allpre || settings.linenums ) {
$pre = $( "pre" );
if ( settings.allpre ) {
$pre.addClass( "prettyprint" );
}
if ( settings.linenums ) {
$pre.filter( ".prettyprint" ).addClass( "linenums" );
}
}
// Load the required dependencies and prettify the code once finished
Modernizr.load({
load: deps,
complete: function() {
wb.doc.trigger( "prettyprint.wb-prettify" );
}
});
}
- Date modified: