Ikiwiki code plugin

The code plugin for Ikiwiki enables syntax highlighting of source code fragments and whole files via GNU source-highlight. See the FortranWiki for actual usage examples.


This plugin is free software, licensed under the GNU GPL version 2 or later.


Add code to the add_plugins line in your ikiwiki.setup file and add the following lines (modifying as necessary):

# code plugin
code_command => "/usr/local/bin/source-highlight",
code_languages = {
    bib => bib,
    c => c,
    cc => cpp,
    patch => diff,
    f77 => fixed-fortran,
    f90 => fortran,
    tex => latex,
    pl => perl,
    py => python,
    rb => ruby,
    sh => shell,
    sql => sql,
    xml => xml,

If you do not specify code_command, it defaults to /usr/bin/source-highlight.

The hash code_languages maps file extensions to GNU source-highlight language names. This controls which file extensions are rendered as HTML for whole source file processing. Files with matching extensions will be processed and an HTML page will be created with syntax-highlighting with a download link at the top.

Consider the file hello.pl. Given the example code_languages hash above, this file will be processed as perl code. The URL to the resulting syntax-highlighted HTML page will be <BASE_URL>/hello.pl/ and the download link will point to the raw file at <BASE_URL>/hello.pl/hello.pl. The file extension is kept in the page name to prevent conflicts with other source code files such as hello.c.

Preprocessor directive

The code plugin provides the \[[!code]] preprocessor directive which has the following parameters:


[[!code language="fortran" content="""
module example
  implicit none
  subroutine test(a, b)
     real, intent(in) :: a
     integer, intent(out) :: b
     ! subroutine body
  end subroutine test
end module example


This plugin has only been tested with GNU source-highlight version 2.10. I believe the behavior of some command-line options has changed since previous versions so modifications might be required to use this plugin with other versions. Note also that the latest Debian package is version 2.4.


Source code fragments, generated by the preprocessor directive, are wrapped in <pre><tt> tags with <span> elements denoting keywords, etc. The code for whole file pages is also wrapped in a <div class="sourcecode">. The following stylesheet, taken from the source-highlight manual, lists some of the possible <span> classes:

div.sourcecode pre { }

.keyword { color: blue; font-weight: bold; }
.type, .classname { color: darkgreen; }
.string { color: red; font-family: monospace; }
.regexp { color: orange; }
.specialchar { color: pink; font-family: monospace; }
.comment { color: brown; font-style: italic; }
.number { color: purple; }
.preproc { color: darkblue; font-weight: bold; }
.symbol { color: darkred; }
.function { color: black; font-weight: bold; }
.cbracket { color: red; }
.todo { font-weight: bold; background-color: cyan; }

/* line numbers */
.linenum { color: black; font-family: monospace; }

/* Internet related */
.url { color: blue; text-decoration: underline; font-family: monospace; }

/* for Prolog, Perl */
.variable { color: darkgreen; }
.italics { color: darkgreen; font-style: italic; }
.bold { color: darkgreen; font-weight: bold; }

/* for LaTeX */
.underline { color: darkgreen; text-decoration: underline; }
.fixed { color: green; font-family: monospace; }
.argument, .optionalargument { color: darkgreen; }
.math { color: orange; }
.bibtex { color: blue; }

/* for diffs */
.oldfile { color: orange; }
.newfile { color: darkgreen; }
.difflines { color: blue; }

/* for css */
.selector { color: purple; }
.property { color: blue; }
.value { color: darkgreen; font-style: italic; }