File: PhDHelper.class.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 (243 lines) | stat: -rw-r--r-- 6,761 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*  $Id: PhDHelper.class.php 281268 2009-05-28 07:35:09Z cweiske $ */

/**
* Base class for PhDTheme
*/
class PhDHelper
{
    /**
    * Array of all ids as key and their "attributes" (not xml-atts)
    * as array.
    * Created by mktoc.php
    *
    * @var array
    */
    private $IDs = array();

    /**
    * Array of &lt;refname&gt; tag contents as key,
    * and the ID of the section they appear in as value.
    * Created by mktoc.php
    *
    * @var array
    */
    private $refs = array();

    private $classes        = array();
    private $vars           = array();

    /* abstract */ protected $elementmap  = array();
    /* abstract */ protected $textmap     = array();
    private static $autogen         = array();

    /**
    * Array of highlighter objects
    * Key is the format, value the object
    *
    * @var array
    */
    private static $highlighters = array();



    /**
    * Creates the helper object.
    *
    * @param array $a Array with ID array as first value, ref array as second.
    *
    * @see $IDs
    * @see $refs
    */
    public function __construct(array $a) {
        $this->IDs = $a[0];
        $this->refs = $a[1];
        $this->classes = $a[2];
        $this->vars    = $a[3];
    }

    /**
    * When the tag associated with that $id is to be chunked,
    * the filename (without extension) is returned.
    *
    * @param string $id ID of an element
    *
    * @return mixed Filename without extension (string) or false if the element
    *               is not to be chunked
    *
    * @see $IDs
    */
    final public function getFilename($id)
    {
        return isset($this->IDs[$id]) ? $this->IDs[$id]["filename"] : false;
    }

    final public function getDescription($id, $long = false) {
        return $long ?
            ($this->IDs[$id]["ldesc"] ? $this->IDs[$id]["ldesc"] : $this->IDs[$id]["sdesc"]) :
            ($this->IDs[$id]["sdesc"] ? $this->IDs[$id]["sdesc"] : $this->IDs[$id]["ldesc"]);
    }

    /**
    * Returns an array of direct children tags with IDs.
    *
    * @param string $id ID of an element
    *
    * @return array Array of children IDs (empty array when no kids)
    *
    * @see $IDs
    */
    final public function getChildren($id)
    {
        return $this->IDs[$id]["children"];
    }

    /**
    * Returns the toc entry of the given ID.
    *
    * @param string $id ID of an element
    *
    * @return array toc entry array
    *
    * @see $IDs
    */
    final public function getSelf($id)
    {
        return $this->IDs[$id];
    }

    /**
    * Returns ID of the first parental block tag with an ID.
    *
    * @param string $id ID of an element
    *
    * @return string ID of the parent tag
    *
    * @see $IDs
    */
    final public function getParent($id)
    {
        return $this->IDs[$id]["parent"];
    }

    /**
    * Returns the id of the reference page for a given function/method/class.
    *
    * @param string $ref Name of referenced object (e.g. "$class", "$class-$method")
    *
    * @return mixed ID (string) of reference page or null if not found.
    *
    * @see $refs
    */
    public function getRefnameLink($ref)
    {
        return isset($this->refs[$ref]) ? $this->refs[$ref] : null;
    }

    public function getClassnameLink($class) {
        return isset($this->classes[$class]) ? $this->classes[$class] : null;
    }
    public function getVarnameLink($var) {
        return isset($this->vars[$var]) ? $this->vars[$var] : null;
    }

    final public function getElementMap() {
        return $this->elementmap;
    }
    final public function getTextMap() {
        return $this->textmap;
    }

    /**
    * Reads $phddir/include/langs/$lang.xml and returns the translation
    * for the given $text.
    *
    * @note
    * This function is used only internally, it has nothing to do with the
    * manual content. Used e.g. for autogenerated table of contents titles.
    *
    * @param string $text Text to translate ("warning", "toc", "by")
    * @param string $lang Two-letter ISO code of language (de, en, ..)
    *
    * @return string Translated text
    */
    final public function autogen($text, $lang)
    {
        if (isset(PhDHelper::$autogen[$lang])) {
            return PhDHelper::$autogen[$lang][$text];
        }

        $filename = dirname(__FILE__) ."/langs/$lang.xml";
        $r = new XMLReader;
        if (!file_exists($filename) || !$r->open($filename)) {
            if ($lang == "en") {
                throw new Exception("Cannot open $filename");
            }
            return $this->autogen($text, "en");
        }
        $autogen = array();
        while ($r->read()) {
            if ($r->nodeType != XMLReader::ELEMENT) {
                continue;
            }
            if ($r->name == "term") {
                $r->read();
                $k = $r->value;
                $autogen[$k] = "";
            } else if ($r->name == "simpara") {
                $r->read();
                $autogen[$k] = $r->value;
            }
        }
        PhDHelper::$autogen[$lang] = $autogen;
        return PhDHelper::$autogen[$lang][$text];
    }

    /**
    * Highlight (color) the given piece of source code
    *
    * @param string $text   Text to highlight
    * @param string $role   Source code role to use (php, xml, html, ...)
    * @param string $format Format to highlight (pdf, xhtml, troff, ...)
    *
    * @return string Highlighted code
    */
    public function highlight($text, $role = 'php', $format = 'xhtml')
    {
        if (!isset(self::$highlighters[$format])) {
            $class = $GLOBALS['OPTIONS']['highlighter'];

            if (!class_exists($class, true)) {
                if ($class == 'PhDHighlighter') {
                    //hard coded path for phdhighlighter, since phd doesn't follow
                    // pear file conventions
                    require_once dirname(__FILE__) . '/PhDHighlighter.class.php';
                } else {
                    //simple autoload replacement for pear-style classes
                    require_once str_replace('_', '/', $class) . '.php';
                }

                if (!class_exists($class, false)) {
                    trigger_error(
                        'Highlighter class ' . $class . ' is not available',
                        E_USER_ERROR
                    );
                }
            }
            self::$highlighters[$format] = call_user_func(
                array($class, 'factory'), $format
            );

        }

        return self::$highlighters[$format]->highlight(
            $text, $role, $format
        );
    }//public function highlight(..)
}

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