bjori Fri Jul 27 21:37:09 2007 UTC
Added files:
/phd/include PhDReader.class.php
Modified files:
/phd/formats xhtml.php
/phd build.php
Log:
Initial commit of PhDReader and PhDXHTMLReader
http://cvs.php.net/viewvc.cgi/phd/formats/xhtml.php?r1=1.1&r2=1.2&diff_format=u
Index: phd/formats/xhtml.php
diff -u phd/formats/xhtml.php:1.1 phd/formats/xhtml.php:1.2
--- phd/formats/xhtml.php:1.1 Wed Jul 25 04:52:54 2007
+++ phd/formats/xhtml.php Fri Jul 27 21:37:08 2007
@@ -0,0 +1,136 @@
+<?php
+class PhDXHTMLReader extends PhDReader {
+ protected $map = array( /* {{{ */
+ 'application' => 'span',
+ 'classname' => 'span',
+ 'code' => 'code',
+ 'collab' => 'span',
+ 'collabname' => 'span',
+ 'command' => 'span',
+ 'computeroutput' => 'span',
+ 'constant' => 'span',
+ 'emphasis' => 'em',
+ 'enumname' => 'span',
+ 'envar' => 'span',
+ 'filename' => 'span',
+ 'glossterm' => 'span',
+ 'holder' => 'span',
+ 'informaltable' => 'table',
+ 'itemizedlist' => 'ul',
+ 'listitem' => 'li',
+ 'literal' => 'span',
+ 'mediaobject' => 'div',
+ 'methodparam' => 'span',
+ 'member' => 'li',
+ 'note' => 'div',
+ 'option' => 'span',
+ 'orderedlist' => 'ol',
+ 'para' => 'p',
+ 'parameter' => 'span',
+ 'productname' => 'span',
+ 'propname' => 'span',
+ 'property' => 'span',
+ 'proptype' => 'span',
+ 'simplelist' => 'ul',
+ 'simpara' => 'p',
+ 'title' => 'h1',
+ 'year' => 'span',
+ ); /* }}} */
+
+ public function format_refentry($open) {
+ if($open) {
+ return '<div>';
+ }
+
+ echo "</div>";
+ if ($this->hasAttributes && $this->moveToAttributeNs("id",
"http://www.w3.org/XML/1998/namespace")) {
+ $id = $this->value;
+ }
+ $content = ob_get_contents();
+ ob_clean();
+ file_put_contents("cache/$id.html", $content);
+ }
+ public function format_function($open) {
+ return sprintf('<a href="function.%1$s.html">%1$s</a>',
$this->readContent());
+ }
+ public function format_refsect1($open) {
+ if($open) {
+ return sprintf('<div class="refsect %s">',
$this->readAttribute("role"));
+ }
+ return "</div>\n";
+ }
+ public function format_link($open) {
+ $this->moveToNextAttribute();
+ $href = $this->value;
+ $class = $this->name;
+ $content = $this->readContent("link");
+ return sprintf('<a href="%s" class="%s">%s</a>', $href, $class,
$content);
+ }
+ public function format_methodsynopsis($open) {
+ /* We read this element to END_ELEMENT so $open is useless */
+
+ $content = '<div class="methodsynopsis">';
+ $root = $this->name;
+
+ while($this->readNode($root)) {
+ if($this->nodeType == XMLReader::END_ELEMENT) {
+ $content .= "</span>\n";
+ continue;
+ }
+ $name = $this->name;
+ switch($name) {
+ case "type":
+ case "parameter":
+ case "methodname":
+ $content .= sprintf('<span
class="%s">%s</span>', $name, $this->readContent($name));
+ break;
+ case "methodparam":
+ $content .= '<span class="methodparam">';
+ break;
+ }
+ }
+ $content .= "</div>";
+ return $content;
+ }
+ public function transormFromMap($open, $name) {
+ $tag = $this->map[$name];
+ if($open) {
+ return sprintf('<%s class="%s">', $tag, $name);
+ }
+ return "</$tag>";
+ }
+ public function format_listing_hyperlink_function($matches) {
+ $link = str_replace('_', '-', $matches[1]);
+ $link = "function{$link}.html";
+ return '<a class="phpfunc" href="' . $link . '">' . $matches[1]
. '</a></span>' . $matches[2];
+ }
+ public function highlight_php_code($str) { /* copy&paste from livedocs
*/
+ if (is_array($str)) {
+ $str = $str[0];
+ }
+
+ $tmp = str_replace(
+ array(
+ ' ',
+ '<font color="', // for PHP 4
+ '<span style="color: ', // for PHP 5.0.0RC1
+ '</font>',
+ "\n ",
+ ' '
+ ),
+ array(
+ ' ',
+ '<span class="',
+ '<span class="',
+ '</span>',
+ "\n ",
+ ' '
+ ),
+ highlight_string($str, true)
+ );
+
+ $tmp =
preg_replace_callback('{([\w_]+)\s*</span>(\s*<span\s+class="keyword">\s*\()}m',
array($this, 'format_listing_hyperlink_function'), $tmp);
+ return sprintf('<div class="phpcode">%s</div>', $tmp);
+ }
+}
+
http://cvs.php.net/viewvc.cgi/phd/build.php?r1=1.2&r2=1.3&diff_format=u
Index: phd/build.php
diff -u phd/build.php:1.2 phd/build.php:1.3
--- phd/build.php:1.2 Wed Jul 25 21:59:54 2007
+++ phd/build.php Fri Jul 27 21:37:08 2007
@@ -0,0 +1,15 @@
+<?php
+require "include/PhDReader.class.php";
+require "formats/xhtml.php";
+
+
+$phd = new PhDXHTMLReader("/home/bjori/php/doc/.manual.xml");
+$phd->seek("function.dotnet-load");
+echo date(DATE_RSS), " done seeking\n";
+
+ob_start();
+while($phd->nextNode()) {
+ print $phd->transform();
+}
+$phd->close();
+
http://cvs.php.net/viewvc.cgi/phd/include/PhDReader.class.php?view=markup&rev=1.1
Index: phd/include/PhDReader.class.php
+++ phd/include/PhDReader.class.php
<?php
class PhDReader extends XMLReader {
protected $map = array();
public function __construct($file, $encoding = "UTF-8", $options =
null) {
if (!parent::open($file, $encoding, $options)) {
throw new Exception();
}
}
public function seek($id) {
while(parent::read()) {
if ($this->nodeType == XMLREADER::ELEMENT &&
$this->hasAttributes && $this->moveToAttributeNs("id",
"http://www.w3.org/XML/1998/namespace") && $this->value == $id) {
return $this->moveToElement();
}
}
return false;
}
public function nextNode() {
while($this->read()) {
switch($this->nodeType) {
case XMLReader::ELEMENT:
if($this->isEmptyElement) { continue; }
case XMLReader::TEXT:
case XMLReader::CDATA:
case XMLReader::END_ELEMENT:
return true;
}
}
return false;
}
public function readNode($nodeName) {
return $this->read() && !($this->nodeType ==
XMLReader::END_ELEMENT && $this->name == $nodeName);
}
public function readContent($node = null) {
$retval = "";
if (!$node) {
$node = $this->name;
}
if ($this->readNode($node)) {
$retval = $this->value;
$this->read(); // Jump over END_ELEMENT too
}
return $retval;
}
public function readAttribute($attr) {
return $this->moveToAttribute($attr) ? $this->value : "";
}
public function __call($func, $args) {
if($this->nodeType == XMLReader::END_ELEMENT) { /* ignore */
return;}
trigger_error("No mapper for $func", E_USER_WARNING);
/* NOTE:
* The _content_ of the element will get processed even
though we dont
* know how to handle the elment itself
*/
return "";
}
public function transform() {
$type = $this->nodeType;
$name = $this->name;
switch($type) {
case XMLReader::ELEMENT:
case XMLReader::END_ELEMENT:
if(isset($this->map[$name])) {
return $this->transormFromMap($type ==
XMLReader::ELEMENT, $name);
}
return call_user_func(array($this, "format_" . $name),
$type == XMLReader::ELEMENT);
break;
case XMLReader::TEXT:
return $this->value;
break;
case XMLReader::CDATA:
return $this->highlight_php_code($this->value);
break;
case XMLReader::COMMENT:
case XMLReader::WHITESPACE:
case XMLReader::SIGNIFICANT_WHITESPACE:
/* swallow it */
$this->read();
return $this->transform();
default:
trigger_error("Dunno what to do with {$this->name}
{$this->nodeType}", E_USER_ERROR);
return "";
}
}
}