3 * Parser for XML DocBook-based phpDocumentor tutorials
\r
5 * phpDocumentor :: automatic documentation generator
\r
7 * PHP versions 4 and 5
\r
9 * Copyright (c) 2002-2007 Gregory Beaver
\r
13 * This library is free software; you can redistribute it
\r
14 * and/or modify it under the terms of the GNU Lesser General
\r
15 * Public License as published by the Free Software Foundation;
\r
16 * either version 2.1 of the License, or (at your option) any
\r
19 * This library is distributed in the hope that it will be useful,
\r
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
22 * Lesser General Public License for more details.
\r
24 * You should have received a copy of the GNU Lesser General Public
\r
25 * License along with this library; if not, write to the Free Software
\r
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
28 * @category ToolsAndUtilities
\r
29 * @package phpDocumentor
\r
30 * @subpackage Parsers
\r
31 * @author Gregory Beaver <cellog@php.net>
\r
32 * @copyright 2002-2007 Gregory Beaver
\r
33 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
\r
34 * @version CVS: $Id: XMLpackagePageParser.inc,v 1.14 2007/11/14 01:31:24 ashnazg Exp $
\r
35 * @tutorial tutorials.pkg
\r
36 * @link http://www.phpdoc.org
\r
37 * @link http://pear.php.net/PhpDocumentor
\r
39 * @todo CS cleanup - change package to PhpDocumentor
\r
40 * @todo CS cleanup - PHPCS needs to ignore CVS Id length
\r
43 * when <programlisting> is found
\r
45 define('PHPDOCUMENTOR_PDP_EVENT_PROGRAMLISTING', 600);
\r
47 * when <programlisting> is found
\r
49 define('PHPDOCUMENTOR_PDP_STATE_PROGRAMLISTING', 700);
\r
51 * when a DocBook <tag> is found
\r
53 define('PHPDOCUMENTOR_PDP_EVENT_TAG', 601);
\r
55 * when a DocBook <tag> is found
\r
57 define('PHPDOCUMENTOR_PDP_STATE_TAG', 701);
\r
59 * when <![CDATA[ ]]> is found
\r
61 define('PHPDOCUMENTOR_PDP_EVENT_CDATA', 602);
\r
63 * when <![CDATA[ ]]> is found
\r
65 define('PHPDOCUMENTOR_PDP_STATE_CDATA', 702);
\r
67 * when tag attributes name="value" are found
\r
69 define('PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES', 603);
\r
71 * when tag attributes name="value" are found
\r
73 define('PHPDOCUMENTOR_PDP_STATE_ATTRIBUTES', 703);
\r
75 * when tag attributes name="value" are found
\r
77 define('PHPDOCUMENTOR_PDP_EVENT_ENTITY', 604);
\r
79 * when tag attributes name="value" are found
\r
81 define('PHPDOCUMENTOR_PDP_STATE_ENTITY', 704);
\r
84 * Used to parse XML DocBook-based tutorials
\r
86 * @category ToolsAndUtilities
\r
87 * @package phpDocumentor
\r
88 * @subpackage Parsers
\r
89 * @author Gregory Beaver <cellog@php.net>
\r
90 * @copyright 2002-2007 Gregory Beaver
\r
91 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
\r
92 * @version Release: 1.4.1
\r
93 * @link http://www.phpdoc.org
\r
94 * @link http://pear.php.net/PhpDocumentor
\r
96 * @todo CS cleanup - change package to PhpDocumentor
\r
98 class XMLPackagePageParser extends Parser
\r
103 var $eventHandlers = array(
\r
104 PHPDOCUMENTOR_PDP_EVENT_TAG => 'handleTag',
\r
105 PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES => 'handleAttributes',
\r
106 PHPDOCUMENTOR_PDP_EVENT_CDATA => 'handleCData',
\r
107 PARSER_EVENT_NOEVENTS => 'defaultHandler',
\r
108 PARSER_EVENT_COMMENTBLOCK => 'ignoreHandler',
\r
109 PARSER_EVENT_OUTPHP => 'ignoreHandler',
\r
110 PARSER_EVENT_QUOTE => 'handleQuote',
\r
111 PHPDOCUMENTOR_PDP_EVENT_ENTITY => 'handleEntity',
\r
117 var $pars = array();
\r
119 var $refsect1id = false;
\r
120 var $refsect2id = false;
\r
121 var $refsect3id = false;
\r
123 * @var array the tag stack
\r
126 /**#@+ @access private */
\r
127 var $_gettoc = false;
\r
128 var $_toc = array();
\r
129 var $_cursection = 0;
\r
132 * Set up the wordparser
\r
136 * @uses ObjectWordParser
\r
138 function XMLPackagePageParser()
\r
140 $this->wp = new ObjectWordParser(true);
\r
145 * @param string $parse_data the parse data
\r
146 * @param array $tutorial for format, see {@link Io::getTutorials()}
\r
149 * @staticvar integer used for recursion limiting
\r
150 * if a handler for an event is not found
\r
151 * @uses parserTutorial using {@link Publisher::PublishEvent()}, a new tutorial
\r
152 * is created from the file parsed, and passed to the
\r
153 * Intermediate Parser
\r
155 function parse ($parse_data, $tutorial)
\r
157 $tempparse = new ppageParser;
\r
158 $parse_data = $tempparse->
\r
159 parse($parse_data, true, $tutorial['package'],
\r
160 $tutorial['subpackage'], basename($tutorial['path']),
\r
161 $tutorial['category'], $tutorial['path']);
\r
163 static $endrecur = 0;
\r
164 if (!is_array($parse_data) || count($parse_data) == 0) {
\r
167 $this->setupStates();
\r
169 // initialize variables so E_ALL error_reporting doesn't complain
\r
172 $this->p_vars['start'] = true;
\r
173 $this->p_vars['event_stack'] = new EventStack;
\r
175 $this->wp->setup($parse_data, false);
\r
176 $this->wp->setWhitespace(true);
\r
177 $this->context = array();
\r
178 if (isset($this->curtag)) {
\r
179 unset($this->curtag);
\r
183 $lpevent = $pevent;
\r
184 $pevent = $this->p_vars['event_stack']->getEvent();
\r
185 if ($lpevent != $pevent) {
\r
186 $this->p_vars['last_pevent'] = $lpevent;
\r
189 if ($this->p_vars['last_pevent'] != $pevent) {
\r
190 // its a new event so the word parser needs to be reconfigured
\r
191 $this->configWordParser($pevent);
\r
195 $this->p_vars['last_word'] = $word;
\r
196 $word = $this->wp->getWord();
\r
198 if (PHPDOCUMENTOR_DEBUG == true) {
\r
199 echo "----------------\n";
\r
200 echo "LAST: |" . $this->p_vars['last_word'] . "|\n";
\r
201 echo "INDEX: ".$this->p_vars['curpar']."\n";
\r
202 echo "PEVENT: " . $this->getParserEventName($pevent) . "\n";
\r
203 echo "LASTPEVENT: " .
\r
204 $this->getParserEventName($this->p_vars['last_pevent']) . "\n";
\r
205 echo $this->wp->getPos() . " WORD: |$word|\n\n";
\r
206 echo '"'.$this->p_vars['quote_data']."\"\n";
\r
208 if (isset($this->eventHandlers[$pevent])) {
\r
209 $handle = $this->eventHandlers[$pevent];
\r
210 if ($word !== false) {
\r
211 $this->$handle($word, $pevent);
\r
214 debug('WARNING: possible error, ' .
\r
215 'no XMLPackagePageParser handler for event number '. $pevent);
\r
216 if ($endrecur++ == 25) {
\r
217 die("FATAL ERROR, recursion limit reached");
\r
220 $this->p_vars['start'] = false;
\r
221 } while (!($word === false));
\r
222 if (count($this->_toc) && isset($this->p_vars['toc'])) {
\r
223 $a = $this->curtag->getTOC($this->p_vars['toc']);
\r
224 $a->setTOC($this->_toc);
\r
225 $a->setPath($tutorial['path']);
\r
226 $this->curtag->setTOC($this->p_vars['toc'], $a);
\r
228 $this->PublishEvent(PHPDOCUMENTOR_EVENT_TUTORIAL,
\r
229 new parserTutorial($this->curtag, $tutorial));
\r
230 return $this->curtag;
\r
234 * @param string|parserInlineTag $word token
\r
235 * @param integer $token parser event
\r
238 * @todo CS cleanup - PHPCS needs to recognize docblock template tags
\r
241 * handler for default events
\r
243 function defaultHandler($word, $pevent)
\r
245 if (is_string($word) && $this->checkEventPush($word, $pevent)) {
\r
251 * handler for ignore events
\r
253 function ignoreHandler($word, $pevent)
\r
255 $this->checkEventPop($word, $pevent);
\r
259 * handler for QUOTE
\r
261 * this handler recognizes strings defined with
\r
262 * double quotation marks (") and handles them correctly
\r
263 * in any place that they legally appear in php code
\r
265 function handleQuote($word, $pevent)
\r
267 if ($this->p_flags['reset_quote_data'] === true) {
\r
268 $this->p_flags['reset_quote_data'] = false;
\r
269 $this->p_vars['quote_data'] = "";
\r
271 if (!is_object($word)) {
\r
272 $this->checkEventPush($word, $pevent);
\r
274 if (is_object($word)) {
\r
275 $this->p_vars['quote_data'] = $word;
\r
277 if ($word != "\"") {
\r
278 if (!is_object($this->p_vars['quote_data'])) {
\r
279 $this->p_vars['quote_data'] .= $word;
\r
282 if ($word == '>') {
\r
283 if (is_object($this->p_vars['quote_data'])) {
\r
284 $this->p_vars['quote_data'] =
\r
285 '{@id '.$this->p_vars['quote_data']->id.'}';
\r
287 addErrorDie(PDERROR_UNTERMINATED_ATTRIB, $this->curtag->name,
\r
288 $this->p_vars['attrname'], $this->p_vars['quote_data']);
\r
290 if ($this->checkEventPop($word, $pevent)) {
\r
291 $this->p_flags['reset_quote_data'] = true;
\r
297 * Handles all XML DocBook tags
\r
299 * @todo replace commented-out debug lines with debug() func
\r
301 function handleTag($word, $pevent)
\r
303 if (isset($this->curtag) && $this->curtag->hasTitle() &&
\r
304 $this->_gettoc && $this->_gettoc->name == $this->curtag->name
\r
306 if (isset($this->_toc[$this->_cursection])) {
\r
307 $this->_toc[$this->_cursection]['title'] = $this->curtag->_title;
\r
308 $this->_cursection++;
\r
310 $this->_gettoc = false;
\r
312 if ($this->p_vars['last_word'] == '<') {
\r
314 $this->p_flags['begin_tag'] = true;
\r
315 array_push($this->context, $word);
\r
316 //DEBUG if (isset($this->curtag)) debug("pushed " . $this->curtag->name);
\r
317 if (isset($this->curtag)) {
\r
318 array_push($this->pars, $this->curtag);
\r
320 $this->curtag = new parserXMLDocBookTag($word);
\r
321 } elseif ($this->p_vars['last_word'] == '</' || $word == '/>') {
\r
322 $tag = array_pop($this->context);
\r
323 if ($word == '/>') {
\r
325 $this->checkEventPop($word, $pevent);
\r
328 if ($tag != $word) {
\r
329 addErrorDie(PDERROR_UNMATCHED_TUTORIAL_TAG,
\r
330 $tag, $word, $this->curtag->getString());
\r
332 if (in_array($this->curtag->name,
\r
333 array('refentry', 'refsect1', 'refsect2', 'refsect3'))
\r
335 if (!isset($this->curtag->_id)) {
\r
337 if (isset($this->curtag->_title)) {
\r
338 $title = $this->curtag->_title->getString();
\r
340 addWarning(PDERROR_NO_DOCBOOK_ID, $this->curtag->name, $title);
\r
343 $this->p_flags['begin_tag'] = false;
\r
344 $curtag = @array_pop($this->pars);
\r
345 //DEBUG debug("popped $tag ".$curtag->name.' I am '.$this->curtag->name);
\r
347 if ($this->curtag->name == 'refsect1') $this->refsect1id = false;
\r
348 if ($this->curtag->name == 'refsect2') $this->refsect2id = false;
\r
349 if ($this->curtag->name == 'refsect3') $this->refsect3id = false;
\r
350 $curtag->add($this->curtag);
\r
351 //DEBUG debug("added " . $this->curtag->name .
\r
352 //DEBUG " to " . $curtag->name . ' ' . $curtag->id);
\r
353 $this->curtag = $curtag;
\r
355 //DEBUG debug("here");
\r
357 } elseif (is_string($word)) {
\r
358 if (!($e = $this->checkEventPush($word, $pevent))) {
\r
359 if ($this->checkEventPop($word, $pevent)) {
\r
360 if ($this->p_flags['begin_tag']) {
\r
361 $this->p_vars['event_stack']->
\r
362 pushEvent(PHPDOCUMENTOR_PDP_EVENT_TAG);
\r
363 $this->p_vars['event_stack']->
\r
364 pushEvent(PHPDOCUMENTOR_PDP_EVENT_CDATA);
\r
365 $this->p_vars['last_tag'] = array_pop($this->context);
\r
366 array_push($this->context, $this->p_vars['last_tag']);
\r
367 $this->p_flags['in_cdata'] = false;
\r
372 $this->p_flags['start_attr'] = true;
\r
373 $this->p_flags['end_attr'] = false;
\r
376 addErrorDie(PDERROR_CANT_HAVE_INLINE_IN_TAGNAME);
\r
381 * Handle CData sections
\r
383 function handleCData($word, $pevent)
\r
385 if ($this->curtag->name == 'refentry' &&
\r
386 phpDocumentor_get_class($word) == 'parsertocinlinetag'
\r
388 $this->p_vars['toc'] = $this->curtag->getTOC();
\r
390 if (is_string($word) && !$this->p_flags['in_cdata']) {
\r
391 if ($this->checkEventPop($word, $pevent)) {
\r
394 if ($this->checkEventPush($word, $pevent)) {
\r
398 if (is_string($word) && $word == '<![CDATA[') {
\r
399 $this->curtag->startCData();
\r
400 $this->p_flags['in_cdata'] = true;
\r
401 } elseif ($this->p_flags['in_cdata'] &&
\r
402 is_string($word) && $word == ']]>'
\r
404 $this->curtag->endCData();
\r
405 $this->p_flags['in_cdata'] = false;
\r
407 if ($this->p_flags['in_cdata']) {
\r
408 $this->curtag->addCData($word);
\r
410 $this->curtag->add($word);
\r
416 * Handle Entities like ”
\r
418 function handleEntity($word, $pevent)
\r
421 if (!isset($this->p_vars['entity_name'])) {
\r
422 $this->p_vars['entity_name'] = '';
\r
424 addErrorDie(PDERROR_UNTERMINATED_ENTITY, $this->p_vars['entity_name']);
\r
426 $e = $this->checkEventPop($word, $pevent);
\r
427 if ($word && !$e) {
\r
428 $this->p_vars['entity_name'] = $word;
\r
431 $entity = new parserEntity($this->p_vars['entity_name']);
\r
432 unset($this->p_vars['entity_name']);
\r
433 $this->curtag->add($entity);
\r
438 * Handle Tag attributes name="value"
\r
440 * @todo replace commented-out debug lines with debug() func
\r
442 function handleAttributes($word, $pevent)
\r
444 if ($this->checkEventPush($word, $pevent)) {
\r
447 if ($word == '=') {
\r
448 $this->p_flags['start_attr'] = false;
\r
449 $this->p_vars['end_attr'] = true;
\r
451 if ($this->p_flags['start_attr']) {
\r
452 $this->p_vars['attrname'] = $word;
\r
454 if (isset($this->p_vars['attrname'])) {
\r
455 $value = $this->p_vars['quote_data'];
\r
456 if (phpDocumentor_get_class($value) == 'parseridinlinetag') {
\r
457 // "inherit" the parent section's id, so
\r
459 // <!-- id is 'test' -->
\r
460 // <refsect1 id="{@id test"}>
\r
462 // <!-- id is 'test.me' -->
\r
463 // <refsect2 id="{@id me}">
\r
465 // <!-- id is 'test.me.out' -->
\r
466 // <refsect3 id="{@id out}">
\r
468 // <!-- id is 'test.me.out.withexample' -->
\r
469 // <example id="{@id withexample}">
\r
471 $a = ($this->refsect1id ? $this->refsect1id . '.' : '');
\r
472 $a .= ($this->refsect2id ? $this->refsect2id . '.' : '');
\r
473 $a .= ($this->refsect3id ? $this->refsect3id . '.' : '');
\r
474 if ($this->curtag->name == 'refsect1') {
\r
475 $this->refsect1id = $value->id;
\r
477 if ($this->curtag->name == 'refsect2') {
\r
478 $this->refsect2id = $value->id;
\r
480 if ($this->curtag->name == 'refsect3') {
\r
481 $this->refsect3id = $value->id;
\r
483 //DEBUG debug($value->id . ' is now ' . $a . $value->id);
\r
484 $value->id = $a . $value->id;
\r
485 if ($value->id != '') {
\r
486 if (isset($this->_toc[$this->_cursection])) {
\r
487 $this->_cursection++;
\r
489 $this->_toc[$this->_cursection]['id'] = $value;
\r
490 $this->_toc[$this->_cursection]['tag'] =
\r
491 new parserXMLDocBookTag($this->curtag->name);
\r
492 //DEBUG debug("set gettoc to " . $this->curtag->name .
\r
493 //DEBUG ' ' . $value->id);
\r
494 $this->_gettoc = $this->curtag;
\r
497 $this->curtag->addAttribute($this->p_vars['attrname'], $value);
\r
498 unset($this->p_vars['attrname']);
\r
499 if (is_string($word) && $this->checkEventPop($word, $pevent)) {
\r
500 $this->p_flags['start_attr'] = true;
\r
501 $this->p_flags['end_attr'] = false;
\r
502 $this->wp->setPos($this->wp->getPos() - strlen($word));
\r
504 $this->wp->setPos($this->wp->getPos() - strlen($word));
\r
510 if (is_string($word) && $this->checkEventPop($word, $pevent)) {
\r
511 $this->p_flags['start_attr'] = true;
\r
512 $this->p_flags['end_attr'] = false;
\r
513 $this->wp->setPos($this->wp->getPos() - strlen($word));
\r
519 * setup the parser tokens, and the pushEvent/popEvent arrays
\r
522 * @see $tokens, $pushEvent, $popEvent
\r
524 function setupStates()
\r
526 $this->_gettoc = false;
\r
527 $this->_toc = array();
\r
528 $this->_cursection = 0;
\r
529 if (isset($this->p_vars['toc'])) {
\r
530 unset($this->p_vars['toc']);
\r
533 $this->tokens[STATE_NOEVENTS]
\r
534 = array('</','<!--','<!','<?','<');
\r
535 $this->tokens[STATE_COMMENTBLOCK]
\r
537 $this->tokens[STATE_OUTPHP]
\r
539 $this->tokens[STATE_QUOTE]
\r
540 = array("\\\"","\\\\","\"",'>');
\r
541 $this->tokens[STATE_ESCAPE]
\r
542 = false;// this tells the word parser to just cycle
\r
543 $this->tokens[PHPDOCUMENTOR_PDP_STATE_TAG]
\r
544 = array('>',' ','/>');
\r
545 $this->tokens[PHPDOCUMENTOR_PDP_STATE_CDATA]
\r
546 = array('&','<!--','</','<![CDATA[','<',']]>');
\r
547 $this->tokens[PHPDOCUMENTOR_PDP_STATE_ATTRIBUTES]
\r
548 = array('=','>','/>','"');
\r
549 $this->tokens[PHPDOCUMENTOR_PDP_STATE_ENTITY]
\r
552 // For each event word to event mapings
\r
553 $this->pushEvent[PARSER_EVENT_NOEVENTS] =
\r
555 '<!--' => PARSER_EVENT_COMMENTBLOCK,
\r
556 '<!' => PARSER_EVENT_OUTPHP,
\r
557 "</" => PHPDOCUMENTOR_PDP_EVENT_TAG,
\r
558 '<?' => PARSER_EVENT_OUTPHP,
\r
559 "<" => PHPDOCUMENTOR_PDP_EVENT_TAG,
\r
560 '&' => PHPDOCUMENTOR_PDP_EVENT_ENTITY,
\r
562 //##########################
\r
564 $this->pushEvent[PHPDOCUMENTOR_PDP_EVENT_TAG] =
\r
566 ' ' => PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES,
\r
569 $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_TAG] = array(">","/>");
\r
570 //##########################
\r
571 $this->pushEvent[PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES] =
\r
573 '"' => PARSER_EVENT_QUOTE,
\r
576 $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES] = array(">","/>");
\r
577 //##########################
\r
579 $this->popEvent[PARSER_EVENT_COMMENTBLOCK] = array("-->");
\r
580 //##########################
\r
581 $this->pushEvent[PARSER_EVENT_QUOTE] =
\r
583 "\\" => PARSER_EVENT_ESCAPE
\r
585 $this->popEvent[PARSER_EVENT_QUOTE] = array("\"");
\r
586 //##########################
\r
588 $this->popEvent[PARSER_EVENT_OUTPHP] = array("?>",">");
\r
589 //##########################
\r
591 $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_ENTITY] = array(";");
\r
592 //##########################
\r
594 $this->pushEvent[PHPDOCUMENTOR_PDP_EVENT_CDATA] =
\r
596 "<" => PHPDOCUMENTOR_PDP_EVENT_TAG,
\r
597 '<!--' => PARSER_EVENT_COMMENTBLOCK,
\r
598 '<?' => PARSER_EVENT_OUTPHP,
\r
599 '&' => PHPDOCUMENTOR_PDP_EVENT_ENTITY,
\r
601 $this->popEvent[PHPDOCUMENTOR_PDP_EVENT_CDATA] = array("</");
\r
605 * debugging function
\r
609 * @param mixed $value a value
\r
611 * @return mixed the looked up value if found,
\r
612 * else the original value
\r
615 function getParserEventName ($value)
\r
618 PARSER_EVENT_NOEVENTS
\r
619 => "PARSER_EVENT_NOEVENTS",
\r
620 PHPDOCUMENTOR_PDP_EVENT_TAG
\r
621 => "PHPDOCUMENTOR_PDP_EVENT_TAG",
\r
622 PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES
\r
623 => "PHPDOCUMENTOR_PDP_EVENT_ATTRIBUTES",
\r
624 PHPDOCUMENTOR_PDP_EVENT_CDATA
\r
625 => "PHPDOCUMENTOR_PDP_EVENT_CDATA",
\r
626 PHPDOCUMENTOR_PDP_EVENT_LIST
\r
627 => "PHPDOCUMENTOR_PDP_EVENT_LIST",
\r
629 => "PARSER_EVENT_QUOTE",
\r
630 PHPDOCUMENTOR_PDP_EVENT_ENTITY
\r
631 => "PHPDOCUMENTOR_PDP_EVENT_ENTITY",
\r
632 PHPDOCUMENTOR_PDP_EVENT_COMMENT
\r
633 => "PHPDOCUMENTOR_PDP_EVENT_COMMENT",
\r
634 PHPDOCUMENTOR_PDP_EVENT_PI
\r
635 => "PHPDOCUMENTOR_PDP_EVENT_PI",
\r
637 if (isset($lookup[$value])) {
\r
638 return $lookup[$value];
\r