6 |
|
# processed. |
7 |
|
# Interface |
8 |
|
# --------- |
9 |
+ |
# |
10 |
|
# new(file,objectref) : A new object - filename of file to parse |
11 |
< |
# objectref->of the methods |
11 |
> |
# objectref->of the methods |
12 |
|
# usetags(tagobjref) : Specify a tagcontainer set to direct to |
13 |
|
# to the desired routines |
14 |
|
# usegroupchecker(groupchecker) : Set a groupchecker |
15 |
|
# parse() : Parse the file |
16 |
< |
# line() : return the current line number of the parse |
17 |
< |
# tagstartline() : return the line number on which the current |
18 |
< |
# tag was opened |
19 |
< |
# stream(filehandle) : stream output to the filehandle if not handled |
20 |
< |
# in any other way |
16 |
> |
# line() : return the current line number of the parse |
17 |
> |
# tagstartline() : return the line number on which the current |
18 |
> |
# tag was opened |
19 |
> |
# stream(filehandle) : stream output to the filehandle if not handled |
20 |
> |
# in any other way |
21 |
|
package ActiveDoc::Switcher; |
22 |
+ |
use Utilities::Verbose; |
23 |
|
require 5.001; |
24 |
|
use Carp; |
25 |
|
|
26 |
+ |
@ISA=qw(Utilities::Verbose); |
27 |
+ |
|
28 |
|
sub new { |
29 |
|
my $class=shift; |
30 |
|
my $file=shift; |
52 |
|
$self->{streamexclude}{$tag}=1; |
53 |
|
} |
54 |
|
|
55 |
< |
sub _initialise (hash1) { |
56 |
< |
my $self=shift; |
57 |
< |
$self->{filename}=shift; |
58 |
< |
|
59 |
< |
# add a default groupchecker |
60 |
< |
use ActiveDoc::GroupChecker; |
61 |
< |
$self->{groupchecker}=GroupChecker->new(); |
62 |
< |
$self->{groupchecker}->include("all"); |
63 |
< |
|
64 |
< |
# Add a default TagContainer |
65 |
< |
use ActiveDoc::TagContainer; |
66 |
< |
$self->{tagcontainer}=ActiveDoc::TagContainer->new(); |
67 |
< |
|
68 |
< |
} |
55 |
> |
sub _initialise (hash1) |
56 |
> |
{ |
57 |
> |
my $self=shift; |
58 |
> |
$self->{filename}=shift; |
59 |
> |
$self->verbose(">> New ActiveDoc::Switcher created."); |
60 |
> |
# add a default groupchecker |
61 |
> |
use ActiveDoc::GroupChecker; |
62 |
> |
$self->{groupchecker}=GroupChecker->new(); |
63 |
> |
$self->{groupchecker}->include("all"); |
64 |
> |
|
65 |
> |
# Add a default TagContainer |
66 |
> |
use ActiveDoc::TagContainer; |
67 |
> |
$self->{tagcontainer}=ActiveDoc::TagContainer->new(); |
68 |
> |
|
69 |
> |
} |
70 |
|
|
71 |
|
sub usetags { |
72 |
|
my $self=shift; |
95 |
|
use FileHandle; |
96 |
|
local $filehandle; |
97 |
|
$filehandle=FileHandle->new(); |
98 |
+ |
|
99 |
+ |
$self->verbose(">> Reading file: ".$self->{filename}." "); |
100 |
+ |
|
101 |
|
$filehandle->open("<".$self->{filename}) |
102 |
|
or return 1; |
103 |
|
# The buffering seems all messed up - best not to use it |
105 |
|
|
106 |
|
# Start file processing |
107 |
|
while ( ($_=<$filehandle>) ) { |
108 |
+ |
# Skip lines that start with a hash. A better way |
109 |
+ |
# of adding comments than ignore tags: |
110 |
+ |
next if (/^#/); |
111 |
|
$self->{linecount}++; |
112 |
|
$self->{currentline}=$_; |
113 |
|
$self->{stringpos}=0; |
119 |
|
# make sure we close the last buffer |
120 |
|
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
121 |
|
$self->_getstore()); |
111 |
– |
#$self->_printstream(); |
122 |
|
} |
123 |
|
|
124 |
|
# |
159 |
|
|
160 |
|
# ---- In a tag |
161 |
|
if ( $self->{tagcontext}=~/tag/ ) { |
162 |
+ |
$self->{tagbuff}=$self->{tagbuff}.$char; |
163 |
|
if ( ! $self->_quotetest($char) ) { |
164 |
|
if ( ! $self->_labeltest($char) ) { |
165 |
|
if ( $char eq ">") { $self->_closetag(); } |
182 |
|
my $self=shift; |
183 |
|
my $char; |
184 |
|
$char=substr($self->{currentline},$self->{stringpos}++,1); |
174 |
– |
#print "Debug : Fetching character $char\n"; |
185 |
|
|
186 |
|
# Keep a record for any stream processes |
187 |
|
$self->{streamstore}=$self->{streamstore}.$char; |
205 |
|
|
206 |
|
# Do we have an opening or closing tag? |
207 |
|
if ( ($char=$self->_nextchar()) eq "/" ) { #we have a closing tag |
208 |
+ |
$self->{tagbuff}="<".$char; |
209 |
|
$self->{tagcontext}="endtag"; |
210 |
|
} |
211 |
|
else { # an opening tag |
212 |
+ |
$self->{tagbuff}="<"; |
213 |
|
$self->{tagcontext}="starttag"; |
214 |
|
$self->_checkchar($char); |
215 |
|
} |
204 |
– |
#print "\nDebug : Opening $self->{tagcontext}\n"; |
216 |
|
} |
217 |
|
|
218 |
|
# |
230 |
|
$tagroutine=$self->{tagname}."_".$self->{tagcontext}; |
231 |
|
$self->_calltag($tagroutine, $self->{tagname}, |
232 |
|
$self->{tagvar}); |
222 |
– |
#print "\nDebug : Closing Tag $tagroutine\n"; |
233 |
|
|
234 |
|
# -- Now make sure the text context is set for calling routines to |
235 |
|
# -- deal with text portions outside of tags |
236 |
< |
if ( $self->{tagcontext} eq "starttag" ) { |
237 |
< |
push @{$self->{textstack}} , $self->{textcontext}; |
238 |
< |
$self->{textcontext}=$self->{tagname}; |
236 |
> |
if ( ($self->{tagcontext} eq "starttag") ) { |
237 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
238 |
> |
push @{$self->{textstack}} , $self->{textcontext}; |
239 |
> |
$self->{textcontext}=$self->{tagname}; |
240 |
> |
} |
241 |
|
} |
242 |
|
else { |
243 |
|
if ( $#{$self->{textstack}} > -1 ) { |
244 |
< |
if ( $self->{textcontext} eq $self->{tagname} ) { |
245 |
< |
$self->{textcontext}=pop @{$self->{textstack}}; |
246 |
< |
} |
244 |
> |
if ( $self->{textcontext} eq $self->{tagname} ) { |
245 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
246 |
> |
# -- watch out for valid tags we ignore in this parse |
247 |
> |
$self->{textcontext}=pop @{$self->{textstack}}; |
248 |
> |
} |
249 |
> |
} |
250 |
|
else { #The tag we are closing is not the last one so |
251 |
|
# we keep our current context. |
252 |
|
$self->_removefromstack($self->{tagname},$self->{textstack}); |
254 |
|
|
255 |
|
} |
256 |
|
else { # more close tags than open ones |
257 |
+ |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
258 |
|
print "Warning : Unmatched </...> tag on line ". |
259 |
|
$self->line()."\n"; |
260 |
+ |
} |
261 |
|
} |
262 |
|
} |
263 |
|
} |
322 |
|
my $this; |
323 |
|
|
324 |
|
undef @tempstack; |
308 |
– |
#print "In ----".$#{$stack}; |
325 |
|
# Keep popping until we find our string |
326 |
|
while ( ($this=(pop @{$stack})) ne "$name") { |
327 |
|
push @tempstack, $this; |
332 |
|
$this=pop @tempstack; |
333 |
|
push @{$stack}, $this; |
334 |
|
} |
319 |
– |
#print " Out ----".$#{$stack}; |
335 |
|
} |
336 |
|
|
337 |
|
# |
393 |
|
|
394 |
|
# Do we have a label name? |
395 |
|
if ( $self->{lastlabel} ne "" ) { |
396 |
< |
$self->{tagvar}{$self->{lastlabel}}=$self->_getstore(); |
396 |
> |
(my $label=$self->{lastlabel})=~tr[A-Z][a-z]; |
397 |
> |
$self->{tagvar}{$label}=$self->_getstore(); |
398 |
|
$self->{lastlabel}=""; |
399 |
|
} |
400 |
|
elsif ( $self->_getstore() ne "") { |
403 |
|
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
404 |
|
} |
405 |
|
else { |
406 |
< |
die ">Tag syntax error in $self->{tagname} on line ". |
407 |
< |
$self->line()." of file \n$self->{filename}"; |
406 |
> |
# do not die anymore - breaks non tag documents |
407 |
> |
#die ">Tag syntax error in $self->{tagname} on line ". |
408 |
> |
# $self->line()." of file \n$self->{filename}"; |
409 |
> |
# -- assume that this is plain text |
410 |
> |
$self->{tagcontext}="text"; |
411 |
> |
$self->_resetstore(); |
412 |
> |
$self->_unshiftstore($self->{tagbuff}); |
413 |
> |
$self->{tagbuff}=""; |
414 |
> |
return; |
415 |
|
} |
416 |
|
} |
417 |
|
$self->_resetstore(); |
427 |
|
$self->{stringbuff}=$self->{stringbuff}.$char; |
428 |
|
} |
429 |
|
|
430 |
+ |
sub _unshiftstore() { |
431 |
+ |
my $self=shift; |
432 |
+ |
my $char=shift; |
433 |
+ |
|
434 |
+ |
$self->{stringbuff}=$char.$self->{stringbuff}; |
435 |
+ |
} |
436 |
+ |
|
437 |
|
sub _getstore() { |
438 |
|
my $self=shift; |
439 |
|
|