BoxLang 🚀 A New JVM Dynamic Language Learn More...
This module allows you to do code formatting and syntax highlighting according to the pygments library and its gazillion of language lexers.
Just run a simple box install cbCodeHighlight
with
CommandBox CLI!
You can add the following settings to your
config/ColdBox.cfc
under the moduleSettings
struct:
moduleSettings = {
cbCodeHighlight = {
// The code theme to activate
// Themes: autumn,borland,bw,colorful,default,emacs,friendly,fruity,manny,monokai,murphy,native,pastie,perldoc,tango,trac,vim,vs
theme = "default",
// Default language
defaultLexer = "java",
// Listen to `preRender()` and convert any `<pre></pre>` tags to syntax highlighting
preRenderer = false
}
};
theme
: The css theme to usedefaultLexer
: If not syntax
attribute is
added to <pre></pre>
tags, then use this as
the default languagepreRender
: If true, it will activate the ColdBox
preRender
interceptor and will automatically process
all content and translate <pre></pre>
tags
with our awesome processor.This module comes with two model classes to help you with code formatting and highlighting. Below are the WireBox Id's you can use:
cbCodeHighlight@cbCodeHighlight
: Render out formatting
using a code block and options.preRenderer@cbCodeHighlight
: Process an HTML block and
replace all <pre></pre>
tags with the
appropriate syntax highlight.The cbCodeHighlight
model has a nice method to deal with
converting code blocks to pretty ones:
/**
* Take a string and format it as HTML
*
* @code The code block to format
* @syntax The syntax to apply, if not passed it defaults to the defaultLexer setting
* @fileName The filename + extension this code syntax represents
*/
function format(
required string code,
string syntax=variables.settings.defaultLexer,
string fileName = ''
)
Pass in a block of code
, choose a language
syntax
and an optional fileName
that will be
added to the output block.
This code formatter needs CSS to visualize the goodness. You must
make sure you add these css styles to your layout. You can call the
getCssAssets()
on the cbCodeHighlight
object
and it will give you a nice list of the css files to load.
function preHandler(){
addAsset( cbCodeHighlight.getCssAssets() );
}
That's it, this will load the css assets to the DOM and off you go.
We have provided an interceptor that listens to all ColdBox
preRender
interceptions so it can automatically process
the incoming HTML and make those <pre></pre>
shine! Just make sure the preRenderer
setting is true
.
You can also do a-la-carte transformations by accessing the
preRenderer@cbCodeHighlight
model and calling it's
process( content )
method. You can transform ANY
string content by inspecting it for
<pre></pre>
tags and replacing them inline.
var content = contentService.getContent( rc.id );
return getInstance( "preRenderer@cbCodeHighlight" ).process( content.render() );
The <pre>
tags also can have two more atrributes :
syntax
: A valid pygments syntax definition: http://pygments.org/languages/fileName
: A filename that the pre code represents.
This will output a nice filename information box.<pre syntax="javascript" fileName="component.js">
<pre syntax="sql" fileName="export.sql">
<pre syntax="coldfusion" fileName="myFile.cfm">
Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp www.ortussolutions.com
Because of His grace, this project exists. If you don't like this, then don't read it, its not for you.
"Therefore being justified by faith, we have peace with God through our Lord Jesus Christ: By whom also we have access by faith into this grace wherein we stand, and rejoice in hope of the glory of God. And not only so, but we glory in tribulations also: knowing that tribulation worketh patience; And patience, experience; and experience, hope: And hope maketh not ashamed; because the love of God is shed abroad in our hearts by the Holy Ghost which is given unto us. ." Romans 5:5
"I am the way, and the truth, and the life; no one comes to the Father, but by me (JESUS)" Jn 14:1-12
$
box install cbCodeHighlight