Code
<?php
require("Cogent.php");
/**
* NotesPane
* A class for scanning for research notes exported by emacs into a
* target directory, and parsing out the desired content
*
* Outline:
* NotesPane("<directory name>")
* NotesPane->run(); // runs HTML output for all notes found
*
* Author: Jonathan Scholz
* 1/28/2012
*/
class NotesPane extends Cogent
{
protected $password = null;
public function run() {
parent::run();
$this->handle_anchor();
}
/*
* Adds a content password
*/
public function set_password($password) {
$this->password = $password;
}
/*
* Generates the HTML for provided files
*
* In this case involves scanning the requested directory
* and installing _GET anchors for each one
*/
public function generate($dir, $files){
// write heading for this project
if ($this->depth == 0) {
print '<h3 id="'.$dir.'">'.$dir."</h3>\n";
}
// count elements in array
$numfiles = count($files);
// loop over files to install url GET link
for ($file=0; $file < $numfiles; $file++) {
print '<a href="'.
"notes.php?targ=$this->rootdir/$dir/$files[$file]".'">'.
"$files[$file]</a><br>\n";
}
}
/* Installs the block for handling the _GET url argument, and
* generates the appropriate html for that file.
*
*/
protected function handle_anchor() {
if (isset($_GET['targ'])) {
// Check the url _GET that we NotesPane installed for us
$targ = $_GET['targ'];
// Extract content from target file
$content = "<hr>\n".
'<div class="notesbox">'.
'<h3 style="text-align: center">'.$targ.'</h3>'.
'<div class="scrollbox">'.
$this->parse_note($targ).
'</div>'.
'</div>';
// Display content if password is provided
if ($this->password != null) {
require("resources/site-code/support/Protector.php");
$p = new Protector($this->password);
$p->protect($content);
} else {
print $content;
}
}
}
/*
* Helper function: Parses the specified file
*
* In this case involves parsing out the scripts and body
* of each file, and re-serializing.
*/
protected function parse_note($file_name) {
// Loads string from research.html, but parses out the body only
$DOM = new DOMDocument;
$DOM->loadHTMLFile($file_name);
// extract stuff we want
$body = $DOM->getElementsByTagName("body");
$scripts = $DOM->getElementsByTagName("script");
// Re-serialize
// Simple approach supported on PHP 5.3.6+
// Print all javascripts (need the MathJax for latex)
// for ($i = 0; $i < $scripts->length; $i++)
// print $DOM->saveHTML($scripts->item($i));
// Print the HTML body
// print $DOM->saveHTML($body->item(0));
// Shitty approach pre PHP 5.3.6
$doc = new DOMDocument('1.0');
// Extract all javascripts (need the MathJax for latex)
for ($i = 0; $i < $scripts->length; $i++)
$doc->appendChild($doc->importNode($scripts->item($i),true));
// Grab the HTML body
$doc->appendChild($doc->importNode($DOM->getElementsByTagName("body")->item(0),true));
return $doc->saveHTML();
}
} // End: class NotesPane
?>
About me