GNU Source-highlight Examples - GNU Project - Free Software Foundation (FSF)
Author: Lorenzo Bettini

# GNU Source-highlight Examples

by Lorenzo Bettini

## Usage and examples

source-highlight only does a lexical analysis of the source code, so the program source is assumed to be correct!
Finally, there is texinfo documentation. It is also available as an html file, and it is also available on-line.

Here we only show some examples of generated files.

Here are some links to some of the C and C++ sources of source-highlight colored with source-highlight itself:

> source-highlight.cc.html

generated with the following command

>     source-highlight -s cpp -f html --doc source-highlight.cc

And obviously it works with header file as well:

> cmdline.h.html

created with the command:

>     source-highlight -s cpp -f html *.h --css="mono.css"

Here are some examples of Java files processed with source-highlight (in most of them we have used the Hello.java to make some test). Here's how Hello1.html, Hello2.html, Hello3.html, Hello4.html, Hello5.html, Hello_h_f.html, Hello_lines.html, Hello_xhtml.html, Hello_xhtml2.html, Hello_notfixed.html were created:

    source-highlight -s java -f html -i Hello.java -o Hello1.html
    source-highlight -s java -f html --input Hello.java --output Hello2.html --doc
    source-highlight -s java -f html -i Hello.java -o Hello3.html --title "Happy Java with java2html :-)" --tab 3
    source-highlight -s java -f html < Hello.java > Hello4.html --title "and what about CSS :-)" --css "Hello.css"
    source-highlight -s java -f html < Hello.java > Hello5.html --title "Wooo... this is quite dark ;-D" --css "mono-alt.css"
    source-highlight -s java -f html --doc *.java
    Processed Hello.java   (creates Hello.java.html)
    source-highlight -s java -f html --doc -i Hello.java -o Hello_h_f.html --style-file="default.style" --header="header.html" --footer="footer.html"
    source-highlight -s java -f html --line-number --doc -i Hello.java -o Hello_lines.html
    source-highlight -s java -f xhtml --doc -i Hello.java -o Hello_xhtml.html --css xhtml.css
    source-highlight -s java -f xhtml --doc -i Hello.java -o Hello_xhtml2.html --style-file default.style
    source-highlight -s java --outlang-def=html_notfixed.outlang -i Hello.java -o Hello_notfixed.html --style-file default.style

Here's another example:

> Hello_h_f.html

Created with the command (notice that this one uses both CSS file and headers and footers):

>     source-highlight -s java -f html -i Hello.java -o Hello_h_f.html
>                 --css="cpp2html.css"
>                 --header="./header.html" --footer="./footer.html"

Source-highlight can also generate HTML code to be embedded (i.e., copied and pasted) inside a Javadoc comment (or another kind of comment framework such as Doxygen). This way, when you run javadoc on such a file, the resulting generated html documentation files will have the code snippets highlighted. For instance, consider this piece of code, UsageExample.java, representing an example of usage of the class SimpleClass; we would like to embed this code (formatted) into a javadoc comment of SimpleClass, so we process it with the option -f javadoc and we copy the result into the comment, as illustrated in SimpleClass.java. When we process this file with javadoc, we obtain that code formatted in the documentation html file: SimpleClass-doc.html. Doesn't it look nice :-)
You can also specify several line ranges, so that you produce an output only with those specific lines of your input file; you can specify a number of lines for the sorrounding context, and the separator of ranges; for instance, this output was produced, starting from this input, with the following command (note how the context is shown in gray, as specified in the style file):

>     source-highlight -s java -f html
>                 --line-range="12-18","29-34" --line-number --range-context=3 --range-separator="<...>"
>                 -i test.java -o test_linerange_sep.html

Range of output can also be specified using regular expressions: the beginning of the range will be detected by a line containing (in any point) a string matching the specified regular expression; the end will be detected by a line containing a string matching the same regular expression that started the range. This feature is very useful when we want to document some code (e.g., in this very manual) by showing only specific parts, that are delimited in a ad-hoc way in the source code (e.g., with specific comment patterns). for instance, this output was produced, starting from this input, with the following command

>     ssource-highlight -s java -f html
>                 --regex-range="/\* [[:alpha:]]+ \*/" --line-number
>                 -i test.java -o test_regexrange4.html

Here's the output of source-highlight applied to a Prolog program (through the option ``source-highlight -s prolog``: test.pl.html. This one is an highlighted Perl program (through the option ``source-highlight -s prolog``): test.pm.html. Here's an highlighted PHP3 program: test.php3.html. And here's a Python program: test.py.html. This the flex scanner for flex files flex_scanner.ll.html. This is a Ruby program: test.rb.html. This is a Javascript program test.js.html. This is a Lua test program test.lua.html. This is a Caml test program test.ml.html and this is an Sml test program test.sml.html.
Here are some log files formatted using the color file syslog.style:

- test.log.html,
- access.log.html,
- auth.log.html,
- error.log.html,
- syslog.log.html.

This is the language definition file for C/C++: cpp.lang.html, the one for the log files above, log.lang.html, and the language definition file for the language definition syntax itself: langdef.lang.html.

Here's the html output of test.java.html formatted in html itself: test.htm.html.
and here's the output of test.java.tex formatted in html: test.java.tex.html

These HTML outputs contain cross references, test_refs.h.html, test_refs.cpp.html, generated with the following command:

>     source-highlight -s cpp -f html --title="Contains references to tags"
>      --gen-references=inline test_refs.h test_refs.cpp

## Style files

Since version 2.6, source-highlight can use CSS style files (limited support) as style files for coloring and formatting the output. Here you can find the output using all these files shipped with source-highlight (these were adapted from those provided by SHJS):

Style examples (huge file).