FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

In Command: Syntax Highlighting your code




In Command: Syntax Highlighting your code

When you publish your source-code online for others to read, you might want to use syntax highlighting. A very powerful tool to syntax highlight your programs is GNU Source-highlight.

Have a look at this program without syntax highlighting: source.java. After running Source-highlight on it, using the command below, it looks like this: source.java.html.

Code:

# source-highlight --src-lang java source.java



Installation

Linux:
  • Installing Source-highlight on Linux is simple. Just use your favorite package manager. For example:

    Code:
    apt-get install source-highlight

    Or download and install from http://www.gnu.org/software/src-highlite/
  • To make the php-directive work, open the file lang.map in the directory \usr\share\source-highlight. Change the line
    Code:

    php = php3.lang

    to
    Code:

    php = php.lang

    and save.

Windows:
  • Install the Src-Highlite package from http://gnuwin32.sourceforge.net. See The Windows Command Prompt for instructions.
  • To make the php-directive work, open the file lang.map in the directory \gnu\share\source-highlight (I assume you installed the package to \gnu). Change the line
    Code:

    php = php3.lang

    to
    Code:

    php = php.lang

    and save. This is to make it work for PHP (a little oddity in the Windows distribution).
  • To make source-highlight find its configuration files, you have to add the directory containing the config files to your path. See The Windows Command Prompt for instructions on how to add a directory to your path. The directory you have to add is \gnu\share\source-highlight, or if you installed the source-highlight package to \tools you have to add \tools\share\source-highlight.


Try it

Create a new directory somewhere, and save the file http://www.mrbluesky.frih.net/blog/srchili/common.txt as common.php in the new directory. Start the commandline, go to the directory you created and issue this command:

Code:
source-highlight --src-lang php common.php


Now open the file common.php.html in your browser. If everything was OK you will have a syntax highlighted version of common.php.

Processing multiple files

When you don't specify the source language, source-highlight uses the extension of the input files to decide which language they contain (.php for PHP, .java for Java, etc.).

You can also specify more than one file to process. You do this by either listing the files explicitly, or by using wildcards. If you want a syntax highlighted version of all html and PHP files in the current directory, you can issue this command:

Code:

source-highlight *.php *.html


Or if you want to process only some specific files, you can do this:

Code:

source-highlight index.html test.java hello.php


To see which languages are supported, issue this command:

Code:

source-highlight --lang-list


Additional options

There are additional options to tailor the output. To insert line numbers in the output file use -n. To specify a tab-length (the default is 8 characters) use --tab=n, where n is the length of all tabs.

To create a standalone HTML document with doctype declarations, <html> and <body>-tags, etc. use the --doc options. When you use the --doc option you can also specify a title for the page using the --title=sometext option.

You can also use your own customized headers and footers. For example, if you have created a file header.txt in the current directory:

Code:

<center><h1>My Source Code</h1></center>
<hr>
Created by John Doe
<hr>


you can use the --header option to include it in all your syntax highlighted code files:

Code:

source-highlight --title=Test --doc --header=header.txt *.php


The result will start like this:

Code:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight 2.1.2
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>Test</title>
</head>
<body>
<center><h1>My Source Code</h1></center>
<hr>
Created by John Doe
<hr>


You can also use an external style sheet you created, by using the --css option:

Code:

source-highlight --title=Test --doc --css=mystyle.css *


Further information:

For more information check the online manual at http://www.gnu.org/software/src-highlite/source-highlight.html.

Update: Windows users: Adding the directory with the configuration files to your path doesn't work. You have to specify the directory with each invocation of source-highlight. So, for example, instead of
Code:

source-highlight *.php *.html

you have to use:
Code:

source-highlight --data-dir=\gnu\share\sourcehighlight *.php *.html

To save some typing you can set an environment variable when you start the command prompt:
Code:

set shl=--data-dir=c:\gnu\share\source-highlight

After this you can invoke source-highlight with the the parameter %shl%:
Code:

source-highlight %shl% *.php *.html

You have to set the shl envorinment variable every time you start a new command prompt.



2 blog comments below

geshi is easy to use for the user and creator
mahirharoon on Mon Jan 07, 2008 3:34 pm
geshi is easy to use for the user and creator

Geshi is nice if you want to output formatted code from a PHP-script, but it is not very usefull for converting a large amount of files, especially if these files contain different languages. To use it, you will have to create a script which reads the files, converts them and output the formatted code to new files. This assumes you know PHP and have PHP installed, and then you still don't have complete, stand-alone HTML documents. But, of course, it is possible to use Geshi.
MrBlueSky on Mon Jan 07, 2008 11:46 pm



FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.