Code
<?php
require_once("Cogent.php");
/**
* CodePane
* A class for generating HTML links by scanning for source files in
* the specified locations. Content is displayed in separate divs,
* and a google analytics tracker is installed
*
* Outline:
* CodePane("<directory name>", array_change_key_case('ext1','ext2',...))
* CodePane->run(); // runs HTML output for all images found, organized by album
*
* The CSS id of the top-level div is "pics", in accordance with the formatting scheme that I
* use for my website. For other applications, just define the "pics" properties as desired
*
* Author: Jonathan Scholz
* 2/3/2010
*/
class CodePane extends Cogent
{
/*
* Generates the HTML for this directory listing
*
* In this case involves dumping a link to the file, and
* installing the analytics tracker
*/
public function generate($dir, $files){
// write heading for this project
if ($this->depth == 0) {
print '<h3 id="'.$dir.'">Project: '.$dir."</h3>\n";
}
// scan project for content (matching provided extensions):
$files = $this->list_content($this->rootdir."/".$dir);
// count elements in array
$numfiles = count($files);
// loop over files to install url GET link and google analytics tracker
for ($file=0; $file < $numfiles; $file++) {
print '<a href="'.
"code.php?targ=$this->rootdir/$dir/$files[$file]".'" '. // set url GET arg
'onClick="_gaq.push(["_trackEvent", "Code", "Downloaded", "'.$files[$file].'"]);">'. // install tracker
"$files[$file]</a>     \n";
}
}
/* Installs the block for handling the _GET url argument, and
* generates the appropriate html for that file.
*
*/
public function handle_anchor() {
// Add the block that displays the specified file
if (isset($_GET['targ'])) {
$targ = $_GET['targ'];
// Extract content from target file
$content = "<hr>\n".
'<a href="'.$targ.'">Click here to download '.'"'.$targ.'"</a>';
$ext = substr(strrchr($targ,'.'),1);
// Highlight & enclose in a notes box
if ($ext != "zip" && $ext != "tgz" && $ext != "tar") {
$content = $content.
'<div class="notesbox">'.
'<h3 style="text-align: center">'.$targ.'</h3>'.
'<div class="scrollbox">'.
$this->highlight($targ, $ext).
"</div></div>\n";
}
print $content;
}
}
public function highlight($targ, $ext) {
require_once("resources/site-code/support/geshi.php");
if ($ext == "h" || $ext == "cpp" || $ext == "pde") {
$lang = "cpp";
} elseif ($ext == "py") {
$lang = "python";
} elseif ($ext == "php") {
$lang = "php";
} elseif ($ext == "java") {
$lang = "java";
} elseif ($ext == "m") {
$lang = "matlab";
} else {
$lang = "txt";
}
$path = "resources/site-code/support/geshi";
$src = file_get_contents($targ);
return geshi_highlight($src, $lang, $path, true);
}
} // End: class CodePane
?>
About me