jablonka.czprosek.czf

websvn

Subversion Repositories:
[/] [include/] [template.php] - Blame information for rev 1

 

Line No. Rev Author Line
11simandl<?php
2// WebSVN - Subversion repository viewing via the web using PHP
3// Copyright (C) 2004-2006 Tim Armes
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18//
19// --
20//
21// templates.php
22//
23// Templating system to allow advanced page customisation
24 
25$ignore = false;
26 
27// Stack of previous test results
28$ignorestack = array();
29 
30// Number of test levels currently ignored
31$ignorelevel = 0;
32 
33// parseCommand
34//
35// Parse a special command
36 
37function parseCommand($line, $vars, $handle) {
38 global $ignore, $ignorestack, $ignorelevel;
39 
40 // Check for test conditions
41 if (strncmp(trim($line), "[websvn-test:", 13) == 0) {
42 if (!$ignore) {
43 $line = trim($line);
44 $var = substr($line, 13, -1);
45 if (empty($vars[$var])) {
46 array_push($ignorestack, $ignore);
47 $ignore = true;
48 }
49 } else {
50 $ignorelevel++;
51 }
52 
53 return true;
54 }
55 
56 if (strncmp(trim($line), "[websvn-else]", 13) == 0) {
57 if ($ignorelevel == 0) {
58 $ignore = !$ignore;
59 }
60 
61 return true;
62 }
63 
64 if (strncmp(trim($line), "[websvn-endtest]", 16) == 0) {
65 if ($ignorelevel > 0) {
66 $ignorelevel--;
67 } else {
68 $ignore = array_pop($ignorestack);
69 }
70 
71 return true;
72 }
73 
74 if (strncmp(trim($line), "[websvn-getlisting]", 19) == 0) {
75 global $path, $rev, $svnrep;
76 
77 if (!$ignore) {
78 $svnrep->listFileContents($path, $rev);
79 }
80 
81 return true;
82 }
83 
84 if (strncmp(trim($line), "[websvn-defineicons]", 19) == 0) {
85 global $icons;
86 
87 if (!isset($icons)) {
88 $icons = array();
89 }
90 
91 // Read all the lines until we reach the end of the definition, storing
92 // each one...
93 
94 if (!$ignore) {
95 while (!feof($handle)) {
96 $line = trim(fgets($handle));
97 
98 if (strncmp($line, "[websvn-enddefineicons]", 22) == 0) {
99 return true;
100 }
101 
102 $eqsign = strpos($line, "=");
103 
104 $match = substr($line, 0, $eqsign);
105 $def = substr($line, $eqsign + 1);
106 
107 $icons[$match] = $def;
108 }
109 }
110 
111 return true;
112 }
113 
114 if (strncmp(trim($line), "[websvn-icon]", 13) == 0) {
115 global $icons, $vars;
116 
117 if (!$ignore) {
118 // The current filetype should be defined my $vars["filetype"]
119 
120 if (!empty($icons[$vars["filetype"]])) {
121 echo parseTags($icons[$vars["filetype"]], $vars);
122 } else if (!empty($icons["*"])) {
123 echo parseTags($icons["*"], $vars);
124 }
125 }
126 
127 return true;
128 }
129 
130 if (strncmp(trim($line), "[websvn-treenode]", 17) == 0) {
131 global $icons, $vars;
132 
133 if (!$ignore) {
134 if ((!empty($icons["i-node"])) && (!empty($icons["t-node"])) && (!empty($icons["l-node"]))) {
135 for ($n = 1; $n < $vars["level"]; $n++) {
136 if ($vars["last_i_node"][$n]) {
137 echo parseTags($icons["e-node"], $vars);
138 } else {
139 echo parseTags($icons["i-node"], $vars);
140 }
141 }
142 
143 if ($vars["level"] != 0) {
144 if ($vars["node"] == 0) {
145 echo parseTags($icons["t-node"], $vars);
146 } else {
147 echo parseTags($icons["l-node"], $vars);
148 $vars["last_i_node"][$vars["level"]] = TRUE;
149 }
150 }
151 }
152 }
153 
154 return true;
155 }
156 
157 return false;
158}
159 
160// parseTemplate
161//
162// Parse the given template, replacing the variables with the values passed
163 
164function parseTemplate($template, $vars, $listing) {
165 global $ignore, $vars;
166 
167 if (!is_file($template)) {
168 print"No template file found ($template)";
169 exit;
170 }
171 
172 $handle = fopen($template, "r");
173 $inListing = false;
174 $ignore = false;
175 $listLines = array();
176 
177 while (!feof($handle)) {
178 $line = fgets($handle);
179 
180 // Check for the end of the file list
181 if ($inListing) {
182 if (strcmp(trim($line), "[websvn-endlisting]") == 0) {
183 $inListing = false;
184 
185 // For each item in the list
186 foreach ($listing as $listvars) {
187 // Copy the value for this list item into the $vars array
188 foreach ($listvars as $id => $value) {
189 $vars[$id] = $value;
190 }
191 
192 // Output the list item
193 foreach ($listLines as $line) {
194 if (!parseCommand($line, $vars, $handle)) {
195 if (!$ignore) {
196 print parseTags($line, $vars);
197 }
198 }
199 }
200 }
201 } else {
202 if ($ignore == false) {
203 $listLines[] = $line;
204 }
205 }
206 } else if (parseCommand($line, $vars, $handle)) {
207 continue;
208 } else {
209 // Check for the start of the file list
210 if (strncmp(trim($line), "[websvn-startlisting]", 21) == 0) {
211 $inListing = true;
212 } else {
213 if ($ignore == false) {
214 print parseTags($line, $vars);
215 }
216 }
217 }
218 }
219 
220 fclose($handle);
221}
222 
223// parseTags
224//
225// Replace all occurences of [websvn:varname] with the give variable
226 
227function parseTags($line, $vars) {
228 global $lang;
229 
230 $l = '';
231 // Replace the websvn variables
232 while (ereg("\[websvn:([a-zA-Z0-9_]+)\]", $line, $matches)) {
233 // Find beginning
234 $p = strpos($line, $matches[0]);
235 
236 // add everything up to beginning
237 if ($p > 0) {
238 $l .= substr($line, 0, $p);
239 }
240 
241 // Replace variable (special token, if not exists)
242 $l .= isset($vars[$matches[1]]) ? $vars[$matches[1]]: ('?'.$matches[1].'?');
243 
244 // Remove allready processed part of line
245 $line = substr($line, $p + strlen($matches[0]));
246 }
247 
248 // Rebuild line, add remaining part of line
249 $line = $l.$line;
250 
251 // Replace the language strings
252 while (ereg("\[lang:([a-zA-Z0-9_]+)\]", $line, $matches)) {
253 // Make sure that the variable exists
254 if (!isset($lang[$matches[1]])) {
255 $lang[$matches[1]] = "?${matches[1]}?";
256 }
257 
258 $line = str_replace($matches[0], $lang[$matches[1]], $line);
259 }
260 
261 // Return the results
262 return $line;
263}

Powered by WebSVN 2.2.1