cvs: livedocs / error.php livedoc.php style_mapping.php xml_classes.php

From: Date: Sat, 17 Jan 2004 11:32:29 +0000
Subject: cvs: livedocs / error.php livedoc.php style_mapping.php xml_classes.php
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969358253@lists.php.net to get a copy of this message
nlopess Sat Jan 17 06:32:29 2004 EDT Added files: /livedocs error.php style_mapping.php xml_classes.php Modified files: /livedocs livedoc.php Log: didou patch to split livedoc.php

Index: livedocs/livedoc.php diff -u livedocs/livedoc.php:1.72 livedocs/livedoc.php:1.73 --- livedocs/livedoc.php:1.72 Mon Jan 12 18:04:30 2004 +++ livedocs/livedoc.php Sat Jan 17 06:32:28 2004 @@ -18,103 +18,19 @@ // | Generate an HTML version of a phpdoc/docbook page on the fly | // +----------------------------------------------------------------------+ // -// $Id: livedoc.php,v 1.72 2004/01/12 23:04:30 didou Exp $ +// $Id: livedoc.php,v 1.73 2004/01/17 11:32:28 nlopess Exp $ include './common.php'; +include './xml_classes.php'; +include './style_mapping.php'; +// Handle the case where if (!isset($_GET['q'])) { - if (!empty($_SERVER['REDIRECT_URL'])) { - $current_page = substr($_SERVER['REDIRECT_URL'], strlen(WEBBASE)); - - if (strncmp(substr($current_page, -5), ".html", 5)) { - if (preg_match('|([A-Za-z_]{2,5})/(.*)|', $current_page, $m)) { - $current_page = $m[2]; - } - - $lookup_name = preg_replace('!.*\.!', '', strtolower($current_page)); - $lookup_name = preg_replace('![^a-z0-9]!', '', $lookup_name); - - $db_m = sqlite_array_query($idx, "SELECT path, prio FROM searchi WHERE (skey='" . sqlite_escape_string($lookup_name) . "' OR skey='".sqlite_escape_string(metaphone($lookup_name))."') ORDER BY prio LIMIT 1", SQLITE_NUM); - if ($db_m) { - if ($db_m[0][1] < 5 || (sqlite_fetch_string(sqlite_unbuffered_query($idx, "SELECT count(*) FROM searchi WHERE skey='" . sqlite_escape_string($lookup_name) . "' AND prio=5")) == 1)) { - $current_page = $lang . '/' . $db_m[0][0] . '.html'; - if (@file_exists(OUTPUTDIR . '/' . $current_page)) { - header('Location: ' . WEBBASE . $current_page . '#' . $db_m[0][0]); - exit; - } - } else { - list($title, $filename, $fileid, $dirid) = do_nav($idx, $fb_idx, $lang, 'bookinfo', $nav, $children); - $date = date("d-m-Y H:i:s"); - if (empty($file_revision)) { - $file_revision = 'unknown revision'; - } - $db_m = sqlite_array_query($idx, "SELECT path FROM searchi WHERE skey='" . sqlite_escape_string($lookup_name) . "' AND prio=5 LIMIT 10", SQLITE_NUM); - $rs_size = count($db_m); - $fpath = WEBBASE . $lang . '/'; -echo <<<FUNC_HEAD -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html lang="$lang"> -<head> - <title>$title</title> - <link rel="stylesheet" href="$css_url" /> -</head> -<body> -<table border="0" cellpadding="0" cellspacing="0" width="100%"> -<tr> - <td height="1"></td> - <td rowspan="2" valign="top"> - <h2 class="title">Possible Matches</h2> - <p class="simpara"> - Unfortunately your query was not specific enough to determine the exact function you were trying to find. The - following $rs_size functions represent the closest possible matches to your query. - </p> - <p class="simpara"> -FUNC_HEAD; - - foreach($db_m as $v) { - if (($p = strrpos($v[0], '.')) !== FALSE) { - $f_name = substr($v[0], ($p + 1)); - } else { - $f_name = $v[0]; - } - $f_name = str_replace('-', '_', $f_name); -echo <<<FUNC_MATCH -<a class="function" href="{$fpath}{$v[0]}.html#{$v[0]}">{$f_name}()</a><br /> -FUNC_MATCH; - - } -echo <<<FUNC_FOOT - </p> -<div class='footer'>$filename $file_revision | generated: $date</div> -</td></tr> -<tr><td valign="top"> -$nav -</td></tr></table> -</body> -</html> -FUNC_FOOT; - - exit; - } - } - } - - $current_page = preg_replace('|^/|', '', $current_page); - $current_page = preg_replace('|\.html$|', '', $current_page); - if (preg_match('|([A-Za-z_]{2,5})/(.*)|', $current_page, $m)) { - $current_page = preg_replace(IDREG, '', $m[2]); - $save_url = TRUE; - } else if (preg_match('|(.*)|', $current_page, $m)) { - $current_page = preg_replace(IDREG, '', $m[1]); - $save_url = TRUE; - } else { - $current_page = 'manual'; - } - } - if (empty($current_page)) { - $current_page = 'manual'; - } + // Handles the 404. If a matching page is found, we redirect to it. + // Else, we display a page with the closest matches and we stop the script. + // If something goes wrong with the database, $current_page will default + // to some value. + include './error.php'; } else { $current_page = preg_replace(IDREG, '', $_GET['q']); } @@ -136,96 +52,9 @@ 'security' => 'security.index' ); - -/* almost XPATH.. ;-) */ -$map = array( - 'acronym' => 'acronym', - 'appendix/title' => 'h1', - 'author' => 'format_name', - 'authorgroup' => 'format_authorgroup', - 'bookinfo/title' => 'h1', - 'chapter/title' => 'h1', - 'classname' => 'span', - 'classsynopsis' => 'format_class_synopsis', - 'code' => 'code', - 'collab' => 'span', - 'collabname' => 'span', - 'command' => 'span', - 'computeroutput' => 'span', - 'constant' => 'span', - 'copyright' => 'format_copyright', - 'copyright/year' => 'span', - 'copyright/holder' => 'span', - 'editor' => 'format_name', - 'emphasis' => 'em', - 'enumname' => 'span', - 'envar' => 'span', - 'filename' => 'span', - 'funcsynopsis' => 'format_funcsynopsis', - 'function' => 'format_function', - 'glossterm' => 'span', - 'htmlcode' => 'format_htmlcode', - 'informaltable' => 'table', - 'informaltable/title' => 'caption', - 'itemizedlist' => 'ul', - 'itemizedlist/listitem' => 'li', - 'legalnotice/title' => 'h3', - 'link' => 'format_link', - 'literal' => 'span', - 'literallayout' => 'format_listing', - 'methodparam' => 'span', - 'methodsynopsis' => 'format_params', - 'option' => 'span', - 'orderedlist' => 'ol', - 'orderedlist/listitem' => 'li', - 'parameter' => 'span', - 'phpdoc_include' => 'handle_include', - 'preface/title' => 'h1', - 'productname' => 'span', - 'programlisting' => 'format_listing', - 'propname' => 'span', - 'proptype' => 'span', - 'pubdate' => 'format_pubdate', - 'reference/title' => 'h1', - 'refnamediv/refname' => 'format_refname', - 'refnamediv/refpurpose' => 'h2', - 'refsect1/title' => 'h3', - 'refsect2/title' => 'h4', - 'replaceable' => 'span', - 'screen' => 'format_listing', - 'section/title' => 'h1', - 'sect1/title' => 'h2', - 'sect2/title' => 'h3', - 'sect3/title' => 'h4', - 'sect4/title' => 'h5', - 'sect5/title' => 'h6', - 'signalname' => 'span', - 'simpara' => 'p', - 'simplelist' => 'ul', - 'simplelist/member' => 'li', - 'subscript' => 'span', - 'systemitem' => 'span', - 'table' => 'table', - 'table/title' => 'caption', - 'tbody' => 'tbody', - 'tbody/row' => 'tr', - 'tbody/row/entry' => 'td', - 'tgroup' => '', - 'thead' => 'thead', - 'thead/row' => 'tr', - 'thead/row/entry' => 'th', - 'titleabbrev' => '', - 'type' => 'span', - 'ulink' => 'format_ulink', - 'userinput' => 'span', - 'varname' => 'span', - 'warning/title' => 'format_warning_title', - 'xref' => 'format_link', -); - if (isset($_GET['s'])) { include './livedoc-index.php'; - exit(); + exit; } /***************************************************************************** @@ -240,302 +69,6 @@ } } -/***************************************************************************** - * Two XML handling classes for the docbook to html transformation - */ -class Node { /* {{{ */ - var $tagname = ""; - var $attributes = array(); - var $children = array(); - var $parent = null; - var $index = 0; - var $content = ""; - var $nodeid = 0; - - function transform($map) { - if (strlen($this->tagname) == 0) { - return htmlspecialchars($this->content, ENT_NOQUOTES); - } - - $tagname = 'div'; - $attributes = $this->attributes; - $attributes['class'] = $this->tagname; - - /* do we match any of the patterns in the map ? */ - foreach ($map as $mapent) { - if ($mapent[0][0] == $this->tagname) { - $n =& $this->parent; - $match = true; - for ($i = 1; $i < count($mapent[0]); $i++) { - if ($n->tagname != $mapent[0][$i]) { - $match = false; - break; - } - $n =& $n->parent; - } - if ($match) { - if (is_callable($mapent[1])) { - return call_user_func($mapent[1], $this); - } else { - $tagname = $mapent[1]; - } - } - } - } - - if (isset($this->attributes['id'])) { - $anchor = sprintf('<a name="%s"></a>', $this->attributes['id']); - } else { - $anchor = ''; - } - - if (strlen($tagname)) { - $xml = '<' . $tagname; - foreach ($attributes as $name => $value) { - $xml .= ' ' . $name . '="' . htmlspecialchars($value) . '"'; - } - } else { - $xml = ''; - } - - $content = ""; - - if (count($this->children) == 0) { - - if (strlen($tagname) == 0) { - return ''; - } - - if (strlen($this->content)) { - $content = htmlspecialchars($this->content, ENT_NOQUOTES); - } - } else { - for ($i = 0; $i < count($this->children); $i++) { - $child = $this->children[$i]; - $content .= $child->transform($map); - } - } - - if (strlen($tagname)) { - $xml .= '>'; - } - - if ($tagname == 'th' && strlen($content) == 0) { - $content = '&nbsp;'; - } - - $xml .= $anchor; - $xml .= $content; - if (strlen($tagname)) { - $xml .= '</' . $tagname . '>'; - } - - return $xml; - } - - function &select_node($id) { - if (isset($this->attributes['id']) && $this->attributes['id'] == $id) { - return $node; - } - // look for children that match - for ($i = 0; $i < count($this->children); $i++) { - $x =& $this->select_node($this->children[$i], $id); - if (is_object($x)) { - return $x; - } - } - return null; - } - - function Node($tagname, $attributes) { - static $nodeid = 0; - - $this->nodeid = $nodeid++; - $this->tagname = $tagname; - $this->attributes = $attributes; - } - - function set_parent(&$parent) { - $this->parent =& $parent; - $this->index = count($parent->children); - $parent->children[$this->index] =& $this; - } - - function compress() { - if (count($this->children) == 1 && $this->children[0]->tagname == '') { - $this->content = $this->children[0]->content; - $this->children = array(); - } - } - - function as_xml() { - if (strlen($this->tagname) == 0) { - return htmlspecialchars($this->content, ENT_NOQUOTES); - } - - $xml = '<' . $this->tagname; - foreach ($this->attributes as $name => $value) { - $xml .= ' ' . $name . '="' . htmlspecialchars($value) . '"'; - } - if (count($this->children) == 0) { - if (strlen($this->content) == 0) { - $xml .= '/>'; - } else { - $xml .= '>' . htmlspecialchars($this->content, ENT_NOQUOTES) . '</' . $this->tagname . '>'; - } - } else { - $xml .= '>'; - for ($i = 0; $i < count($this->children); $i++) { - $xml .= $this->children[$i]->as_xml(); - } - $xml .= '</' . $this->tagname . '>'; - } - return $xml; - } - - - function add_child(&$child) { - if (count($this->children) == 0 && strlen($this->content)) { - if (strlen(trim($this->content)) > 0) { - // promote content to an anonymous cdata node - $node = new Node("", array()); - $node->content = $this->content; - $node->set_parent($this); - } - $this->content = ""; - } - $child->set_parent($this); - } - - function add_cdata($data) { - if (count($this->children) == 0) { - $this->content .= $data; - } else { - $node = new Node("", array()); - $node->content = $data; - $this->add_child($node); - } - } - -} -/* }}} */ - -class DocBookToHTML { /* {{{ */ - var $html = ""; - var $top = null; - var $current = null; - - function DocBookToHTML($xml, $id = null) { - if (strlen($xml) == 0) { - return; - } - - $p = xml_parser_create(); - xml_set_object($p, $this); - xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); - xml_set_element_handler($p, 'start_elem', 'end_elem'); - xml_set_character_data_handler($p, 'cdata'); - - if (!xml_parse($p, $xml, true)) { - printf("XML: %d:%d %s\n", - xml_get_current_line_number($p), - xml_get_current_column_number($p), - xml_error_string(xml_get_error_code($p)) - ); - $lines = explode("\n", $xml); - $l = xml_get_current_line_number($p); - echo "\nLine: $l is \n" . htmlentities($lines[$l-1]) . "\n"; - echo htmlentities($xml); - } - xml_parser_free($p); - - if ($id !== null) { - $newtop = $this->top->select_node($id); - if (is_object($newtop)) { - $this->top = $newtop; - $this->top->parent = null; - } - } - } - - /* apply a transformation map to content */ - function transform($map) { - /* pre-parse the map */ - - if (!is_object($this->top)) { - return "<div class=\"warning\">XML document contained no data</div>"; - } - - $pmap = array(); - foreach ($map as $pat => $rep) { - $h = array_reverse(explode('/', $pat)); - $pmap[] = array($h, $rep); - } - return $this->top->transform($pmap); - } - - /* returns structure as XML */ - function as_xml() { - return $this->_as_xml($this->top); - } - - function _as_xml(&$node) { - if (strlen($node->tagname) == 0) { - return htmlspecialchars($node->content, ENT_NOQUOTES); - } - - $xml = '<' . $node->tagname; - foreach ($node->attributes as $name => $value) { - $xml .= ' ' . $name . '="' . htmlspecialchars($value) . '"'; - } - if (count($node->children) == 0) { - if (strlen($node->content) == 0) { - $xml .= '/>'; - } else { - $xml .= '>' . htmlspecialchars($node->content, ENT_NOQUOTES) . '</' . $node->tagname . '>'; - } - } else { - $xml .= '>'; - for ($i = 0; $i < count($node->children); $i++) { - $xml .= $this->_as_xml($node->children[$i]); - } - $xml .= '</' . $node->tagname . '>'; - } - return $xml . "<!-- $node->nodeid -->"; - } - - function start_elem($parser, $tagname, $attributes) { - /* create a new node as a child of the current node */ - $node = new Node($tagname, $attributes); - - if ($this->top === null) { - /* set the top node */ - $this->top =& $node; - $this->current =& $node; - } else { - /* child of current */ - $this->current->add_child($node); - $this->current =& $node; - } - } - - function end_elem($parser, $tagname) { - /* pop up to parent of the current node */ - if ($this->current === null) { - return; - } - /* optimize the node; if it only contains a single cdata (anonymous) node, - * compress that node into the content */ - $this->current->compress(); - - $this->current = &$this->current->parent; - } - - function cdata($parser, $data) { - $this->current->add_cdata($data); - } -} -/* }}} */ /***************************************************************************** * Loading title and navigation data @@ -611,7 +144,8 @@ /***************************************************************************** * Helper functions for navigation */ -function do_nav($idx, $fb_idx, $lang, $current_page, &$nav, &$children) { +function do_nav($idx, $fb_idx, $lang, $current_page, &$nav, &$children) +{ $nav = "<table class='nav' border='0' cellpadding='0' cellspacing='0' width='150'>"; @@ -681,7 +215,8 @@ } -function generate_url_for_id($lang, $ref) { +function generate_url_for_id($lang, $ref) +{ static $cache = array(); if (isset($cache[$lang][$ref])) { @@ -770,7 +305,8 @@ /***************************************************************************** * Helper functions for transformation */ -function &load_xml_doc($filename, $included = false, $fallback_filename = '') { +function &load_xml_doc($filename, $included = false, $fallback_filename = '') +{ global $file_revision, $idx; $replace = array(); @@ -846,7 +382,8 @@ } -function lookup_title($nodeid) { +function lookup_title($nodeid) +{ static $ids = array(); /* Define your own title mappings here: */ @@ -887,7 +424,8 @@ return null; } -function make_function_link($ref) { +function make_function_link($ref) +{ $parts = explode('.', $ref); $id = 'function.'. $parts[3]; $title = lookup_title($id); @@ -903,7 +441,8 @@ return "<div class=\"function_link\"><function>$func_name</function>$title</div>"; } -function make_xref($ref) { +function make_xref($ref) +{ $parts = explode('.', $ref); if ($parts[0] == 'reference' && $parts[2] == 'functions') { $id = 'function.'. $parts[3]; @@ -915,7 +454,8 @@ return "<xref linkend=\"$id\">$title</xref>"; } -function handle_funcref() { +function handle_funcref() +{ global $current_page_title, $children, $lang; $ret = "<h1>$current_page_title</h1>\n"; @@ -926,7 +466,8 @@ return $ret."<br /><br />"; } -function handle_appendixes() { +function handle_appendixes() +{ global $current_page_title, $children, $lang; $ret = "<h1>$current_page_title</h1>\n"; @@ -934,11 +475,12 @@ $url = generate_url_for_id($lang, $id); $ret .= "<h2 class='funcref'><a href='$url'>$title</a></h2>\n"; } - return $ret."<br /><br />"; + return $ret . "<br /><br />"; } -function do_contents($id, $level) { +function do_contents($id, $level) +{ global $lang; $q = "SELECT id, docbook_id FROM toc WHERE parent_id = $id"; $r = sqlite_query($GLOBALS['idx'], $q); @@ -963,13 +505,15 @@ return $ret .= "</ul>\n"; } -function handle_contents() { +function handle_contents() +{ global $current_page_title, $children, $lang; return "<center><h1>PHP Manual</h1></center><h2>Contents</h2>" . do_contents(1, 0); } -function handle_include($node) { +function handle_include($node) +{ $filename = BASE . strtr($node->attributes['ref'], '.', '/') . '.xml'; $fallback_filename = FALLBACK_BASE . strtr($node->attributes['ref'], '.', '/') . '.xml'; @@ -977,401 +521,9 @@ return $doc->transform($GLOBALS['map']); } - -/***************************************************************************** - * Helper functions for formatting elements - */ -function format_authorgroup($node) { - $l = ''; - $cl = array(); - - foreach ($node->children as $child) { - if ($child->tagname == "author" || $child->tagname == "editor") { - $l .= format_name($child); - } - if ($child->tagname == "collab") { - $cl[] = $child->children[0]->content; - } - } - switch ($node->attributes['id']) { - case "authors": - $prefix = "Authors:<br />"; - break; - case "editors": - $prefix = "Edited by:<br />"; - break; - case "translators"; - $prefix = $cl[0]."<br />"; - unset($cl[0]); - break; - default: - $prefix = $node->attributes['id']; - } - - return sprintf('<div class="group%s"><b>%s</b>%s %s%s</div>', $node->attributes['id'], $prefix, $node->content, $l, implode(", ", $cl)); -} - -function format_copyright($node) { - $y = array(); - - foreach ($node->children as $child) { - if ($child->tagname == "year") { - $y[] = $child->content; - } else if ($child->tagname == "holder") { - $holder = $child->content; - } - } - - return sprintf('<div class="copyright">Copyright &copy; %s by %s</div>', implode(", ", $y), $holder); -} - -function format_refname($node) { - global $current_page, $idx; - - $vers = ''; - - if (substr($current_page, 0, 9) == 'function.') { - $func_name = sqlite_escape_string(strtr(substr($current_page, 9), '-', '_')); - $q = sqlite_query($idx, "SELECT versinfo FROM funcs where funcname='$func_name'"); - if ($q && $r = sqlite_fetch_array($q, SQLITE_NUM)) { - $vers = sprintf('<div class="versinfo">%s</div>', $r[0]); - } - } - - return sprintf('<h1 class="%s">%s</h1>%s', $node->tagname, $node->content, $vers); -} - -function format_name($node) { - $class = $node->parent->attributes['id']; - foreach ($node->children as $child) { - if ($child->tagname == 'firstname') { - $firstname = $child->content; - } - if ($child->tagname == 'surname') { - $surname = $child->content; - } - } - switch ($class) { - case 'authors': - case 'editors': - $tag = 'div'; - break; - case 'translators': - $tag = 'span'; - break; - default: - $tag = 'span'; - } - return sprintf('<%s class="%s">%s %s</%s>', $tag, $class, $firstname, $surname, $tag); -} - -function format_parameter($node) { - return sprintf('<span class="%s">$%s</span>', $node->tagname, $node->content); -} - -function format_pubdate($node) { - return sprintf('<div class="%s">Published on: %s</div>', $node->tagname, $node->content); -} - -function format_ulink($node) { - return sprintf('<a target="_blank" class="ulink" href="%s">%s</a>', - $node->attributes['url'], - $node->content - ); -} - -function format_warning_title($node) { - return sprintf('<span class="warning_title">%s</span>', $node->content); -} - -function format_link($node) { - global $lang; - - $caption = $node->content; - if (strlen($caption) == 0) { - $caption = lookup_title($node->attributes['linkend']); - } - if (strlen($caption) == 0) { - $caption = $node->attributes['linkend']; - } - $url = generate_url_for_id($lang, $node->attributes['linkend']); - return sprintf('<a class="%s" href="%s">%s</a>', - $node->tagname, - $url, - $caption - ); -} - -function format_function($node) { - global $current_page, $lang; - - $itemid = strtr($node->content, '_', '-'); - - $class = 'function'; - if (isset($node->attributes['class'])) { - $class = 'method'; - $dest = $node->attributes['class'] . '.method.' . $itemid; - } else { - $dest = 'function.' . $itemid; - } - - if ($current_page == $dest) { - return sprintf('<span class="%s">%s()</span>', $class, $node->content); - } - - $url = generate_url_for_id($lang, $dest); - return sprintf('<a class="%s" href="%s">%s()</a>', - $class, - $url, - $node->content - ); -} - -function format_listing($node) { - $content = trim($node->content); - - if (substr($content, 0, 5) == '<?php') { - $node->attributes['role'] = 'php'; - } - if (!isset($node->attributes['role'])) { - $node->attributes['role'] = ''; - } - - switch ($node->attributes['role']) { - case 'php': - $content = preg_replace(array( - '@<br />@', - '@<font color="([^"]+)">@', - '@</font>@', - ), - array( - "<br />\n", - '<span class="\\1">', - '</span>', - ), - highlight_string($content, 1)); - break; - case 'shell': - case 'html': - case 'xml': - case 'c': - $content = '<pre>' . htmlentities($content) . '</pre>'; - break; - default: - $content = htmlspecialchars($content); - } - - return sprintf('<div class="%scode">%s</div>', $node->attributes['role'], $content); -} - -function format_params($node) { - $node->parent = null; - - /* Check role */ - if (isset($node->attributes['role'])) { - $role = $node->attributes['role']; - } else { - $role = 'php'; - } - - /* build up prototype */ - $type = $node->children[0]->content; - $fname = $node->children[1]->content; - $params = ""; - $optclose = ""; - - for ($i = 2; $i < count($node->children); $i++) { - $k = $node->children[$i]; - - if (strlen($k->tagname) == 0 || $k->tagname == 'void') { - continue; - } - - $p = ''; - if (strlen($params)) { - $p .= ','; - } - - if (isset($k->attributes['choice']) && $k->attributes['choice'] == 'opt') { - $p = '[' . $p ; - $optclose .= ']'; - if (strlen($params)) { - $p = ' ' . $p; - } - } - - if (strlen($p)) { - $p .= ' '; - } - - $p .= '<span class="methodparam"><span class="type">' . $k->children[0]->content . '</span>'; - - $name = $k->children[1]->content; - switch ($role) { - case 'c': - break; - default: - if ($name && $name{0} == '&') { - $name = '&$' . substr($name, 1); - } else { - $name = '$' . $name; - } - } - - $p .= ' <span class="varname">' . $name . '</span></span>'; - - $params .= $p; - } - - return "<div class=\"methodsynopsis\"><span class=\"type\">$type</span> <span class=\"function\">$fname(</span>$params$optclose<span class=\"function\">)</span></div>"; -} - -function format_class_synopsis($node) { - $classname = $node->children[0]->children[0]->content; - - $head = "<div class=\"classsynopsis\">class <span class=\"classname\">$classname</span> {\n<div class=\"classcontents\">"; - $foot = "</div>}\n</div>"; - $body = ""; - - for ($i = 1; $i < count($node->children); $i++) { - $k = $node->children[$i]; - - switch ($k->tagname) { - case 'constructorsynopsis': - case 'methodsynopsis': - $body .= format_params($k); - break; - case 'fieldsynopsis': - $body .= "<div class=\"fieldsynopsis\"><span class=\"type\">" . - $k->children[0]->content . "</span> <span class=\"varname\">" . - $k->children[1]->content . "</span></div>\n"; - break; - } - } - - - return $head . $body . $foot; -} - -function format_funcsynopsis($node) { - $html = "<div class=\"funcsynopsis\">"; - - /* for each prototype in the synopsis */ - for ($proto = 0; $proto < count($node->children); $proto++) { - $p = $node->children[$proto]; - - if ($p->tagname != 'funcprototype') - continue; - - if (isset($p->attributes['role'])) { - $role = $node->attributes['role']; - } else { - $role = 'php'; - } - - $funcdef = $p->children[0]; - $rettype = ""; - $funcname = ""; - - for ($i = 0; $i < count($funcdef->children); $i++) { - $k = $funcdef->children[$i]; - switch($k->tagname) { - case '': - $rettype = $k->content; - break; - case 'function': - $funcname = $k->content; - break; - } - } - - if ($rettype == '') { - $rettype = 'void'; - } - - - $html .= "<div class=\"funcprototype\"><span class=\"type\">$rettype</span> <span class=\"function\">$funcname</span>"; - - $params = ""; - $optclose = ""; - - for ($i = 1; $i < count($p->children); $i++) { - $def = $p->children[$i]; - if ($def->tagname != 'paramdef') { - continue; - } - $paramhtml = ""; - $paramtype = ""; - $param = null; - - for ($j = 0; $j < count($def->children); $j++) { - $k = $def->children[$j]; - switch($k->tagname) { - case 'parameter': - $param = $k; - break; - case 'enumname': - $paramtype = "<span class=\"enumname\">{$k->content}</enumname>"; - break; - case '': - if (strlen($paramtype) == 0) - $paramtype = $k->content; - break; - } - } - if ($paramtype == '') { - $paramtype = 'void'; - } - - if (count($param->children) > 1) { - $param = $param->children[1]; - $optional = true; - } else { - $optional = false; - } - $paramname = trim($param->content); - - if (strlen($params)) { - $paramhtml .= ','; - } - if ($optional) { - $paramhtml = '[' . $paramhtml; - $optclose .= ']'; - if (strlen($params)) { - $paramhtml = ' ' . $parmhtml; - } - } - - if (strlen($paramhtml)) { - $paramhtml .= ' '; - } - - $paramhtml .= '<span class=\"paramdef\"><span class=\"type\">' . $paramtype . '</span>'; - - switch ($role) { - case 'c': - break; - default: - if ($paramname && $paramname{0} == '&') { - $paramname = '&$' . substr($paramname, 1); - } else { - $paramname = '$' . $paramname; - } - } - - $paramhtml .= ' <span class=\"parameter\">' . $paramname . '</span></span>'; - - $params .= $paramhtml; - } - - $html .= '(' . $params . $optclose . ")</div>"; - } - - return $html . "</div>"; -} - -/* Handle the case where a requested node id was not found */ -function handle_missing_index() { +// Handle the case where a requested node id was not found +function handle_missing_index() +{ global $current_page, $lang; header('Location: ' . WEBBASE . 'search.php?q=' . urlencode($current_page) . '&l=' . $lang); Index: livedocs/error.php +++ livedocs/error.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at the following url: | // | 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. | // +----------------------------------------------------------------------+ // | Authors: Wez Furlong, Derick Rethans, Ilia Alshanetsky | // +----------------------------------------------------------------------+ // | Handles the 404 error and try to redirect to the good place. | // +----------------------------------------------------------------------+ // // $Id: if (!empty($_SERVER['REDIRECT_URL'])) { // This is the requested page that caused the error $current_page = substr($_SERVER['REDIRECT_URL'], strlen(WEBBASE)); if (strncmp(substr($current_page, -5), ".html", 5)) { if (preg_match('|([A-Za-z_]{2,5})/(.*)|', $current_page, $m)) { $current_page = $m[2]; } $lookup_name = preg_replace('!.*\.!', '', strtolower($current_page)); $lookup_name = preg_replace('![^a-z0-9]!', '', $lookup_name); $db_m = sqlite_array_query($idx, "SELECT path, prio FROM searchi WHERE (skey='" . sqlite_escape_string($lookup_name) . "' OR skey='".sqlite_escape_string(metaphone($lookup_name))."') ORDER BY prio LIMIT 1", SQLITE_NUM); if ($db_m) { if ($db_m[0][1] < 5 || (sqlite_fetch_string(sqlite_unbuffered_query($idx, "SELECT count(*) FROM searchi WHERE skey='" . sqlite_escape_string($lookup_name) . "' AND prio=5")) == 1)) { $current_page = $lang . '/' . $db_m[0][0] . '.html'; if (@file_exists(OUTPUTDIR . '/' . $current_page)) { header('Location: ' . WEBBASE . $current_page . '#' . $db_m[0][0]); exit; } } else { list($title, $filename, $fileid, $dirid) = do_nav($idx, $fb_idx, $lang, 'bookinfo', $nav, $children); $date = date("d-m-Y H:i:s"); if (empty($file_revision)) { $file_revision = 'unknown revision'; } $db_m = sqlite_array_query($idx, "SELECT path FROM searchi WHERE skey='" . sqlite_escape_string($lookup_name) . "' AND prio=5 LIMIT 10", SQLITE_NUM); $rs_size = count($db_m); $fpath = WEBBASE . $lang . '/'; echo <<<FUNC_HEAD <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="$lang"> <head> <title>$title</title> <link rel="stylesheet" href="$css_url" /> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td height="1"></td> <td rowspan="2" valign="top"> <h2 class="title">Possible Matches</h2> <p class="simpara"> Unfortunately your query was not specific enough to determine the exact function you were trying to find. The following $rs_size functions represent the closest possible matches to your query. </p> <p class="simpara"> FUNC_HEAD; foreach($db_m as $v) { if (($p = strrpos($v[0], '.')) !== FALSE) { $f_name = substr($v[0], ($p + 1)); } else { $f_name = $v[0]; } $f_name = str_replace('-', '_', $f_name); echo <<<FUNC_MATCH <a class="function" href="{$fpath}{$v[0]}.html#{$v[0]}">{$f_name}()</a><br /> FUNC_MATCH; } echo <<<FUNC_FOOT </p> <div class='footer'>$filename $file_revision | generated: $date</div> </td></tr> <tr><td valign="top"> $nav </td></tr></table> </body> </html> FUNC_FOOT; exit; } } } $current_page = preg_replace('|^/|', '', $current_page); $current_page = preg_replace('|\.html$|', '', $current_page); if (preg_match('|([A-Za-z_]{2,5})/(.*)|', $current_page, $m)) { $current_page = preg_replace(IDREG, '', $m[2]); $save_url = TRUE; } else if (preg_match('|(.*)|', $current_page, $m)) { $current_page = preg_replace(IDREG, '', $m[1]); $save_url = TRUE; } else { $current_page = 'manual'; } } if (empty($current_page)) { $current_page = 'manual'; } Index: livedocs/style_mapping.php +++ livedocs/style_mapping.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at the following url: | // | 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. | // +----------------------------------------------------------------------+ // | Authors: Wez Furlong, Derick Rethans, Ilia Alshanetsky | // +----------------------------------------------------------------------+ // | Helper functions for formatting elements | // +----------------------------------------------------------------------+ // // $Id: // almost XPATH.. ;-) $map = array( 'acronym' => 'acronym', 'appendix/title' => 'h1', 'author' => 'format_name', 'authorgroup' => 'format_authorgroup', 'bookinfo/title' => 'h1', 'chapter/title' => 'h1', 'classname' => 'span', 'classsynopsis' => 'format_class_synopsis', 'code' => 'code', 'collab' => 'span', 'collabname' => 'span', 'command' => 'span', 'computeroutput' => 'span', 'constant' => 'span', 'copyright' => 'format_copyright', 'copyright/year' => 'span', 'copyright/holder' => 'span', 'editor' => 'format_name', 'emphasis' => 'em', 'enumname' => 'span', 'envar' => 'span', 'filename' => 'span', 'funcsynopsis' => 'format_funcsynopsis', 'function' => 'format_function', 'glossterm' => 'span', 'htmlcode' => 'format_htmlcode', 'informaltable' => 'table', 'informaltable/title' => 'caption', 'itemizedlist' => 'ul', 'itemizedlist/listitem' => 'li', 'legalnotice/title' => 'h3', 'link' => 'format_link', 'literal' => 'span', 'literallayout' => 'format_listing', 'methodparam' => 'span', 'methodsynopsis' => 'format_params', 'option' => 'span', 'orderedlist' => 'ol', 'orderedlist/listitem' => 'li', 'parameter' => 'span', 'phpdoc_include' => 'handle_include', 'preface/title' => 'h1', 'productname' => 'span', 'programlisting' => 'format_listing', 'propname' => 'span', 'proptype' => 'span', 'pubdate' => 'format_pubdate', 'reference/title' => 'h1', 'refnamediv/refname' => 'format_refname', 'refnamediv/refpurpose' => 'h2', 'refsect1/title' => 'h3', 'refsect2/title' => 'h4', 'replaceable' => 'span', 'screen' => 'format_listing', 'section/title' => 'h1', 'sect1/title' => 'h2', 'sect2/title' => 'h3', 'sect3/title' => 'h4', 'sect4/title' => 'h5', 'sect5/title' => 'h6', 'signalname' => 'span', 'simpara' => 'p', 'simplelist' => 'ul', 'simplelist/member' => 'li', 'subscript' => 'span', 'systemitem' => 'span', 'table' => 'table', 'table/title' => 'caption', 'tbody' => 'tbody', 'tbody/row' => 'tr', 'tbody/row/entry' => 'td', 'tgroup' => '', 'thead' => 'thead', 'thead/row' => 'tr', 'thead/row/entry' => 'th', 'titleabbrev' => '', 'type' => 'span', 'ulink' => 'format_ulink', 'userinput' => 'span', 'varname' => 'span', 'warning/title' => 'format_warning_title', 'xref' => 'format_link', ); function format_authorgroup($node) { $l = ''; $cl = array(); foreach ($node->children as $child) { if ($child->tagname == "author" || $child->tagname == "editor") { $l .= format_name($child); } if ($child->tagname == "collab") { $cl[] = $child->children[0]->content; } } switch ($node->attributes['id']) { case "authors": $prefix = "Authors:<br />"; break; case "editors": $prefix = "Edited by:<br />"; break; case "translators"; $prefix = $cl[0]."<br />"; unset($cl[0]); break; default: $prefix = $node->attributes['id']; } return sprintf('<div class="group%s"><b>%s</b>%s %s%s</div>', $node->attributes['id'], $prefix, $node->content, $l, implode(", ", $cl)); } function format_copyright($node) { $y = array(); foreach ($node->children as $child) { if ($child->tagname == "year") { $y[] = $child->content; } else if ($child->tagname == "holder") { $holder = $child->content; } } return sprintf('<div class="copyright">Copyright &copy; %s by %s</div>', implode(", ", $y), $holder); } function format_refname($node) { global $current_page, $idx; $vers = ''; if (substr($current_page, 0, 9) == 'function.') { $func_name = sqlite_escape_string(strtr(substr($current_page, 9), '-', '_')); $q = sqlite_query($idx, "SELECT versinfo FROM funcs where funcname='$func_name'"); if ($q && $r = sqlite_fetch_array($q, SQLITE_NUM)) { $vers = sprintf('<div class="versinfo">%s</div>', $r[0]); } } return sprintf('<h1 class="%s">%s</h1>%s', $node->tagname, $node->content, $vers); } function format_name($node) { $class = $node->parent->attributes['id']; foreach ($node->children as $child) { if ($child->tagname == 'firstname') { $firstname = $child->content; } if ($child->tagname == 'surname') { $surname = $child->content; } } switch ($class) { case 'authors': case 'editors': $tag = 'div'; break; case 'translators': $tag = 'span'; break; default: $tag = 'span'; } return sprintf('<%s class="%s">%s %s</%s>', $tag, $class, $firstname, $surname, $tag); } function format_parameter($node) { return sprintf('<span class="%s">$%s</span>', $node->tagname, $node->content); } function format_pubdate($node) { return sprintf('<div class="%s">Published on: %s</div>', $node->tagname, $node->content); } function format_ulink($node) { return sprintf('<a target="_blank" class="ulink" href="%s">%s</a>', $node->attributes['url'], $node->content ); } function format_warning_title($node) { return sprintf('<span class="warning_title">%s</span>', $node->content); } function format_link($node) { global $lang; $caption = $node->content; if (strlen($caption) == 0) { $caption = lookup_title($node->attributes['linkend']); } if (strlen($caption) == 0) { $caption = $node->attributes['linkend']; } $url = generate_url_for_id($lang, $node->attributes['linkend']); return sprintf('<a class="%s" href="%s">%s</a>', $node->tagname, $url, $caption ); } function format_function($node) { global $current_page, $lang; $itemid = strtr($node->content, '_', '-'); $class = 'function'; if (isset($node->attributes['class'])) { $class = 'method'; $dest = $node->attributes['class'] . '.method.' . $itemid; } else { $dest = 'function.' . $itemid; } if ($current_page == $dest) { return sprintf('<span class="%s">%s()</span>', $class, $node->content); } $url = generate_url_for_id($lang, $dest); return sprintf('<a class="%s" href="%s">%s()</a>', $class, $url, $node->content ); } function format_listing($node) { $content = trim($node->content); if (substr($content, 0, 5) == '<?php') { $node->attributes['role'] = 'php'; } if (!isset($node->attributes['role'])) { $node->attributes['role'] = ''; } switch ($node->attributes['role']) { case 'php': $content = preg_replace(array( '@<br />@', '@<font color="([^"]+)">@', '@</font>@', ), array( "<br />\n", '<span class="\\1">', '</span>', ), highlight_string($content, 1)); break; case 'shell': case 'html': case 'xml': case 'c': $content = '<pre>' . htmlentities($content) . '</pre>'; break; default: $content = htmlspecialchars($content); } return sprintf('<div class="%scode">%s</div>', $node->attributes['role'], $content); } function format_params($node) { $node->parent = null; /* Check role */ if (isset($node->attributes['role'])) { $role = $node->attributes['role']; } else { $role = 'php'; } /* build up prototype */ $type = $node->children[0]->content; $fname = $node->children[1]->content; $params = ""; $optclose = ""; for ($i = 2; $i < count($node->children); $i++) { $k = $node->children[$i]; if (strlen($k->tagname) == 0 || $k->tagname == 'void') { continue; } $p = ''; if (strlen($params)) { $p .= ','; } if (isset($k->attributes['choice']) && $k->attributes['choice'] == 'opt') { $p = '[' . $p ; $optclose .= ']'; if (strlen($params)) { $p = ' ' . $p; } } if (strlen($p)) { $p .= ' '; } $p .= '<span class="methodparam"><span class="type">' . $k->children[0]->content . '</span>'; $name = $k->children[1]->content; switch ($role) { case 'c': break; default: if ($name && $name{0} == '&') { $name = '&$' . substr($name, 1); } else { $name = '$' . $name; } } $p .= ' <span class="varname">' . $name . '</span></span>'; $params .= $p; } return "<div class=\"methodsynopsis\"><span class=\"type\">$type</span> <span class=\"function\">$fname(</span>$params$optclose<span class=\"function\">)</span></div>"; } function format_class_synopsis($node) { $classname = $node->children[0]->children[0]->content; $head = "<div class=\"classsynopsis\">class <span class=\"classname\">$classname</span> {\n<div class=\"classcontents\">"; $foot = "</div>}\n</div>"; $body = ""; for ($i = 1; $i < count($node->children); $i++) { $k = $node->children[$i]; switch ($k->tagname) { case 'constructorsynopsis': case 'methodsynopsis': $body .= format_params($k); break; case 'fieldsynopsis': $body .= "<div class=\"fieldsynopsis\"><span class=\"type\">" . $k->children[0]->content . "</span> <span class=\"varname\">" . $k->children[1]->content . "</span></div>\n"; break; } } return $head . $body . $foot; } function format_funcsynopsis($node) { $html = "<div class=\"funcsynopsis\">"; /* for each prototype in the synopsis */ for ($proto = 0; $proto < count($node->children); $proto++) { $p = $node->children[$proto]; if ($p->tagname != 'funcprototype') continue; if (isset($p->attributes['role'])) { $role = $node->attributes['role']; } else { $role = 'php'; } $funcdef = $p->children[0]; $rettype = ""; $funcname = ""; for ($i = 0; $i < count($funcdef->children); $i++) { $k = $funcdef->children[$i]; switch($k->tagname) { case '': $rettype = $k->content; break; case 'function': $funcname = $k->content; break; } } if ($rettype == '') { $rettype = 'void'; } $html .= "<div class=\"funcprototype\"><span class=\"type\">$rettype</span> <span class=\"function\">$funcname</span>"; $params = ""; $optclose = ""; for ($i = 1; $i < count($p->children); $i++) { $def = $p->children[$i]; if ($def->tagname != 'paramdef') { continue; } $paramhtml = ""; $paramtype = ""; $param = null; for ($j = 0; $j < count($def->children); $j++) { $k = $def->children[$j]; switch($k->tagname) { case 'parameter': $param = $k; break; case 'enumname': $paramtype = "<span class=\"enumname\">{$k->content}</enumname>"; break; case '': if (strlen($paramtype) == 0) $paramtype = $k->content; break; } } if ($paramtype == '') { $paramtype = 'void'; } if (count($param->children) > 1) { $param = $param->children[1]; $optional = true; } else { $optional = false; } $paramname = trim($param->content); if (strlen($params)) { $paramhtml .= ','; } if ($optional) { $paramhtml = '[' . $paramhtml; $optclose .= ']'; if (strlen($params)) { $paramhtml = ' ' . $parmhtml; } } if (strlen($paramhtml)) { $paramhtml .= ' '; } $paramhtml .= '<span class=\"paramdef\"><span class=\"type\">' . $paramtype . '</span>'; switch ($role) { case 'c': break; default: if ($paramname && $paramname{0} == '&') { $paramname = '&$' . substr($paramname, 1); } else { $paramname = '$' . $paramname; } } $paramhtml .= ' <span class=\"parameter\">' . $paramname . '</span></span>'; $params .= $paramhtml; } $html .= '(' . $params . $optclose . ")</div>"; } return $html . "</div>"; } Index: livedocs/xml_classes.php +++ livedocs/xml_classes.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at the following url: | // | 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. | // +----------------------------------------------------------------------+ // | Authors: Wez Furlong, Derick Rethans, Ilia Alshanetsky | // +----------------------------------------------------------------------+ // | Two XML handling classes for the docbook to html transformation | // +----------------------------------------------------------------------+ // // $Id: class Node { /* {{{ */ var $tagname = ""; var $attributes = array(); var $children = array(); var $parent = null; var $index = 0; var $content = ""; var $nodeid = 0; function transform($map) { if (strlen($this->tagname) == 0) { return htmlspecialchars($this->content, ENT_NOQUOTES); } $tagname = 'div'; $attributes = $this->attributes; $attributes['class'] = $this->tagname; /* do we match any of the patterns in the map ? */ foreach ($map as $mapent) { if ($mapent[0][0] == $this->tagname) { $n =& $this->parent; $match = true; for ($i = 1; $i < count($mapent[0]); $i++) { if ($n->tagname != $mapent[0][$i]) { $match = false; break; } $n =& $n->parent; } if ($match) { if (is_callable($mapent[1])) { return call_user_func($mapent[1], $this); } else { $tagname = $mapent[1]; } } } } if (isset($this->attributes['id'])) { $anchor = sprintf('<a name="%s"></a>', $this->attributes['id']); } else { $anchor = ''; } if (strlen($tagname)) { $xml = '<' . $tagname; foreach ($attributes as $name => $value) { $xml .= ' ' . $name . '="' . htmlspecialchars($value) . '"'; } } else { $xml = ''; } $content = ""; if (count($this->children) == 0) { if (strlen($tagname) == 0) { return ''; } if (strlen($this->content)) { $content = htmlspecialchars($this->content, ENT_NOQUOTES); } } else { for ($i = 0; $i < count($this->children); $i++) { $child = $this->children[$i]; $content .= $child->transform($map); } } if (strlen($tagname)) { $xml .= '>'; } if ($tagname == 'th' && strlen($content) == 0) { $content = '&nbsp;'; } $xml .= $anchor; $xml .= $content; if (strlen($tagname)) { $xml .= '</' . $tagname . '>'; } return $xml; } function &select_node($id) { if (isset($this->attributes['id']) && $this->attributes['id'] == $id) { return $node; } // look for children that match for ($i = 0; $i < count($this->children); $i++) { $x =& $this->select_node($this->children[$i], $id); if (is_object($x)) { return $x; } } return null; } function Node($tagname, $attributes) { static $nodeid = 0; $this->nodeid = $nodeid++; $this->tagname = $tagname; $this->attributes = $attributes; } function set_parent(&$parent) { $this->parent =& $parent; $this->index = count($parent->children); $parent->children[$this->index] =& $this; } function compress() { if (count($this->children) == 1 && $this->children[0]->tagname == '') { $this->content = $this->children[0]->content; $this->children = array(); } } function as_xml() { if (strlen($this->tagname) == 0) { return htmlspecialchars($this->content, ENT_NOQUOTES); } $xml = '<' . $this->tagname; foreach ($this->attributes as $name => $value) { $xml .= ' ' . $name . '="' . htmlspecialchars($value) . '"'; } if (count($this->children) == 0) { if (strlen($this->content) == 0) { $xml .= '/>'; } else { $xml .= '>' . htmlspecialchars($this->content, ENT_NOQUOTES) . '</' . $this->tagname . '>'; } } else { $xml .= '>'; for ($i = 0; $i < count($this->children); $i++) { $xml .= $this->children[$i]->as_xml(); } $xml .= '</' . $this->tagname . '>'; } return $xml; } function add_child(&$child) { if (count($this->children) == 0 && strlen($this->content)) { if (strlen(trim($this->content)) > 0) { // promote content to an anonymous cdata node $node = new Node("", array()); $node->content = $this->content; $node->set_parent($this); } $this->content = ""; } $child->set_parent($this); } function add_cdata($data) { if (count($this->children) == 0) { $this->content .= $data; } else { $node = new Node("", array()); $node->content = $data; $this->add_child($node); } } } /* }}} */ class DocBookToHTML { /* {{{ */ var $html = ""; var $top = null; var $current = null; function DocBookToHTML($xml, $id = null) { if (strlen($xml) == 0) { return; } $p = xml_parser_create(); xml_set_object($p, $this); xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); xml_set_element_handler($p, 'start_elem', 'end_elem'); xml_set_character_data_handler($p, 'cdata'); if (!xml_parse($p, $xml, true)) { printf("XML: %d:%d %s\n", xml_get_current_line_number($p), xml_get_current_column_number($p), xml_error_string(xml_get_error_code($p)) ); $lines = explode("\n", $xml); $l = xml_get_current_line_number($p); echo "\nLine: $l is \n" . htmlentities($lines[$l-1]) . "\n"; echo htmlentities($xml); } xml_parser_free($p); if ($id !== null) { $newtop = $this->top->select_node($id); if (is_object($newtop)) { $this->top = $newtop; $this->top->parent = null; } } } /* apply a transformation map to content */ function transform($map) { /* pre-parse the map */ if (!is_object($this->top)) { return "<div class=\"warning\">XML document contained no data</div>"; } $pmap = array(); foreach ($map as $pat => $rep) { $h = array_reverse(explode('/', $pat)); $pmap[] = array($h, $rep); } return $this->top->transform($pmap); } /* returns structure as XML */ function as_xml() { return $this->_as_xml($this->top); } function _as_xml(&$node) { if (strlen($node->tagname) == 0) { return htmlspecialchars($node->content, ENT_NOQUOTES); } $xml = '<' . $node->tagname; foreach ($node->attributes as $name => $value) { $xml .= ' ' . $name . '="' . htmlspecialchars($value) . '"'; } if (count($node->children) == 0) { if (strlen($node->content) == 0) { $xml .= '/>'; } else { $xml .= '>' . htmlspecialchars($node->content, ENT_NOQUOTES) . '</' . $node->tagname . '>'; } } else { $xml .= '>'; for ($i = 0; $i < count($node->children); $i++) { $xml .= $this->_as_xml($node->children[$i]); } $xml .= '</' . $node->tagname . '>'; } return $xml . "<!-- $node->nodeid -->"; } function start_elem($parser, $tagname, $attributes) { /* create a new node as a child of the current node */ $node = new Node($tagname, $attributes); if ($this->top === null) { /* set the top node */ $this->top =& $node; $this->current =& $node; } else { /* child of current */ $this->current->add_child($node); $this->current =& $node; } } function end_elem($parser, $tagname) { /* pop up to parent of the current node */ if ($this->current === null) { return; } /* optimize the node; if it only contains a single cdata (anonymous) node, * compress that node into the content */ $this->current->compress(); $this->current = &$this->current->parent; } function cdata($parser, $data) { $this->current->add_cdata($data); } } /* }}} */
« previous php.doc (#969358253) next »