Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Here is my take on this. It is pretty similar, except the tag attributes ( src=... alt=...)are printed in the order they appear in the original text (you could also achieve this, if you used 'attrseq'). I however, use HTML::TokeParse and URI, and this one only modifies absolute urls (like links to other http|ftp sites, unlike yours, which will turn a '' into $new_url.''), but can easily be extended to modify any kind of URIs (that is Uniform Resource Identifier, not just Locator's ;D). If you were goning to make this more modular, i'd sugguest not doing it this way (extend HTML::Parser instead, but for a script, this is the perfect strategy, IMHO). Here goes:
#!/usr/bin/perl -w use strict; use LWP::Simple qw(get); use HTML::TokeParser; use URI; my $new_link = ""; my $url = $ARGV[0] or die "usage: ". __FILE__ ." +r.html\n"; my $file = get($url) or die "Cannot get the page '$url'\n"; my $P = HTML::TokeParser->new(\$file); while (my $T = $P->get_token() ) { # 0 1 2 3 4 if($$T[0] eq "S") # ["S", $tag, $attr, $attrseq, $text] { if($$T[1] eq 'img') { &handle_link($T, 'src'); } elsif($$T[1] eq 'a') { &handle_link($T, 'href'); } elsif($$T[1] eq 'script') { &handle_link($T, 'src'); } else # nothing we wanna change { print $$T[4]; } } elsif($$T[0] =~ /^(?:E|PI)$/ ) # end tag | process instruction { print $$T[2]; } elsif($$T[0] =~ /^(?:T|C|D)$/ ) # text | comment | declaration { print $$T[1]; } } # endof while (my $T = $P->get_token) sub handle_link { my ( $T, $A) = @_; my $URI = new URI( $$T[2]->{$A} ); # 0 1 2 3 4 # ["S", $tag, $attr, $attrseq, $text] my $scheme = $URI->scheme(); if( $scheme and $scheme =~ /^(http|ftp)$/i ) # only if its not rel +ative url (yes scheme) { $$T[2]->{$A} = $new_link . $URI->path(); # path is escaped } print "<$$T[1]", ( map { qq| $_="$$T[2]->{$_}"| } @{$$T[3]} ) , '>'; }

Disclaimer: Don't blame. It came from inside the void

perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

In reply to (crazyinsomniac) Re: HTML Link Modifier by crazyinsomniac
in thread HTML Link Modifier by OeufMayo

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2018-01-18 18:21 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (214 votes). Check out past polls.