[git] phd.git branch GWYNNE_PLAYS_HERE created. 6c1cf2b

From: Date: Fri, 02 Mar 2012 14:28:00 +0000
Subject: [git] phd.git branch GWYNNE_PLAYS_HERE created. 6c1cf2b
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-9785@lists.php.net to get a copy of this message
The branch, GWYNNE_PLAYS_HERE on phd.git has been created at 6c1cf2b5ae5f59a87df2d902b0598760af63e5bd (commit) -- Log ---------------------------------------- commit 6c1cf2b5ae5f59a87df2d902b0598760af63e5bd Author: Gwynne Raskind <gwynne@php.net> Date: Mon Jul 30 09:48:08 2007 +0000 Paradigm shift. Formats and themes are no longer subclasses of PhDReader, since it doesn't really make sense in terms of object-oriented design ("is a" versus "has a"). Also changed the way format mappings are done almost completely; I didn't yet show how a theme would really modify things, but the beginnings of the flexibility idea are here. What's here right now produces a messy and invalid bigxhtml file when given .manual.xml, but the transformations that *are* implemented do work. The test data in build.php is a better example of the functionality that currently exists. More work to come soon, but once I got this far I decided to take a break; it feels like my neurons are melting. diff --git a/build.php b/build.php index daac25e..088d56a 100644 --- a/build.php +++ b/build.php @@ -2,29 +2,33 @@ error_reporting( E_ALL | E_STRICT ); require_once 'config.php'; -require_once 'formats/xhtml.php'; +require_once 'include/PhDReader.class.php'; file_put_contents( dirname( __FILE__ ) . "/temp.xml", <<<~XML <?xml version="1.0" encoding="utf-8"?> <book> - <part id="part1"> - <chapter id="chap1"> - Using the application <application>autoconf</application>, we can do some - fun stuff, since <command>php</command> does other fun things, and dolor - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod - tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, - quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo - consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse - cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat - non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + <part xml:id="part1"> + <chapter xml:id="chap1"> + <sect1 xml:id="part1.chap1.sect1"> + <title>First section!</title> + Using the application <application>autoconf</application>, we can do some + fun stuff, since <command>php</command> does other fun things, and dolor + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim + veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea + commodo consequat. Duis aute irure dolor in reprehenderit in voluptate + velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat + cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id + est laborum. + </sect1> </chapter> </part> </book> XML ); -$phd = new PhDReader_XHTML( dirname( __FILE__ ) . "/temp.xml", NULL, 2 ); +$phd = new PhDReader( $OPTIONS[ 'xml_root' ] . "/.manual.xml", NULL, 2 ); while ( $phd->transformChunk( $chunk ) ) { print "{$chunk}\n"; diff --git a/formats/xhtml.php b/formats/xhtml.php index 2846615..fab7566 100644 --- a/formats/xhtml.php +++ b/formats/xhtml.php @@ -12,18 +12,76 @@ | http://phd.php.net/LICENSE | +-------------------------------------------------------------------------+ | The XHTML output format class. This should not be instantiated | - | directly; it is intended for extension by a theme class. | - | XXX This is temporarily untrue for "let's get it started" purposes. | + | directly; it is intended for use only by PHDReader. | +-------------------------------------------------------------------------+ */ /* Grab the PhDReader parent class. */ require_once 'include/PhDReader.class.php'; -class PhDReader_XHTML extends PhDReader { +class PhD_xhtml_Format implements PhD_OutputFormat { + + protected $reader = NULL; + protected $handlerMap = array(); + protected $IDStack = array(); + protected $nameStack = array(); + + protected function translationSpec( $name ) { + return array( "<{$name} class=\"%n%\">", "</{$name}>", FALSE ); + } + + public function __construct( $reader ) { + + $this->reader = $reader; + + $span = $this->translationSpec( 'span' ); + $div = $this->translationSpec( 'div' ); + + $this->handlerMap = array( + 'application' => $span, + 'classname' => $span, + 'code' => $this->translationSpec( 'code' ), + 'collab' => $span, + 'collabname' => $span, + 'command' => $span, + 'computerOutput' => $span, + 'constant' => $span, + 'emphasis' => $this->translationSpec( 'em' ), + 'enumname' => $span, + 'envar' => $span, + 'filename' => $span, + 'glossterm' => $span, + 'holder' => $span, + 'informaltable' => $this->translationSpec( 'table' ), + 'itemizedlist' => $this->translationSpec( 'ul' ), + 'listitem' => $this->translationSpec( 'li' ), + 'literal' => $span, + 'mediaobject' => $div, + 'methodparam' => $span, + 'member' => $this->translationSpec( 'li' ), + 'note' => $div, + 'option' => $span, + 'orderedlist' => $this->translationSpec( 'ol' ), + 'para' => $this->translationSpec( 'p' ), + 'parameter' => $span, + 'partintro' => $div, + 'productname' => $span, + 'propname' => $span, + 'property' => $span, + 'proptype' => $span, + 'section' => array( '<div class="%n%" id="%i%" xml:id="%i%">', '</div>', TRUE ), + 'sect1' => array( '%push=i%%push=n%<div class="%n%" id="%i%" xml:id="%i%">', '</div>%pop=n%%pop=i%', TRUE ), + 'title' => array( '<h1 class="%top=n%"><a name="%top=i%">', '</a></h1>', FALSE ), + 'simplelist' => $this->translationSpec( 'ul' ), + 'simpara' => $this->translationSpec( 'p' ), + 'year' => $span, + 'refentry' => array( '<div id="%i%">', '</div>', TRUE ), + 'reference' => $div, + 'function' => array( '<b>', '</b>', FALSE ), + 'refsect1' => $div, + '__default' => array( $this, 'unknownElement' ), + ); - public function __construct( $file, $encoding = 'utf-8', $options = NULL ) { - parent::__construct( $file, $encoding, $options ); } public function __destruct() { @@ -35,7 +93,7 @@ class PhDReader_XHTML extends PhDReader { } - protected function transformNode( $name, $type, &$output ) { + public function transformNode( $name, $type, &$output ) { switch ( $type ) { @@ -45,11 +103,11 @@ class PhDReader_XHTML extends PhDReader { break; case XMLReader::TEXT: - $output = $this->value; + $output = $this->reader->value; return FALSE; case XMLReader::CDATA: - $output = $this->processCDATA( $this->value ); + $output = $this->processCDATA( $this->reader->value ); return FALSE; case XMLReader::ENTITY_REF: @@ -61,84 +119,16 @@ class PhDReader_XHTML extends PhDReader { } protected function processElement( $name, $isOpen, &$output ) { - static $handlerMap = NULL; - if ( is_null( $handlerMap ) ) { - $spanName = array( '<span class="%n%">', FALSE, '</span>', FALSE ); - $divName = array( '<div class="%n%">', FALSE, '</div>', FALSE ); - $divNameChunked = array( '<div class="%n%">', FALSE, '</div>', TRUE ); - $oneToOne = array( '<%n%>', FALSE, '</%n%>', FALSE ); - - $handlerMap = array( - 'application' => $spanName, - 'classname' => $spanName, - 'code' => $oneToOne, - 'collab' => $spanName, - 'collabname' => $spanName, - 'command' => $spanName, - 'computerOutput' => $spanName, - 'constant' => $spanName, - 'emphasis' => $oneToOne, - 'enumname' => $spanName, - 'envar' => $spanName, - 'filename' => $spanName, - 'glossterm' => $spanName, - 'holder' => $spanName, - 'informatlable' => array( '<table>', FALSE, '</table>', FALSE ), - 'itemizedlist' => array( '<ul>', FALSE, '</ul>', FALSE ), - 'listitem' => array( '<li>', FALSE, '</li>', FALSE ), - 'literal' => $spanName, - 'mediaobject' => $divName, - 'methodparam' => $spanName, - 'member' => array( '<li>', FALSE, '</li>', FALSE ), - 'note' => $divName, - 'option' => $spanName, - 'orderedlist' => array( '<ol>', FALSE, '</ol>', FALSE ), - 'para' => array( '<p>', FALSE, '</p>', FALSE ), - 'parameter' => $spanName, - 'partintro' => $divName, - 'productname' => $spanName, - 'propname' => $spanName, - 'property' => $spanName, - 'proptype' => $spanName, - 'section' => $divNameChunked, - 'simplelist' => array( '<ul>', FALSE, '</ul>', FALSE ), - 'simpara' => array( '<p>', FALSE, '</p>', FALSE ), - 'title' => array( 'checkparentname', array( '__default' => 'h1', 'refsect1' => 'h3', 'example' => 'h4' ) ), - 'year' => $spanName, - 'refentry' => array( '<div id="%i%" class="refentry">', FALSE, '</div>', TRUE, TRUE ), - 'reference' => array( $this, 'format_reference' ), - 'function' => array( '<a href="function.%v%.html">', FALSE, '</a>', FALSE ), - 'refsect1' => array( '<div class="refsect_%r%">', FALSE, '</div>', FALSE ), - '__default' => array( $this, 'unknownElement' ), - ); - } + $mapping = isset( $this->handlerMap[ $name ] ) ? $this->handlerMap[ $name ] : $this->handlerMap[ '__default' ]; + + if ( is_callable( $mapping ) ) { + return call_user_func( $mapping, $name, $isOpen, &$output ); + + } else if ( is_array( $mapping ) ) { + $output = $this->formatMappingString( $name, $this->reader->getID(), $isOpen ? $mapping[ 0 ] : $mapping[ 1 ] ); + return ( $isOpen ? FALSE : $mapping[ 2 ] ); - $mapping = isset( $handlerMap[ $name ] ) ? $handlerMap[ $name ] : $handlerMap[ '__default' ]; - if ( is_array( $mapping ) ) { - if ( is_string( $mapping[ 0 ] ) ) { - switch ( $mapping[ 0 ] ) { - case 'checkparentname': - $output = '<div class="warning">NOT IMPLEMENTED YET.</div>'; - return FALSE; - default: - $id = $this->getID(); - $output = $this->formatMappingString( $name, $id, $isOpen ? $mapping[ 0 ] : $mapping[ 2 ] ); - if ( !empty( $mapping[ 4 ] ) ) { - $this->pushStack( $id ); - } - return ( $isOpen ? $mapping[ 1 ] : $mapping[ 3 ] ); - } - } else if ( is_callable( $mapping ) ) { - return call_user_func( $mapping, $name, $isOpen, &$output ); - } - } else if ( is_string( $mapping ) ) { - if ( $isOpen ) { - $output = $this->formatMappingString( $name, $this->getID(), $mapping ); - } else { - $output = ''; - } - return FALSE; } $output = '<div class="warning">Bad handler string for '.$name.'!</div>'; return FALSE; @@ -147,31 +137,53 @@ class PhDReader_XHTML extends PhDReader { protected function processCDATA( $content ) { - return '<div class="phpcode">' . highlight_string( $content ) . '</div>'; + return "<div>{$content}</div>"; } - protected function formatMappingString( $name, $id, $string ) { + protected function mappingFormatter( $matches ) { + + if ( empty( $matches[ 1 ] ) ) { + if ( $matches[ 4 ] == 'n' ) { + return $this->_mapping_name; + } else if ( $matches[ 4 ] == 'i' ) { + return $this->_mapping_id; + } + } else { + if ( $matches[ 3 ] == 'n' ) { + $a = &$this->nameStack; + $v = $this->_mapping_name; + } else if ( $matches[ 3 ] == 'i' ) { + $a = &$this->IDStack; + $v = $this->_mapping_id; + } + + if ( $matches[ 2 ] == 'push' ) { + array_push( $a, $v ); + return ''; + } else if ( $matches[ 2 ] == 'pop' ) { + array_pop( $a ); + return ''; + } else if ( $matches[ 2 ] == 'top' ) { + return $a[ 0 ]; + } + } + return 'INVALID MAPPING DATA:' . var_export($matches,1); + + } - // XXX Yes, this needs heavy optimization, it's example for now. - return str_replace( array( '%n%', '%i%', '%v%', '%r' ), - array( $name, $id, $this->readInnerXML(), $this->getAttribute( 'role' ) ), - $string ); - } + protected function formatMappingString( $name, $id, $string ) { + + $sc = 'ni'; // sc == stack chars + $fc = 'ni'; // fc == formatted chars + $this->_mapping_name = $name; + $this->_mapping_id = $id; + $s = preg_replace_callback( "/%(?:((push|pop|top)=([{$sc}]))|([{$fc}]))%/", array( $this, 'mappingFormatter' ), $string ); + unset( $this->_mapping_name ); + unset( $this->_mapping_id ); + return $s; - protected function format_reference( $name, $isOpen, $output ) { - if ( $isOpen ) { - $output = sprintf( '<div id="%s" class="reference">', $this->getID() ); - return FALSE; - } - $output = '</div>' . - '<ul class="funclist">'; - foreach ( $this->popStack() as $func => $desc ) { - $output .= sptrinf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); - } - $output .= '</ul>'; - return TRUE; } protected function unknownElement( $name, $isOpen, $output ) { diff --git a/include/PhDReader.class.php b/include/PhDReader.class.php index 758de85..d2fc0f5 100644 --- a/include/PhDReader.class.php +++ b/include/PhDReader.class.php @@ -11,25 +11,110 @@ | world-wide-web at the following url: | | http://phd.php.net/LICENSE | +-------------------------------------------------------------------------+ - | The base class for reading the giant XML blob. This is intended for | - | extension by output formats, and then for further extension by output | - | themes. This class should not be instantiated directly. | + | The base class for reading the giant XML blob. This is intended to have | + | a format and theme plugged into it. | +-------------------------------------------------------------------------+ */ -abstract class PhDReader extends XMLReader { +// *** +// Output format classes must implement this interface. +// THIS IS THE OFFICIAL OUTPUT FORMAT INTERFACE. +interface PhD_OutputFormat { + + // proto string getFormatName( void ) + // Return the name of the format. + public function getFormatName(); + + // proto string transformNode( string name, int type, string &output ) + // Transform a given node, returning the string output. Binary strings ARE + // handled safely. This function will be called for all element, text, + // cdata, entity reference, and end element nodes. It is NOT valid for + // this method to advance the parser's input. Return TRUE to create a + // chunk boundary, FALSE otherwise. + public function transformNode( $name, $type, &$output ); + +} + +// *** +// Output theme classes must implement this interface. +// THIS IS THE OFFICIAL OUTPUT THEME INTERFACE. +interface PhD_OutputTheme { + + // proto string getThemeName( void ) + // Return the name of the theme. + public function getThemeName(); + + // proto string transformNode( string name, int type, PhD_OutputFormat formatter, string &output ) + // Transform a given node, returning the string output. Call the given + // formatter for all inputs, altering its output as appropriate, unless + // the theme handles the entire transformation by itself. + public function transformNode( $name, $type, $formatter, &$output ); + + // proto string chunkHeader( void ) + // Return a header for a chunk. The current node will be the first node in + // the chunk. + public function chunkHeader(); + + // proto string chunkFooter( void ) + // Return a footer for a chunk. The current node will be the END_ELEMENT + // of the chunk's first node. + public function chunkFooter(); + +} + + +class PhDReader extends XMLReader { + const XMLNS_XML = "http://www.w3.org/XML/1998/namespace"; - const XMLNS_XLINK = "http://www.w3.org/1999/xlinK"; + const XMLNS_XLINK = "http://www.w3.org/1999/xlink"; const XMLNS_PHD = "http://www.php.net/ns/phd"; + protected $outputFormat = NULL; + protected $outputTheme = NULL; + protected $stack = array(); + protected $state = array(); + + // *** + // Debugging functions + public function debuggingOn() { return $GLOBALS[ 'OPTIONS' ][ 'debug' ]; } + public function debugMessage( $msg ) { + if ( $this->debuggingOn() ) { + fwrite( STDERR, date( DATE_RFC2822 ) . ": {$msg}\n" ); + } + } + public function debugAssert( $condition, $desc ) { + if ( $this->debuggingOn() && $condition == FALSE ) { + $this->debugMessage( "ASSERTION FAILURE for '{$desc}'.\nBacktrace:" ); + $this->debugMessage( print_r( array_slice( debug_backtrace(), 1 ), 1 ) ); + exit( 1 ); + } + } + + // *** + // Constructor and destructor + public function __construct( $file ) { + global $OPTIONS; - public function __construct( $file, $encoding = "utf-8", $options = NULL ) { - - if ( !parent::open( $file, $encoding, $options ) ) { + if ( !parent::open( $file, NULL, 0 ) ) { throw new Exception(); } + // Position the reader at the first node. $this->read(); + + // Grab the output format. + require dirname( __FILE__ ) . "/../formats/{$OPTIONS[ 'output_format' ]}.php"; + $formatClassName = "PhD_" . str_replace( '-', '_', $OPTIONS[ 'output_format' ] ) . '_Format'; + $this->outputFormat = new $formatClassName( $this ); + $this->debugMessage( "Loaded output format '{$OPTIONS[ 'output_format' ]}' using class '{$formatClassName}'." ); + + // Grab the output theme. + require dirname( __FILE__ ) . "/../themes/{$OPTIONS[ 'output_theme' ]}/theme.php"; + $themeClassName = "PhD_" . str_replace( '-', '_', $OPTIONS[ 'output_theme' ] ) . '_Theme'; + $this->outputTheme = new $themeClassName( $this ); + $this->debugMessage( "Loaded output theme '{$OPTIONS[ 'output_theme' ]}' using class '{$themeClassName}'." ); + + // TODO: Set up the output encoding if necessary. } @@ -37,31 +122,18 @@ abstract class PhDReader extends XMLReader { } // *** - // Format subclasses must implement these to make them real formats. - // THIS IS THE OFFICIAL OUTPUT FORMAT INTERFACE. - - // proto string getFormatName( void ) - // Return the name of the format. - abstract public function getFormatName(); - - // proto string transformNode( string name, int type, string &output ) - // Transform a given node, returning the binary string output. Binary - // strings ARE handled safely. This function will be called for all - // element, text, cdata, entity reference, and end element nodes. It - // is always valid for this method to make the parser move around in - // the file. Return TRUE to create a chunk boundary, FALSE otherwise. - abstract protected function transformNode( $name, $type, &$output ); - - // *** // Protected methods (intended for internal and subclass use only) + // *** + // Public methods + // proto array getAllAttributes( void ) // Return all the attributes in the current element node as name:ns => // value pairs. Prefer the getAttribute*() methods defined by XMLReader // when possible; use this only when you really do need all the // attributes. An element without any attributes will result in an empty // array, while a non-element node will result in a return of FALSE. - protected function getAttributes() { + public function getAttributes() { $type = $this->nodeType; if ( $type != XMLReader::ELEMENT && $type != XMLReader::END_ELEMENT ) { @@ -83,7 +155,7 @@ abstract class PhDReader extends XMLReader { // proto string getID( void ) // Get the ID of the current element. Works on element and end element // nodes only, returning an empty string in all other cases. - protected function getID() { + public function getID() { if ( $this->hasAttributes && $this->moveToAttributeNs( "id", self::XMLNS_XML ) ) { $id = $this->value; @@ -93,49 +165,23 @@ abstract class PhDReader extends XMLReader { return ""; } - - // protected void pushStack( mixed value ) - // Push a value of any kind onto the parser stack. The stack is not used - // by the parser; it is intended as a cheap data store for formats and - // themes. - protected function pushStack( $value ) { - - array_push( $this->stack, $value ); - - } - - // protected mixed stackTop( void ) - // Return the top value on the stack. - protected function stackTop() { - - return count( $this->stack ) ? $this->stack[ 0 ] : NULL; - - } - - // protected mixed popStack( void ) - // Pop the top value off the stack and return it. - protected function popStack() { - - return array_pop( $this->stack ); - - } - - // *** - // Public methods - + // proto bool seek( string id ) // Seek to an ID. This is used to start the parser somewhere that isn't at // the beginning (duh). Be careful; this does not cause the parser to halt // at the closing element of a successful seek. Don't forget to check the // return value. public function seek( $id ) { - + + $this->debugMessage( "Starting seek to {$id}..." ); while( parent::read() ) { if ( $this->nodeType === XMLREADER::ELEMENT && $this->hasAttributes && $this->moveToAttributeNs( "id", self::XMLNS_XML ) && $this->value === $id ) { + $this->debugMessage( "Seek complete." ); return $this->moveToElement(); } } + $this->debugMessage( "Seek failed." ); return FALSE; } @@ -145,11 +191,11 @@ abstract class PhDReader extends XMLReader { // format's chunker. Returns the tree, or FALSE on error. public function transform() { - $allData = ''; - while ( ( $data = $this->transformChunk() ) !== FALSE ) { + $allData = $this->outputTheme->chunkHeader(); + while ( $this->transformChunk( $data, FALSE ) ) { $allData .= $data; } - return $allData; + return $allData . $this->outputTheme->chunkFooter(); } @@ -157,26 +203,27 @@ abstract class PhDReader extends XMLReader { // Transform nodes until the output format says it's time to output a // chunking boundary or the parser runs out of data. Returns TRUE on // success, FALSE on EOF. $data contains the transformed data, if any. - public function transformChunk( &$outData ) { + public function transformChunk( &$outData, $applyHeaderFooter = TRUE ) { global $OPTIONS; $hasMore = TRUE; $data = fopen( "php://temp/maxmemory:{$OPTIONS[ 'chunking_memory_limit' ]}", "r+" ); + fwrite( $data, $this->outputTheme->chunkHeader() ); $isChunk = FALSE; do { $nodeName = $this->name; $nodeType = $this->nodeType; switch ( $nodeType ) { - case XMLReader::NONE: - break; - case XMLReader::ELEMENT: case XMLReader::END_ELEMENT: case XMLReader::TEXT: case XMLReader::CDATA: case XMLReader::ENTITY_REF: - $isChunk = $this->transformNode( $nodeName, $nodeType, $output ); + $isChunk = $this->outputTheme->transformNode( $nodeName, $nodeType, $this->outputFormat, $output ); fwrite( $data, $output ); + if ( $isChunk ) { + fwrite( $data, $this->outputTheme->chunkFooter() ); + } break; case XMLReader::ENTITY: @@ -189,8 +236,12 @@ abstract class PhDReader extends XMLReader { case XMLReader::SIGNIFICANT_WHITESPACE: case XMLReader::END_ENTITY: case XMLReader::XML_DECLARATION: + case XMLReader::COMMENT: // Eat it for lunch. break; + + default: + die( "Unknown node type {$nodeType} while transforming. Can not continue." ); } $hasMore = $this->read(); } while ( !$isChunk && $hasMore ); @@ -203,43 +254,25 @@ abstract class PhDReader extends XMLReader { } /* - public function getID() { - if ( $this->hasAttributes && $this->moveToAttributeNs("id", self::XMLNS_XML) ) { - $id = $this->value; - $this->moveToElement(); - return $id; - } - return ""; - } - 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; - + 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; @@ -249,28 +282,9 @@ abstract class PhDReader extends XMLReader { $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 notXPath( $tag ) { $depth = $this->depth; @@ -284,54 +298,6 @@ abstract class PhDReader extends XMLReader { return $tag; } - - public function transform() { - - $type = $this->nodeType; - $name = $this->name; - - switch( $type ) { - - case XMLReader::ELEMENT: - $this->STACK[ $this->depth ] = $name; - - case XMLReader::END_ELEMENT: - $funcname = "format_$name"; - if ( isset( $this->map[ $name ] ) ) { - $tag = $this->map[ $name ]; - if ( is_array( $tag ) ) { - $tag = $this->notXPath( $tag ); - } - if ( strncmp( $tag, "format_", 7 ) ) { - return $this->transormFromMap( $type == XMLReader::ELEMENT, $tag, $name ); - } - $funcname = $tag; - } - return call_user_func( array( $this, $funcname ), $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 - // XXX This could lead to a recursion overflow if a lot of comment nodes get strung together. - $this->read(); - return $this->transform(); - - default: - trigger_error( "Dunno what to do with {$this->name} {$this->nodeType}", E_USER_ERROR ); - return ""; - } - - } */ } diff --git a/phpdotnet/phd/Format/XHTML.php b/phpdotnet/phd/Format/XHTML.php index 2846615..fab7566 100644 --- a/phpdotnet/phd/Format/XHTML.php +++ b/phpdotnet/phd/Format/XHTML.php @@ -12,18 +12,76 @@ | http://phd.php.net/LICENSE | +-------------------------------------------------------------------------+ | The XHTML output format class. This should not be instantiated | - | directly; it is intended for extension by a theme class. | - | XXX This is temporarily untrue for "let's get it started" purposes. | + | directly; it is intended for use only by PHDReader. | +-------------------------------------------------------------------------+ */ /* Grab the PhDReader parent class. */ require_once 'include/PhDReader.class.php'; -class PhDReader_XHTML extends PhDReader { +class PhD_xhtml_Format implements PhD_OutputFormat { + + protected $reader = NULL; + protected $handlerMap = array(); + protected $IDStack = array(); + protected $nameStack = array(); + + protected function translationSpec( $name ) { + return array( "<{$name} class=\"%n%\">", "</{$name}>", FALSE ); + } + + public function __construct( $reader ) { + + $this->reader = $reader; + + $span = $this->translationSpec( 'span' ); + $div = $this->translationSpec( 'div' ); + + $this->handlerMap = array( + 'application' => $span, + 'classname' => $span, + 'code' => $this->translationSpec( 'code' ), + 'collab' => $span, + 'collabname' => $span, + 'command' => $span, + 'computerOutput' => $span, + 'constant' => $span, + 'emphasis' => $this->translationSpec( 'em' ), + 'enumname' => $span, + 'envar' => $span, + 'filename' => $span, + 'glossterm' => $span, + 'holder' => $span, + 'informaltable' => $this->translationSpec( 'table' ), + 'itemizedlist' => $this->translationSpec( 'ul' ), + 'listitem' => $this->translationSpec( 'li' ), + 'literal' => $span, + 'mediaobject' => $div, + 'methodparam' => $span, + 'member' => $this->translationSpec( 'li' ), + 'note' => $div, + 'option' => $span, + 'orderedlist' => $this->translationSpec( 'ol' ), + 'para' => $this->translationSpec( 'p' ), + 'parameter' => $span, + 'partintro' => $div, + 'productname' => $span, + 'propname' => $span, + 'property' => $span, + 'proptype' => $span, + 'section' => array( '<div class="%n%" id="%i%" xml:id="%i%">', '</div>', TRUE ), + 'sect1' => array( '%push=i%%push=n%<div class="%n%" id="%i%" xml:id="%i%">', '</div>%pop=n%%pop=i%', TRUE ), + 'title' => array( '<h1 class="%top=n%"><a name="%top=i%">', '</a></h1>', FALSE ), + 'simplelist' => $this->translationSpec( 'ul' ), + 'simpara' => $this->translationSpec( 'p' ), + 'year' => $span, + 'refentry' => array( '<div id="%i%">', '</div>', TRUE ), + 'reference' => $div, + 'function' => array( '<b>', '</b>', FALSE ), + 'refsect1' => $div, + '__default' => array( $this, 'unknownElement' ), + ); - public function __construct( $file, $encoding = 'utf-8', $options = NULL ) { - parent::__construct( $file, $encoding, $options ); } public function __destruct() { @@ -35,7 +93,7 @@ class PhDReader_XHTML extends PhDReader { } - protected function transformNode( $name, $type, &$output ) { + public function transformNode( $name, $type, &$output ) { switch ( $type ) { @@ -45,11 +103,11 @@ class PhDReader_XHTML extends PhDReader { break; case XMLReader::TEXT: - $output = $this->value; + $output = $this->reader->value; return FALSE; case XMLReader::CDATA: - $output = $this->processCDATA( $this->value ); + $output = $this->processCDATA( $this->reader->value ); return FALSE; case XMLReader::ENTITY_REF: @@ -61,84 +119,16 @@ class PhDReader_XHTML extends PhDReader { } protected function processElement( $name, $isOpen, &$output ) { - static $handlerMap = NULL; - if ( is_null( $handlerMap ) ) { - $spanName = array( '<span class="%n%">', FALSE, '</span>', FALSE ); - $divName = array( '<div class="%n%">', FALSE, '</div>', FALSE ); - $divNameChunked = array( '<div class="%n%">', FALSE, '</div>', TRUE ); - $oneToOne = array( '<%n%>', FALSE, '</%n%>', FALSE ); - - $handlerMap = array( - 'application' => $spanName, - 'classname' => $spanName, - 'code' => $oneToOne, - 'collab' => $spanName, - 'collabname' => $spanName, - 'command' => $spanName, - 'computerOutput' => $spanName, - 'constant' => $spanName, - 'emphasis' => $oneToOne, - 'enumname' => $spanName, - 'envar' => $spanName, - 'filename' => $spanName, - 'glossterm' => $spanName, - 'holder' => $spanName, - 'informatlable' => array( '<table>', FALSE, '</table>', FALSE ), - 'itemizedlist' => array( '<ul>', FALSE, '</ul>', FALSE ), - 'listitem' => array( '<li>', FALSE, '</li>', FALSE ), - 'literal' => $spanName, - 'mediaobject' => $divName, - 'methodparam' => $spanName, - 'member' => array( '<li>', FALSE, '</li>', FALSE ), - 'note' => $divName, - 'option' => $spanName, - 'orderedlist' => array( '<ol>', FALSE, '</ol>', FALSE ), - 'para' => array( '<p>', FALSE, '</p>', FALSE ), - 'parameter' => $spanName, - 'partintro' => $divName, - 'productname' => $spanName, - 'propname' => $spanName, - 'property' => $spanName, - 'proptype' => $spanName, - 'section' => $divNameChunked, - 'simplelist' => array( '<ul>', FALSE, '</ul>', FALSE ), - 'simpara' => array( '<p>', FALSE, '</p>', FALSE ), - 'title' => array( 'checkparentname', array( '__default' => 'h1', 'refsect1' => 'h3', 'example' => 'h4' ) ), - 'year' => $spanName, - 'refentry' => array( '<div id="%i%" class="refentry">', FALSE, '</div>', TRUE, TRUE ), - 'reference' => array( $this, 'format_reference' ), - 'function' => array( '<a href="function.%v%.html">', FALSE, '</a>', FALSE ), - 'refsect1' => array( '<div class="refsect_%r%">', FALSE, '</div>', FALSE ), - '__default' => array( $this, 'unknownElement' ), - ); - } + $mapping = isset( $this->handlerMap[ $name ] ) ? $this->handlerMap[ $name ] : $this->handlerMap[ '__default' ]; + + if ( is_callable( $mapping ) ) { + return call_user_func( $mapping, $name, $isOpen, &$output ); + + } else if ( is_array( $mapping ) ) { + $output = $this->formatMappingString( $name, $this->reader->getID(), $isOpen ? $mapping[ 0 ] : $mapping[ 1 ] ); + return ( $isOpen ? FALSE : $mapping[ 2 ] ); - $mapping = isset( $handlerMap[ $name ] ) ? $handlerMap[ $name ] : $handlerMap[ '__default' ]; - if ( is_array( $mapping ) ) { - if ( is_string( $mapping[ 0 ] ) ) { - switch ( $mapping[ 0 ] ) { - case 'checkparentname': - $output = '<div class="warning">NOT IMPLEMENTED YET.</div>'; - return FALSE; - default: - $id = $this->getID(); - $output = $this->formatMappingString( $name, $id, $isOpen ? $mapping[ 0 ] : $mapping[ 2 ] ); - if ( !empty( $mapping[ 4 ] ) ) { - $this->pushStack( $id ); - } - return ( $isOpen ? $mapping[ 1 ] : $mapping[ 3 ] ); - } - } else if ( is_callable( $mapping ) ) { - return call_user_func( $mapping, $name, $isOpen, &$output ); - } - } else if ( is_string( $mapping ) ) { - if ( $isOpen ) { - $output = $this->formatMappingString( $name, $this->getID(), $mapping ); - } else { - $output = ''; - } - return FALSE; } $output = '<div class="warning">Bad handler string for '.$name.'!</div>'; return FALSE; @@ -147,31 +137,53 @@ class PhDReader_XHTML extends PhDReader { protected function processCDATA( $content ) { - return '<div class="phpcode">' . highlight_string( $content ) . '</div>'; + return "<div>{$content}</div>"; } - protected function formatMappingString( $name, $id, $string ) { + protected function mappingFormatter( $matches ) { + + if ( empty( $matches[ 1 ] ) ) { + if ( $matches[ 4 ] == 'n' ) { + return $this->_mapping_name; + } else if ( $matches[ 4 ] == 'i' ) { + return $this->_mapping_id; + } + } else { + if ( $matches[ 3 ] == 'n' ) { + $a = &$this->nameStack; + $v = $this->_mapping_name; + } else if ( $matches[ 3 ] == 'i' ) { + $a = &$this->IDStack; + $v = $this->_mapping_id; + } + + if ( $matches[ 2 ] == 'push' ) { + array_push( $a, $v ); + return ''; + } else if ( $matches[ 2 ] == 'pop' ) { + array_pop( $a ); + return ''; + } else if ( $matches[ 2 ] == 'top' ) { + return $a[ 0 ]; + } + } + return 'INVALID MAPPING DATA:' . var_export($matches,1); + + } - // XXX Yes, this needs heavy optimization, it's example for now. - return str_replace( array( '%n%', '%i%', '%v%', '%r' ), - array( $name, $id, $this->readInnerXML(), $this->getAttribute( 'role' ) ), - $string ); - } + protected function formatMappingString( $name, $id, $string ) { + + $sc = 'ni'; // sc == stack chars + $fc = 'ni'; // fc == formatted chars + $this->_mapping_name = $name; + $this->_mapping_id = $id; + $s = preg_replace_callback( "/%(?:((push|pop|top)=([{$sc}]))|([{$fc}]))%/", array( $this, 'mappingFormatter' ), $string ); + unset( $this->_mapping_name ); + unset( $this->_mapping_id ); + return $s; - protected function format_reference( $name, $isOpen, $output ) { - if ( $isOpen ) { - $output = sprintf( '<div id="%s" class="reference">', $this->getID() ); - return FALSE; - } - $output = '</div>' . - '<ul class="funclist">'; - foreach ( $this->popStack() as $func => $desc ) { - $output .= sptrinf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); - } - $output .= '</ul>'; - return TRUE; } protected function unknownElement( $name, $isOpen, $output ) { diff --git a/phpdotnet/phd/Reader/Legacy.php b/phpdotnet/phd/Reader/Legacy.php index 758de85..d2fc0f5 100644 --- a/phpdotnet/phd/Reader/Legacy.php +++ b/phpdotnet/phd/Reader/Legacy.php @@ -11,25 +11,110 @@ | world-wide-web at the following url: | | http://phd.php.net/LICENSE | +-------------------------------------------------------------------------+ - | The base class for reading the giant XML blob. This is intended for | - | extension by output formats, and then for further extension by output | - | themes. This class should not be instantiated directly. | + | The base class for reading the giant XML blob. This is intended to have | + | a format and theme plugged into it. | +-------------------------------------------------------------------------+ */ -abstract class PhDReader extends XMLReader { +// *** +// Output format classes must implement this interface. +// THIS IS THE OFFICIAL OUTPUT FORMAT INTERFACE. +interface PhD_OutputFormat { + + // proto string getFormatName( void ) + // Return the name of the format. + public function getFormatName(); + + // proto string transformNode( string name, int type, string &output ) + // Transform a given node, returning the string output. Binary strings ARE + // handled safely. This function will be called for all element, text, + // cdata, entity reference, and end element nodes. It is NOT valid for + // this method to advance the parser's input. Return TRUE to create a + // chunk boundary, FALSE otherwise. + public function transformNode( $name, $type, &$output ); + +} + +// *** +// Output theme classes must implement this interface. +// THIS IS THE OFFICIAL OUTPUT THEME INTERFACE. +interface PhD_OutputTheme { + + // proto string getThemeName( void ) + // Return the name of the theme. + public function getThemeName(); + + // proto string transformNode( string name, int type, PhD_OutputFormat formatter, string &output ) + // Transform a given node, returning the string output. Call the given + // formatter for all inputs, altering its output as appropriate, unless + // the theme handles the entire transformation by itself. + public function transformNode( $name, $type, $formatter, &$output ); + + // proto string chunkHeader( void ) + // Return a header for a chunk. The current node will be the first node in + // the chunk. + public function chunkHeader(); + + // proto string chunkFooter( void ) + // Return a footer for a chunk. The current node will be the END_ELEMENT + // of the chunk's first node. + public function chunkFooter(); + +} + + +class PhDReader extends XMLReader { + const XMLNS_XML = "http://www.w3.org/XML/1998/namespace"; - const XMLNS_XLINK = "http://www.w3.org/1999/xlinK"; + const XMLNS_XLINK = "http://www.w3.org/1999/xlink"; const XMLNS_PHD = "http://www.php.net/ns/phd"; + protected $outputFormat = NULL; + protected $outputTheme = NULL; + protected $stack = array(); + protected $state = array(); + + // *** + // Debugging functions + public function debuggingOn() { return $GLOBALS[ 'OPTIONS' ][ 'debug' ]; } + public function debugMessage( $msg ) { + if ( $this->debuggingOn() ) { + fwrite( STDERR, date( DATE_RFC2822 ) . ": {$msg}\n" ); + } + } + public function debugAssert( $condition, $desc ) { + if ( $this->debuggingOn() && $condition == FALSE ) { + $this->debugMessage( "ASSERTION FAILURE for '{$desc}'.\nBacktrace:" ); + $this->debugMessage( print_r( array_slice( debug_backtrace(), 1 ), 1 ) ); + exit( 1 ); + } + } + + // *** + // Constructor and destructor + public function __construct( $file ) { + global $OPTIONS; - public function __construct( $file, $encoding = "utf-8", $options = NULL ) { - - if ( !parent::open( $file, $encoding, $options ) ) { + if ( !parent::open( $file, NULL, 0 ) ) { throw new Exception(); } + // Position the reader at the first node. $this->read(); + + // Grab the output format. + require dirname( __FILE__ ) . "/../formats/{$OPTIONS[ 'output_format' ]}.php"; + $formatClassName = "PhD_" . str_replace( '-', '_', $OPTIONS[ 'output_format' ] ) . '_Format'; + $this->outputFormat = new $formatClassName( $this ); + $this->debugMessage( "Loaded output format '{$OPTIONS[ 'output_format' ]}' using class '{$formatClassName}'." ); + + // Grab the output theme. + require dirname( __FILE__ ) . "/../themes/{$OPTIONS[ 'output_theme' ]}/theme.php"; + $themeClassName = "PhD_" . str_replace( '-', '_', $OPTIONS[ 'output_theme' ] ) . '_Theme'; + $this->outputTheme = new $themeClassName( $this ); + $this->debugMessage( "Loaded output theme '{$OPTIONS[ 'output_theme' ]}' using class '{$themeClassName}'." ); + + // TODO: Set up the output encoding if necessary. } @@ -37,31 +122,18 @@ abstract class PhDReader extends XMLReader { } // *** - // Format subclasses must implement these to make them real formats. - // THIS IS THE OFFICIAL OUTPUT FORMAT INTERFACE. - - // proto string getFormatName( void ) - // Return the name of the format. - abstract public function getFormatName(); - - // proto string transformNode( string name, int type, string &output ) - // Transform a given node, returning the binary string output. Binary - // strings ARE handled safely. This function will be called for all - // element, text, cdata, entity reference, and end element nodes. It - // is always valid for this method to make the parser move around in - // the file. Return TRUE to create a chunk boundary, FALSE otherwise. - abstract protected function transformNode( $name, $type, &$output ); - - // *** // Protected methods (intended for internal and subclass use only) + // *** + // Public methods + // proto array getAllAttributes( void ) // Return all the attributes in the current element node as name:ns => // value pairs. Prefer the getAttribute*() methods defined by XMLReader // when possible; use this only when you really do need all the // attributes. An element without any attributes will result in an empty // array, while a non-element node will result in a return of FALSE. - protected function getAttributes() { + public function getAttributes() { $type = $this->nodeType; if ( $type != XMLReader::ELEMENT && $type != XMLReader::END_ELEMENT ) { @@ -83,7 +155,7 @@ abstract class PhDReader extends XMLReader { // proto string getID( void ) // Get the ID of the current element. Works on element and end element // nodes only, returning an empty string in all other cases. - protected function getID() { + public function getID() { if ( $this->hasAttributes && $this->moveToAttributeNs( "id", self::XMLNS_XML ) ) { $id = $this->value; @@ -93,49 +165,23 @@ abstract class PhDReader extends XMLReader { return ""; } - - // protected void pushStack( mixed value ) - // Push a value of any kind onto the parser stack. The stack is not used - // by the parser; it is intended as a cheap data store for formats and - // themes. - protected function pushStack( $value ) { - - array_push( $this->stack, $value ); - - } - - // protected mixed stackTop( void ) - // Return the top value on the stack. - protected function stackTop() { - - return count( $this->stack ) ? $this->stack[ 0 ] : NULL; - - } - - // protected mixed popStack( void ) - // Pop the top value off the stack and return it. - protected function popStack() { - - return array_pop( $this->stack ); - - } - - // *** - // Public methods - + // proto bool seek( string id ) // Seek to an ID. This is used to start the parser somewhere that isn't at // the beginning (duh). Be careful; this does not cause the parser to halt // at the closing element of a successful seek. Don't forget to check the // return value. public function seek( $id ) { - + + $this->debugMessage( "Starting seek to {$id}..." ); while( parent::read() ) { if ( $this->nodeType === XMLREADER::ELEMENT && $this->hasAttributes && $this->moveToAttributeNs( "id", self::XMLNS_XML ) && $this->value === $id ) { + $this->debugMessage( "Seek complete." ); return $this->moveToElement(); } } + $this->debugMessage( "Seek failed." ); return FALSE; } @@ -145,11 +191,11 @@ abstract class PhDReader extends XMLReader { // format's chunker. Returns the tree, or FALSE on error. public function transform() { - $allData = ''; - while ( ( $data = $this->transformChunk() ) !== FALSE ) { + $allData = $this->outputTheme->chunkHeader(); + while ( $this->transformChunk( $data, FALSE ) ) { $allData .= $data; } - return $allData; + return $allData . $this->outputTheme->chunkFooter(); } @@ -157,26 +203,27 @@ abstract class PhDReader extends XMLReader { // Transform nodes until the output format says it's time to output a // chunking boundary or the parser runs out of data. Returns TRUE on // success, FALSE on EOF. $data contains the transformed data, if any. - public function transformChunk( &$outData ) { + public function transformChunk( &$outData, $applyHeaderFooter = TRUE ) { global $OPTIONS; $hasMore = TRUE; $data = fopen( "php://temp/maxmemory:{$OPTIONS[ 'chunking_memory_limit' ]}", "r+" ); + fwrite( $data, $this->outputTheme->chunkHeader() ); $isChunk = FALSE; do { $nodeName = $this->name; $nodeType = $this->nodeType; switch ( $nodeType ) { - case XMLReader::NONE: - break; - case XMLReader::ELEMENT: case XMLReader::END_ELEMENT: case XMLReader::TEXT: case XMLReader::CDATA: case XMLReader::ENTITY_REF: - $isChunk = $this->transformNode( $nodeName, $nodeType, $output ); + $isChunk = $this->outputTheme->transformNode( $nodeName, $nodeType, $this->outputFormat, $output ); fwrite( $data, $output ); + if ( $isChunk ) { + fwrite( $data, $this->outputTheme->chunkFooter() ); + } break; case XMLReader::ENTITY: @@ -189,8 +236,12 @@ abstract class PhDReader extends XMLReader { case XMLReader::SIGNIFICANT_WHITESPACE: case XMLReader::END_ENTITY: case XMLReader::XML_DECLARATION: + case XMLReader::COMMENT: // Eat it for lunch. break; + + default: + die( "Unknown node type {$nodeType} while transforming. Can not continue." ); } $hasMore = $this->read(); } while ( !$isChunk && $hasMore ); @@ -203,43 +254,25 @@ abstract class PhDReader extends XMLReader { } /* - public function getID() { - if ( $this->hasAttributes && $this->moveToAttributeNs("id", self::XMLNS_XML) ) { - $id = $this->value; - $this->moveToElement(); - return $id; - } - return ""; - } - 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; - + 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; @@ -249,28 +282,9 @@ abstract class PhDReader extends XMLReader { $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 notXPath( $tag ) { $depth = $this->depth; @@ -284,54 +298,6 @@ abstract class PhDReader extends XMLReader { return $tag; } - - public function transform() { - - $type = $this->nodeType; - $name = $this->name; - - switch( $type ) { - - case XMLReader::ELEMENT: - $this->STACK[ $this->depth ] = $name; - - case XMLReader::END_ELEMENT: - $funcname = "format_$name"; - if ( isset( $this->map[ $name ] ) ) { - $tag = $this->map[ $name ]; - if ( is_array( $tag ) ) { - $tag = $this->notXPath( $tag ); - } - if ( strncmp( $tag, "format_", 7 ) ) { - return $this->transormFromMap( $type == XMLReader::ELEMENT, $tag, $name ); - } - $funcname = $tag; - } - return call_user_func( array( $this, $funcname ), $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 - // XXX This could lead to a recursion overflow if a lot of comment nodes get strung together. - $this->read(); - return $this->transform(); - - default: - trigger_error( "Dunno what to do with {$this->name} {$this->nodeType}", E_USER_ERROR ); - return ""; - } - - } */ } diff --git a/themes/default/theme.php b/themes/default/theme.php new file mode 100644 index 0000000..e69de29 diff --git a/themes/phd/theme.php b/themes/phd/theme.php new file mode 100644 index 0000000..e69de29 diff --git a/themes/php/theme.php b/themes/php/theme.php new file mode 100644 index 0000000..85946f8 --- /dev/null +++ b/themes/php/theme.php @@ -0,0 +1,57 @@ +<?php + +/* $Id$ + +-------------------------------------------------------------------------+ + | Copyright(c) 2007 | + | Authors: | + | Gwynne Raskind <gwynne@php.net> | + | Hannes Magnusson <bjori@php.net> | + | This source file is subject to the license that is bundled with this | + | package in the file LICENSE, and is available through the | + | world-wide-web at the following url: | + | http://phd.php.net/LICENSE | + +-------------------------------------------------------------------------+ + | The XHTML output format class. This should not be instantiated | + | directly; it is intended for extension by a theme class. | + | XXX This is temporarily untrue for "let's get it started" purposes. | + +-------------------------------------------------------------------------+ +*/ + +/* Grab the PhDReader parent class. */ +require_once 'include/PhDReader.class.php'; + +class PhD_php_Theme implements PhD_OutputTheme { + + protected $reader = NULL; + + public function __construct( $reader ) { + + $this->reader = $reader; + + } + + public function __destruct() { + } + + public function getThemeName() { + + return 'PHPweb Theme for PhD'; + + } + + public function transformNode( $name, $type, $formatter, &$output ) { + return $formatter->transformNode( $name, $type, $output ); + } + + public function chunkHeader() { + return ''; + } + + public function chunkFooter() { + return ''; + } + +} + +?> + diff --git a/themes/phpinternals/theme.php b/themes/phpinternals/theme.php new file mode 100644 index 0000000..e69de29 commit da517c2aaea6412842f2dbe71cea2e3c80103788 Author: Gwynne Raskind <gwynne@php.net> Date: Sun Jul 29 14:31:54 2007 +0000 Committed to my branch ONLY. This is my vision of some of what should be going on in an output format, though a lot of this should be in a theme really. Discussion versus Hannes' method is requested :). diff --git a/build.php b/build.php index b361b74..daac25e 100644 --- a/build.php +++ b/build.php @@ -1,15 +1,36 @@ <?php +error_reporting( E_ALL | E_STRICT ); + require_once 'config.php'; require_once 'formats/xhtml.php'; -$phd = new PhDXHTMLReader( "${OPTIONS[ 'xml_root' ]}/.manual.xml" ); -$phd->seek( "function.dotnet-load" ); -echo date( DATE_RSS )." done seeking\n"; +file_put_contents( dirname( __FILE__ ) . "/temp.xml", <<<~XML +<?xml version="1.0" encoding="utf-8"?> + +<book> + <part id="part1"> + <chapter id="chap1"> + Using the application <application>autoconf</application>, we can do some + fun stuff, since <command>php</command> does other fun things, and dolor + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, + quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo + consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse + cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat + non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + </chapter> + </part> +</book> +XML + ); -ob_start(); -while( $phd->nextNode() ) { - print $phd->transform(); +$phd = new PhDReader_XHTML( dirname( __FILE__ ) . "/temp.xml", NULL, 2 ); + +while ( $phd->transformChunk( $chunk ) ) { + print "{$chunk}\n"; } +print "{$chunk}\n"; + $phd->close(); ?> diff --git a/formats/xhtml.php b/formats/xhtml.php index 162d927..2846615 100644 --- a/formats/xhtml.php +++ b/formats/xhtml.php @@ -20,53 +20,7 @@ /* Grab the PhDReader parent class. */ require_once 'include/PhDReader.class.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', - 'partintro' => 'div', - 'productname' => 'span', - 'propname' => 'span', - 'property' => 'span', - 'proptype' => 'span', - 'section' => 'div', - 'simplelist' => 'ul', - 'simpara' => 'p', - 'title' => array( - /* DEFAULT */ 'h1', - 'refsect1' => 'h3', - 'example' => 'h4', - ), - 'year' => 'span', - ); - protected $CURRENT_FUNCTION_ID = ""; - protected $CURRENT_REFERENCE_ID = ""; - protected $functionList = array(); +class PhDReader_XHTML extends PhDReader { public function __construct( $file, $encoding = 'utf-8', $options = NULL ) { parent::__construct( $file, $encoding, $options ); @@ -80,51 +34,153 @@ class PhDXHTMLReader extends PhDReader { return 'XHTML 1.0 Transitional'; } - - public function format_refentry( $open ) { - - if ( $open ) { - $this->CURRENT_FUNCTION_ID = $id = $this->getID(); - - return sprintf( '<div id="%s" class="refentry">', $id ); - } - $this->CURRENT_FUNCTION_ID = ""; - return "</div>"; - } - public function format_reference( $open ) { - if ( $open ) { - $this->CURRENT_REFERENCE_ID = $id = $this->getID(); - - return sprintf( '<div id="%s" class="reference">', $id ); - - } - $content = "</div>"; - $content .= '<ul class="funclist">'; - foreach( $this->functionList as $func => $desc ) { - $content .= sprintf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); - } - $content .= "</ul>\n"; - $this->CURRENT_REFERENCE_ID = ""; - $this->functionList = array(); - return $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"; + protected function transformNode( $name, $type, &$output ) { + + switch ( $type ) { + + case XMLReader::ELEMENT: + case XMLReader::END_ELEMENT: + return $this->processElement( $name, $type == XMLReader::ELEMENT, $output ); + break; + + case XMLReader::TEXT: + $output = $this->value; + return FALSE; + + case XMLReader::CDATA: + $output = $this->processCDATA( $this->value ); + return FALSE; + + case XMLReader::ENTITY_REF: + $output = '<div class="error">WARNING: UNRESOLVED ENTITY '.htmlspecialchars( $name ).'</div>'; + return FALSE; + + } + + } + + protected function processElement( $name, $isOpen, &$output ) { + static $handlerMap = NULL; + + if ( is_null( $handlerMap ) ) { + $spanName = array( '<span class="%n%">', FALSE, '</span>', FALSE ); + $divName = array( '<div class="%n%">', FALSE, '</div>', FALSE ); + $divNameChunked = array( '<div class="%n%">', FALSE, '</div>', TRUE ); + $oneToOne = array( '<%n%>', FALSE, '</%n%>', FALSE ); + + $handlerMap = array( + 'application' => $spanName, + 'classname' => $spanName, + 'code' => $oneToOne, + 'collab' => $spanName, + 'collabname' => $spanName, + 'command' => $spanName, + 'computerOutput' => $spanName, + 'constant' => $spanName, + 'emphasis' => $oneToOne, + 'enumname' => $spanName, + 'envar' => $spanName, + 'filename' => $spanName, + 'glossterm' => $spanName, + 'holder' => $spanName, + 'informatlable' => array( '<table>', FALSE, '</table>', FALSE ), + 'itemizedlist' => array( '<ul>', FALSE, '</ul>', FALSE ), + 'listitem' => array( '<li>', FALSE, '</li>', FALSE ), + 'literal' => $spanName, + 'mediaobject' => $divName, + 'methodparam' => $spanName, + 'member' => array( '<li>', FALSE, '</li>', FALSE ), + 'note' => $divName, + 'option' => $spanName, + 'orderedlist' => array( '<ol>', FALSE, '</ol>', FALSE ), + 'para' => array( '<p>', FALSE, '</p>', FALSE ), + 'parameter' => $spanName, + 'partintro' => $divName, + 'productname' => $spanName, + 'propname' => $spanName, + 'property' => $spanName, + 'proptype' => $spanName, + 'section' => $divNameChunked, + 'simplelist' => array( '<ul>', FALSE, '</ul>', FALSE ), + 'simpara' => array( '<p>', FALSE, '</p>', FALSE ), + 'title' => array( 'checkparentname', array( '__default' => 'h1', 'refsect1' => 'h3', 'example' => 'h4' ) ), + 'year' => $spanName, + 'refentry' => array( '<div id="%i%" class="refentry">', FALSE, '</div>', TRUE, TRUE ), + 'reference' => array( $this, 'format_reference' ), + 'function' => array( '<a href="function.%v%.html">', FALSE, '</a>', FALSE ), + 'refsect1' => array( '<div class="refsect_%r%">', FALSE, '</div>', FALSE ), + '__default' => array( $this, 'unknownElement' ), + ); + } + + $mapping = isset( $handlerMap[ $name ] ) ? $handlerMap[ $name ] : $handlerMap[ '__default' ]; + if ( is_array( $mapping ) ) { + if ( is_string( $mapping[ 0 ] ) ) { + switch ( $mapping[ 0 ] ) { + case 'checkparentname': + $output = '<div class="warning">NOT IMPLEMENTED YET.</div>'; + return FALSE; + default: + $id = $this->getID(); + $output = $this->formatMappingString( $name, $id, $isOpen ? $mapping[ 0 ] : $mapping[ 2 ] ); + if ( !empty( $mapping[ 4 ] ) ) { + $this->pushStack( $id ); + } + return ( $isOpen ? $mapping[ 1 ] : $mapping[ 3 ] ); + } + } else if ( is_callable( $mapping ) ) { + return call_user_func( $mapping, $name, $isOpen, &$output ); + } + } else if ( is_string( $mapping ) ) { + if ( $isOpen ) { + $output = $this->formatMappingString( $name, $this->getID(), $mapping ); + } else { + $output = ''; + } + return FALSE; + } + $output = '<div class="warning">Bad handler string for '.$name.'!</div>'; + return FALSE; - } + } + + protected function processCDATA( $content ) { + + return '<div class="phpcode">' . highlight_string( $content ) . '</div>'; + + } + + protected function formatMappingString( $name, $id, $string ) { + + // XXX Yes, this needs heavy optimization, it's example for now. + return str_replace( array( '%n%', '%i%', '%v%', '%r' ), + array( $name, $id, $this->readInnerXML(), $this->getAttribute( 'role' ) ), + $string ); + + } + + protected function format_reference( $name, $isOpen, $output ) { + if ( $isOpen ) { + $output = sprintf( '<div id="%s" class="reference">', $this->getID() ); + return FALSE; + } + $output = '</div>' . + '<ul class="funclist">'; + foreach ( $this->popStack() as $func => $desc ) { + $output .= sptrinf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); + } + $output .= '</ul>'; + return TRUE; + } + + protected function unknownElement( $name, $isOpen, $output ) { + $output = "Can't handle a {$name}.\n"; + return FALSE; + + } +/* public function format_link( $open ) { $this->moveToNextAttribute(); @@ -137,7 +193,7 @@ class PhDXHTMLReader extends PhDReader { public function format_methodsynopsis( $open ) { - /* We read this element to END_ELEMENT so $open is useless */ + /* We read this element to END_ELEMENT so $open is useless * $content = '<div class="methodsynopsis">'; $root = $this->name; @@ -182,56 +238,7 @@ class PhDXHTMLReader extends PhDReader { return sprintf( '<div class="refnamediv"><span class="refname">%s</span><span class="refpurpose">%s</span></div>', $refname, $refpurpose ); } - protected function transormFromMap($open, $tag, $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( - '&nbsp;', - '<font color="', // for PHP 4 - '<span style="color: ', // for PHP 5.0.0RC1 - '</font>', - "\n ", - ' ' - ), - array( - ' ', - '<span class="', - '<span class="', - '</span>', - "\n&nbsp;", - ' &nbsp;' - ), - 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 ); - - } - +*/ } /* diff --git a/include/PhDReader.class.php b/include/PhDReader.class.php index dd230ee..758de85 100644 --- a/include/PhDReader.class.php +++ b/include/PhDReader.class.php @@ -22,27 +22,112 @@ abstract class PhDReader extends XMLReader { const XMLNS_XLINK = "http://www.w3.org/1999/xlinK"; const XMLNS_PHD = "http://www.php.net/ns/phd"; - protected $map = array(); - protected $STACK = array(); + protected $stack = array(); public function __construct( $file, $encoding = "utf-8", $options = NULL ) { if ( !parent::open( $file, $encoding, $options ) ) { throw new Exception(); } + $this->read(); } - + public function __destruct() { } - /* Format subclasses must implement these to make them real formats. */ + // *** + // Format subclasses must implement these to make them real formats. + // THIS IS THE OFFICIAL OUTPUT FORMAT INTERFACE. + + // proto string getFormatName( void ) + // Return the name of the format. abstract public function getFormatName(); - abstract protected function transformFromMap( $open, $name ); + + // proto string transformNode( string name, int type, string &output ) + // Transform a given node, returning the binary string output. Binary + // strings ARE handled safely. This function will be called for all + // element, text, cdata, entity reference, and end element nodes. It + // is always valid for this method to make the parser move around in + // the file. Return TRUE to create a chunk boundary, FALSE otherwise. + abstract protected function transformNode( $name, $type, &$output ); + + // *** + // Protected methods (intended for internal and subclass use only) + + // proto array getAllAttributes( void ) + // Return all the attributes in the current element node as name:ns => + // value pairs. Prefer the getAttribute*() methods defined by XMLReader + // when possible; use this only when you really do need all the + // attributes. An element without any attributes will result in an empty + // array, while a non-element node will result in a return of FALSE. + protected function getAttributes() { + + $type = $this->nodeType; + if ( $type != XMLReader::ELEMENT && $type != XMLReader::END_ELEMENT ) { + return FALSE; + } + $attrCount = $this->attributeCount; + $attrs = array(); + if ( $attrCount > 0 ) { + for ( $i = 0; $i < $attrCount; ++$i ) { + $this->moveToAttributeNo( $i ); + $attrs[ $this->name ] = $this->value; + } + $this->moveToElement(); + } + return $attrs; + + } + + // proto string getID( void ) + // Get the ID of the current element. Works on element and end element + // nodes only, returning an empty string in all other cases. + protected function getID() { + + if ( $this->hasAttributes && $this->moveToAttributeNs( "id", self::XMLNS_XML ) ) { + $id = $this->value; + $this->moveToElement(); + return $id; + } + return ""; + + } + + // protected void pushStack( mixed value ) + // Push a value of any kind onto the parser stack. The stack is not used + // by the parser; it is intended as a cheap data store for formats and + // themes. + protected function pushStack( $value ) { + + array_push( $this->stack, $value ); + + } + + // protected mixed stackTop( void ) + // Return the top value on the stack. + protected function stackTop() { + + return count( $this->stack ) ? $this->stack[ 0 ] : NULL; + + } + + // protected mixed popStack( void ) + // Pop the top value off the stack and return it. + protected function popStack() { + + return array_pop( $this->stack ); + + } - /* These are new functions, extending XMLReader. */ + // *** + // Public methods - /* Seek to an ID within the file. */ + // proto bool seek( string id ) + // Seek to an ID. This is used to start the parser somewhere that isn't at + // the beginning (duh). Be careful; this does not cause the parser to halt + // at the closing element of a successful seek. Don't forget to check the + // return value. public function seek( $id ) { while( parent::read() ) { @@ -54,6 +139,70 @@ abstract class PhDReader extends XMLReader { return FALSE; } + + // proto string transform( void ) + // Transform the whole tree as one giant chunk, IGNORING the output + // format's chunker. Returns the tree, or FALSE on error. + public function transform() { + + $allData = ''; + while ( ( $data = $this->transformChunk() ) !== FALSE ) { + $allData .= $data; + } + return $allData; + + } + + // proto bool transformChunk( string &outData ) + // Transform nodes until the output format says it's time to output a + // chunking boundary or the parser runs out of data. Returns TRUE on + // success, FALSE on EOF. $data contains the transformed data, if any. + public function transformChunk( &$outData ) { + global $OPTIONS; + + $hasMore = TRUE; + $data = fopen( "php://temp/maxmemory:{$OPTIONS[ 'chunking_memory_limit' ]}", "r+" ); + $isChunk = FALSE; + do { + $nodeName = $this->name; + $nodeType = $this->nodeType; + switch ( $nodeType ) { + case XMLReader::NONE: + break; + + case XMLReader::ELEMENT: + case XMLReader::END_ELEMENT: + case XMLReader::TEXT: + case XMLReader::CDATA: + case XMLReader::ENTITY_REF: + $isChunk = $this->transformNode( $nodeName, $nodeType, $output ); + fwrite( $data, $output ); + break; + + case XMLReader::ENTITY: + case XMLReader::PI: + case XMLReader::DOC_TYPE: + case XMLReader::DOC: + case XMLReader::DOC_FRAGMENT: + case XMLReader::NOTATION: + case XMLReader::WHITESPACE: + case XMLReader::SIGNIFICANT_WHITESPACE: + case XMLReader::END_ENTITY: + case XMLReader::XML_DECLARATION: + // Eat it for lunch. + break; + } + $hasMore = $this->read(); + } while ( !$isChunk && $hasMore ); + + rewind( $data ); + $outData = stream_get_contents( $data ); + fclose( $data ); + return $hasMore; + + } + +/* public function getID() { if ( $this->hasAttributes && $this->moveToAttributeNs("id", self::XMLNS_XML) ) { $id = $this->value; @@ -63,7 +212,6 @@ abstract class PhDReader extends XMLReader { return ""; } - /* Go to the next useful node in the file. */ public function nextNode() { while( $this->read() ) { @@ -84,14 +232,12 @@ abstract class PhDReader extends XMLReader { } - /* Read a node with the right name? */ public function readNode( $nodeName ) { return $this->read() && !( $this->nodeType == XMLReader::END_ELEMENT && $this->name == $nodeName ); } - /* Get the content of a named node, or the current node. */ public function readContent( $node = NULL ) { $retval = ""; @@ -106,25 +252,23 @@ abstract class PhDReader extends XMLReader { } - /* Get the attribute value by name, if exists. */ public function readAttribute( $attr ) { return $this->moveToAttribute( $attr ) ? $this->value : ""; } - /* Handle unmapped nodes. */ public function __call( $func, $args ) { if ( $this->nodeType == XMLReader::END_ELEMENT ) { - /* ignore */ return; + /* 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 ""; } @@ -141,7 +285,6 @@ abstract class PhDReader extends XMLReader { return $tag; } - /* Perform a transformation. */ public function transform() { $type = $this->nodeType; @@ -178,8 +321,8 @@ abstract class PhDReader extends XMLReader { case XMLReader::COMMENT: case XMLReader::WHITESPACE: case XMLReader::SIGNIFICANT_WHITESPACE: - /* swallow it */ - /* XXX This could lead to a recursion overflow if a lot of comment nodes get strung together. */ + // swallow it + // XXX This could lead to a recursion overflow if a lot of comment nodes get strung together. $this->read(); return $this->transform(); @@ -189,7 +332,7 @@ abstract class PhDReader extends XMLReader { } } - +*/ } /* diff --git a/phpdotnet/phd/Format/XHTML.php b/phpdotnet/phd/Format/XHTML.php index 162d927..2846615 100644 --- a/phpdotnet/phd/Format/XHTML.php +++ b/phpdotnet/phd/Format/XHTML.php @@ -20,53 +20,7 @@ /* Grab the PhDReader parent class. */ require_once 'include/PhDReader.class.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', - 'partintro' => 'div', - 'productname' => 'span', - 'propname' => 'span', - 'property' => 'span', - 'proptype' => 'span', - 'section' => 'div', - 'simplelist' => 'ul', - 'simpara' => 'p', - 'title' => array( - /* DEFAULT */ 'h1', - 'refsect1' => 'h3', - 'example' => 'h4', - ), - 'year' => 'span', - ); - protected $CURRENT_FUNCTION_ID = ""; - protected $CURRENT_REFERENCE_ID = ""; - protected $functionList = array(); +class PhDReader_XHTML extends PhDReader { public function __construct( $file, $encoding = 'utf-8', $options = NULL ) { parent::__construct( $file, $encoding, $options ); @@ -80,51 +34,153 @@ class PhDXHTMLReader extends PhDReader { return 'XHTML 1.0 Transitional'; } - - public function format_refentry( $open ) { - - if ( $open ) { - $this->CURRENT_FUNCTION_ID = $id = $this->getID(); - - return sprintf( '<div id="%s" class="refentry">', $id ); - } - $this->CURRENT_FUNCTION_ID = ""; - return "</div>"; - } - public function format_reference( $open ) { - if ( $open ) { - $this->CURRENT_REFERENCE_ID = $id = $this->getID(); - - return sprintf( '<div id="%s" class="reference">', $id ); - - } - $content = "</div>"; - $content .= '<ul class="funclist">'; - foreach( $this->functionList as $func => $desc ) { - $content .= sprintf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); - } - $content .= "</ul>\n"; - $this->CURRENT_REFERENCE_ID = ""; - $this->functionList = array(); - return $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"; + protected function transformNode( $name, $type, &$output ) { + + switch ( $type ) { + + case XMLReader::ELEMENT: + case XMLReader::END_ELEMENT: + return $this->processElement( $name, $type == XMLReader::ELEMENT, $output ); + break; + + case XMLReader::TEXT: + $output = $this->value; + return FALSE; + + case XMLReader::CDATA: + $output = $this->processCDATA( $this->value ); + return FALSE; + + case XMLReader::ENTITY_REF: + $output = '<div class="error">WARNING: UNRESOLVED ENTITY '.htmlspecialchars( $name ).'</div>'; + return FALSE; + + } + + } + + protected function processElement( $name, $isOpen, &$output ) { + static $handlerMap = NULL; + + if ( is_null( $handlerMap ) ) { + $spanName = array( '<span class="%n%">', FALSE, '</span>', FALSE ); + $divName = array( '<div class="%n%">', FALSE, '</div>', FALSE ); + $divNameChunked = array( '<div class="%n%">', FALSE, '</div>', TRUE ); + $oneToOne = array( '<%n%>', FALSE, '</%n%>', FALSE ); + + $handlerMap = array( + 'application' => $spanName, + 'classname' => $spanName, + 'code' => $oneToOne, + 'collab' => $spanName, + 'collabname' => $spanName, + 'command' => $spanName, + 'computerOutput' => $spanName, + 'constant' => $spanName, + 'emphasis' => $oneToOne, + 'enumname' => $spanName, + 'envar' => $spanName, + 'filename' => $spanName, + 'glossterm' => $spanName, + 'holder' => $spanName, + 'informatlable' => array( '<table>', FALSE, '</table>', FALSE ), + 'itemizedlist' => array( '<ul>', FALSE, '</ul>', FALSE ), + 'listitem' => array( '<li>', FALSE, '</li>', FALSE ), + 'literal' => $spanName, + 'mediaobject' => $divName, + 'methodparam' => $spanName, + 'member' => array( '<li>', FALSE, '</li>', FALSE ), + 'note' => $divName, + 'option' => $spanName, + 'orderedlist' => array( '<ol>', FALSE, '</ol>', FALSE ), + 'para' => array( '<p>', FALSE, '</p>', FALSE ), + 'parameter' => $spanName, + 'partintro' => $divName, + 'productname' => $spanName, + 'propname' => $spanName, + 'property' => $spanName, + 'proptype' => $spanName, + 'section' => $divNameChunked, + 'simplelist' => array( '<ul>', FALSE, '</ul>', FALSE ), + 'simpara' => array( '<p>', FALSE, '</p>', FALSE ), + 'title' => array( 'checkparentname', array( '__default' => 'h1', 'refsect1' => 'h3', 'example' => 'h4' ) ), + 'year' => $spanName, + 'refentry' => array( '<div id="%i%" class="refentry">', FALSE, '</div>', TRUE, TRUE ), + 'reference' => array( $this, 'format_reference' ), + 'function' => array( '<a href="function.%v%.html">', FALSE, '</a>', FALSE ), + 'refsect1' => array( '<div class="refsect_%r%">', FALSE, '</div>', FALSE ), + '__default' => array( $this, 'unknownElement' ), + ); + } + + $mapping = isset( $handlerMap[ $name ] ) ? $handlerMap[ $name ] : $handlerMap[ '__default' ]; + if ( is_array( $mapping ) ) { + if ( is_string( $mapping[ 0 ] ) ) { + switch ( $mapping[ 0 ] ) { + case 'checkparentname': + $output = '<div class="warning">NOT IMPLEMENTED YET.</div>'; + return FALSE; + default: + $id = $this->getID(); + $output = $this->formatMappingString( $name, $id, $isOpen ? $mapping[ 0 ] : $mapping[ 2 ] ); + if ( !empty( $mapping[ 4 ] ) ) { + $this->pushStack( $id ); + } + return ( $isOpen ? $mapping[ 1 ] : $mapping[ 3 ] ); + } + } else if ( is_callable( $mapping ) ) { + return call_user_func( $mapping, $name, $isOpen, &$output ); + } + } else if ( is_string( $mapping ) ) { + if ( $isOpen ) { + $output = $this->formatMappingString( $name, $this->getID(), $mapping ); + } else { + $output = ''; + } + return FALSE; + } + $output = '<div class="warning">Bad handler string for '.$name.'!</div>'; + return FALSE; - } + } + + protected function processCDATA( $content ) { + + return '<div class="phpcode">' . highlight_string( $content ) . '</div>'; + + } + + protected function formatMappingString( $name, $id, $string ) { + + // XXX Yes, this needs heavy optimization, it's example for now. + return str_replace( array( '%n%', '%i%', '%v%', '%r' ), + array( $name, $id, $this->readInnerXML(), $this->getAttribute( 'role' ) ), + $string ); + + } + + protected function format_reference( $name, $isOpen, $output ) { + if ( $isOpen ) { + $output = sprintf( '<div id="%s" class="reference">', $this->getID() ); + return FALSE; + } + $output = '</div>' . + '<ul class="funclist">'; + foreach ( $this->popStack() as $func => $desc ) { + $output .= sptrinf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); + } + $output .= '</ul>'; + return TRUE; + } + + protected function unknownElement( $name, $isOpen, $output ) { + $output = "Can't handle a {$name}.\n"; + return FALSE; + + } +/* public function format_link( $open ) { $this->moveToNextAttribute(); @@ -137,7 +193,7 @@ class PhDXHTMLReader extends PhDReader { public function format_methodsynopsis( $open ) { - /* We read this element to END_ELEMENT so $open is useless */ + /* We read this element to END_ELEMENT so $open is useless * $content = '<div class="methodsynopsis">'; $root = $this->name; @@ -182,56 +238,7 @@ class PhDXHTMLReader extends PhDReader { return sprintf( '<div class="refnamediv"><span class="refname">%s</span><span class="refpurpose">%s</span></div>', $refname, $refpurpose ); } - protected function transormFromMap($open, $tag, $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( - '&nbsp;', - '<font color="', // for PHP 4 - '<span style="color: ', // for PHP 5.0.0RC1 - '</font>', - "\n ", - ' ' - ), - array( - ' ', - '<span class="', - '<span class="', - '</span>', - "\n&nbsp;", - ' &nbsp;' - ), - 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 ); - - } - +*/ } /* diff --git a/phpdotnet/phd/Reader/Legacy.php b/phpdotnet/phd/Reader/Legacy.php index dd230ee..758de85 100644 --- a/phpdotnet/phd/Reader/Legacy.php +++ b/phpdotnet/phd/Reader/Legacy.php @@ -22,27 +22,112 @@ abstract class PhDReader extends XMLReader { const XMLNS_XLINK = "http://www.w3.org/1999/xlinK"; const XMLNS_PHD = "http://www.php.net/ns/phd"; - protected $map = array(); - protected $STACK = array(); + protected $stack = array(); public function __construct( $file, $encoding = "utf-8", $options = NULL ) { if ( !parent::open( $file, $encoding, $options ) ) { throw new Exception(); } + $this->read(); } - + public function __destruct() { } - /* Format subclasses must implement these to make them real formats. */ + // *** + // Format subclasses must implement these to make them real formats. + // THIS IS THE OFFICIAL OUTPUT FORMAT INTERFACE. + + // proto string getFormatName( void ) + // Return the name of the format. abstract public function getFormatName(); - abstract protected function transformFromMap( $open, $name ); + + // proto string transformNode( string name, int type, string &output ) + // Transform a given node, returning the binary string output. Binary + // strings ARE handled safely. This function will be called for all + // element, text, cdata, entity reference, and end element nodes. It + // is always valid for this method to make the parser move around in + // the file. Return TRUE to create a chunk boundary, FALSE otherwise. + abstract protected function transformNode( $name, $type, &$output ); + + // *** + // Protected methods (intended for internal and subclass use only) + + // proto array getAllAttributes( void ) + // Return all the attributes in the current element node as name:ns => + // value pairs. Prefer the getAttribute*() methods defined by XMLReader + // when possible; use this only when you really do need all the + // attributes. An element without any attributes will result in an empty + // array, while a non-element node will result in a return of FALSE. + protected function getAttributes() { + + $type = $this->nodeType; + if ( $type != XMLReader::ELEMENT && $type != XMLReader::END_ELEMENT ) { + return FALSE; + } + $attrCount = $this->attributeCount; + $attrs = array(); + if ( $attrCount > 0 ) { + for ( $i = 0; $i < $attrCount; ++$i ) { + $this->moveToAttributeNo( $i ); + $attrs[ $this->name ] = $this->value; + } + $this->moveToElement(); + } + return $attrs; + + } + + // proto string getID( void ) + // Get the ID of the current element. Works on element and end element + // nodes only, returning an empty string in all other cases. + protected function getID() { + + if ( $this->hasAttributes && $this->moveToAttributeNs( "id", self::XMLNS_XML ) ) { + $id = $this->value; + $this->moveToElement(); + return $id; + } + return ""; + + } + + // protected void pushStack( mixed value ) + // Push a value of any kind onto the parser stack. The stack is not used + // by the parser; it is intended as a cheap data store for formats and + // themes. + protected function pushStack( $value ) { + + array_push( $this->stack, $value ); + + } + + // protected mixed stackTop( void ) + // Return the top value on the stack. + protected function stackTop() { + + return count( $this->stack ) ? $this->stack[ 0 ] : NULL; + + } + + // protected mixed popStack( void ) + // Pop the top value off the stack and return it. + protected function popStack() { + + return array_pop( $this->stack ); + + } - /* These are new functions, extending XMLReader. */ + // *** + // Public methods - /* Seek to an ID within the file. */ + // proto bool seek( string id ) + // Seek to an ID. This is used to start the parser somewhere that isn't at + // the beginning (duh). Be careful; this does not cause the parser to halt + // at the closing element of a successful seek. Don't forget to check the + // return value. public function seek( $id ) { while( parent::read() ) { @@ -54,6 +139,70 @@ abstract class PhDReader extends XMLReader { return FALSE; } + + // proto string transform( void ) + // Transform the whole tree as one giant chunk, IGNORING the output + // format's chunker. Returns the tree, or FALSE on error. + public function transform() { + + $allData = ''; + while ( ( $data = $this->transformChunk() ) !== FALSE ) { + $allData .= $data; + } + return $allData; + + } + + // proto bool transformChunk( string &outData ) + // Transform nodes until the output format says it's time to output a + // chunking boundary or the parser runs out of data. Returns TRUE on + // success, FALSE on EOF. $data contains the transformed data, if any. + public function transformChunk( &$outData ) { + global $OPTIONS; + + $hasMore = TRUE; + $data = fopen( "php://temp/maxmemory:{$OPTIONS[ 'chunking_memory_limit' ]}", "r+" ); + $isChunk = FALSE; + do { + $nodeName = $this->name; + $nodeType = $this->nodeType; + switch ( $nodeType ) { + case XMLReader::NONE: + break; + + case XMLReader::ELEMENT: + case XMLReader::END_ELEMENT: + case XMLReader::TEXT: + case XMLReader::CDATA: + case XMLReader::ENTITY_REF: + $isChunk = $this->transformNode( $nodeName, $nodeType, $output ); + fwrite( $data, $output ); + break; + + case XMLReader::ENTITY: + case XMLReader::PI: + case XMLReader::DOC_TYPE: + case XMLReader::DOC: + case XMLReader::DOC_FRAGMENT: + case XMLReader::NOTATION: + case XMLReader::WHITESPACE: + case XMLReader::SIGNIFICANT_WHITESPACE: + case XMLReader::END_ENTITY: + case XMLReader::XML_DECLARATION: + // Eat it for lunch. + break; + } + $hasMore = $this->read(); + } while ( !$isChunk && $hasMore ); + + rewind( $data ); + $outData = stream_get_contents( $data ); + fclose( $data ); + return $hasMore; + + } + +/* public function getID() { if ( $this->hasAttributes && $this->moveToAttributeNs("id", self::XMLNS_XML) ) { $id = $this->value; @@ -63,7 +212,6 @@ abstract class PhDReader extends XMLReader { return ""; } - /* Go to the next useful node in the file. */ public function nextNode() { while( $this->read() ) { @@ -84,14 +232,12 @@ abstract class PhDReader extends XMLReader { } - /* Read a node with the right name? */ public function readNode( $nodeName ) { return $this->read() && !( $this->nodeType == XMLReader::END_ELEMENT && $this->name == $nodeName ); } - /* Get the content of a named node, or the current node. */ public function readContent( $node = NULL ) { $retval = ""; @@ -106,25 +252,23 @@ abstract class PhDReader extends XMLReader { } - /* Get the attribute value by name, if exists. */ public function readAttribute( $attr ) { return $this->moveToAttribute( $attr ) ? $this->value : ""; } - /* Handle unmapped nodes. */ public function __call( $func, $args ) { if ( $this->nodeType == XMLReader::END_ELEMENT ) { - /* ignore */ return; + /* 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 ""; } @@ -141,7 +285,6 @@ abstract class PhDReader extends XMLReader { return $tag; } - /* Perform a transformation. */ public function transform() { $type = $this->nodeType; @@ -178,8 +321,8 @@ abstract class PhDReader extends XMLReader { case XMLReader::COMMENT: case XMLReader::WHITESPACE: case XMLReader::SIGNIFICANT_WHITESPACE: - /* swallow it */ - /* XXX This could lead to a recursion overflow if a lot of comment nodes get strung together. */ + // swallow it + // XXX This could lead to a recursion overflow if a lot of comment nodes get strung together. $this->read(); return $this->transform(); @@ -189,7 +332,7 @@ abstract class PhDReader extends XMLReader { } } - +*/ } /* commit 347324e29b6b25112f82e10a69896d9881e28825 Author: Gwynne Raskind <gwynne@php.net> Date: Sun Jul 29 09:46:39 2007 +0000 Commit bjori's latest onto a separate branch. The branch was created so that I could mess around with some of my ideas without interfering with the mainstream development until we'd had a chance to discuss and approve them concurrently. diff --git a/formats/phpnetchunkedreader.php b/formats/phpnetchunkedreader.php new file mode 100644 index 0000000..d485280 --- /dev/null +++ b/formats/phpnetchunkedreader.php @@ -0,0 +1,50 @@ +<?php +class PHPNETChunkedReader extends PhDXHTMLReader { + protected $CURRENT_FUNCTION = ""; + + public function format_refentry( $open ) { + if ( $open ) { + $retval = parent::format_refentry( $open ); + $this->CURRENT_FUNCTION = str_replace( array( "function.", "_" ), array( "", "-" ), $this->CURRENT_FUNCTION_ID ); + ob_start(); + + return $retval; + } + $funcname = $this->CURRENT_FUNCTION; + + echo parent::format_refentry( $open ); + $content = ob_get_contents(); + ob_end_clean(); + + file_put_contents( "cache/function.{$funcname}.html", $content ); + + $this->CURRENT_FUNCTION = ""; + + return ""; + } + public function format_reference( $open ) { + if ( $open ) { + ob_start(); + return parent::format_reference( $open ); + } + $refid = $this->CURRENT_REFERENCE_ID; + echo parent::format_reference( $open ); + $content = ob_get_contents(); + ob_end_clean(); + + file_put_contents( "cache/{$refid}.html", $content ); + + return ""; + } + public function format_function( $open ) { + $func = $this->readContent(); + $func = str_replace( "_", "-", $func ); + + if ( $func == $this->CURRENT_FUNCTION ) { + return sprintf( "<b>%s()</b>", $func ); + } + + return sprintf( '<a href="function.%1$s.html">%1$s()</a>', $func ); + } +} + diff --git a/formats/xhtml.php b/formats/xhtml.php index 133a950..162d927 100644 --- a/formats/xhtml.php +++ b/formats/xhtml.php @@ -49,16 +49,25 @@ class PhDXHTMLReader extends PhDReader { 'orderedlist' => 'ol', 'para' => 'p', 'parameter' => 'span', + 'partintro' => 'div', 'productname' => 'span', 'propname' => 'span', 'property' => 'span', 'proptype' => 'span', + 'section' => 'div', 'simplelist' => 'ul', 'simpara' => 'p', - 'title' => 'h1', + 'title' => array( + /* DEFAULT */ 'h1', + 'refsect1' => 'h3', + 'example' => 'h4', + ), 'year' => 'span', ); - + protected $CURRENT_FUNCTION_ID = ""; + protected $CURRENT_REFERENCE_ID = ""; + protected $functionList = array(); + public function __construct( $file, $encoding = 'utf-8', $options = NULL ) { parent::__construct( $file, $encoding, $options ); } @@ -75,16 +84,29 @@ class PhDXHTMLReader extends PhDReader { public function format_refentry( $open ) { if ( $open ) { - return '<div>'; + $this->CURRENT_FUNCTION_ID = $id = $this->getID(); + + return sprintf( '<div id="%s" class="refentry">', $id ); } + $this->CURRENT_FUNCTION_ID = ""; + return "</div>"; + } + public function format_reference( $open ) { + if ( $open ) { + $this->CURRENT_REFERENCE_ID = $id = $this->getID(); + + return sprintf( '<div id="%s" class="reference">', $id ); - 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 ); + $content = "</div>"; + $content .= '<ul class="funclist">'; + foreach( $this->functionList as $func => $desc ) { + $content .= sprintf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); + } + $content .= "</ul>\n"; + $this->CURRENT_REFERENCE_ID = ""; + $this->functionList = array(); + return $content; } @@ -141,8 +163,26 @@ class PhDXHTMLReader extends PhDReader { return $content; } + public function format_refnamediv( $open ) { + $root = $this->name; + + while ( $this->readNode( $root ) ) { + $name = $this->name; + switch( $name ) { + case "refname": + $refname = $this->readContent( $name ); + break; + case "refpurpose": + $refpurpose = $this->readContent( $name ); + break; + } + } + + $this->functionList[ $refname ] = $refpurpose; + return sprintf( '<div class="refnamediv"><span class="refname">%s</span><span class="refpurpose">%s</span></div>', $refname, $refpurpose ); + } - protected function transformFromMap( $open, $name ) { + protected function transormFromMap($open, $tag, $name) { $tag = $this->map[ $name ]; if($open) { diff --git a/include/PhDReader.class.php b/include/PhDReader.class.php index 1007772..dd230ee 100644 --- a/include/PhDReader.class.php +++ b/include/PhDReader.class.php @@ -18,8 +18,12 @@ */ abstract class PhDReader extends XMLReader { + const XMLNS_XML = "http://www.w3.org/XML/1998/namespace"; + const XMLNS_XLINK = "http://www.w3.org/1999/xlinK"; + const XMLNS_PHD = "http://www.php.net/ns/phd"; protected $map = array(); + protected $STACK = array(); public function __construct( $file, $encoding = "utf-8", $options = NULL ) { @@ -42,15 +46,23 @@ abstract class PhDReader extends XMLReader { 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 ) { + if ( $this->nodeType === XMLREADER::ELEMENT && $this->hasAttributes && + $this->moveToAttributeNs( "id", self::XMLNS_XML ) && $this->value === $id ) { return $this->moveToElement(); } } return FALSE; } - + public function getID() { + if ( $this->hasAttributes && $this->moveToAttributeNs("id", self::XMLNS_XML) ) { + $id = $this->value; + $this->moveToElement(); + return $id; + } + return ""; + } + /* Go to the next useful node in the file. */ public function nextNode() { @@ -116,7 +128,19 @@ abstract class PhDReader extends XMLReader { return ""; } + public function notXPath( $tag ) { + $depth = $this->depth; + do { + if ( isset( $tag[ $this->STACK[ --$depth ] ] ) ) { + $tag = $tag[ $this->STACK[ $depth ] ]; + } else { + $tag = $tag[0]; + } + } while ( is_array( $tag ) ); + return $tag; + } + /* Perform a transformation. */ public function transform() { @@ -126,11 +150,21 @@ abstract class PhDReader extends XMLReader { switch( $type ) { case XMLReader::ELEMENT: + $this->STACK[ $this->depth ] = $name; + case XMLReader::END_ELEMENT: - if( isset( $this->map[ $name ] ) ) { - return $this->transformFromMap( $type == XMLReader::ELEMENT, $name ); - } - return call_user_func( array( $this, "format_${name}" ), $type == XMLReader::ELEMENT ); + $funcname = "format_$name"; + if ( isset( $this->map[ $name ] ) ) { + $tag = $this->map[ $name ]; + if ( is_array( $tag ) ) { + $tag = $this->notXPath( $tag ); + } + if ( strncmp( $tag, "format_", 7 ) ) { + return $this->transormFromMap( $type == XMLReader::ELEMENT, $tag, $name ); + } + $funcname = $tag; + } + return call_user_func( array( $this, $funcname ), $type == XMLReader::ELEMENT ); break; case XMLReader::TEXT: diff --git a/phpdotnet/phd/Format/XHTML.php b/phpdotnet/phd/Format/XHTML.php index 133a950..162d927 100644 --- a/phpdotnet/phd/Format/XHTML.php +++ b/phpdotnet/phd/Format/XHTML.php @@ -49,16 +49,25 @@ class PhDXHTMLReader extends PhDReader { 'orderedlist' => 'ol', 'para' => 'p', 'parameter' => 'span', + 'partintro' => 'div', 'productname' => 'span', 'propname' => 'span', 'property' => 'span', 'proptype' => 'span', + 'section' => 'div', 'simplelist' => 'ul', 'simpara' => 'p', - 'title' => 'h1', + 'title' => array( + /* DEFAULT */ 'h1', + 'refsect1' => 'h3', + 'example' => 'h4', + ), 'year' => 'span', ); - + protected $CURRENT_FUNCTION_ID = ""; + protected $CURRENT_REFERENCE_ID = ""; + protected $functionList = array(); + public function __construct( $file, $encoding = 'utf-8', $options = NULL ) { parent::__construct( $file, $encoding, $options ); } @@ -75,16 +84,29 @@ class PhDXHTMLReader extends PhDReader { public function format_refentry( $open ) { if ( $open ) { - return '<div>'; + $this->CURRENT_FUNCTION_ID = $id = $this->getID(); + + return sprintf( '<div id="%s" class="refentry">', $id ); } + $this->CURRENT_FUNCTION_ID = ""; + return "</div>"; + } + public function format_reference( $open ) { + if ( $open ) { + $this->CURRENT_REFERENCE_ID = $id = $this->getID(); + + return sprintf( '<div id="%s" class="reference">', $id ); - 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 ); + $content = "</div>"; + $content .= '<ul class="funclist">'; + foreach( $this->functionList as $func => $desc ) { + $content .= sprintf( '<li><a href="function.%1$s.html" class="refentry">%1$s</a></li>', $func ); + } + $content .= "</ul>\n"; + $this->CURRENT_REFERENCE_ID = ""; + $this->functionList = array(); + return $content; } @@ -141,8 +163,26 @@ class PhDXHTMLReader extends PhDReader { return $content; } + public function format_refnamediv( $open ) { + $root = $this->name; + + while ( $this->readNode( $root ) ) { + $name = $this->name; + switch( $name ) { + case "refname": + $refname = $this->readContent( $name ); + break; + case "refpurpose": + $refpurpose = $this->readContent( $name ); + break; + } + } + + $this->functionList[ $refname ] = $refpurpose; + return sprintf( '<div class="refnamediv"><span class="refname">%s</span><span class="refpurpose">%s</span></div>', $refname, $refpurpose ); + } - protected function transformFromMap( $open, $name ) { + protected function transormFromMap($open, $tag, $name) { $tag = $this->map[ $name ]; if($open) { diff --git a/phpdotnet/phd/Reader/Legacy.php b/phpdotnet/phd/Reader/Legacy.php index 1007772..dd230ee 100644 --- a/phpdotnet/phd/Reader/Legacy.php +++ b/phpdotnet/phd/Reader/Legacy.php @@ -18,8 +18,12 @@ */ abstract class PhDReader extends XMLReader { + const XMLNS_XML = "http://www.w3.org/XML/1998/namespace"; + const XMLNS_XLINK = "http://www.w3.org/1999/xlinK"; + const XMLNS_PHD = "http://www.php.net/ns/phd"; protected $map = array(); + protected $STACK = array(); public function __construct( $file, $encoding = "utf-8", $options = NULL ) { @@ -42,15 +46,23 @@ abstract class PhDReader extends XMLReader { 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 ) { + if ( $this->nodeType === XMLREADER::ELEMENT && $this->hasAttributes && + $this->moveToAttributeNs( "id", self::XMLNS_XML ) && $this->value === $id ) { return $this->moveToElement(); } } return FALSE; } - + public function getID() { + if ( $this->hasAttributes && $this->moveToAttributeNs("id", self::XMLNS_XML) ) { + $id = $this->value; + $this->moveToElement(); + return $id; + } + return ""; + } + /* Go to the next useful node in the file. */ public function nextNode() { @@ -116,7 +128,19 @@ abstract class PhDReader extends XMLReader { return ""; } + public function notXPath( $tag ) { + $depth = $this->depth; + do { + if ( isset( $tag[ $this->STACK[ --$depth ] ] ) ) { + $tag = $tag[ $this->STACK[ $depth ] ]; + } else { + $tag = $tag[0]; + } + } while ( is_array( $tag ) ); + return $tag; + } + /* Perform a transformation. */ public function transform() { @@ -126,11 +150,21 @@ abstract class PhDReader extends XMLReader { switch( $type ) { case XMLReader::ELEMENT: + $this->STACK[ $this->depth ] = $name; + case XMLReader::END_ELEMENT: - if( isset( $this->map[ $name ] ) ) { - return $this->transformFromMap( $type == XMLReader::ELEMENT, $name ); - } - return call_user_func( array( $this, "format_${name}" ), $type == XMLReader::ELEMENT ); + $funcname = "format_$name"; + if ( isset( $this->map[ $name ] ) ) { + $tag = $this->map[ $name ]; + if ( is_array( $tag ) ) { + $tag = $this->notXPath( $tag ); + } + if ( strncmp( $tag, "format_", 7 ) ) { + return $this->transormFromMap( $type == XMLReader::ELEMENT, $tag, $name ); + } + $funcname = $tag; + } + return call_user_func( array( $this, $funcname ), $type == XMLReader::ELEMENT ); break; case XMLReader::TEXT: commit 6de6cbac08c256e9b6353ea6b5b4c13cfa95e57f Author: SVN Migration <svn@php.net> Date: Sat Jul 28 23:58:07 2007 +0000 This commit was manufactured by cvs2svn to create branch 'GWYNNE_PLAYS_HERE'. Thank you for your contribution.

« previous php.doc.cvs (#9785) next »