Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Well this is pretty nifty.

I am building a workflow application at work, and the heart of the matter is dealt with by a Perl script on the web. The documentation sits in a webified Lotus database on another machine. I have to include the documentation from the Lotus front page in my script.

It was obvious that HTML::Parser was the answer, but it took a bit of fiddling about to get it right. The thing to remember is that a decent module should always include an eg/ directory to help you out. By mulling over the contents of the examples included with H::P I was able to come up with the following code.

The idea is to look at the tags as they go by, and keep an eye out when we enter <script> or <body> tags. While I was writing this, I found it helpful to use the DEBUG setting, to change the opening less-than character of a tag to an left square bracket, which made it easier to see what was happening.

I'm not a heavy user of H::P; this is perhaps the most complex thing I've ever done with it, so I'm quite pleased the way it turned out. See if you can spot the pun in the code.

use strict; use LWP::Simple; use HTML::Parser; use constant DEBUG => 0; my $URI_HOST = ''; my $URI_PATH = '/sample/database.nsf'; my $content = get( "$URI_HOST$URI_PATH" ); if( not $content ) { $content = q{<p><small>The documentation is unavailable</small></p +>}; } else { my $new = ''; my %in_tag; my $tag_watch = sub { my $tag = shift; my $in = shift; my $attr = shift; $in_tag{$tag} += $in; return unless $in_tag{script} or $in_tag{body} or grep { $tag eq $_ } qw( script body ) ; my $out = (DEBUG ? '[' : '<') . ($in < 1 ? '/' : '') . $tag; if( $attr ) { my @attr_pair; while( my( $key, $value ) = each %$attr ) { # renormalise things that look like URIs $value =~ s{^($URI_PATH/)}{$URI_HOST$1}; push @attr_pair, qq{$key="$value"}; } $out .= join( ' ', ('', @attr_pair)); } $out .= '>'; $new .= $out; }; my $p = HTML::Parser->new( api_version => 3, start_h => [$tag_watch, "tagname, '+1', attr"], end_h => [$tag_watch, "tagname, '-1'"], text_h => [sub { $new .= $_[0] if $in_tag{script} or $in_ta +g{body} }, "dtext"], comment_h => [sub { $new .= $_[0] if $in_tag{script} }, + "dtext"], ); $p->parse($content); $p->eof(); $content = $new; } # now include $content in a templating/include output mechanism of you +r choice

• another intruder with the mooring in the heart of the Perl

In reply to Pulling part of another web page into your output on the fly (with HTML::Parser) by grinder

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2020-02-26 21:30 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (117 votes). Check out past polls.