Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

My end solution to appending a unique marker to each url in a file

by cat2014 (Monk)
on Aug 09, 2001 at 02:27 UTC ( [id://103261]=note: print w/replies, xml ) Need Help??


in reply to appending a unique marker to each url in a file

For the curious, I'll post what I ended up doing. One of the main requirements of this script was that it carefully preserved the formatting of the files that it ran on- the only change could be the addition of the url markers, so that influenced my solution. the script would be called with two files- one html and one plain text. Sample html file to call with:
this a link: <br> <a href="http://www.somewhere.org/foo">somewhere</a> +<p> <a href= https://somewhere.else.net/bar/>another place to go</a>. +<p>
And sample text file pair to the html file:
this a link: http://www.somewhere.org/foo another place to go: https://somewhere.else.net/bar/
so here's my code to tag the urls in these files:
#$unprocessed_html holds the text of the html version of my file #$unprocessed_text holds the plain text version of my file #@markers holds the unique url markers, ie [/qfk33pe][/nnd92093] #handle html version my $processed_html = ""; while (length($unprocessed_html) > 0) { if($unprocessed_html =~ s/^(.*?\b(href|action)\s*=\s*)//si) { $processed_html .= $1; } else { $processed_html .= $unprocessed_html; last; } if (not(@markers)){ $processed_html .= $unprocessed_html; warn "no more markers available for remaining links\n"; last; } if($unprocessed_html =~ s/^([^\"\'][^<>\s]*)//) { my $url = $1 . shift(@markers); #strip double //s due to sloppy input $url =~ s|//|/|g; $processed_html .= $url; } elsif($unprocessed_html =~ s/^([\"\'])([^<>]*?)\1//) { my $url = $1 . $2 . shift(@markers) . $1; #strip double //s that can result from sloppy input $url =~ s|//|/|g; $processed_html .= $url; } else { die "something happened here"; } } #handle text version my $processed_text = ""; while (length($unprocessed_text) > 0) { if($unprocessed_text =~ s/^(.*?\b)http/http/si) { $processed_text .= $1; } else { $processed_text .= $unprocessed_text; last; } if (not(@markers)){ $processed_text .= $unprocessed_text; warn "no more markers available for remaining links\n"; last; } if($unprocessed_text =~ s/(http[\S]+)\s+//){ my $urlfound = $1 . shift (@markers); $urlfound =~ s|//|/|g; $processed_text .= "$urlfound\n"; } elsif($unprocessed_text =~ s/(http[\S]+)$//){ my $urlfound = $1 . shift (@markers); $urlfound =~ s|//|/|g; $processed_text .= "$urlfound\n"; } }
-- cat

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://103261]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-26 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found