htmlop.pm untested

filter: Statementstmt 0.0% 0 / 279 Branchbran 0.0% 0 / 176 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 561 CC 89 SCAR 82.2
CC
89
Cov
0%
process
5112.0
gettoken
42.0
gettag
42.0
CRAP 3710.8
1 # -*-perl-*-
2 # htmlop.pl: Do operations on html documents.
3 package htmlop;
4 $VERSION=0.2.6;
5 #
6 # Original source from Bjørn Borud, without it I would not have atempted
7 # this. In this incarnation it bears no resemblance to Bjørns code.
8 # - Nicolai Langfeldt 18/11/95.
9 #
10 # htmlop.pl does operations on html files, possebly many at a time.
11 # Operations:
12 # - Absolitify urls
13 # - Relativify urls
14 # - Gather list of urls
15 # - Callback with url
16 # - Canonify document w.r.t. SGML.
17 #
18 # Authors:
19 # - Nicolai Langfeldt (janl@ifi.uio.no)
20 # - Chris Szurgot (szurgot@itribe.net)
21 #
22 # Changes:
23 # janl 18/11/95 - Initial version
24 # szurgot 09/02/96 - Code in htmlop'process to remove <BASE> Tag if we are
25 # returning the form. An unchanged base destroys local
26 # fragments.
27 # janl 22/02/96 - Added URLSUB functions. <BASE> will only be removed from
28 # the returned doc, never from the origninal doc.
29 # janl 16/05/96 - Removed I* options and added single NODOC option/modifier
30 # to replace them.
31 # janl 09/09/96 - Added URLPROC and NREL opcodes, for a better URL
32 # processing model. -> 0.1.7
33 # janl 24/09/96 - Various cosmetics, no longer inserting a !SGML tag,
34 # only !DOCTYPE and HTML tags. -> 0.1.8
35 # janl 11/10/96 - URLs in HTML 3.2 tags are now found.
36 # janl 6/11/96 - Added netscape SCRIPT tag.
37 # janl 20/11/96 - 'BORDER' is different from 'BORDER=0' -> 0.1.10
38 # janl 06/11/96 - Added userdata argument to URLPROC.
39 # Added SAVEURLS and USESAVED opcodes. -> 0.1.11
40 # janl 11/04/97 - Changed to URI::URL and got into strict harness
41 # janl 13/04/97 - Fixed comment processing, extended it to processing
42 # directives. Bug reported by Chris Johnson. -> 0.1.12
43 # janl 08/04/97 - Made ISMAP into ISMAP=, bad! -> 0.1.13
44 # janl 23/05/97 - Treating <script> and <style> as verbatim -> 0.1.14
45 # janl 06/06/97 - Canonify: <!SGML tag confused it and !DOCTYPE was mangled
46 # -> 0.1.15
47 # janl 30/07/97 - Now supporting <BASE> tag.
48 # - Added TAGCALLBACK opcode -> 0.1.16
49 # janl 16/10/97 - Empty string must be quoted, reported by Bart Barenburg
50 # -> 0.1.17
51 # janl 01/12/97 - More HTML URL tags/attributes recognized (Greg Lindhorst)
52 # janl 13/12/97 - Can't delete the whole <BASE> tag. Netscape has
53 # extended it for frame use. So only delete the HREF
54 # attr -> 0.1.18
55 # janl 01/01/98 - Realized applet/object support can't work and changed
56 # so it's able to work, if conditions are right.
57 # janl 04/01/98 - Added html 4.0 tags and attributes.
58 # janl 08/02/98 - Hacked for speed. Went from 43s to 9s on a 170K
59 # document -> 0.2. Thanks to Rune Frøysa who taunted me.
60 # janl 09/04/98 - More tolerant about what constitutes a newline -> 0.2.1
61 # janl 09/05/98 - Export %isdir -> 0.2.2
62 # janl 13/04/99 - Remove leading /../ sequences in path component in
63 # ABS code. -> 0.2.3
64 # janl 28/05/99 - The code was buggy, now it's not. -> 0.2.4
65 # janl 04/02/01 - Use epath instead of path -> 0.2.6
66  
67 package htmlop;
68  
69 use URI::URL;
70  
71 use strict;
72 # Global variables
73 use vars qw($ABS $REL $LIST $CANON $URLSUB $NODOC $URLPROC $NREL);
74 use vars qw($SAVEURL $USESAVED $TAGCALLBACK $debug %isdir);
75  
76 # These are for the smartrel routines
77 my $url_origin;
78 my $doc_top;
79 my $doc_top_re;
80 my $choped_url_or;
81  
82 # HTML operation codes for process_html. The first argument is the
83 # html document to do operations on. It will not be changed. If
84 # NODOC is not passed a new document edited as specified will be
85 # returned. No more than one document will be returned by one
86 # invocation of htmlop.
87  
88 $ABS = 1; # Absolutify urls. Arg: Origin. The
89 # ABS function absolutifies URLS,
90 # assuming they are relative to the
91 # argument.
92 # If a <BASE> tag is found the origin
93 # given by it will be used instead of
94 # the arguemnt given.
95  
96 $REL = 2; # Relativize all urls. Arg: Origin.
97 # The REL function simply removes the
98 # argument string from any urls
99 # matching it. For this reason the
100 # origin string is interprated as a
101 # RE, which may have unexpected
102 # results, unless RE specials are
103 # escaped, which they should be.
104  
105 $LIST = 3; # List all urls, Ret: urls
106  
107 $CANON = 4; # See that missing opening (and
108 # ending) SGML tags are injected.
109  
110 $URLSUB = 5; # Do regular expression substitution
111 # on urls. Arg: RE, substitute.
112  
113 $NODOC = 6; # Do not return a rebuilt document.
114 # Saves memory, and time
115  
116 # NOTE: I SUSPECT URLPROC IS BROKEN IF A BASE TAG APPEARS IN THE TEXT.
117 # THE URL PROCESSOR NEEDS TO BE PASSED htmlop::process' IDEA OF WHAT THE
118 # BASE URL IS.
119  
120 $URLPROC = 7; # Apply function on urls (process
121 # urls). Arg: Pointer to function to
122 # apply, userdata. The function will
123 # be passed the url, modified by any
124 # previous operations and the
125 # userdata. The function must return
126 # the new url.
127  
128 $NREL = 8; # New relativisation function, works
129 # much better than the old one. Arg:
130 # Origin, Top.
131  
132 $SAVEURL = 9; # Save urls in tag with modified name.
133 # Arg: attribute prefix.
134 # Example: <a href=foo> becomes
135 # <a href=foo w3mir-href=foo> if no other
136 # processing of the url is done.
137  
138 $USESAVED = 10; # Use saved urls. Arg: attribute prefix
139  
140 $TAGCALLBACK = 11; # Procedure to call for each Tag.
141 # Args: procedure, userdata (one item)
142  
143 # Args to procedure: userdata, Base
144 # URL, tag name, reference to array of
145 # URL attributes, reference to hash of
146 # all attributes. The base url is
147 # derived from the one used in ABS or
148 # the BASE tag.
149  
150 $debug=0; # Debugging level in this package
151  
152 # process_html returns a array. The first component of the array is
153 # the new html document. The rest of the array is the urls. If a
154 # document is not to be returned a empty string is returned. If a url
155 # list is not to be returned a empty array is returned.
156  
157 # HERE BE DRAGONS:
158  
159 # Where to find URLs in various tags. The second compoent is a array
160 # reference.
161  
162 my(%urls) = (
163 HEAD => [ 'PROFILE' ],
164 BLOCKQUOTE => [ 'CITE' ],
165 Q => [ 'CITE' ],
166 INS => [ 'CITE' ],
167 DEL => [ 'CITE' ],
168 A => [ 'HREF' ] ,
169 IMG => [ 'SRC' ,'LOWSRC' ,'USEMAP', 'LONGDESC' ] ,
170 EMBED => [ 'SRC' ],
171 FRAME => [ 'SRC', 'LONGDESC' ],
172 IFRAME => [ 'SRC', 'LONGDESC' ],
173 BODY => [ 'BACKGROUND' ],
174 AREA => [ 'HREF' ],
175 LINK => [ 'HREF' ],
176  
177 # The APPLET and OBJECT tags do not fit into my model for URL
178 # manipulation. Just looking at CODEBASE might work, if the
179 # URL it names is a browseable directory...
180 APPLET => [ 'CODEBASE' ], # If the codebase dir is browseable
181 OBJECT => [ 'CODEBASE' ], # Ditto. Can't handle DATA attribute now
182  
183 INPUT => [ 'SRC', 'USEMAP' ],
184 MAP => [ 'HREF' ],
185 SCRIPT => [ 'SRC', 'FOR' ],# 'FOR's semantics is not defined, the
186 # attribute is just reserved for possible
187 # future use...
188 BGSOUND => [ 'SRC' ],
189 FORM => [ 'ACTION' ], # Is this asking for trouble?
190 # Maybe it should just be absolutized...
191 # On the other hand: It's CGI...
192 );
193  
194 my(%relative) = (
195 # Identify URL attributes containing urls that are relative to
196 # the named URL attribute. When processing these they should
197 # be absolitized and then relativized relative to the BASE attribute.
198 # This is just window dressing for now; it is not used for anything.
199  
200 # ARCHIVE is really a URI _list_.
201 CODEBASE => [ 'CLASSID', 'DATA', 'CODE', 'ARCHIVE' ],
202 );
203  
204 %isdir = (
205 # These tags refer to directories:
206 CODEBASE => 1
207 );
208  
209 # Tags that enclose bits we want to leave absolutely alone because they
210 # are not very like HTML, or some such.
211  
212 # The material between the start and end tags is copied with no
213 # processing at all. The end tag is left to be processed.
214 # The endtag match is case insensitive.
215 my(%verbatim) = (
216 SCRIPT => quotemeta('</SCRIPT>'), # Embeded scripts
217 STYLE => quotemeta('</STYLE>'), # Embeded stylesheet
218 );
219  
220 # These are the functions that pick the HTML to pieces. It will not
221 # work esp. good on a random SGML document since the HTML application
222 # of SGML has simpler quoting than it might.
223  
224 sub gettoken {
225 # Get one token from the argument, removing it from the argument.
226 # BUG: There should be whitespace at the end of the examined string.
227 my($c,$token,$i);
228
229 # Skip whitespace and newlines
230 return '' unless defined(@_) && defined($_[0]);
231 $_[0] =~ s/^[\r\n\s]*//;
232
233 return '' if ($_[0] eq '');
234
235 $c = substr($_[0],0,1);
236 substr($_[0],0,1)='';
237
238 if ($c eq '"' || $c eq "\'") { # Quoted material
239 $i=index($_[0],$c);
240 # End-quote missing, just gobble the rest of the doc
241 $i=length($_[0]) if $i == -1;
242 # Extract and remove token
243 $token=substr($_[0],0,$i);
244 substr($_[0],0,$i+1)='';
245 } elsif ($c eq '=') {
246 $token='=';
247 } else { # Non-quoted material, ends in whitespace or =
248 $_[0] =~ m/[=\s\n\r]/;
249 $_[0] = $&.$';
250 $token=$c.$`;
251 }
252 # print "Token: '$token'\t\tRest: '",$_[0],"'\n";
253 return $token;
254 }
255  
256  
257 sub tagtoken {
258 # Pick the tag to pieces (also knonw as tokens). Return an
259 # associative array of attributes. The attribute-names are changed
260 # to uppercase. The attribute-values are left as is.
261 my($tok,$lasttok,%tokens);
262 # Append a space, gettoken needs it - silly? Yes!
263 # Change it to test on boundrary things rather than ...?
264 $_[0].=' ';
265  
266 $lasttok='';
267 while (1) {
268 last if (($tok=uc &gettoken($_[0])) eq '');
269  
270 if ($tok eq '=') {
271 # print STDERR " -bad html-" if ($lasttok eq '');
272 $tokens{$lasttok}.=&gettoken($_[0]);
273 print STDERR "STORED: $lasttok = ",$tokens{$lasttok},"\n" if $debug;
274 } else {
275 $tokens{$tok}=undef;
276 $lasttok=$tok;
277 }
278 }
279 return %tokens;
280 }
281  
282  
283 sub gettag {
284 # Pick out the following things from the remaining html doc:
285 # Everything leading up to the first tag. The first tag, and its
286 # contents. Modify @_ directly to reduce number of copies of
287 # possebly huge documents kept in memory at once. Return the body,
288 # the tag name, and the attributes (associative array)
289 my(%attr,$tagn,$tagc,$body,$tag,$doc);
290  
291 $doc=\$_[0];
292  
293 my($start,$end,$length);
294
295 $start=index($$doc,'<');
296  
297 if ($start<$[) {
298 # EOF
299 $body=$$doc;
300 $$doc='';
301 return ($body,'',());
302 }
303  
304 $end=index($$doc,'>',$start+1);
305
306 if ($end<$[) {
307 # This sucks, found no end of the tag...
308 $body=$$doc;
309 $$doc='';
310 return ($body,'',());
311 }
312  
313 $length=$end-$start-1;
314  
315 $body=substr($$doc,0,$start);
316 $tag=substr($$doc,$start+1,$length);
317  
318 # This shortens the string in each itteration, some kind of mechanism
319 # to do it once in a while would speed things up further. HOWEVER, when
320 # I tried to code this all I got was a _nasty_ memory leak.
321 substr($$doc,0,$end+1)='';
322  
323 # print STDERR "------\n";
324
325 # print STDERR "BODY: /$body/\n";
326 print STDERR "COMPLETE TAG: /$tag/\n" if $debug;
327 # print STDERR "REST: /",substr($$doc,0,20),"/\n";
328  
329 # print STDERR "------\n";
330  
331 # Examine tag contents
332 if ($tag =~ /^([!?]--)/ || $tag =~ /^(!\w+)/) {
333 # Comment or processing dicective, handle specially
334 $tagn=$&;
335 $tagc=$';
336 return ($body,$tagn,("$tagc",undef));
337 }
338  
339 # Everything else
340 ($tagn,$tagc) = split(/[\s\n\r]+/,$tag,2);
341 $tagn="\U$tagn";
342  
343 return ($body,$tagn,()) if !defined($tagc);
344
345 return ($body,$tagn,&tagtoken($tagc));
346 }
347  
348  
349 # This is meant for general consumption:
350  
351 sub process {
352 # Process a html file. Into one end you put a html file. Out
353 # of the other end you get something dependent on the operations
354 # you specified.
355 # I cannot gobble my arguments. I need to examine them several times.
356
357 my($arg,$doc,$i,$retdoc,$canon,$newdoc,@urllist,$Q,$cont);
358 my($origin,$baseurl);
359
360 $origin=$baseurl='';
361  
362 $retdoc=$canon=0;
363
364 # Get the document from the argument list
365 $doc=shift(@_);
366
367 $retdoc=1;
368 $i=0;
369 # Argument checking
370 while (defined($arg=$_[$i++])) {
371 if (! ($arg =~ /^\d+$/)) {
372 print STDERR "ERROR IN HTMLOP::process:\n";
373 print STDERR "Args: ",join(',',@_),"\n";
374 print STDERR "ARG: $arg is not a opcode.\n";
375 exit(1);
376 }
377 if ($arg == $ABS) {
378 $baseurl=$origin=$_[$i++];
379 } elsif ($arg == $REL || $arg == $SAVEURL || $arg == $USESAVED) {
380 $i++; # Skip one arg
381 } elsif ($arg == $URLSUB || $arg == $NREL || $arg == $URLPROC ||
382 $arg == $TAGCALLBACK ) {
383 $i += 2; # Skip two args
384 } elsif ($arg == $LIST) {
385 # do nothing
386 } elsif ($arg == $CANON) {
387 $canon=1;
388 } elsif ($arg == $NODOC) {
389 $retdoc=0;
390 } else {
391 die "htmlop: Incorrect invocation of html_process\n";
392 }
393 }
394
395 my($endhtml)=0; # Have we seen </html>?
396 my($SGML)=1; # 1: !DOCTYPE not seen, 2: !DOCTYPE seen, -1: it's OK
397  
398 # These are used to store the tag components
399 my($textpart);
400 my($tagname);
401 my(%attrval);
402  
403 my($wholetag); # The whole tag, put together again
404 my($moretag); # TMP storage of tag attributes
405 my($attr); # Looping thru attributes, misnomer.
406 my($RE);
407 my($subst);
408 my($prefix);
409 my($fun); # Function to apply to url
410 my($verbatim)='';
411 my($url_o,$path); # URL object Used in ABS to remove leading ..
412  
413 # Welcome to the machine
414  
415 $newdoc='';
416 while ($doc ne '') {
417 ($textpart,$tagname,%attrval)=&gettag($doc);
418
419 # Process the tag
420 { # Need a way to get out of this, last is my friend
421 # 'Canonize'
422 if ($canon) {
423 $endhtml=1 if ($tagname eq '/HTML');
424 if ($SGML!=-1 && $canon && $tagname ne '!--' && $tagname ne '!SGML') {
425 $wholetag=$tagname;
426 $moretag=join('',keys %attrval);
427 $wholetag.=' '.$moretag if ($moretag ne '');
428 if ($SGML==1) {
429 $SGML=2;
430 if ($tagname eq '!DOCTYPE') {
431 $tagname=$wholetag;
432 %attrval=();
433 last;
434 }
435  
436 # We have no idea what DTD this doc follows so we put in
437 # something kinda non-comitting
438 $newdoc.='<!DOCTYPE HTML PUBLIC "html.dtd">'."\n";
439 }
440 if ($SGML==2) {
441 $SGML=-1;
442 if ($tagname eq 'HTML') {
443 $tagname=$wholetag;
444 %attrval=();
445 last;
446 }
447 $newdoc.='<HTML>'."\n";
448 }
449 } # sgml!=-1
450 } #canon
451  
452 # Tack on text part before I bail out, if wanted
453 $newdoc.=$textpart if $retdoc;
454  
455 if ($tagname eq 'BASE') {
456 if (exists($attrval{'HREF'})) {
457 if ($origin) {
458 $baseurl=(url($attrval{'HREF'})->abs($origin,1))->as_string;
459 } else {
460 $baseurl=$attrval{'HREF'};
461 }
462 # Get rid of the HREF attribute. Netscape 4.0 puts (other) stuff
463 # into BASE that is not found in the HTML 4.0 spec.
464 delete $attrval{'HREF'};
465 print STDERR "\nBase tag: $baseurl\n" if $debug;
466 }
467 }
468  
469 # URL processing
470 $i=0;
471 while (defined($arg=$_[$i])) {
472 if (! ($arg =~ /^\d+$/)) {
473 print STDERR "Args: ",join(',',@_),"\n";
474 print STDERR "ARG: $arg ($i) is not a opcode.\n";
475 exit(1);
476 }
477 $i++;
478 if ($arg == $ABS) {
479 $origin=$baseurl || $_[$i++];
480 $i++;
481 # Want it to be a URL object
482 $origin=url $origin unless ref $origin;
483 print STDERR 'ABS: ',$origin->as_string,"\n" if $debug;
484 next unless defined($urls{$tagname});
485 foreach $attr (@{$urls{$tagname}}) {
486 if (defined($attrval{$attr})) {
487 # Ugly: Remove leading /../ sequences in path component
488 $url_o=url($attrval{$attr})->abs($origin,1);
489 # Remove .. and . parts that old versions of URI module does
490 # not handle
491 # print "URL: ",$url_o->as_string,"\n";
492 # print "PATH: ",$url_o->epath,"\n";
493 if (defined($path=$url_o->epath) &&
494 ($path =~ /\.\./ || $path =~ /\/\.$/)) {
495 # Trailing ..: /foo/bar/.. => /foo/
496 $path =~ s~[^/]*/\.\.$~~;
497 # Leading: /../../foo => /foo
498 $path =~ s~^/(\.\./)*~/~g;
499 # Trailing .: foo/. => foo/
500 $path =~ s~/\.~/~;
501
502 $url_o->epath($path);
503 }
504 $attrval{$attr}=$url_o->as_string;
505 }
506 }
507 } elsif ($arg == $REL) {
508 $origin=$_[$i++];
509 # Want it to be a string
510 $origin=$origin->as_string if ref $origin;
511 print STDERR 'REL: ',$origin,"\n" if $debug;
512 next unless defined($urls{$tagname});
513 foreach $attr (@{$urls{$tagname}}) {
514 $attrval{$attr} =~ s/^$origin//
515 if defined($attrval{$attr});
516 }
517 } elsif ($arg == $URLSUB) {
518 $RE=$_[$i++];
519 $subst=$_[$i++];
520 warn "URLSUB: $RE -> $subst\n" if $debug;
521 next unless defined($urls{$tagname});
522 foreach $attr (@{$urls{$tagname}}) {
523 $attrval{$attr} =~ s/$RE/$subst/
524 if defined($attrval{$attr});
525 }
526 } elsif ($arg == $LIST) {
527 warn "LIST;\n" if $debug;
528 next unless exists($urls{$tagname});
529 foreach $attr (@{$urls{$tagname}}) {
530 if (exists($attrval{$attr})) {
531 $attrval{$attr}.='/'
532 if $isdir{$attr} && $attrval{$attr} =~ m~[^/]$~;
533 push(@urllist,$attrval{$attr});
534 }
535 }
536 } elsif ($arg == $SAVEURL) {
537 warn "SAVEURL;\n" if $debug;
538 $prefix=$_[$i++];
539 next unless exists($urls{$tagname});
540 foreach $attr (@{$urls{$tagname}}) {
541 $attrval{"$prefix$attr"}=$attrval{$attr}
542 if defined($attrval{$attr});
543 }
544 } elsif ($arg == $USESAVED) {
545 warn "USESAVED;\n" if $debug;
546 $prefix=$_[$i++];
547 next unless exists($urls{$tagname});
548 foreach $attr (@{$urls{$tagname}}) {
549 $attrval{$attr}=$attrval{"$prefix$attr"}
550 if (defined($attrval{"$prefix$attr"}));
551 # bug compatability, drop sometime after w3mir 1.0
552 $attrval{$attr}=$attrval{"$prefix-$attr"}
553 if (defined($attrval{"$prefix-$attr"}));
554 }
555 } elsif ($arg == $NODOC) {
556 warn "NODOC;\n" if $debug;
557 } elsif ($arg == $CANON) {
558 warn "CANON;\n" if $debug;
559 } elsif ($arg == $URLPROC || $arg == $NREL) {
560 # Apply a function to all urls.
561 # NREL = Special case of $URLPROC, apply internal function.
562 if ($arg == $URLPROC) {
563 warn "URLPROC;\n" if $debug;
564 $fun=$_[$i++];
565 $arg=$_[$i++];
566 } else {
567 $fun=\&smartrel;
568 $arg=undef;
569 $url_origin=$_[$i++];
570 $doc_top=$_[$i++];
571 $doc_top_re=quotemeta $doc_top;
572 ($choped_url_or=$url_origin) =~ s/^$doc_top_re//;
573 warn "NREL $url_origin $doc_top\n" if $debug;
574 }
575  
576 next unless (defined($urls{$tagname}));
577  
578 foreach $attr (@{$urls{$tagname}}) {
579 $attrval{$attr}=&$fun($attrval{$attr},$arg)
580 if defined($attrval{$attr});
581 }
582 } elsif ($arg == $TAGCALLBACK) {
583 $fun=$_[$i++];
584 $arg=$_[$i++];
585 warn "TAGCALLBACK($tagname);\n" if $debug;
586 &$fun($arg,$baseurl,$tagname,
587 (defined($urls{$tagname})?($urls{$tagname}):undef),
588 \%attrval);
589 } else {
590 print STDERR "Internal error. opcode: $arg, i: $i, args: ",
591 join(',',@_),"\n";
592 }
593 }
594 last;
595 }
596  
597 # That ends URL processing
598  
599 # Was this a verbatim leadin tag? If yes, atempt to fish out text
600 # between here and the end tag, (minimal match) and substitute it
601 # with nothing. The end tag is kept. And the fished out text is
602 # re-inserted in the result with no changes.
603  
604 $verbatim=$1
605 if defined($verbatim{$tagname}) &&
606 ($doc =~ s/^(.*?)($verbatim{$tagname})/$2/is);
607
608 # Tack on the tag, if wanted.
609 if ($retdoc) {
610 if ($tagname) {
611 $newdoc.='<'.$tagname;
612 foreach $attr (keys %attrval) {
613 $newdoc.=' '.$attr;
614 if (defined($cont=$attrval{$attr})) {
615 $Q='"';
616 $Q="'" if $cont =~ m/\"/;
617 $newdoc.='='.$Q.$cont.$Q;
618 }
619 }
620 $newdoc.='>'.$verbatim;
621 $verbatim='';
622 }
623 }
624 print STDERR "NEW: $newdoc\n" if $debug > 2;
625 }
626 $newdoc.="</HTML>\n" if ($canon && !$endhtml);
627 return ($newdoc,@urllist);
628 }
629  
630  
631 sub smartrel {
632 # 'smart' relativisation function, uses .. to refer to things up to
633 # $doc_top level. Outside that scope it stays absolute.
634 #
635 # The rel function itself is now in libwww-perl
636
637 my($url_o)=@_;
638  
639 return if (!(defined($url_o) && $url_o));
640
641 $url_o=url $url_o unless ref $url_o;
642  
643 # print STDERR "\nsmartrel: $url_o->as_string\n",
644 # " from: $url_origin\n",
645 # " within: $doc_top\n";
646
647 # Check if within scope of our doc
648 $url_o = $url_o->rel($url_origin)
649 if $url_o->as_string =~ m/^$doc_top_re/;
650  
651 return $url_o->as_string;
652 }
653  
654  
655 sub smart_setup {
656 # Setup routine for smartrel.
657  
658 ($url_origin,$doc_top) = @_;
659  
660 $url_origin=$url_origin->as_string if ref $url_origin;
661 $doc_top=$doc_top->as_string if ref $doc_top;
662  
663 $doc_top_re=quotemeta $doc_top;
664  
665 ($choped_url_or=$url_origin) =~ s/^$doc_top_re//;
666 }
667  
668 1;