cvs: php-gtk-doc / gen_manual.sh live.php liveGen.php liveTest.php
| From: | Christian Weiske | Date: | Mon, 02 May 2005 12:24:28 +0000 |
| Subject: | cvs: php-gtk-doc / gen_manual.sh live.php liveGen.php liveTest.php | ||
| Groups: | php.gtk.doc | ||
| Request: | Send a blank email to php-gtk-doc+get-1419@lists.php.net to get a copy of this message | ||
cweiske Mon May 2 08:24:28 2005 EDT
Added files:
/php-gtk-doc live.php liveGen.php liveTest.php
Modified files:
/php-gtk-doc gen_manual.sh
Log:
I'm proud to present you the PHP-Gtk2 liveDocs
I had some time (7+ hours) in my train to Amsterdam
and implemented livedocs specifically for the php-gtk2 manual.
Just call the live.php via the webbrowser, and it will generate
the reference manual files on the fly as you request it.
Caution: Files like the reference overview will take some time, for
me it's 16 seconds on my 1.6GHz P4M. But all the other pages (small
ones) will take 1 or 2 seconds to be generated, so you can test the docs
pretty easy now.
http://cvs.php.net/diff.php/php-gtk-doc/gen_manual.sh?r1=1.1&r2=1.2&ty=u Index: php-gtk-doc/gen_manual.sh diff -u php-gtk-doc/gen_manual.sh:1.1 php-gtk-doc/gen_manual.sh:1.2 --- php-gtk-doc/gen_manual.sh:1.1 Mon Apr 11 17:04:07 2005 +++ php-gtk-doc/gen_manual.sh Mon May 2 08:24:28 2005 @@ -5,7 +5,7 @@ if [ $# -lt 2 ]; then echo "Usage: ./gen_manual.sh <language> <type>" - echo "type: html phpweb" + echo "type: html phpweb test" exit 1 fi @@ -14,6 +14,8 @@ stylesheetdir=stylesheets php=php +manualfile=manual.xml +test=0 if [ ! -d manual/$language ]; then echo "There is no manual for language $language" @@ -28,6 +30,20 @@ mkdir build/$language fi +if [ $type == "test" ]; then + if [ $# -lt 3 ]; then + echo "Please pass the id as third parameter" + exit 4 + fi + test=1 + manualfile=test_manual.xml + type=html + + #generate the file + $php -q liveGen.php $3 +fi + + if [ $type == "html" ]; then build_dir=build/$language/html xslfile=$stylesheetdir/html/chunk.xsl @@ -39,7 +55,14 @@ exit 3 fi +if [ $test == "1" ]; then + build_dir=build/$language/test +fi + +# +# we've got all necessary data; generate the manual +# if [ ! -d $build_dir ]; then mkdir $build_dir else @@ -47,8 +70,8 @@ rm -R $build_dir/* fi -xsltproc --param base.dir "'./$build_dir/'" $xslfile manual/$language/manual.xml +xsltproc --param base.dir "'./$build_dir/'" $xslfile manual/$language/$manualfile -if [ $type == "html" ]; then +if [ $type == "html" ] && [ $test == "0" ]; then $php -q distribute_html.php $language -fi \ No newline at end of file +fi http://cvs.php.net/co.php/php-gtk-doc/live.php?r=1.1&p=1 Index: php-gtk-doc/live.php +++ php-gtk-doc/live.php <?php /** * PHP-Gtk2 Livedocs server file * * see liveGen.php for an explanation * * @author Christian Weiske <cweiske@cweiske.de> */ class PhpGtkLiveDocs { var $strLang = 'en'; function PhpGtkLiveDocs() { $id = $this->getId(); if ($this->genManual($id)) { //apache has some problems header('Content-Type: text/html; charset=UTF-8'); echo $this->replaceLinks($this->getGeneratedFileContents($id)); } else { die("Error generating testmanual"); } }//function PhpGtkLiveDocs() /** * just returns the base path below which the manual files should be located */ function getBasePath() { return 'build/' . $this->strLang . '/test/'; }//function getBasePath() function getId() { if (!isset($_GET['id'])) { return 'gtk.gtkaboutdialog'; } return $_GET['id']; }//function getId() function genManual($id) { /* $hdl = popen('./gen_manual.sh en test ' . $id . ' 2>&1', 'r'); var_dump(fread($hdl, 2048)); pclose($hdl);*/ shell_exec('./gen_manual.sh en test ' . $id . ' 2>&1'); return true; }//function genManual($id) function getGeneratedFileContents($id) { return file_get_contents($this->getFileFromId($id)); }//function getGeneratedFileContents($id) function getFileFromId($id) { return $this->getBasePath() . $id . '.html'; }//function getFileFromId($id) function replaceLinks($strContent) { return preg_replace( '/href="([^"]+)\\.html(#[^"]+)?"/', 'href="live.php?id=$1"', $strContent ); }//function replaceLinks($strContent) }//class PhpGtkLiveDocs new PHPGtkLiveDocs(); ?> http://cvs.php.net/co.php/php-gtk-doc/liveGen.php?r=1.1&p=1 Index: php-gtk-doc/liveGen.php +++ php-gtk-doc/liveGen.php <?php /** * let's see if I can build my own livedocs :) * * @author Christian Weiske <cweiske@cweiske.de> * * == What it does == * The main problem with the php-gtk manual is the size. It has over 2400 functions, * signals and properties. This leads to a build time of over 5 minutes, which is * far too long to test the recently written function documentation * * So we've got to issues to solve: Keep the build size very short (one or two seconds) * and make look the manual as realistic as possible, which means that the original * xsl stylesheets should be used. * As we use the original XSL files, we can't simply extract that * one method/signal/property and let xsltproc run over it. We need a complete * manual, but this has to be stripped down to the most necessary things: * - the method/signal/property itself * - predecessor and successor method/signal/property to the one as we * want the navigation links to work * - all the surrounding tags like <classet>, <book> and so * * All this is done in this php script, and you can see that it works. * * * FIXME: * - links to other functions/methods/enums/... need to be replaced / methods included * - extend it to support tutorials and other sections as well * - cache generated files, especially the large ones (gtk.functions, gtkwidget) */ class PhpGtkLiveDocGenerator { var $arReferencePrefixes = array('atk', 'gdk', 'gtk', 'pango'); //relative to manual/en directory var $arBookFiles = array('../reference.xml', '../tutorials.xml'); var $arChapters = null; var $strLang = 'en'; var $strTmpFile = 'test_manual.xml'; var $tpl_manual = null; function PhpGtkLiveDocGenerator() { global $tpl_manual; chdir(dirname(__FILE__)); $this->tpl_manual = $tpl_manual; $this->loadChapterents(); }//function PhpGtkLiveDocGenerator() /** * just returns the base path below which the manual files should be located */ function getBasePath() { return 'manual/' . $this->strLang . '/'; }//function getBasePath() /** * find the file in which $id should be described * * @param string $id The id for which the manual is built * @return string The file in which the id should be. NULL if it doesn't exist */ function findFile($id) { foreach ($this->arReferencePrefixes as $prefix) { $preflen = strlen($prefix); $dotpos = strpos($id, '.', $preflen + 1); if ($dotpos === false) { $dotpos = strlen($id); } if (substr($id, 0, $preflen + 1) == $prefix . '.') { //seems it's in the reference $part = substr($id, $preflen + 1, $dotpos - $preflen - 1); if ($part == 'method' || $part == 'functions') { $part = $prefix . '-functions'; } $file = 'reference/' . $prefix . '/' . $part . '.xml'; return $file; } } //not found yet... foreach ($this->arBookFiles as $strBookFile) { $strContent = file_get_contents($this->getBasePath() . $strBookFile); if (strpos($strContent, ' id="' . $id . '"') !== false) { return $strBookFile; } } return null; }//function findFile($id) /** * checks if the given file is a special book file */ function isBookFile($strFile) { return in_array($strFile, $this->arBookFiles); }//function isBookFile($strFile) /** * find the neighbour files to the given one */ function findNeighbours($file) { if ($file === null) { return null; } if (substr($file, 0, 10) != 'reference/') { return null; } $fileid = $this->findChapterId($file); $arLines = file('manual/reference.xml'); $arFiles = array(); $bFound = false; $strPrevious = null; $strNext = null; foreach ($arLines as $nLine => $strLine) { $strLine = trim($strLine); if ($strLine[0] == '&') { $strId = substr($strLine, 1, -1); $strFile = $this->arChapters[$strId]; $arFiles[] = $strFile; if ($strFile == $file) { $bFound = true; $strPrevious = $arFiles[count($arFiles) - 2]; continue; } } else if (substr($strLine, 0, 9) == '<classset') { $arFiles[] = 'reference.xml'; } else { continue; } if ($bFound) { $strNext = $arFiles[count($arFiles) - 1]; break; } } return array($strPrevious, $strNext); }//function findNeighbours($file) /** * parses the chapterents file and loads it into an array */ function loadChapterents() { if ($this->arChapters !== null) { return; } $arLines = file($this->getBasePath() . '/chapters.ent'); foreach ($arLines as $nLine => $strLine) { if ($nLine == 0) { continue; } $nSpacePos = strpos($strLine, ' ', 10); $strId = substr($strLine, 9, $nSpacePos - 9); $strFile = substr($strLine, $nSpacePos + 9, -4); $this->arChapters[$strId] = $strFile; } }//function loadChapterents() /** * finds the chapter id to the given filename * e.g. "reference.gtk.gtkaboutdialog" for "reference/gtk/gtkaboutdialog.xml" * uses chapters.ent for lookup * * @param string $file The filename to check * @return string The chapterid used in chapters.ent */ function findChapterId($file) { $this->loadChapterents(); foreach ($this->arChapters as $id => $strFile) { if ($strFile == $file) { return $id; } } return null; }//function findChapterId($file) /** * Finds out if the given id in the file is the first or the last id in the given file. * This is necessary as we may need to include the predecessor or * successor file to have the real manual feeling. * * @param string $id The id for which the manual is built * @param string $file The filename in which the id should be * @return array Array with two boolean values: first one tells if $id is the first one, the second if $id is the last id in the file */ function getPositionInfo($id, $file) { $arLines = file($this->getBasePath() . $file); $bFirst = false; $bLast = false; $nIds = 0; $nMyPos = -1; foreach ($arLines as $strLine) { if (preg_match('/id="[^"]+">/', $strLine)) { if (strpos($strLine, 'id="' . $id . '">') !== false) { if ($nIds == 0) { $bFirst = true; } $nMyPos = $nIds + 1; } $nIds++; } } if ($nMyPos == $nIds) { $bLast = true; } return array($bFirst, $bLast); }//function getPositionInfo($id, $file) /** * builds the manual file with its contents stripped * down to only have necessary sections for the given id * * @param string $id The id for which the test manual should be build */ function buildManualFile($id) { $manual = str_replace('&book;', $this->stripDownBook('reference.xml', $id), $this->tpl_manual); $strFile = $this->getBasePath() . $this->strTmpFile; echo 'Generating test manual file ' . $strFile . ' '; $hdl = fopen($strFile, 'w+'); fwrite($hdl, $manual); fclose($hdl); echo 'done' . "\r\n"; }//function buildManualFile($id) /** * strips down the book file (reference.xml, tutorials.xml) * and replaces the required entities * * @param string $bookfile The book file (like "reference.xml") * @param string $id The function id to generate (e.g. "gtk.method.accel_map_load") * @return string The book's contents */ function stripDownBook($bookfile, $id) { $file = $this->findFile($id); if ($this->isBookFile($file)) { return $this->stripDownBookOverview($bookfile, $id); } else { return $this->stripDownBookNormal($bookfile, $id, $file); } }//function stripDownBook($bookfile, $id) /** * implements stripping down of book files for *normal* ids, * e.g. classes and class methods * NOT class overviews */ function stripDownBookNormal($bookfile, $id, $file) { $chapid = $this->findChapterId($file); list($bIsFirst, $bIsLast) = $this->getPositionInfo($id, $file); $arNeighbours = $this->findNeighbours($file); $arNeighbourIds = array( $this->findChapterId($arNeighbours[0]), $this->findChapterId($arNeighbours[1]) ); $arLines = file('manual/' . $bookfile); $strBookContent = ''; foreach ($arLines as $nLine => $strLine) { if (substr($strLine, 0, 3) != ' &') { if ($nLine > 0) { $strBookContent .= $strLine; } continue; } $strLine2 = trim($strLine); $nFileType = 0; //the $nFileType = <number> assignments in the if are intended if ( ($strLine2 == '&' . $chapid . ';' && ($nFileType = 0) !== false) || ($arNeighbourIds[0] !== null && $bIsFirst && $strLine2 == '&' . $arNeighbourIds[0] . ';' && ($nFileType = -1) !== false) || ($arNeighbourIds[1] !== null && $bIsLast && $strLine2 == '&' . $arNeighbourIds[1] . ';' && ($nFileType = 1) !== false) ) { $strBookContent .= $this->getFileContents(substr($strLine2, 1, -1), $id, $nFileType); } } //Fixme: remove empty classsets and such unneeded things return $strBookContent; }//function stripDownBookNormal($bookfile, $id, $file) /** * strips down the book itself and includes all the (stripped down) classes below it * especially reference.xml * * @return string The stripped & extendet book */ function stripDownBookOverview($bookfile, $id) { $arLines = file('manual/' . $bookfile); $strBookContent = ''; $bInIt = false; foreach ($arLines as $nLine => $strLine) { $strLine2 = trim($strLine); if (substr($strLine, 0, 3) != ' &') { if ($nLine > 0) { if ($strLine2 == '<classset id="' . $id . '">') { $bInIt = true; } else if ($bInIt && $strLine2 == '</classset>') { $bInIt = false; } $strBookContent .= $strLine; } continue; } if ($bInIt) { $strBookContent .= $this->getFileContents(substr($strLine2, 1, -1), $id, 1); } } //Fixme: remove empty classsets and such unneeded things return $strBookContent; }//function stripDownBookOverview($bookfile, $id) /** * loads the file's contents, strips them down to contain only * required ids (top id, function id itself, predecessor and successor functions of the id's function itself) * * @param string $refid The id which the file in the book (e.g. reference.xml) has * @param string $id id of the function to be displayed in live * @param int $nFileType 0 if its the file itself, -1 if the predecessor, 1 for the successor */ function getFileContents($refid, $id, $nFileType) { $arContents = file($this->getBasePath() . $this->arChapters[$refid]); $strStrippedContents = ''; $bWaiting = false; $strTag = ''; $nIdNum = 0;//number of current id $nIdPos = 0;//position of $id in id array if ($nFileType == 0) { //we're in the main file of the given id and need the position of the id among all other ids $arMatches = array(); preg_match_all('/ id="([^"]+)"/', implode("\r\n", $arContents), $arMatches); unset($arMatches[0]); $nMaxId = count($arMatches[1]); //find the right position foreach ($arMatches[1] as $nThisIdPos => $strThisId) { if ($strThisId == $id) { $nIdPos = $nThisIdPos + 1; break; } } } else { $nMaxId = substr_count(implode("\r\n", $arContents), ' id="'); } foreach ($arContents as $strLine) { if (!$bWaiting) { //find an id $nPos = strpos($strLine, ' id='); if ($nPos !== false) { $nIdNum++; if ($nIdNum == 1 || $nIdPos == 1 || ($nFileType == -1 && $nIdNum == $nMaxId) || ($nFileType == 0 && abs($nIdPos - $nIdNum) <= 1) ) { $strStrippedContents .= $strLine; } else { $nPosOpen = strpos($strLine, '<') + 1; $strTag = substr($strLine, $nPosOpen, $nPos - $nPosOpen); $bWaiting = true; } } else { if (trim($strLine) != '') { $strStrippedContents .= $strLine; } } } else { //find closing tag if (strpos($strLine, '</' . $strTag . '>') !== false) { $bWaiting = false; } } } return $strStrippedContents; }//function getFileContents($refid, $nFileType) }//class PhpGtkLiveDocGenerator $tpl_manual = <<<EOD <?xml version='1.0' encoding='utf-8' ?> <!DOCTYPE book PUBLIC "-//PHP Group//DTD DocBk XML V3.1.7-Based Extension//EN" "../../dbxml/phpgtkdoc.dtd"[ <!ENTITY % language-defs SYSTEM "language-defs.ent"> %language-defs; <!ENTITY phpgtk2doc.build-date "PHPGTKLIVEDOCS"> <!ENTITY % global.entities SYSTEM "../global.ent"> %global.entities; ]> <set id="phpgtk.docs" lang="en"> &book; </set> EOD; if (!isset($GLOBALS['liveUnitTest']) || !$GLOBALS['liveUnitTest']) { $ld = new PhpGtkLiveDocGenerator(); if (!isset($argv[1])) { die('Pass the ID as parameter' . "\r\n"); } $ld->buildManualFile($argv[1]); } ?> http://cvs.php.net/co.php/php-gtk-doc/liveTest.php?r=1.1&p=1 Index: php-gtk-doc/liveTest.php +++ php-gtk-doc/liveTest.php <?php /** * unit tests for the PHP-Gtk2 LiveDocs * * @author Christian Weiske <cweiske@cweiske.de> */ require_once 'PHPUnit.php'; $GLOBALS['liveUnitTest'] = true; require_once('liveGen.php'); class PhpGtkLiveDocGeneratorTest extends PHPUnit_TestCase { var $ld = null; // constructor of the test suite function PhpGtkLiveDocGenerator($name) { $this->PHPUnit_TestCase($name); } function setUp() { $this->ld = new PhpGtkLiveDocGenerator(); } function testFindFile() { $this->assertEquals('reference/gtk/gtkbox.xml', $this->ld->findFile('gtk.gtkbox.method.get_homogeneous')); $this->assertEquals('reference/gtk/gtkbox.xml', $this->ld->findFile('gtk.gtkbox')); $this->assertEquals('reference/gtk/gtk-functions.xml', $this->ld->findFile('gtk.functions')); $this->assertEquals('reference/gtk/gtk-functions.xml', $this->ld->findFile('gtk.method.gtk_about_dialog_get_type')); $this->assertEquals('reference/gtk/gtkbutton.xml', $this->ld->findFile('gtk.gtkbutton.signal.pressed')); $this->assertEquals('reference/gtk/gtkwidget.xml', $this->ld->findFile('gtk.gtkwidget.signal.property-notify-event')); $this->assertEquals('reference/gtk/gtkwidget.xml', $this->ld->findFile('gtk.gtkwidget.property.allocation')); $this->assertEquals('../reference.xml', $this->ld->findFile('gtkclasses')); $this->assertEquals('../reference.xml', $this->ld->findFile('gdkclasses')); } function testIsBookFile() { $this->assertTrue($this->ld->isBookFile('../reference.xml')); $this->assertTrue($this->ld->isBookFile('../tutorials.xml')); $this->assertFalse($this->ld->isBookFile('reference/gtk/gtkbox.xml')); $this->assertFalse($this->ld->isBookFile('dumptida.xml')); } function testLoadChapterents() { $this->ld->loadChapterents(); $this->assertTrue(isset($this->ld->arChapters['reference.gdk.gdkdevice'])); $this->assertEquals('reference/gdk/gdkdevice.xml', $this->ld->arChapters['reference.gdk.gdkdevice']); } function testFindChapterId() { $this->assertEquals('reference.gtk.gtkcolorselection', $this->ld->findChapterId('reference/gtk/gtkcolorselection.xml')); } function testFindNeighbours() { $this->assertEquals( array('reference/gtk/gtkbin.xml', 'reference/gtk/gtkbutton.xml'), $this->ld->findNeighbours('reference/gtk/gtkbox.xml') ); $this->assertEquals( array('reference/atk/atkstateset.xml', 'reference.xml'), $this->ld->findNeighbours('reference/atk/atkutil.xml') ); $this->assertEquals( array('reference.xml', 'reference/gdk/gdkcolormap.xml'), $this->ld->findNeighbours('reference/gdk/gdk-functions.xml') ); } function testGetPositionInfo() { $this->assertEquals( array(true, false), $this->ld->getPositionInfo('gtk.functions', $this->ld->findFile('gtk.functions')) ); $this->assertEquals( array(false, false), $this->ld->getPositionInfo('gtk.method.gtk_about_dialog_get_type', $this->ld->findFile('gtk.method.gtk_about_dialog_get_type')) ); $this->assertEquals( array(false, true), $this->ld->getPositionInfo('gtk.method.window_set_default_icon_name', $this->ld->findFile('gtk.method.window_set_default_icon_name')) ); }//function testGetPositionInfo() }//class PhpGtkLiveDocsTest extends PHPUnit_TestCase $suite = new PHPUnit_TestSuite( "PhpGtkLiveDocGeneratorTest"); $result = PHPUnit::run( $suite); echo $result->toString(); ?>
http://cvs.php.net/diff.php/php-gtk-doc/gen_manual.sh?r1=1.1&r2=1.2&ty=u Index: php-gtk-doc/gen_manual.sh diff -u php-gtk-doc/gen_manual.sh:1.1 php-gtk-doc/gen_manual.sh:1.2 --- php-gtk-doc/gen_manual.sh:1.1 Mon Apr 11 17:04:07 2005 +++ php-gtk-doc/gen_manual.sh Mon May 2 08:24:28 2005 @@ -5,7 +5,7 @@ if [ $# -lt 2 ]; then echo "Usage: ./gen_manual.sh <language> <type>" - echo "type: html phpweb" + echo "type: html phpweb test" exit 1 fi @@ -14,6 +14,8 @@ stylesheetdir=stylesheets php=php +manualfile=manual.xml +test=0 if [ ! -d manual/$language ]; then echo "There is no manual for language $language" @@ -28,6 +30,20 @@ mkdir build/$language fi +if [ $type == "test" ]; then + if [ $# -lt 3 ]; then + echo "Please pass the id as third parameter" + exit 4 + fi + test=1 + manualfile=test_manual.xml + type=html + + #generate the file + $php -q liveGen.php $3 +fi + + if [ $type == "html" ]; then build_dir=build/$language/html xslfile=$stylesheetdir/html/chunk.xsl @@ -39,7 +55,14 @@ exit 3 fi +if [ $test == "1" ]; then + build_dir=build/$language/test +fi + +# +# we've got all necessary data; generate the manual +# if [ ! -d $build_dir ]; then mkdir $build_dir else @@ -47,8 +70,8 @@ rm -R $build_dir/* fi -xsltproc --param base.dir "'./$build_dir/'" $xslfile manual/$language/manual.xml +xsltproc --param base.dir "'./$build_dir/'" $xslfile manual/$language/$manualfile -if [ $type == "html" ]; then +if [ $type == "html" ] && [ $test == "0" ]; then $php -q distribute_html.php $language -fi \ No newline at end of file +fi http://cvs.php.net/co.php/php-gtk-doc/live.php?r=1.1&p=1 Index: php-gtk-doc/live.php +++ php-gtk-doc/live.php <?php /** * PHP-Gtk2 Livedocs server file * * see liveGen.php for an explanation * * @author Christian Weiske <cweiske@cweiske.de> */ class PhpGtkLiveDocs { var $strLang = 'en'; function PhpGtkLiveDocs() { $id = $this->getId(); if ($this->genManual($id)) { //apache has some problems header('Content-Type: text/html; charset=UTF-8'); echo $this->replaceLinks($this->getGeneratedFileContents($id)); } else { die("Error generating testmanual"); } }//function PhpGtkLiveDocs() /** * just returns the base path below which the manual files should be located */ function getBasePath() { return 'build/' . $this->strLang . '/test/'; }//function getBasePath() function getId() { if (!isset($_GET['id'])) { return 'gtk.gtkaboutdialog'; } return $_GET['id']; }//function getId() function genManual($id) { /* $hdl = popen('./gen_manual.sh en test ' . $id . ' 2>&1', 'r'); var_dump(fread($hdl, 2048)); pclose($hdl);*/ shell_exec('./gen_manual.sh en test ' . $id . ' 2>&1'); return true; }//function genManual($id) function getGeneratedFileContents($id) { return file_get_contents($this->getFileFromId($id)); }//function getGeneratedFileContents($id) function getFileFromId($id) { return $this->getBasePath() . $id . '.html'; }//function getFileFromId($id) function replaceLinks($strContent) { return preg_replace( '/href="([^"]+)\\.html(#[^"]+)?"/', 'href="live.php?id=$1"', $strContent ); }//function replaceLinks($strContent) }//class PhpGtkLiveDocs new PHPGtkLiveDocs(); ?> http://cvs.php.net/co.php/php-gtk-doc/liveGen.php?r=1.1&p=1 Index: php-gtk-doc/liveGen.php +++ php-gtk-doc/liveGen.php <?php /** * let's see if I can build my own livedocs :) * * @author Christian Weiske <cweiske@cweiske.de> * * == What it does == * The main problem with the php-gtk manual is the size. It has over 2400 functions, * signals and properties. This leads to a build time of over 5 minutes, which is * far too long to test the recently written function documentation * * So we've got to issues to solve: Keep the build size very short (one or two seconds) * and make look the manual as realistic as possible, which means that the original * xsl stylesheets should be used. * As we use the original XSL files, we can't simply extract that * one method/signal/property and let xsltproc run over it. We need a complete * manual, but this has to be stripped down to the most necessary things: * - the method/signal/property itself * - predecessor and successor method/signal/property to the one as we * want the navigation links to work * - all the surrounding tags like <classet>, <book> and so * * All this is done in this php script, and you can see that it works. * * * FIXME: * - links to other functions/methods/enums/... need to be replaced / methods included * - extend it to support tutorials and other sections as well * - cache generated files, especially the large ones (gtk.functions, gtkwidget) */ class PhpGtkLiveDocGenerator { var $arReferencePrefixes = array('atk', 'gdk', 'gtk', 'pango'); //relative to manual/en directory var $arBookFiles = array('../reference.xml', '../tutorials.xml'); var $arChapters = null; var $strLang = 'en'; var $strTmpFile = 'test_manual.xml'; var $tpl_manual = null; function PhpGtkLiveDocGenerator() { global $tpl_manual; chdir(dirname(__FILE__)); $this->tpl_manual = $tpl_manual; $this->loadChapterents(); }//function PhpGtkLiveDocGenerator() /** * just returns the base path below which the manual files should be located */ function getBasePath() { return 'manual/' . $this->strLang . '/'; }//function getBasePath() /** * find the file in which $id should be described * * @param string $id The id for which the manual is built * @return string The file in which the id should be. NULL if it doesn't exist */ function findFile($id) { foreach ($this->arReferencePrefixes as $prefix) { $preflen = strlen($prefix); $dotpos = strpos($id, '.', $preflen + 1); if ($dotpos === false) { $dotpos = strlen($id); } if (substr($id, 0, $preflen + 1) == $prefix . '.') { //seems it's in the reference $part = substr($id, $preflen + 1, $dotpos - $preflen - 1); if ($part == 'method' || $part == 'functions') { $part = $prefix . '-functions'; } $file = 'reference/' . $prefix . '/' . $part . '.xml'; return $file; } } //not found yet... foreach ($this->arBookFiles as $strBookFile) { $strContent = file_get_contents($this->getBasePath() . $strBookFile); if (strpos($strContent, ' id="' . $id . '"') !== false) { return $strBookFile; } } return null; }//function findFile($id) /** * checks if the given file is a special book file */ function isBookFile($strFile) { return in_array($strFile, $this->arBookFiles); }//function isBookFile($strFile) /** * find the neighbour files to the given one */ function findNeighbours($file) { if ($file === null) { return null; } if (substr($file, 0, 10) != 'reference/') { return null; } $fileid = $this->findChapterId($file); $arLines = file('manual/reference.xml'); $arFiles = array(); $bFound = false; $strPrevious = null; $strNext = null; foreach ($arLines as $nLine => $strLine) { $strLine = trim($strLine); if ($strLine[0] == '&') { $strId = substr($strLine, 1, -1); $strFile = $this->arChapters[$strId]; $arFiles[] = $strFile; if ($strFile == $file) { $bFound = true; $strPrevious = $arFiles[count($arFiles) - 2]; continue; } } else if (substr($strLine, 0, 9) == '<classset') { $arFiles[] = 'reference.xml'; } else { continue; } if ($bFound) { $strNext = $arFiles[count($arFiles) - 1]; break; } } return array($strPrevious, $strNext); }//function findNeighbours($file) /** * parses the chapterents file and loads it into an array */ function loadChapterents() { if ($this->arChapters !== null) { return; } $arLines = file($this->getBasePath() . '/chapters.ent'); foreach ($arLines as $nLine => $strLine) { if ($nLine == 0) { continue; } $nSpacePos = strpos($strLine, ' ', 10); $strId = substr($strLine, 9, $nSpacePos - 9); $strFile = substr($strLine, $nSpacePos + 9, -4); $this->arChapters[$strId] = $strFile; } }//function loadChapterents() /** * finds the chapter id to the given filename * e.g. "reference.gtk.gtkaboutdialog" for "reference/gtk/gtkaboutdialog.xml" * uses chapters.ent for lookup * * @param string $file The filename to check * @return string The chapterid used in chapters.ent */ function findChapterId($file) { $this->loadChapterents(); foreach ($this->arChapters as $id => $strFile) { if ($strFile == $file) { return $id; } } return null; }//function findChapterId($file) /** * Finds out if the given id in the file is the first or the last id in the given file. * This is necessary as we may need to include the predecessor or * successor file to have the real manual feeling. * * @param string $id The id for which the manual is built * @param string $file The filename in which the id should be * @return array Array with two boolean values: first one tells if $id is the first one, the second if $id is the last id in the file */ function getPositionInfo($id, $file) { $arLines = file($this->getBasePath() . $file); $bFirst = false; $bLast = false; $nIds = 0; $nMyPos = -1; foreach ($arLines as $strLine) { if (preg_match('/id="[^"]+">/', $strLine)) { if (strpos($strLine, 'id="' . $id . '">') !== false) { if ($nIds == 0) { $bFirst = true; } $nMyPos = $nIds + 1; } $nIds++; } } if ($nMyPos == $nIds) { $bLast = true; } return array($bFirst, $bLast); }//function getPositionInfo($id, $file) /** * builds the manual file with its contents stripped * down to only have necessary sections for the given id * * @param string $id The id for which the test manual should be build */ function buildManualFile($id) { $manual = str_replace('&book;', $this->stripDownBook('reference.xml', $id), $this->tpl_manual); $strFile = $this->getBasePath() . $this->strTmpFile; echo 'Generating test manual file ' . $strFile . ' '; $hdl = fopen($strFile, 'w+'); fwrite($hdl, $manual); fclose($hdl); echo 'done' . "\r\n"; }//function buildManualFile($id) /** * strips down the book file (reference.xml, tutorials.xml) * and replaces the required entities * * @param string $bookfile The book file (like "reference.xml") * @param string $id The function id to generate (e.g. "gtk.method.accel_map_load") * @return string The book's contents */ function stripDownBook($bookfile, $id) { $file = $this->findFile($id); if ($this->isBookFile($file)) { return $this->stripDownBookOverview($bookfile, $id); } else { return $this->stripDownBookNormal($bookfile, $id, $file); } }//function stripDownBook($bookfile, $id) /** * implements stripping down of book files for *normal* ids, * e.g. classes and class methods * NOT class overviews */ function stripDownBookNormal($bookfile, $id, $file) { $chapid = $this->findChapterId($file); list($bIsFirst, $bIsLast) = $this->getPositionInfo($id, $file); $arNeighbours = $this->findNeighbours($file); $arNeighbourIds = array( $this->findChapterId($arNeighbours[0]), $this->findChapterId($arNeighbours[1]) ); $arLines = file('manual/' . $bookfile); $strBookContent = ''; foreach ($arLines as $nLine => $strLine) { if (substr($strLine, 0, 3) != ' &') { if ($nLine > 0) { $strBookContent .= $strLine; } continue; } $strLine2 = trim($strLine); $nFileType = 0; //the $nFileType = <number> assignments in the if are intended if ( ($strLine2 == '&' . $chapid . ';' && ($nFileType = 0) !== false) || ($arNeighbourIds[0] !== null && $bIsFirst && $strLine2 == '&' . $arNeighbourIds[0] . ';' && ($nFileType = -1) !== false) || ($arNeighbourIds[1] !== null && $bIsLast && $strLine2 == '&' . $arNeighbourIds[1] . ';' && ($nFileType = 1) !== false) ) { $strBookContent .= $this->getFileContents(substr($strLine2, 1, -1), $id, $nFileType); } } //Fixme: remove empty classsets and such unneeded things return $strBookContent; }//function stripDownBookNormal($bookfile, $id, $file) /** * strips down the book itself and includes all the (stripped down) classes below it * especially reference.xml * * @return string The stripped & extendet book */ function stripDownBookOverview($bookfile, $id) { $arLines = file('manual/' . $bookfile); $strBookContent = ''; $bInIt = false; foreach ($arLines as $nLine => $strLine) { $strLine2 = trim($strLine); if (substr($strLine, 0, 3) != ' &') { if ($nLine > 0) { if ($strLine2 == '<classset id="' . $id . '">') { $bInIt = true; } else if ($bInIt && $strLine2 == '</classset>') { $bInIt = false; } $strBookContent .= $strLine; } continue; } if ($bInIt) { $strBookContent .= $this->getFileContents(substr($strLine2, 1, -1), $id, 1); } } //Fixme: remove empty classsets and such unneeded things return $strBookContent; }//function stripDownBookOverview($bookfile, $id) /** * loads the file's contents, strips them down to contain only * required ids (top id, function id itself, predecessor and successor functions of the id's function itself) * * @param string $refid The id which the file in the book (e.g. reference.xml) has * @param string $id id of the function to be displayed in live * @param int $nFileType 0 if its the file itself, -1 if the predecessor, 1 for the successor */ function getFileContents($refid, $id, $nFileType) { $arContents = file($this->getBasePath() . $this->arChapters[$refid]); $strStrippedContents = ''; $bWaiting = false; $strTag = ''; $nIdNum = 0;//number of current id $nIdPos = 0;//position of $id in id array if ($nFileType == 0) { //we're in the main file of the given id and need the position of the id among all other ids $arMatches = array(); preg_match_all('/ id="([^"]+)"/', implode("\r\n", $arContents), $arMatches); unset($arMatches[0]); $nMaxId = count($arMatches[1]); //find the right position foreach ($arMatches[1] as $nThisIdPos => $strThisId) { if ($strThisId == $id) { $nIdPos = $nThisIdPos + 1; break; } } } else { $nMaxId = substr_count(implode("\r\n", $arContents), ' id="'); } foreach ($arContents as $strLine) { if (!$bWaiting) { //find an id $nPos = strpos($strLine, ' id='); if ($nPos !== false) { $nIdNum++; if ($nIdNum == 1 || $nIdPos == 1 || ($nFileType == -1 && $nIdNum == $nMaxId) || ($nFileType == 0 && abs($nIdPos - $nIdNum) <= 1) ) { $strStrippedContents .= $strLine; } else { $nPosOpen = strpos($strLine, '<') + 1; $strTag = substr($strLine, $nPosOpen, $nPos - $nPosOpen); $bWaiting = true; } } else { if (trim($strLine) != '') { $strStrippedContents .= $strLine; } } } else { //find closing tag if (strpos($strLine, '</' . $strTag . '>') !== false) { $bWaiting = false; } } } return $strStrippedContents; }//function getFileContents($refid, $nFileType) }//class PhpGtkLiveDocGenerator $tpl_manual = <<<EOD <?xml version='1.0' encoding='utf-8' ?> <!DOCTYPE book PUBLIC "-//PHP Group//DTD DocBk XML V3.1.7-Based Extension//EN" "../../dbxml/phpgtkdoc.dtd"[ <!ENTITY % language-defs SYSTEM "language-defs.ent"> %language-defs; <!ENTITY phpgtk2doc.build-date "PHPGTKLIVEDOCS"> <!ENTITY % global.entities SYSTEM "../global.ent"> %global.entities; ]> <set id="phpgtk.docs" lang="en"> &book; </set> EOD; if (!isset($GLOBALS['liveUnitTest']) || !$GLOBALS['liveUnitTest']) { $ld = new PhpGtkLiveDocGenerator(); if (!isset($argv[1])) { die('Pass the ID as parameter' . "\r\n"); } $ld->buildManualFile($argv[1]); } ?> http://cvs.php.net/co.php/php-gtk-doc/liveTest.php?r=1.1&p=1 Index: php-gtk-doc/liveTest.php +++ php-gtk-doc/liveTest.php <?php /** * unit tests for the PHP-Gtk2 LiveDocs * * @author Christian Weiske <cweiske@cweiske.de> */ require_once 'PHPUnit.php'; $GLOBALS['liveUnitTest'] = true; require_once('liveGen.php'); class PhpGtkLiveDocGeneratorTest extends PHPUnit_TestCase { var $ld = null; // constructor of the test suite function PhpGtkLiveDocGenerator($name) { $this->PHPUnit_TestCase($name); } function setUp() { $this->ld = new PhpGtkLiveDocGenerator(); } function testFindFile() { $this->assertEquals('reference/gtk/gtkbox.xml', $this->ld->findFile('gtk.gtkbox.method.get_homogeneous')); $this->assertEquals('reference/gtk/gtkbox.xml', $this->ld->findFile('gtk.gtkbox')); $this->assertEquals('reference/gtk/gtk-functions.xml', $this->ld->findFile('gtk.functions')); $this->assertEquals('reference/gtk/gtk-functions.xml', $this->ld->findFile('gtk.method.gtk_about_dialog_get_type')); $this->assertEquals('reference/gtk/gtkbutton.xml', $this->ld->findFile('gtk.gtkbutton.signal.pressed')); $this->assertEquals('reference/gtk/gtkwidget.xml', $this->ld->findFile('gtk.gtkwidget.signal.property-notify-event')); $this->assertEquals('reference/gtk/gtkwidget.xml', $this->ld->findFile('gtk.gtkwidget.property.allocation')); $this->assertEquals('../reference.xml', $this->ld->findFile('gtkclasses')); $this->assertEquals('../reference.xml', $this->ld->findFile('gdkclasses')); } function testIsBookFile() { $this->assertTrue($this->ld->isBookFile('../reference.xml')); $this->assertTrue($this->ld->isBookFile('../tutorials.xml')); $this->assertFalse($this->ld->isBookFile('reference/gtk/gtkbox.xml')); $this->assertFalse($this->ld->isBookFile('dumptida.xml')); } function testLoadChapterents() { $this->ld->loadChapterents(); $this->assertTrue(isset($this->ld->arChapters['reference.gdk.gdkdevice'])); $this->assertEquals('reference/gdk/gdkdevice.xml', $this->ld->arChapters['reference.gdk.gdkdevice']); } function testFindChapterId() { $this->assertEquals('reference.gtk.gtkcolorselection', $this->ld->findChapterId('reference/gtk/gtkcolorselection.xml')); } function testFindNeighbours() { $this->assertEquals( array('reference/gtk/gtkbin.xml', 'reference/gtk/gtkbutton.xml'), $this->ld->findNeighbours('reference/gtk/gtkbox.xml') ); $this->assertEquals( array('reference/atk/atkstateset.xml', 'reference.xml'), $this->ld->findNeighbours('reference/atk/atkutil.xml') ); $this->assertEquals( array('reference.xml', 'reference/gdk/gdkcolormap.xml'), $this->ld->findNeighbours('reference/gdk/gdk-functions.xml') ); } function testGetPositionInfo() { $this->assertEquals( array(true, false), $this->ld->getPositionInfo('gtk.functions', $this->ld->findFile('gtk.functions')) ); $this->assertEquals( array(false, false), $this->ld->getPositionInfo('gtk.method.gtk_about_dialog_get_type', $this->ld->findFile('gtk.method.gtk_about_dialog_get_type')) ); $this->assertEquals( array(false, true), $this->ld->getPositionInfo('gtk.method.window_set_default_icon_name', $this->ld->findFile('gtk.method.window_set_default_icon_name')) ); }//function testGetPositionInfo() }//class PhpGtkLiveDocsTest extends PHPUnit_TestCase $suite = new PHPUnit_TestSuite( "PhpGtkLiveDocGeneratorTest"); $result = PHPUnit::run( $suite); echo $result->toString(); ?>