12 |
|
# to the desired routines |
13 |
|
# usegroupchecker(groupchecker) : Set a groupchecker |
14 |
|
# parse() : Parse the file |
15 |
– |
# checkparam($name,$par) : Exit with an error message if parameter |
16 |
– |
# is undefined in tag $name |
15 |
|
# line() : return the current line number of the parse |
16 |
< |
|
16 |
> |
# tagstartline() : return the line number on which the current |
17 |
> |
# tag was opened |
18 |
> |
# stream(filehandle) : stream output to the filehandle if not handled |
19 |
> |
# in any other way |
20 |
|
package ActiveDoc::Switcher; |
21 |
|
require 5.001; |
22 |
|
use Carp; |
34 |
|
return $self; |
35 |
|
} |
36 |
|
|
37 |
+ |
sub stream { |
38 |
+ |
my $self=shift; |
39 |
+ |
|
40 |
+ |
$self->{stream}=shift; |
41 |
+ |
} |
42 |
+ |
|
43 |
+ |
sub streamexclude { |
44 |
+ |
my $self=shift; |
45 |
+ |
my $tag=shift; |
46 |
+ |
|
47 |
+ |
$tag=~tr/A-Z/a-z/; |
48 |
+ |
$self->{streamexclude}{$tag}=1; |
49 |
+ |
} |
50 |
+ |
|
51 |
|
sub _initialise (hash1) { |
52 |
|
my $self=shift; |
53 |
|
$self->{filename}=shift; |
83 |
|
my $buf; |
84 |
|
$self->{linecount}=0; |
85 |
|
$self->_resetvars(); |
86 |
+ |
$self->{streamstore}=""; |
87 |
+ |
$self->{streamtmp}=""; |
88 |
|
|
89 |
|
# Open the file |
90 |
|
use FileHandle; |
91 |
< |
my $filehandle; |
91 |
> |
local $filehandle; |
92 |
|
$filehandle=FileHandle->new(); |
93 |
< |
$filehandle->open("<$self->{filename}") |
93 |
> |
$filehandle->open("<".$self->{filename}) |
94 |
|
or return 1; |
95 |
|
# The buffering seems all messed up - best not to use it |
96 |
|
$filehandle->setvbuf($buf, _IONBF, 3000); |
105 |
|
} # end char while |
106 |
|
} # End String while loop |
107 |
|
undef $filehandle; |
108 |
< |
} |
109 |
< |
|
110 |
< |
sub checkparam($name, $key) { |
111 |
< |
my $self=shift; |
95 |
< |
my $name=shift; |
96 |
< |
my $key=shift; |
97 |
< |
|
98 |
< |
if ( ! defined $self->{tagvar}{$key} ) { |
99 |
< |
print "Switcher: Badly formed $name tag -". |
100 |
< |
" undefined $key parameter\n"; |
101 |
< |
exit 1; |
102 |
< |
} |
108 |
> |
# make sure we close the last buffer |
109 |
> |
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
110 |
> |
$self->_getstore()); |
111 |
> |
#$self->_printstream(); |
112 |
|
} |
113 |
|
|
114 |
|
# |
118 |
|
my $self=shift; |
119 |
|
return $self->{linecount}; |
120 |
|
} |
121 |
+ |
|
122 |
+ |
# return the line the current tag was opened |
123 |
+ |
sub tagstartline { |
124 |
+ |
my $self=shift; |
125 |
+ |
$self->{tagstart}; |
126 |
+ |
} |
127 |
|
# --------------- Utility routines ---------------------------- |
128 |
|
|
129 |
|
# |
146 |
|
my $char=shift; |
147 |
|
my $string; |
148 |
|
|
149 |
+ |
|
150 |
|
# ---- In a tag |
151 |
|
if ( $self->{tagcontext}=~/tag/ ) { |
152 |
+ |
$self->{tagbuff}=$self->{tagbuff}.$char; |
153 |
|
if ( ! $self->_quotetest($char) ) { |
154 |
|
if ( ! $self->_labeltest($char) ) { |
155 |
|
if ( $char eq ">") { $self->_closetag(); } |
172 |
|
my $self=shift; |
173 |
|
my $char; |
174 |
|
$char=substr($self->{currentline},$self->{stringpos}++,1); |
175 |
< |
# print "Debug : Fetching character $char\n"; |
175 |
> |
#print "Debug : Fetching character $char\n"; |
176 |
> |
|
177 |
> |
# Keep a record for any stream processes |
178 |
> |
$self->{streamstore}=$self->{streamstore}.$char; |
179 |
> |
|
180 |
|
return $char; |
181 |
|
} |
182 |
|
|
184 |
|
my $self=shift; |
185 |
|
my $char; |
186 |
|
|
187 |
+ |
# Keep a record of where the tag started |
188 |
+ |
$self->{tagstart}=$self->line(); |
189 |
+ |
|
190 |
|
# Close the last text segment |
191 |
+ |
$self->{streamtmp}=$self->_popstream(); |
192 |
|
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
193 |
|
$self->_getstore()); |
194 |
|
$self->_resetstore(); |
196 |
|
|
197 |
|
# Do we have an opening or closing tag? |
198 |
|
if ( ($char=$self->_nextchar()) eq "/" ) { #we have a closing tag |
199 |
+ |
$self->{tagbuff}="<".$char; |
200 |
|
$self->{tagcontext}="endtag"; |
201 |
|
} |
202 |
|
else { # an opening tag |
203 |
+ |
$self->{tagbuff}="<"; |
204 |
|
$self->{tagcontext}="starttag"; |
205 |
|
$self->_checkchar($char); |
206 |
|
} |
218 |
|
$self->_closelabel(); |
219 |
|
|
220 |
|
# -- Call the associated tag function if appropriate |
221 |
< |
$tagroutine=$self->{tagname}."_".$self->{tagcontext}; |
222 |
< |
$self->_calltag($tagroutine, $self->{tagname}, |
221 |
> |
if ( defined $self->{tagname} ) { |
222 |
> |
$tagroutine=$self->{tagname}."_".$self->{tagcontext}; |
223 |
> |
$self->_calltag($tagroutine, $self->{tagname}, |
224 |
|
$self->{tagvar}); |
225 |
< |
#print "\nDebug : Closing Tag $tagroutine\n"; |
225 |
> |
#print "\nDebug : Closing Tag $tagroutine\n"; |
226 |
|
|
227 |
< |
# -- Now make sure the text context is set for calling routines to |
228 |
< |
# -- deal with text portions outside of tags |
229 |
< |
if ( $self->{tagcontext} eq "starttag" ) { |
230 |
< |
push @{$self->{textstack}} , $self->{textcontext}; |
231 |
< |
$self->{textcontext}=$self->{tagname}; |
232 |
< |
} |
233 |
< |
else { |
227 |
> |
# -- Now make sure the text context is set for calling routines to |
228 |
> |
# -- deal with text portions outside of tags |
229 |
> |
if ( ($self->{tagcontext} eq "starttag") ) { |
230 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
231 |
> |
push @{$self->{textstack}} , $self->{textcontext}; |
232 |
> |
$self->{textcontext}=$self->{tagname}; |
233 |
> |
} |
234 |
> |
} |
235 |
> |
else { |
236 |
|
if ( $#{$self->{textstack}} > -1 ) { |
237 |
< |
if ( $self->{textcontext} eq $self->{tagname} ) { |
238 |
< |
$self->{textcontext}=pop @{$self->{textstack}}; |
239 |
< |
} |
237 |
> |
if ( $self->{textcontext} eq $self->{tagname} ) { |
238 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
239 |
> |
# -- watch out for valid tags we ignore in this parse |
240 |
> |
$self->{textcontext}=pop @{$self->{textstack}}; |
241 |
> |
} |
242 |
> |
} |
243 |
|
else { #The tag we are closing is not the last one so |
244 |
|
# we keep our current context. |
245 |
|
$self->_removefromstack($self->{tagname},$self->{textstack}); |
247 |
|
|
248 |
|
} |
249 |
|
else { # more close tags than open ones |
250 |
+ |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
251 |
|
print "Warning : Unmatched </...> tag on line ". |
252 |
|
$self->line()."\n"; |
253 |
+ |
} |
254 |
|
} |
255 |
+ |
} |
256 |
|
} |
257 |
|
# Reset context back to text |
258 |
|
$self->{tagcontext}="text"; |
263 |
|
my $tagroutine=shift; |
264 |
|
my @args=@_; |
265 |
|
my $rt; |
266 |
+ |
my $found=0; |
267 |
|
|
268 |
|
if ( $self->{groupchecker}->status() || |
269 |
|
( $self->{tagcontainer}->inquiregroup($tagroutine)) ) { |
270 |
< |
$rt=$self->{tagcontainer}->getroutine($tagroutine); |
270 |
> |
($rt,$obj)=$self->{tagcontainer}->getroutine($tagroutine); |
271 |
|
if ( $rt ne "" ) { |
272 |
+ |
if ( ! defined $obj ) { |
273 |
|
&{$rt}( $self->{allw},@_); |
274 |
+ |
} |
275 |
+ |
else { |
276 |
+ |
&{$rt}( $obj,@_); |
277 |
+ |
} |
278 |
+ |
$found=1; |
279 |
|
} |
280 |
|
} |
281 |
+ |
|
282 |
+ |
# stream function |
283 |
+ |
if ( ! exists $self->{streamexclude}{$tagroutine} ) { |
284 |
+ |
$self->_printstream(); |
285 |
+ |
} |
286 |
+ |
$self->_clearstream(); |
287 |
+ |
} |
288 |
+ |
|
289 |
+ |
sub _clearstream { |
290 |
+ |
my $self=shift; |
291 |
+ |
$self->{streamstore}=(($self->{streamtmp} ne "")?$self->{streamtmp}:""); |
292 |
+ |
$self->{streamtmp}=""; |
293 |
+ |
} |
294 |
+ |
|
295 |
+ |
sub _popstream { |
296 |
+ |
my $self=shift; |
297 |
+ |
$self->{streamstore}=~s/(.*)(.)$/$1/; |
298 |
+ |
return $2; |
299 |
+ |
} |
300 |
+ |
|
301 |
+ |
sub _printstream { |
302 |
+ |
|
303 |
+ |
my $self=shift; |
304 |
+ |
|
305 |
+ |
# Stream output functionality |
306 |
+ |
if ( defined $self->{stream} ) { |
307 |
+ |
print {$self->{stream}} "$self->{streamstore}"; |
308 |
+ |
} |
309 |
|
} |
310 |
|
|
311 |
|
sub _removefromstack { |
380 |
|
sub _resetlabels { |
381 |
|
my $self=shift; |
382 |
|
undef $self->{tagvar}; |
383 |
+ |
undef $self->{tagname}; |
384 |
|
} |
385 |
|
|
386 |
|
sub _closelabel { |
388 |
|
|
389 |
|
# Do we have a label name? |
390 |
|
if ( $self->{lastlabel} ne "" ) { |
391 |
< |
$self->{tagvar}{$self->{lastlabel}}=$self->_getstore(); |
391 |
> |
(my $label=$self->{lastlabel})=~tr[A-Z][a-z]; |
392 |
> |
$self->{tagvar}{$label}=$self->_getstore(); |
393 |
|
$self->{lastlabel}=""; |
394 |
|
} |
395 |
|
elsif ( $self->_getstore() ne "") { |
396 |
< |
#Then it must be the tag name |
397 |
< |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
396 |
> |
# Then it must be the tag name |
397 |
> |
if ( ! defined $self->{tagname} ) { |
398 |
> |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
399 |
> |
} |
400 |
> |
else { |
401 |
> |
# do not die anymore - breaks non tag documents |
402 |
> |
#die ">Tag syntax error in $self->{tagname} on line ". |
403 |
> |
# $self->line()." of file \n$self->{filename}"; |
404 |
> |
# -- assume that this is plain text |
405 |
> |
$self->{tagcontext}="text"; |
406 |
> |
$self->_resetstore(); |
407 |
> |
$self->_unshiftstore($self->{tagbuff}); |
408 |
> |
$self->{tagbuff}=""; |
409 |
> |
return; |
410 |
> |
} |
411 |
|
} |
412 |
|
$self->_resetstore(); |
413 |
|
} |
422 |
|
$self->{stringbuff}=$self->{stringbuff}.$char; |
423 |
|
} |
424 |
|
|
425 |
+ |
sub _unshiftstore() { |
426 |
+ |
my $self=shift; |
427 |
+ |
my $char=shift; |
428 |
+ |
|
429 |
+ |
$self->{stringbuff}=$char.$self->{stringbuff}; |
430 |
+ |
} |
431 |
+ |
|
432 |
|
sub _getstore() { |
433 |
|
my $self=shift; |
434 |
|
|