# code2html.pl
#!/usr/bin/perl
######################################################################
# #
# Code2HTML #
# --------- #
# #
# Code2Html, version 0.6.5, Jun 1999, ppalfrad@cosy.sbg.ac.at #
# #
# AUTHOR #
# Peter Palfrader, computer science student at the #
# University of Salzburg/Austria. Written in 1999. #
# #
# DESCRIPTION #
# code2html.pl is a perlscript which converts a program #
# source code to syntax highlighted HTML by applying a set #
# of regular expressions depending on the language #
# the source code is written. #
# #
# LICENSE #
# Copyright (c) 1999 by Peter Palfrader. #
# Not derived from licensed software. #
# #
# Permission is granted to anyone to use this software for #
# any purpose on any computer system, and to redistribute it #
# in any way, subject to the following restrictions: #
# #
# 1. The author is not responsible for the consequences of #
# use of this software, no matter how awful, even if they #
# arise from defects in it. #
# 2. The origin of this software must not be misrepresented, #
# either by explicit claim or by omission. #
# 3. Altered versions must be plainly marked as such, and #
# must not be misrepresented (by explicit claim or #
# omission) as being the original software. #
# 3a. It would be nice if I got a copy of your improved #
# version sent to ppalfrad@cosy.sbg.ac.at. However this #
# is not obligatory. #
# 4. This notice must not be removed or altered. #
# #
# Thanks to Henry Spencer for this wonderful license :). #
# #
# #
# The regular expressions found in the default database are #
# are not subject to this license. They have been taken #
# from NEdit and were then slightly modified by me. #
# #
# According to Mark Edel who does the support #
# of NEdit they are public domain and so you may do with #
# them whatever you want. #
# #
######################################################################
# #
# HISTORY #
# #
# 0.6.5 - June 1999 #
# * tweaking Perl regular expressions (TNX to rbs@telerama.com) #
# * if a language mode cannot be found the error message now tells #
# you which lang you requested. #
# * new --fallback option. This language mode is used if the #
# given mode by parameter #1 is not available. This feature was #
# requested by rbs@telerama.com #
# * changed 'is called as CGI' heuristics. #
# #
# 0.6.4 - May 1999 #
# * the name attribute in the line numbers was wrong. there should #
# be no # in it. TNX to stifle33@hotmail.com #
# * linking to line numbers now works from the command line too #
# #
# 0.6.3 - May 1999 #
# * fixed C character constant regex. TNX to jesse@lmscadsi.com #
# * line numbers now can link to themselves #
# * line numbers now have a constant width #
# #
# 0.6.2 - May 1999 #
# * fixed something in HTML lang #
# #
# 0.6.1 - May 1999 #
# * added 'plain' language mode #
# * cgi support improoved. should now also handle #
# enctype="multipart/form-data" forms #
# #
# 0.6.0 - May 1999 #
# * fixed Substitution loop at ./code2html.pl (5.2) line 627 #
# * changed string regular expressions: \" is no longer a problem #
# * default locations for the language files have changed: #
# first all the files listed in the enviroment variable #
# CODE2HTML_CONFIG (seperated by colons (:)) will be checked, #
# then, if HOME set, $HOME/.code2html.config and finally #
# /etc/code2html.config #
# * new: --no_header does not print the ... stuff #
# * new: --content_type prints Content-Type: text/html; #
# * C/C++: in preprocessor lines: strings are highlighted now #
# * -n now also names the lines with .. #
# * script may now be run as a CGI script. see the CGI section #
# in the help #
# * get_config_file was rewritten because it was _ugly_ #
# #
# 0.5.2 - May 1999 #
# * fixed Makefile dependency line bug. (a * in regexp instead a +) #
# #
# 0.5.1 - May 1999 #
# * clarified copyright questions on regular expressions #
# * changed -dumb_default_lang to -dump_default_lang :) #
# #
# 0.5.0 - Apr 1999 #
# * changed find_all_matches__create_taglist__insert_placeholders #
# so that it works around a bug in earlier perl versions, in which #
# pos() is not set if the return value of m//g is not used. #
# * changed a regex in get_config_file #
# #
# 0.4.1 - Mar 1999 #
# * changed JavaScript regexps slightly #
# * changed find_all_matches_.. slightly #
# #
# 0.4.0 - Mar 1999 #
# * script no longer needs $' and $` thus beeing faster. $& is still #
# needed #
# * regexps for perl changed slightly #
# * find_all_matches has been completly rewritten. verify is no #
# longer needed. taglist is built while searching. #
# May take a bit longer now, but otherwhise a bug could not have #
# been fixed. #
# * changed (fixed?) start of language_def_block pos evaluation #
# #
# 0.3.3 - Feb 1999 #
# * fixed STDIN/STDOUT parameters when passed a - the scrpit though #
# options would follow and didn't tread this as a handle for STDIN #
# * added -V as shortcut to --version #
# #
# 0.3.2 - Feb 1999 #
# * removed -T from 1st line so activePerl will run the script #
# without any modifications #
# * changed perl string regexps. run now faster and better #
# (and run at all under win) #
# * added --replace_tabs #
# #
# 0.3.1 - Feb 1999 #
# * fixed language files bug (lang_mode no longer #
# starts langmode) #
# * fixed empty html tag bug when reading lang specs #
# * added languages: Makefile, Java, JavaScript, Perl #
# * added --modes #
# #
# 0.3.0 - Feb 1999 #
# * added linenumbers #
# * added and tag in html output #
# * fixed crlf switch works now #
# #
# 0.2.0 - Feb 1999 #
# * first official release #
# #
######################################################################
use strict;
#$\ = "\n";
$/ = "\n";
my $FILES_DISALLOWED_IN_CGI = 0;
# you may set this to true to disallow file reading from your hd in
# cgi mode. This may be good if your httpd runs as 'root' (yes, I've
# seen this!) and so any user could with some knowledge easily read
# your /etc/shadow for example!
&main;
sub main {
my $vernr = "0.6.4";
my $monthshort = "May";
my $monthlong = "May";
my $pure_version_message = "\nCode2Html, version $vernr, $monthshort 1999, ppalfrad\@cosy.sbg.ac.at\n\n";
my $version_message = "\n$pure_version_message\n\n";
my $help_message = "
$pure_version_message
NAME
code2html.pl - Converts a program source code to HTML
SYNOPSIS
code2html.pl [options] language_mode [input_file
[output_file]]
code2html.pl (as a CGI script: see CGI section below)
DESCRIPTION
code2html.pl is a perlscript which converts a program
source code to syntax highlighted HTML by applying a set
of regular expressions depending on the language
the source code is written.
EXAMPLE USAGE
assuming code2html.pl is in the current directory, you may
type
code2html.pl perl code2html.pl code2html.html
to convert the script into a html file.
OPTIONS
language_mode
Specify the set of regular expressions to use.
These have to be defined in a langugage file
(see FILES below). To find out which language modes
are defined, issue a code2html.pl --modes.
This input is treated case-insensitive.
input_file
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.
output_file
Is the file ro write the formatted code to. If not
specified or a minus (-) is given, the code will be
written to STDOUT.
-v, --verbose
Prints progress information to STDERR. You will not
need this.
-r, --crlf
Usually the program output is print with a newline
(\\n) only as a line delimiter. However if you want
to use carrige return/line feed (\\r\\n or crlf) as a
line delimiter, so be it.
-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.
-t, --replace_tabs [SPACE_COUNT]
Replace each occurence of a character with
spaces. The default number of spaces is 4 but this
can be changed by an optional argument.
-l, --language_file
Specify an alternate languages file to take the
regular expressions from (see FILES below).
-m, --modes
Print all defined modes currently defined to STDOUT
and exit succesfully. Also prints modes from a
given language_file if applicable.
--fallback
If the language mode givin with the first parameter
(language_mode) cannot be found then use this mode.
Usefull when code2html is called from a script to
ensure output is created: --fallback plain
--dump_default_lang
Write default language file to STDOUT and exit
succesfully.
-h, -? --help
Print this text and exit succesfully.
-V --version
Print the program version and exit succesfully.
-c --content_type
Prints \"Content-Type: text/html\\n\\n\" prior to the
html file. Usefull if the script is ivoked as a
cgi script.
-H --no_header
do not print the , , stuff. Use-
full if this script is only a part in a bigger
program.
CGI
CGI is in early beta!
If the the script is used as a CGI script
(GATEWAY_INTERFACE envoriment set) code2html ignores all
command line parameters and reads them either from the
query string or from stdin. (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 - required
'c', 'cc', 'pas', etc.
fallback - optional
'plain', 'c', etc.
if language_mode cannot be found, use this one
input_selector - optional
either 'file', 'cgi_input1', or 'cgi_input2'
default: file
filename
file to read from if input_selector is 'file'
cgi_input1
the source code to syntax highlight
see input_selector
cgi_input2
the source code to syntax highlight
see input_selector
line_numbers - optional
'yes', 'no' or 'link'
default: no
replace_tabs - optional
if -1 then tabs are not replaced, if larger then each
tab is replaced by that many spaces.
default: -1
Why two cgi_inputs you may ask: This is to allow your
users to choose vie a