CodeDoc/Data/Docbook.php
[PHP_CodeDoc] / CodeDoc / Data / Docbook.php
1 <?php
2
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license,      |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available at through the world-wide-web at                           |
11 // | http://www.php.net/license/2_02.txt.                                 |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Authors: Alan Knowles <alan@akbkhome.com>                      |
17 // +----------------------------------------------------------------------+
18 //
19 /**
20 *   Docbook data container (for pages and sections)
21 *
22 *   @package  PHP_CodeDoc
23 *   @access   public
24 *   @author   Alan Knowles <alan@akbkhome.com>
25 *
26 */
27 class PHP_CodeDoc_Data_Docbook  {
28     var $pages = array();        //
29     var $name;        // name of parameter
30     var $title;         // title 
31     var $tagname;        // name that appears in url
32     var $sections = array(); // array of sections;
33     var $paras = array();     // array of paragraphs;
34     var $Operations = array();  // book methods
35     var $iOperations = array(); // book iherited Methods
36  
37     var $_activeSection; // link to current section
38     var $_activePage;    // link to active page.
39  
40     function outputDocbook() {
41         // should read an array from para's and output the correct docbook/xml tags
42         
43         foreach ($this->paras as  $para) {
44             echo "<para>\n";
45             if (is_array($para)) {
46                 switch ($para[0]) {
47                     case "LIST":
48                         $this->_outputList($para[1]);
49                         break;
50                     default:
51                         echo "UNKNOWN PARA";
52                 }
53                 
54             } else {
55                 echo htmlspecialchars($para) . "\n";
56             }
57             echo "</para>\n";
58         }
59         
60     }
61     
62     function _outputList($items) {
63         echo "<itemizedlist>\n";
64         foreach($items as $item) {
65             if (trim($item) == "") {    
66                 continue;
67             }
68             echo "  <listitem>\n    ". htmlspecialchars($item) ."\n  </listitem>\n";
69         }
70         echo "<itemizedlist>\n";
71     }
72     
73     function outputSummary() {
74     
75         echo $this->pages['INDEX']->paras[0];
76         unset($this->pages['INDEX']);
77     }
78     
79     
80     
81     function parse($comment) {
82         $ret = "";
83         $flag_inlist = FALSE;
84         $lines = explode("\n",$comment);
85         $buffer = "";
86         foreach ($lines as $line) {
87             // get rid of trailing *
88             $line = preg_replace("/^\s*\/*[\*]+\/*/","",$line);
89             
90             //$line = preg_replace("/^\s*\*/","",$line);
91             $bits = explode(' ', strtolower(trim($line)));
92             //echo "CASE {$bits[0]}\n";
93             switch ($bits[0]) {
94                 case "@docbook":
95                     break;
96                 case "@docbook_page":
97                     $page = preg_replace("/\s*\@docbook_page\s*/i","",$line);
98                     $this->addPara($buffer);
99                     $buffer ="";
100                     $this->addPage($page);
101                     break;
102                 case "@docbook_section":
103                     $section = preg_replace("/\s*\@docbook_section\s*/i","",$line);
104                     $this->addPara($buffer);
105                     $buffer ="";
106                     $this->addSection($section);
107                     break;
108                 case "@docbook_page_summary":
109                     $summary = preg_replace("/\s*\@docbook_page_summary\s*/i","",$line);
110                     $this->addPara($buffer);
111                     $buffer ="";
112                     $this->addPageSummary($summary);
113                     break;
114                      
115                 case "@docbook_package_section":
116                     
117                     $this->addPara($buffer);
118                     $buffer ="";
119                     $ps = preg_replace("/\s*\@docbook_package_section\s*/i","",$line);
120                     $ret = trim($ps);
121                     break;
122                  case "@docbook_package_title":
123                     $this->addPara($buffer);
124                     $buffer ="";
125                     $ps = preg_replace("/\s*\@docbook_package_title\s*/i","",$line);
126                     $this->title = trim($ps);
127                     break;        
128                 case "@docbook_list":
129                     $this->addPara($buffer);
130                     $flag_inlist = TRUE;
131                     $buffer ="";
132                     break;
133                                             /* Standard phpdoc stuff */
134                 case "@param":
135                 case "@return": 
136                 case "@access":         
137                 case "@author":         
138                 case "@copyright":
139                 case "@version":
140                 case "@see": 
141                 case "@link":   
142                 case "@since":          
143                 case "@deprecated":
144                 case "@deprec": 
145                 case "@magic":  
146                 case "@todo":   
147                 case "@exception":      
148                 case "@throws":         
149                 case "@var":    
150                 case "@package":        
151                 case "@subpackage":
152                     break;
153
154                 default:
155                     if (trim($line) == "") {
156                         if (trim($buffer) == "") {
157                             break;
158                         }
159                         if ($flag_inlist) {
160                             $this->addList($buffer);
161                             $flag_inlist= FALSE;
162                         } else {
163                             $this->addPara($buffer);
164                         }
165                         $buffer= "";
166                         break;
167                     }
168                     $buffer .= $line . "\n";
169                     
170             }
171             
172         }
173         $this->addPara($buffer);
174         return $ret;
175     }
176     
177     
178     function &addPage($page) {
179         $page = trim($page);
180          
181         $this->pages[$page]  =  new PHP_CodeDoc_Data_Docbook;
182         /* create links .. */
183         $this->_activeSection = &$this->pages[$page];
184         $this->_activePage    = &$this->pages[$page];
185         $this->_activePage->type = 'Page';
186         unset($this->_activePage->pages);
187          /* set stuff */
188         $this->_activePage->name = $page;
189         $this->_activePage->title = $page;
190         $this->_activePage->tagname = preg_replace("/[^a-z0-9]/i", "-",trim($page));
191         return $this->pages[$page];  
192     }
193     function addPageSummary($summary) {
194         $this->_activePage->summary = $summary;
195     }
196     
197     function addSection($section) {
198         if (!$this->_activePage->type == 'Page') {
199                 $this->addPage($section);
200         }
201         $section = trim($section);
202         $this->_activePage->sections[$section] =  new PHP_CodeDoc_Data_Docbook;
203         $this->_activeSection = &$this->_activePage->sections[$section];
204         $this->_activeSection->name = $section;
205         $this->_activeSection->title = $section;
206         unset($this->_activeSection->pages);
207         unset($this->_activeSection->sections);
208     }
209     function addList($lines) {
210         $this->_activeSection->paras[] = array("LIST", explode("\n",$lines));
211     }
212     function addPara($lines) {
213         if ($lines == "") {
214             return;
215         }
216         $this->_activeSection->paras[] = $lines;
217         
218     }
219     function setMethod(&$method) {
220         $method->mergePhpDoc();
221         $this->_activeSection->method = &$method;
222     }
223     function mergeMethods() {
224         
225         foreach(array_keys($this->Operations) as $id) {
226             $method = &$this->Operations[$id];
227             if ($method->name{0} == "_") {
228                 continue;
229             }
230             
231             $this->addSection($method->class . "::". $method->name);
232             
233             $this->setMethod($method);
234             $this->parse($method->description->original);
235         }    
236         foreach(array_keys($this->iOperations) as $id) {
237                 $this->
238             $method = &$this->iOperations[$id];
239             if ($this->name{0} == "_") {
240                 continue;
241             }
242             
243             $this->addSection($method->class . "::". $method->name);
244             $this->parse($method->description->original);
245             $this->setMethod($method);
246         }
247     }
248      
249     
250 }
251