File: mktoc.php

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (127 lines) | stat: -rw-r--r-- 4,326 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Creates the internal table of contents array.
* It is used to look up referenced ids and referenced names.
*
* The TOC is stored in the $ID variable. It is not saved, just generated.
* Generation works by using PhDReader to iterate over each tag, stopping at
* title (or similar) tags and chunk elements.
* Chunk element names are defined in PhDReader::OPEN_CHUNK and
* PhDReader::CLOSE_CHUNK.
* Title tags are title, titleabbrev, refname and refpurpose.
*
* IDs are taken from the xml:id="" attributes of the tags.
*
* It has the following format:
* array(
*     '$id' => array(
*         'sdesc'    => 'Short description (e.g. title)',
*         'ldesc'    => 'Long description (e.g. titleabbrev)',
*         'children' => array('array', 'of', 'children', 'ids'),
*         'parent'   => 'parent_id',
*         'filename' => 'filename for chunk'
*     ),
*     [..more..]
* )
*
* This script also creates the associative $REFS array, using the normalized
* <refname> value as key and its xml:id as value. This is used in
* PhDHelper::getRefnameLink(), and allows easy refname->link resolution.
*
* @package PhD
* @version CVS: $Id: mktoc.php 268546 2008-11-07 23:42:53Z cweiske $
*/

$r = new PhDReader($OPTIONS);
$VARS = $CLASSES = $REFS = $FILENAMES = array();
$CURRENT_FILENAME = $LAST_CHUNK = "";

#FIXME: This is a workaround for the <legalnotice> element in the PHP manual
$PARENTS = array(-1 => "ROOT", 1 => "manual", 2 => "manual");
// PEAR manual needs this line :  $PARENTS = array(-1 => "ROOT", 1 => "guide");

$lastid = 0;

while ($r->read()) {
    if (!($id = $r->getID())) {
        $name = $r->name;
        if (empty($IDs[$lastid]["sdesc"])) {
            if ($name == "refname") {
                $IDs[$lastid]["sdesc"] = $refname = trim($r->readContent($name));
                if ($r->getTagNameByDepth($r->depth-2) == "phpdoc:varentry") {
                    $VARS[$refname] = $lastid;
                } else {
                    $ref = strtolower(str_replace(array("_", "::", "->"), array("-", "-", "-"), $refname));
                    $REFS[$ref] = $lastid;
                }
                continue;
            }
            else if ($name == "titleabbrev") {
                $IDs[$lastid]["sdesc"] = $class = trim($r->readContent($name));
                $elm = $r->getParentTagName();
                if ($elm == "phpdoc:classref" || $elm == "phpdoc:exceptionref") {
                    $CLASSES[strtolower($class)] = $lastid;
                }
                continue;
            }
        } else if ($name == "refname") {
            $refname = trim($r->readContent($name));
            $ref = strtolower(str_replace(array("_", "::", "->"), array("-", "-", "-"), $refname));
            if ($r->getTagNameByDepth($r->depth-2) == "phpdoc:varentry") {
                $VARS[$refname] = $lastid;
            } else {
                $REFS[$ref] = $lastid;
            }
        }
        if (empty($IDs[$lastid]["ldesc"])) {
            if ($name == "title" || $name == "refpurpose" || $name == "parameter") {
                $IDs[$lastid]["ldesc"] = trim($r->readContent($name));
            }
        }

        continue;
    }

    switch ($r->isChunk) {
    case PhDReader::OPEN_CHUNK:
        $CURRENT_FILENAME = $FILENAMES[] = $PARENTS[$r->depth] = $id;
        break;

    case PhDReader::CLOSE_CHUNK:
        $LAST_CHUNK = array_pop($FILENAMES);
        $CURRENT_FILENAME = end($FILENAMES);
        unset($PARENTS[$r->depth]);

        $IDs[$CURRENT_FILENAME]["children"][$LAST_CHUNK] = $IDs[$LAST_CHUNK];


        continue 2;
    }

    if ($r->nodeType != XMLReader::ELEMENT) {
        continue;
    }

    $IDs[$id] = array(
        "filename" => $CURRENT_FILENAME,
        "parent"   => $r->isChunk
                        ? (isset($PARENTS[$r->depth-1])
                            ? $PARENTS[$r->depth-1]
                            : (isset($PARENTS[$r->depth-2])
                                ? $PARENTS[$r->depth-2]
                                : $PARENTS[$r->depth-3]
                            )
                        )
                        : $CURRENT_FILENAME,
        "sdesc"    => null,
        "ldesc"    => null,
        "children" => array(),
    );

    $lastid = $id;
}

/*
* vim600: sw=4 ts=4 fdm=syntax syntax=php et
* vim<600: sw=4 ts=4
*/