Over a million developers have joined DZone.

Awesome Asciidoctor: Source Syntax Highlighting With Prism

DZone's Guide to

Awesome Asciidoctor: Source Syntax Highlighting With Prism

Prism is a JavaScript library which can be used for syntax highlighting.

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

Asciidoctor has built-in support for a couple of source syntax highlighting libraries like Coderay, Pygments, highlight.js and prettify. In this post we learn how to use the JavaScript library Prism to do the syntax highlighting for our source blocks. Because Prism is a JavaScript library we must remember this only works for the HTML backend of Asciidoctor.

Image title

In the following markup we have two source code listings in Java and Groovy:

= Source highlights with Prism
// Set default language for source code listings to java.
:source-language: java
// Include docinfo.html for Prism CSS file and
// include docinfo-footer.html for Prism Javascript.

== Creating an application

To create a simple Ratpack application we write
the following code:

.Simple Java Ratpack application
package com.mrhaki;

import ratpack.server.RatpackServer;

public class Main {
    public static void main(String... args) throws Exception {
        RatpackServer.start(server ->
                .handlers(chain ->
                        .get(ctx -> ctx.render("Hello World!"))));

.Simple Groovy Ratpack application
package com.mrhaki

import static ratpack.groovy.Groovy.ratpack

ratpack {
    handlers {
        get {
            render "Hello World!"

Each source listing is transformed to HTML with the following structure:

<pre class="highlight"><code class="language-{language}" data-lang="{language}">

This fits perfectly with Prism. Prism expects this format to apply the syntax highlighting. So we only have to add the Prism Javascript and CSS files to the generated HTML file. We download the Prism Javascript and CSS file from the Prism download site. We save the Javascript file as prism.js and the CSS file as prism.css. Next, we create a docinfo.html to include a reference to the prism.css file:

<link href="prism.css" rel="stylesheet" />
/* Override Asciidoctor CSS to get the correct background */
.listingblock pre[class^="highlight "] {
    background: #272822;

We also create the file docinfo-footer.html to reference prism.js:

<script src="prism.js"></script> 

In our markup we have the document attribute docinfo1 set. This means the files docinfo.html and docinfo-footer.html are included in the generated HTML output. Let's see the result in a web browser:

Written with Asciidoctor 1.5.4.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

prism ,javascript ,syntax ,web dev

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}