cvs: pear /XML_Transformer package.xml /XML_Transformer/Transformer/Driver DocBook.php /XML_Transformer/Transformer/Namespace DocBook.php

From: Date: Mon, 30 Sep 2002 08:09:52 +0000
Subject: cvs: pear /XML_Transformer package.xml /XML_Transformer/Transformer/Driver DocBook.php /XML_Transformer/Transformer/Namespace DocBook.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-6771@lists.php.net to get a copy of this message
sebastian Mon Sep 30 04:09:52 2002 EDT Added files: /pear/XML_Transformer/Transformer/Driver DocBook.php Modified files: /pear/XML_Transformer package.xml /pear/XML_Transformer/Transformer/Namespace DocBook.php Log: More work on the DocBook Namespace Handler. It should be used with the new DocBook driver if the DocBook document uses the <xref/> tag, since this requires two transformation passes.

Index: pear/XML_Transformer/package.xml diff -u pear/XML_Transformer/package.xml:1.59 pear/XML_Transformer/package.xml:1.60 --- pear/XML_Transformer/package.xml:1.59 Wed Sep 25 05:08:24 2002 +++ pear/XML_Transformer/package.xml Mon Sep 30 04:09:51 2002 @@ -67,6 +67,7 @@ <dir name="Transformer"> <dir name="Driver"> <file role="php" name="Cache.php"/> + <file role="php" name="DocBook.php"/> <file role="php" name="OutputBuffer.php"/> </dir> Index: pear/XML_Transformer/Transformer/Namespace/DocBook.php diff -u pear/XML_Transformer/Transformer/Namespace/DocBook.php:1.15 pear/XML_Transformer/Transformer/Namespace/DocBook.php:1.16 --- pear/XML_Transformer/Transformer/Namespace/DocBook.php:1.15 Sun Sep 29 10:02:20 2002 +++ pear/XML_Transformer/Transformer/Namespace/DocBook.php Mon Sep 30 04:09:51 2002 @@ -13,7 +13,7 @@ // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // -// $Id: DocBook.php,v 1.15 2002/09/29 14:02:20 sebastian Exp $ +// $Id: DocBook.php,v 1.16 2002/09/30 08:09:51 sebastian Exp $ // require_once 'XML/Transformer/Namespace.php'; @@ -66,59 +66,11 @@ * * * <ulink> * -* Example -* -* <?php -* require_once 'XML/Transformer/Namespace/DocBook.php'; -* require_once 'XML/Transformer/OutputBuffer.php'; -* -* $t = new XML_Transformer_OutputBuffer( -* array( -* 'autoload' => 'DocBook' -* ) -* ); -* -* echo $t->transform(implode('', file('article.xml'))); -* ?> -* <article> -* <section> -* <title> -* Section One -* </title> -* <para> -* <emphasis role="bold"> -* Bold Text. -* </emphasis> -* </para> -* <para> -* <ulink url="http://pear.php.net/">PEAR</ulink> -* </para> -* </section> -* </article> -* -* Output -* -* <html> -* <body> -* <h2 class="title" style="clear: both"> -* 1. Section One -* </h2> -* <p> -* <b> -* Bold Text. -* </b> -* </p> -* <p> -* <a href="http://pear.php.net/"> -* PEAR -* </a> -* </p> -* </body> -* </html> +* * <xref> * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @author Kristian Köhntopp <kris@koehntopp.de> -* @version $Revision: 1.15 $ +* @version $Revision: 1.16 $ * @access public */ class XML_Transformer_Namespace_DocBook extends XML_Transformer_Namespace { @@ -131,7 +83,7 @@ var $defaultNamespacePrefix = '&MAIN'; /** - * @var array + * @var string * @access private */ var $_author = ''; @@ -143,19 +95,19 @@ var $_context = array(); /** - * @var array + * @var string * @access private */ var $_currentExampleNumber = ''; /** - * @var array + * @var string * @access private */ var $_currentFigureNumber = ''; /** - * @var array + * @var string * @access private */ var $_currentSectionNumber = ''; @@ -192,7 +144,7 @@ var $_ids = array(); /** - * @var array + * @var boolean * @access private */ var $_roles = array(); @@ -201,14 +153,26 @@ * @var array * @access private */ - var $_sections = array(); + var $_processXref = false; /** * @var array * @access private */ + var $_sections = array(); + + /** + * @var string + * @access private + */ var $_title = ''; + /** + * @var array + * @access private + */ + var $_xref = ''; + // }}} // {{{ function XML_Transformer_Namespace_DocBook($parameters = array()) @@ -255,9 +219,12 @@ * @access public */ function start_article($attributes) { - $this->_startSection('article'); + $id = $this->_startSection( + 'article', + isset($attributes['id']) ? $attributes['id'] : '' + ); - return '<html><body>'; + return '<html><body>' . $id; } // }}} @@ -271,6 +238,8 @@ function end_article($cdata) { $this->_endSection('article'); + $this->_processXref = true; + return $cdata . '</body></html>'; } @@ -305,9 +274,8 @@ * @access public */ function start_chapter($attributes) { - $this->_startSection('chapter'); - - $id = $this->_setAnchorID( + $id = $this->_startSection( + 'chapter', isset($attributes['id']) ? $attributes['id'] : '' ); @@ -383,9 +351,8 @@ * @access public */ function start_example($attributes) { - $this->_startSection('example'); - - $id = $this->_setAnchorID( + $id = $this->_startSection( + 'example', isset($attributes['id']) ? $attributes['id'] : '' ); @@ -415,9 +382,8 @@ * @access public */ function start_figure($attributes) { - $this->_startSection('figure'); - - $id = $this->_setAnchorID( + $id = $this->_startSection( + 'figure', isset($attributes['id']) ? $attributes['id'] : '' ); @@ -705,9 +671,8 @@ * @access public */ function start_section($attributes) { - $this->_startSection('section'); - - $id = $this->_setAnchorID( + $id = $this->_startSection( + 'section', isset($attributes['id']) ? $attributes['id'] : '' ); @@ -787,10 +752,16 @@ * @access public */ function end_title($cdata) { + $cdata = trim($cdata); + + if (!empty($this->_ids[sizeof($this->_ids)-1])) { + $this->_xref[$this->_ids[sizeof($this->_ids)-1]] = strip_tags($cdata); + } + switch ($this->_context[sizeof($this->_context)-1]) { case 'article': case 'book': { - $this->_title = trim($cdata); + $this->_title = $cdata; } break; @@ -798,12 +769,12 @@ case 'example': case 'figure': case 'section': { - return trim($cdata) . '</h2>'; + return $cdata . '</h2>'; } break; default: { - return trim($cdata); + return $cdata; } } } @@ -833,31 +804,59 @@ } // }}} - // {{{ function _setAnchorID($id) + // {{{ function start_xref($attributes) + + /** + * @param array + * @return string + * @access public + */ + function start_xref($attributes) { + if ($this->_processXref) { + return sprintf( + '<a href="#%s">%s</a>', + + isset($attributes['linkend']) ? $attributes['linkend'] : '', + isset($this->_xref[$attributes['linkend']]) ? $this->_xref[$attributes['linkend']] : '' + ); + } else { + return sprintf( + '<xref%s>', + XML_Transformer_Util::attributesToString($attributes) + ); + } + } + + // }}} + // {{{ function end_xref($cdata) /** * @param string * @return string - * @access private + * @access public */ - function _setAnchorID($id) { - if (!empty($id)) { - $this->_ids[] = $id; - $id = '<a id="' . $id . '" />'; + function end_xref($cdata) { + if (!$this->_processXref) { + $cdata = $cdata . '</xref>'; } - return $id; + return array( + $cdata, + false + ); } // }}} - // {{{ function _startSection($type) + // {{{ function _startSection($type, $id) /** * @param string + * @return string * @access private */ - function _startSection($type) { + function _startSection($type, $id) { array_push($this->_context, $type); + array_push($this->_ids, $id); switch ($type) { case 'article': @@ -911,6 +910,12 @@ } break; } + + if (!empty($id)) { + $id = '<a id="' . $id . '" />'; + } + + return $id; } // }}} Index: pear/XML_Transformer/Transformer/Driver/DocBook.php +++ pear/XML_Transformer/Transformer/Driver/DocBook.php <?php // // +----------------------------------------------------------------------+ // | PEAR :: XML :: Transformer | // +----------------------------------------------------------------------+ // | Copyright (c) 2002 Sebastian Bergmann <sb@sebastian-bergmann.de> and | // | Kristian Köhntopp <kris@koehntopp.de>. | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.00 of the PHP License, | // | that is available at http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // // $Id: DocBook.php,v 1.1 2002/09/30 08:09:51 sebastian Exp $ // require_once 'XML/Transformer.php'; require_once 'XML/Transformer/Namespace/DocBook.php'; /** * Example * * <?php * require_once 'XML/Transformer/Driver/DocBook.php'; * $t = new XML_Transformer_Driver_DocBook(); * ?> * <article> * <artheader> * <title> * An Article * </title> * <author> * <firstname> * Sebastian * </firstname> * <surname> * Bergmann * </surname> * </author> * </artheader> * * <section id="foo"> * <title> * Section One * </title> * </section> * * <section id="bar"> * <title> * Section Two * </title> * * <para> * <xref linkend="foo" /> * </para> * </section> * </article> * * Output * * <html> * <body> * <div class="section"> * <a id="foo"></a> * <h2> * 1. Section One * </h2> * </div> * <div class="section"> * <a id="bar"></a> * <h2> * 2. Section Two * </h2> * <p> * <a href="#foo"> * 1. Section One * </a> * </p> * </div> * </body> * </html> * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @author Kristian Köhntopp <kris@koehntopp.de> * @version $Revision: 1.1 $ * @access public */ class XML_Transformer_Driver_DocBook { // {{{ function XML_Transformer_Driver_DocBook() function XML_Transformer_Driver_DocBook() { ob_start( array( $this, 'transform' ) ); } // }}} // {{{ function transform($xml) /** * Transforms a given DocBook XML document * to HTML using the DocBook Namespace Handler. * * @access public */ function transform($xml) { $t = new XML_Transformer( array( 'autoload' => 'DocBook' ) ); return $t->transform($t->transform($xml)); } // }}} } /* * vim600: et sw=2 ts=2 fdm=marker * vim<600: et sw=2 ts=2 */ ?>
« previous php.pear.cvs (#6771) next »