<?php
////////////////////////////////////////////////////////////////
//
// src.php - display HTMLized source code listing or an index
//
// This script wraps the functionality of the source code
// highighting modele syntax_hilight.php written by Scott
// Yast.
//
// The name of a source code file is passed in as a query
// value (f=filename). The file must have an extension
// that matches the regex givin in $pattern at the top of
// the program (below).
//
// The source file must exist in the CWD where this script
// is being run.
//
// If no query string is given - or if the query string
// appears to be invalid - a hyperlinked index of source
// files in the current directoy is created and output.
//
// Usage is 'src.php?f=filename' where filename is the name
// of a source file in the current directory.
//
// The source code listing hilighting code is by Scott Yang
// (http://scott.yang.id.au/2004/05/syntax-hilight-enscript/)
// and uses GNU enscript(1).
//
// Note that Yang's module is designed to work within the
// WordPress envrionment, but handles this application well.
//
// REVISION HISTORY
// 2005-10-02: pdwilso@gmail.com - Initial version (beta)
//
// TO DO:
// Need to do some checks on incoming parameter to assure
// validity before this script is released.
//
// The directory reading code should be abstracted out into
// its own module as a function.
//
// Add last modification date to footer of listing files.
//
// Add size and date info to index page.
//
// Make the script output XHTML compliant.
//
// BUGS / PROBLEMS
// There are some limitations inherent in the use of enscript
// to produce the hilighting - specifically, there is no easy
// way to combine HTML 'span' elements into CSS styles using
// source code language component syntax. This means that we
// get many 'span' elements for colorization, and many "bad"
// elements ('i' and 'b') mixed in and overlapping. Some of
// these may be replaced using e.g. preg_replace(), but
// combining e.g. span (for colorization) and i for italics
// into a span.comment style to apply to comments in the
// source listing - this becomes problematic since we don't
// have direct access to the "parts of speech" detection used
// by the underlying engine.
//
// SEE ALSO
// enscript(1), states(1), genscript(1)
//
////////////////////////////////////////////////////////////////
//
// The pattern and the types data are (mostly) 'constant'
//
// GLOBAL - source code files will be matched against this
$pattern = "/\.(pl|php|c|h|inc)$/";
// GLOBAL - types hash - uses $pattern elements as keys
$types= array(
'pl'=>"perl",
'inc'=>"php",
'php'=>"php",
'c' => "c",
'h' => "c",
);
// common CSS styles used in index and listing
$css = "
h1
{
font-size: 120%;
border-top: 1px gray solid;
}
h1
{
padding-top: 0.75em;
margin-top: 0.25em;
}
p
{
margin: 0;
font-size: small;
text-align: center
}
a:hover
{
text-decoration: underline;
}
a
{
text-decoration: none;
color: blue;
font-weight: bold;
}
";
// GLOBAL - filename from query string
$fn = $_GET['f'];
// GLOBAL - source file type
$typ = "";
// check for the a known file type in the f param
$matches=array();
if (preg_match($pattern,$fn,$matches)) {
if (array_key_exists($matches[1],$types)) {
$typ = $types[$matches[1]];
}
}
// If we didn't recognize the file type, clear the parameter
// so that when we check it below we get the default indexing
// behaviour. If there is no (useful) query value, we want
// to always generate and display an index.
if (!$typ) { $fn=""; }
if (!$fn)
{
// Generate and display an index of all source files in the
// current directory (according to $pattern).
$dh = opendir(".");
while (($f=readdir($dh)) != false) {
$f=rtrim($f);
if (preg_match($pattern,$f)) {
$files[] = urlencode($f);
}
}
closedir($dh);
$ndxcss = $css;
// display the index:
"<title>Source code index</title>\n".
"<style><!-- $ndxcss --></style>\n".
"<p><a href=\"./\">dir listing</a>\n".
"<h1>Source code index:</h1>\n".
"<ol>\n";
foreach ($files as $fn) {
$query = $_SERVER['PHP_SELF']."?f=$fn";
print "<li><a href=\"$query\">$fn</a></li>\n";
}
print "</ol>\n";
}
else
{
// Show the HTMLized source code listing
// Add listing-specific CSS
$srccss = $css . "
pre
{
width: 92%;
background: rgb(220,220,220);
border: 1px blue solid;
padding: 8pt;
margin-left: 1em;
}
";
// include the source code hilighting module.
require "syntax_hilight.php";
// output the page content, including hilighted listing
"<title>$fn</title>\n".
"<style><!-- $srccss --></style>\n".
"<p><a href=\"./\">dir listing</a> | \n".
"<a href=\"".$_SERVER['PHP_SELF'].
"\">src index</a></p>\n".
"<h1><tt>$fn:</tt></h1>\n".
"<pre>".
SyntaxHilighter::hilight_file($fn, $typ).
"\n</pre>\n";
}
?>
2004/09 2005/03 2005/04 2005/05 2005/06 2005/07 2005/08 2005/09 2005/10 2005/11 2006/01 2006/02 2006/04 2006/05 2006/06 2008/01
Subscribe to Posts [Atom]