OnWorks favicon

code2html - Online in the Cloud

Run code2html in OnWorks free hosting provider over Ubuntu Online, Fedora Online, Windows online emulator or MAC OS online emulator

This is the command code2html that can be run in the OnWorks free hosting provider using one of our multiple free online workstations such as Ubuntu Online, Fedora Online, Windows online emulator or MAC OS online emulator



code2html - Converts a program source code to HTML


(1) code2html [options] [input-file [output-file]]

(2) code2html -p [file [alternate-outfile]]

(3) code2html (as a CGI script; see the section on CGI)


code2html is a perl script which converts a program source code to syntax highlighted
HTML, or any other format for wich rules are defined.

Is the file which contains the program source code to be formatted. If not
specified or a minus (-) is given, the code will be read from STDIN.

Is the file to write the formatted code to. If not specified or a minus (-) is
given, the code will be written to STDOUT.

-l, --language-mode
Specify the set of regular expressions to use. These have to be defined in a
language file (see FILES below). To find out which language modes are defined,
issue a code2html --modes.

This input is treated case-insensitive.

If not given, some heuristics will be used to determine the file language.

-v, --verbose
Prints progress information to STDERR.

-n, --linenumbers
Print out the source code with line numbers.

-N, --linknumbers
Print out the source code with line numbers. The linenumbers will link to
themselves, which makes it easy to send links to lines.

-P, --prefix
Optional prefix to use for line number anchors.

-t, --replace-tabs[=TABSTOP-WIDTH]
Replace each occurence of a <TAB> character with the right amount of spaces to get
to the next tabstop. Default is a tabstop width of 8 characters.

-L, --language-file=LANGUAGE-FILE
Specify an alternate file to take the language and output-format definitions from
(see the section on FILES below).

-m, --modes
Print all language modes and output-formats currently defined to STDOUT and exit
succesfully. Also prints modes from a LANGUAGE-FILE given by --language-file if

If the language mode given with --language-mode cannot be found then use this

--fallback plain for instance is usefull when code2html is called from a script to
ensure output is created.

-h, --help
Print a short help and exit succesfully.

-V, --version
Print the program version and exit succesfully.

-c, --content-type
Prints ”Content-Type: text/html\n\n“ (or whatever the output-format defines as a
content-type) prior to the rest of the output. Usefull if the script is ivoked as
a cgi script.

-o, --output-format
Selects the output-format. html is the default. To find out which outputformats
are defined, issue a code2html --modes.

-H, --no-header
do not make use of the template defined by the output-format. For HTML this means
that there will be no <html>, <head>, and no <typical for patch and CGI modes,pre>

overrides the default template for the given output format. If --no-header is
given too, this has no meaning, since the template is ignored anyway.

-T, --title
Set the title of the produced output file. Only works if the template does
support setting the title.

-w, --linewidth=LINEWIDTH
Wrap lines after LINEWIDTH characters. Default is to not wrap lines at all.

-b, --linebreakprefix=LINEPREFIX
Use fILINEPREFIX at the start of wrapped lines. Default is "» ".

(2) HTML patching
code2html -p [file [alternate-outfile]]

code2html also allows you to have inline source code in an html file. It can then take
this html file and insert the syntax highlighted code.

If no file is given, code2html reads from STDIN and writes to STDOUT. If just one file is
given it replaces this file with the output. If two files are provided, the first one is
read from and the second one written to.

To use this feature, just insert a like like this into your html file:

<!-- code2html add [options] <file> -->

the syntax highlighted file will be inserted at this position enclosed in <pre> tags.

All options that can be given on the command line like --linenumbers etc. work. --help,
--version, etc. work too however it is not very intelligent to use them :). Using
--output-format to choose a non-HTML outputformat is not adviseable. --content-type is

You may also write the program's source code directly in the html file with the following

<!-- code2html add [options]

<your program source code here>


It is usually a good idea to at least give the --language-mode option to specify the

(3) CGI
If the the script is used as a CGI script (GATEWAY_INTERFACE environment set and no
command line arguments given) code2html reads the arguments either from the query string
or from SDTIN. (methods POST and GET).

--content-type is switched on automatically and the output always goes to STDOUT.

The following parameters/options are accepted:

language-mode - optional
`c', `cc', `pas', etc.

if not given, some heuristics are used to find out the language.

fallback - optional
`plain', `c', etc. if language-mode cannot be found, use this one

input-selector - optional
either `file', `cgi-input1', `cgi-input2', or `REDIRECT_URL'

default: file

file to read from if input-selector is `file'

The source code to syntax highlight. For example from a <textarea> or from a
upload. See input-selector.

The source code to syntax highlight. For example from a <textarea> or from a
upload. See input-selector.

line-numbers - optional
`yes', `no' or `link'

default: no

replace-tabs - optional
If 0 then tabs are not replaced, else replace each occurence of a <TAB> character
with the right amount of spaces to get to the next tabstop.

default: 0

title - optional
Set's the title of the file.

no-encoding - optional
By default code2html tries to encode the output as either bz2/gz/Z if the client
supports this (HTTP_ACCEPT_ENCODING) and the needed program is available on the
server. You may need to modify @CGI_ENCODING in the script to match your program

If no-encoding is defined as “true” code2html does not try to encode the output.

Why two cgi-inputs you may ask: This is to allow your users to choose vie a <form>
interface whether they want to insert their file into a <textarea> or user a <browse>
button to select their file. See the example on my home- page.

Note that if $FILES_DISALLOWED_IN_CGI is 0 it is possbile for your users to read all the
files the httpd can read (if you don't run a cgi- wrapper or something like this. By
default this value is set to 1, so file reading via cgi should not be allowed. You can
allow it with setting $FILES_DISALLOWED_IN_CGI to 0 at the top of the script.

The input selector REDIRECT_URL needs a special explaination. The file name is formed from
the two enviroment variables DOCUMENT_ROOT and REDIRECT_URL.

If you want apache to automatically call code2html for all program source code files you
may do this by adding these two lines to your srm.conf:

AddHandler text/x-sourcecode .c .cc .cpp .pas .h .p

Action text/x-sourcecode /cgi-bin/code2html?input-selector=REDIRECT_URL&foo=

or something similar to this. In the AddHandle line you can choose which extensions to
pass through code2html.

WARNING: Do not add .pl to this line and name this script “code2html.pl”. This will result
in a loop.

Also make sure that you load the Action module (srm.conf).

Replace /cgi-bin/code2html with the virtual location under which the file can be accessed.
Note the “foo=” part. Apache appends the URL of the file to display at the end of the
action part. We do not need this since we use the environment variable REDIRECT_URL
however we do not want to get the url addes to the input-selector string. Therefore we
append the “&foo=” part.

Tnx to Kevin Burton <[email protected]> for the idea. He also states that

> It is more powerfull if you use it in an Apache
> <Directory> tag
> <Directory /source>
> #with your Action tag here... this way you can
> #still have regular .java files on your server.
> </Directory>


assuming code2html is in the current directory, you may type

code2html -l perl code2html.pl code2html.html

to convert the script into a html file.

Use code2html online using onworks.net services

Free Servers & Workstations

Download Windows & Linux apps

  • 1
    Brackets is a free, modern open-source
    text editor made especially for Web
    Development. Written in HTML, CSS, and
    JavaScript with focused visual tools and
    Download Brackets
  • 2
    Free Pascal Compiler
    Free Pascal Compiler
    A 32/64/16-bit Pascal compiler for
    Win32/64/CE, Linux, Mac OS X/iOS,
    Android, FreeBSD, OS/2, Game Boy
    Advance, Nintendo NDS and DOS;
    semantically compatible wi...
    Download Free Pascal Compiler
  • 3
    Canon EOS DIGITAL Info
    Canon EOS DIGITAL Info
    Canon doesn�t have shutter count
    included on the EXIF information of an
    image file, as opposed to Nikon and
    Pentax. There�s no official Canon based
    application ...
    Download Canon EOS DIGITAL Info
  • 4
    rEFInd is a fork of the rEFIt boot
    manager. Like rEFIt, rEFInd can
    auto-detect your installed EFI boot
    loaders and it presents a pretty GUI
    menu of boot option...
    Download rEFInd
  • 5
    ExpressLuke GSI
    ExpressLuke GSI
    This SourceForge download page was to
    grant users to download my source built
    GSIs, based upon phhusson's great
    work. I build both Android Pie and
    Android 1...
    Download ExpressLuke GSI
  • 6
    Music Caster
    Music Caster
    Music Caster is a tray music player
    that lets you cast your local music to a
    Google Cast device. On the first run,
    you will need to click the arrow in your
    Download Music Caster
  • More »

Linux commands