Code syntax highlighting examples
On this website, we can enter code snippets directly on the page have the code highlighted and laid out accoriding to how you enter it. We use the Generic Syntax Highlighter (GeSHi) package:http://qbnz.com/highlighter/index.php, a common, open source package.
More information on the Wikimedia extension at http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi
To markup your code, simply including your code in angle brackets with the term source, and the language being used with the term lang. Below are some examples.
<source lang="javascript" lines=0> ...code here... </source>
Contents |
Javascript
<source lang="javascript" lines=0> function reload() {
tmp = findSWF("chart");
x = tmp.reload("gallery-data-26.php");
}
function findSWF(movieName) {
if (navigator.appName.indexOf("Microsoft")!= -1) {
return window["ie_" + movieName];
} else {
return document[movieName];
}
} </source>
HTML
<source lang="html4strict" highlight="5">
| Heading 1 | Heading 2 |
|---|---|
| This is cell 1 text | This is cell 2 text |
</source>
PHP
<source lang="php" lines=1> <?php function account_menu($may_cache) {
global $user;
$items = array();
if (!$may_cache && arg(0) == 'user' && is_numeric(arg(1))) {
$items[] = array(
'path' => 'user/'. arg(1) .'/profile',
'title' => t('Profile'),
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'user/'. arg(1) .'/profile/show',
'title' => t('Show profile'),
'type' => MENU_DEFAULT_LOCAL_TASK,
); ?> </source>
Perl
<source lang="perl" lines=0 highlight="6">
- !C:/Perl/perl.bin
- printenv -- demo CGI program which just prints its environment
print "Content-type: text/plain; charset=iso-8859-1\n\n"; foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s|\n|\\n|g;
$val =~ s|"|\\"|g;
print "${var}=\"${val}\"\n";
} </source>
CSS
<source lang="css" lines=1 highlight="5"> /**************/ /* TYPOGRAPHY */ /**************/
body {
background: #FFF; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; /* web safe fonts */ font-size: 75%; /* 12px */ font-weight: normal; line-height: 160%; /* 19.2px */
}
h1 {
font-size: 2.5em; font-weight: normal;
}
h2 {
font-size: 1.75em; font-weight: normal;
}
</source>