Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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 = 'http://www.example.com'; 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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-03-28 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found