A little HTML cleanup on diff links
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / PHPTAL / Generator.php
blobe0c90e9e7767eb9924653010948fb5f495aa5006
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
23 class PHPTAL_Generator
25 var $_temp_id = 0;
26 var $_str_buffer = "";
27 var $_tab = "";
28 var $_tab_save = "";
29 var $_code = "";
30 var $_closed = false;
31 var $_fname;
32 var $_funcName;
34 var $_macros = array();
35 var $_stacks = array();
36 var $_current_macro = false;
38 var $_gettext_required = false;
40 var $_headers = false;
42 function PHPTAL_Generator($fname, $func_name)
44 $this->_fname = $fname;
45 $this->_funcName = $func_name;
46 $this->appendln('<?php');
47 $this->doComment(str_pad("", 78, "-"));
48 $this->doComment("This code was generate by PHPTAL based on the template source file :");
49 $this->doComment("$fname");
50 $this->doComment(str_pad("", 78, "-"));
51 $this->appendln();
52 $this->appendln('function ', $func_name, '(&$__tpl__)');
53 $this->appendln('{');
54 $this->tabInc();
55 $this->appendln('$__ctx__ =& $__tpl__->getContext();');
56 $this->appendln('$__out__ = new PHPTAL_OutputControl($__ctx__, $__tpl__->getEncoding());');
57 $this->appendln('$__ctx__->set("repeat", array());');
60 function setHeaders($h)
62 $this->_headers = $h;
63 if ($this->_headers) {
64 $this->doHeadersPrint();
68 function requireGettext()
70 $this->_gettext_required = true;
73 function getCode()
75 if (!$this->_closed) {
76 $this->_flushOutput();
77 $this->appendln('return $__out__->toString();');
78 $this->endBlock();
80 foreach ($this->_macros as $name=>$code) {
81 $this->_code .= $code;
83 if ($this->_gettext_required) {
84 $this->_code = preg_replace('/^<\?php/sm',
85 '<?php require_once "GetText.php";',
86 $this->_code,
87 1);
89 $this->append('?>');
90 $this->_closed = true;
92 return $this->_code;
95 function execute($code)
97 $this->_flushOutput();
98 $this->appendln(trim($code), ";");
101 function doComment($str)
103 $this->_flushOutput();
104 $this->appendln('// ',$str);
107 function setSource($tagName, $line)
109 $this->doComment('TAG ' . $tagName . ' AT LINE ' . $line);
110 $this->appendln('$_src_tag = "'. $tagName. '"; ', '$_src_line = '. $line. ';');
113 // loop
114 function doDo()
116 $this->_flushOutput();
117 $this->appendln("do {");
118 $this->tabInc();
121 function doEndDoWhile($condition)
123 $this->tabDec();
124 $this->appendln("} while($condition);");
127 function doWhile($condition)
129 $this->_flushOutput();
130 $this->appendln("while ($condition) {");
131 $this->tabInc();
134 // conditionals
135 function doIf($condition)
137 $this->_flushOutput();
138 $this->appendln("if ($condition) {");
139 $this->tabInc();
142 function doElseIf($condition)
144 $this->_str_buffer = "";
145 $this->endBlock();
146 $this->appendln("else if ($condition) {");
147 $this->tabInc();
150 function doElse()
152 $this->endBlock();
153 $this->appendln("else {");
154 $this->tabInc();
158 function doMacroDeclare($name)
160 $this->_flushOutput();
161 $this->_stacks[] = $this->_code;
162 $this->_code = "";
163 $this->_current_macro = $name;
164 $this->_tab_save = $this->_tab;
165 $this->_tab = "";
166 $this->appendln('function ', $this->_funcName,'_',$name, '(&$__tpl__)');
167 $this->appendln('{');
168 $this->tabInc();
169 $this->appendln('$__ctx__ =& $__tpl__->getContext();');
170 $this->appendln('$__out__ = new PHPTAL_OutputControl($__ctx__, $__tpl__->getEncoding());');
171 if ($this->_headers) {
172 $this->doHeadersPrint();
176 function doHeadersPrint()
178 $this->_flushOutput();
179 $this->doIf('! $__tpl__->_headers');
180 $str = str_replace("'", "\\'", $this->_headers);
181 $str = "'" . $str . "'";
182 $str = PHPTAL_ES_path_in_string($str, "'");
183 $this->appendln('$__tpl__->_headers = ' . $str .';');
184 $this->endBlock();
187 function doMacroEnd()
189 $this->_flushOutput();
190 $this->appendln('return $__out__->toString();');
191 $this->endBlock();
192 $this->_macros[$this->_current_macro] = $this->_code;
193 $this->_code = array_pop($this->_stacks);
194 $this->_current_macro = false;
195 $this->_tab = $this->_tab_save;
198 function doAffectResult($dest, $code)
200 if ($dest[0] != '$') { $dest = '$'.$dest; }
201 // test &
202 $this->appendln("$dest = $code;");
206 function doPrintString()
208 $args = func_get_args();
209 $this->_str_buffer .= join("", $args);
210 // $this->appendln('$__out__->write(', $str, ');');
213 function doPrintVar($var, $structure=false)
215 $this->_flushOutput();
216 if ($var[0] != '$') {
217 $var = '$' . $var;
219 if ($structure) {
220 $this->appendln('$__out__->writeStructure(', $var, ');');
221 } else {
222 $this->appendln('$__out__->write(', $var, ');');
226 function doPrintRes($code, $structure=false)
228 $this->_flushOutput();
229 if ($structure) {
230 $this->appendln('$__out__->writeStructure(', $code, ');');
231 } else {
232 $this->appendln('$__out__->write(', $code, ');');
236 function doPrintContext($path, $structure=false)
238 $code = sprintf('$__ctx__->get(\'%s\')', $path);
239 $this->doPrintRes($code, $structure);
242 // output buffering control
243 function doOBStart()
245 $this->_flushOutput();
246 $this->appendln('$__out__->pushBuffer();');
249 function doOBEnd($dest)
251 $this->_flushOutput();
252 // test &
253 $this->appendln($dest, ' =& $__out__->popBuffer();');
256 function doOBClean()
258 $this->_flushOutput();
259 $this->appendln('$__out__->popBuffer();');
262 function doOBPrint()
264 $this->_flushOutput();
265 $this->appendln('$__out__->writeStructure($__out__->popBuffer());');
268 function doOBEndInContext($dest)
270 $this->doContextSet($dest, '$__out__->popBuffer()');
274 function doReference($dest, $source)
276 $this->_flushOutput();
277 if ($dest[0] != '$') { $dest = '$'.$dest; }
278 if ($source[0] != '$') { $source = '$'.$source; }
279 // test &
280 $this->appendln("$dest =& $source;");
283 function doUnset($var)
285 $this->appendln("unset($var);");
288 // create a new temporary variable (non context) and return its name
289 function newTemporaryVar()
291 return '$temp_' . $this->_temp_id++;
294 function releaseTemporaryVar($name)
296 $this->doUnset($name);
299 // context methods
301 function doContextSet($out, $code)
303 $this->_flushOutput();
304 if ($out[0] != '$')
305 { $out = '"'.$out.'"'; }
306 // test & (Ref)
307 $this->appendln("\$__ctx__->setRef($out, $code);");
310 function doContextGet($out, $path)
312 $this->_flushOutput();
313 if ($out[0] != '$')
314 { $out = '$' . $out; }
315 // test &
316 $this->appendln("$out =& \$__ctx__->get(\"$path\");");
319 function endBlock()
321 $this->tabDec();
322 $this->appendln('}');
325 function tabInc()
327 $this->_flushOutput();
328 $this->_tab .= " ";
331 function tabDec()
333 $this->_flushOutput();
334 $this->_tab = substr($this->_tab, 4);
337 function appendln()
339 $args = func_get_args();
340 $str = join("", $args);
341 $this->_code .= $this->_tab . $str . "\n";
344 function append()
346 $args = func_get_args();
347 $str = join("", $args);
348 $this->_code .= $this->_tab . $str;
351 function _flushOutput()
353 if ($this->_str_buffer == "") return;
354 $this->_str_buffer = str_replace("'", "\\'", $this->_str_buffer);
355 $this->_str_buffer = "'" . $this->_str_buffer . "'";
356 $this->_str_buffer = PHPTAL_ES_path_in_string($this->_str_buffer, "'");
357 $this->appendln('$__out__->writeStructure(', $this->_str_buffer, ');');
358 $this->_str_buffer = "";