Syntax::Highlight::Shell - Highlight shell scripts
Version 0.04
use Syntax::Highlight::Shell; my $highlighter = new Syntax::Highlight::Shell; $output = $highlighter->parse($script);
If $script contains the following shell code:
$script
# an if statement if [ -f /etc/passwd ]; then grep $USER /etc/passwd | awk -F: '{print $5}' /etc/passwd fi
then the resulting HTML contained in $output will render like this:
$output
This module is designed to take shell scripts and highlight them in HTML with meaningful colours using CSS. The resulting HTML output is ready for inclusion in a web page. Note that no reformating is done, all spaces are preserved.
The constructor. Returns a Syntax::Highlight::Shell object, which derives from Shell::Parser.
Syntax::Highlight::Shell
Shell::Parser
Options
nnn - Activate line numbering. Default value: 0 (disabled).
nnn
pre - Surround result by <pre>...</pre> tags. Default value: 1 (enabled).
pre
<pre>...</pre>
syntax - Selects the shell syntax. Check the documentation about the syntax() method in Shell::Parser documentation for more information on the available syntaxes. Default value: bourne.
syntax
syntax()
bourne
tabs - When given a non-nul value, converts tabulations to this number of spaces. Default value: 4.
tabs
Example
To avoid surrounding the result by the <pre>...</pre> tags:
my $highlighter = Syntax::Highlight::Shell->new(pre => 0);
Parse the shell code given in argument and returns the corresponding HTML code, ready for inclusion in a web page.
Examples
$html = $highlighter->parse(q{ echo "hello world" }); $html = $highlighter->parse(<<'END'); # find my name if [ -f /etc/passwd ]; then grep $USER /etc/passwd | awk -F: '{print $5}' /etc/passwd fi END
The following methods are for internal use only.
Shell::Parser callback that does all the work of highlighting the code.
The resulting HTML uses CSS to colourize the syntax. Here are the classes that you can define in your stylesheet.
.s-key - for shell keywords, like if, for, while, do...
.s-key
if
for
while
do
.s-blt - for the builtins commands
.s-blt
.s-cmd - for the external commands
.s-cmd
.s-arg - for the command arguments
.s-arg
.s-mta - for shell metacharacters, like |, >, \, &
.s-mta
|
>
\
&
.s-quo - for the single (') and double (") quotes
.s-quo
'
"
.s-var - for expanded variables: $VARIABLE
.s-var
$VARIABLE
.s-avr - for assigned variables: VARIABLE=value
.s-avr
VARIABLE=value
.s-val - for shell values (inside quotes)
.s-val
.s-cmt - for shell comments
.s-cmt
An example stylesheet can be found in examples/shell-syntax.css.
Here is an example of generated HTML output. It was generated with the script eg/highlight.pl.
The following shell script
#!/bin/sh user="$1" case "$user" in # check if the user is root 'root') echo "You are the BOFH." ;; # for normal users, grep throught /etc/passwd *) passwd=/etc/passwd if [ -f $passwd ]; then grep "$user" $passwd | awk -F: '{print $5}' else echo "No $passwd" fi esac
will be rendered like this (using the CSS stylesheet eg/shell-syntax.css):
1 #!/bin/sh 2 3 user="$1" 4 5 case "$user" in 6 # check if the user is root 7 'root') 8 echo "You are the BOFH." 9 ;; 10 11 # for normal users, grep throught /etc/passwd 12 *) 13 passwd=/etc/passwd 14 if [ -f $passwd ]; then 15 grep "$user" $passwd | awk -F: '{print $5}' 16 else 17 echo "No $passwd" 18 fi 19 esac
Syntax::Highlight::Shell relies on Shell::Parser for parsing the shell code and therefore suffers from the same limitations.
Sébastien Aperghis-Tramoni, <sebastien@aperghis.net>
Please report any bugs or feature requests to bug-syntax-highlight-shell@rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Syntax-Highlight-Shell. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-syntax-highlight-shell@rt.cpan.org
Copyright 2004 Sébastien Aperghis-Tramoni, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Syntax::Highlight::Shell, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Syntax::Highlight::Shell
CPAN shell
perl -MCPAN -e shell install Syntax::Highlight::Shell
For more information on module installation, please visit the detailed CPAN module installation guide.