Since this feature is not available (or if it is, I'm not aware of it), I thought it would make a nice project to write a script that would do this for me. I don't know much about proxy servers or Web automation, so this is a learning experience for me.
The following is the first stab at the code (kind of a "proof of concept"). All this code is supposed to do is display a Web page with a (currently) non-functional download link after each CODE posting. Also, all HREF links are pointed back to this code. The problem lies in the regex and the while loop that it is in. When I run the code, it simply hangs. While running it through a debugger, it seems to identify matches in a random, non-sequential order, thus not permitting the while loop to end.
I know this is probably something ridiculously simple that I have missed, but I am pulling my hair out over this. Any help would be appreciated.#!/usr/bin/perl -w use strict; use CGI; use LWP::Simple; my $query = new CGI; my $basename = 'http://www.perlmonks.org/'; my $script = 'http://www.someserver.com/path/to/script.cgi'; # I track the actual URL as a hidden field in the HTML my $url = defined $query->param('url') ? $query->param('url') : $ +basename; # Default to $basename if no $url exists my $content = get (defined $url ? $url : $basename); # Add a hidden field with actual URL after <BODY> tag $content =~ s!(<BODY[^>]*>)!$1<INPUT TYPE="hidden" NAME="basename" VAL +UE="$url">!; # Have absolute paths go through this script $content =~ s!href="$basename!href="$script?url=$basename!gi; # Have relative paths go through this script $content =~ s!href\s*=\s*"/!href="$script?url=$basename!gi; # In the following regex, note the following: # Code tags are translated as # <PRE><TT><font size="-1">...</font></TT></PRE> # # <font size=...> and </font> are optional. This is turned off if w +e use "Large font code" # Quotes around -1 in the font tag are optional. They don't always +exist. # I discovered that in examining source for "Death to Dot Star!" my $code_regex = '<PRE><TT>(?:<font size="?-1"?>)?([^<]+)(?:</font>)?< +/TT></PRE>'; # These will be used to create the download link my $href1 = '<P><A HREF="' . $script . '?process=download&code='; my $href2 = '&url=' . $url . '">Download this code</A><P>'; my $i = 0; while ($content =~ m!($code_regex)!go) { my $match = $1; $content =~ s!$match!$match$href1$i$href2!; $i++; } print $query->header; print $content;
Cheers,
Ovid
Incidentally, some of the regexes and code above work only because of the layout of Perlmonks. This should not be viewed as any sort of general purpose script.
In reply to Perlmonks Code Proxy by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |