CUFP
epoptai
Merlyn Technique Viewer enhances the [http://web.stonehenge.com/merlyn/WebTechniques|
Web Techniques Perl Columns] of Randal L. Schwartz (aka [merlyn]).
On his home page he says of these articles,
<p><blockquote>
"Because I'm generally busy (or lazy), I simply ran the
source files through the pod2html translator so that I
could put them online here. It makes them ugly sometimes,
but you'll just have to live with that for now until
someone comes up with a better solution."
<p></blockquote>
<big>A better solution</big>
<ol>
<li>Code description interleaved with source code.
<li>Code listed in a form for easy editing and saving.
</ul><p>
This program fetches selected articles and code listings
from their source locations and interleaves the code with
the article, makes some navigation links, and lists the
code in a textarea. An index data file of all the current
web technique articles must be built and refreshed once in
a while.
<p>
Here's a [http://www.monmouth.com/cgi-bin/cgiwrap/epoptai/mtv.pl|live demo].
<p><blockquote><b>Update:</b> Now uses [cpan://HTML::TableExtract]
to resist subtle changes in the source files.</blockquote>
<code>
#!perl -T
# mtv.pl - Merlyn Technique Viewer by epoptai
# 1. Interleaves source code with description.
# 2. Code listed in textarea for editing and saving.
use strict;
use CGI qw(:standard);
use LWP::Simple qw(get getstore);
use HTML::Entities;
use vars qw(%code $code $init $tmp $src $oop $wtc);
use HTML::TableExtract;
use CGI::Carp 'fatalsToBrowser';
my@oops=qw(10 13 28 38 48 55); # columns with double code listings
my$url = url();
my$dat = 'index.dat';
unless(-e $dat){$init=1}
&import_names('IN');
my$merlyn = 'http://web.stonehenge.com/merlyn/WebTechniques';
if($IN::action eq 'bindex'){&indexer}
if(($IN::action eq 'save as')or($IN::codename)){&savecode}
unless($IN::action eq 'view'){
print header,start_html(-title=>'MTV',-bgcolor=>'white',-text=>'black'),blockquote,h1('MTV');
print<<HTML;
Merlyn Technique Viewer enhances the <a href="http://web.stonehenge.com/merlyn/WebTechniques"><i>
Web Techniques Perl Columns</i></a> of Randal L. Schwartz. On his home page he says of these
articles,<blockquote>"Because I'm generally busy (or lazy), I simply ran the source files through
the pod2html translator so that I could put them online here. It makes them ugly sometimes, but
you'll just have to live with that for now until someone comes up with a better solution."
</blockquote><h3>A better solution</h3>
<ol><li>Code description interleaved with source code.<li>Code listed in a form for easy editing
and saving.</ol> This script fetches selected articles and code listings from their source
locations, modifies the contents, and displays the results. An index data file of all the current
web technique articles must be <a href="$url?action=bindex">built</a> and
<a href="$url?action=bindex">refreshed</a> once in a while.
HTML
if($init==1){
print qq~<p><b>Datafile not detected. <a href="$url?action=bindex">Build an index.</a>~;
}
if($init!=1){
my%dex = ();
my$md = (stat($dat))[9];
open(FILE,"< $dat") or die "$!";
my@dat = <FILE>;
close(FILE) or die "$!";
for(@dat){
my($num,$desc)=split(/\t/);
$dex{$num}=$desc
}
my$c=keys(%dex);
my$cc=($c/4);
print qq~<p>Index of $c Web Techniques columns<br><small>~;
printf "%s updated: %s\n", $dat, scalar localtime($md);
print qq~</small><form><select name="getdoc" size="$cc">~;
for(sort {$b<=>$a} keys(%dex)){
my$val=$_;
for(@oops){
if($val=~$_){
$val='00ps. '.$_;
}
}
print qq~<option value="$_">$val. $dex{$_}~;
}
print qq~</select><br><font size=-2>Note: '00ps' marks double code listings (@oops) which choke MTV.<br>They are diplayed without enhancement.</font><br><input type="submit" name="action" value="view"></form><br><p align=right><small><a href="http://perlmonks.org/index.pl?node=merlyn+technique+viewer">coded</a> by <a href="http://perlmonks.org/index.pl?node=epoptai">epoptai</a></small>~;
}
print end_html;
exit;
}
if($IN::action eq 'view'){
$src = 'col' . $IN::getdoc . '.listing.txt';
$tmp = $^T . '.tmp';
my$col = 'col' . $IN::getdoc . '.html';
$wtc = $merlyn.'/'.$col;
my$wts = $merlyn.'/'.$src;
for(@oops){ if($IN::getdoc=~$_){$oop=1}}
if($oop==1){
my$MTV=get($wtc);
print header,blockquote;
print $MTV;
print end_html;
exit(0);
}
my$res;
getstore($wtc,$tmp); # had problems doing this with get
$code = get($wts) or die "$!";
unless(-e $tmp){
print header,start_html(-title=>"Column $IN::getdoc source code listing",-bgcolor=>'white',-text=>'black'),blockquote,p("The newest columns are not available online until after they appear in print in <a href='http://www.webtechniques.com/'>Web Techniques magazine</a>. Until then only the source code is published at <a href='http://web.stonehenge.com/merlyn/WebTechniques/index.html'>Randal's site</a>. Try again soon! <a href='$url'>Back to index.</a>");
encode_entities($code);
print "<pre>$code</pre>";
print end_html;
exit(0);
}
}
open(F,"< $tmp") or die "$!";
my@col = <F>;
close(F) or die "$!";
my$MTV=();
{
open(F,"< $tmp") or die "$!";
local $/ = undef;
$MTV = <F>;
close(F) or die "$!";
}
if($IN::action eq 'view'){ unlink $tmp}
my@c=@col;
for(@col){
if($_=~/=(\d+)=/){ # find code listing lines
my$m=$&; # define hash key
if($_=~/=1=/){$_=~s/<(.*?)>//g} # fix =1=
$m=~s/=(\d+)=/$1/; # strip = = from key
$_=~s/=(\d+)=/$1\./; # and value
$_=~s/^\s+//g; # strip leading whitespace
$code{$m}=$_; # build code hash
}
}
my$h=(keys(%code)+3);
my$form = "<form method='post'><input type='submit' name='action' value='save as'> <input type='text' name='codename' size='30' value='col$IN::getdoc.pl'><br><textarea cols=80 rows='$h' name='codebody'>$code<\/textarea></form>";
$MTV=~s/(([L,l]ines?\s(\d+))(\s(through|to|and)\s(\d+))?)/<mmm><p><merlyn>$1<\/merlyn>/gi;
$MTV=~s/(<\/head>)/$1<base href="http:\/\/web\.stonehenge\.com\/merlyn\/WebTechniques\/">/oi;
$MTV=~s/(<BODY bgcolor=White text=Black>)/$1<blockquote>/oi;
$MTV=~s/(<H1>)<A NAME="(.*?)">(.*?)<\/A>(<\/H1>)/$1<a name="$2" href="$wtc">$3<\/a>$4<b>As rendered by
<a href="http:\/\/perlmonks\.org\/index\.pl\?node=merlyn\+technique\+viewer">MTV<\/a><\/b> - (<a href="$url">index<\/a>)<p>/iso;
$MTV=~s/(<H2>.*?<\/H2>)(.*?)<\/PRE>/$1$form/iso;
my@MTV=split(/<mmm>/,$MTV);
for(@MTV){
if($_=~/^<p><merlyn>(([L,l]ines?\s(\d+))(\s(through|to|and)\s(\d+))?<\/merlyn>)/i){
my$d0=$&;
my$d1=$3;
my$c1=$5;
my$d2=$6;
if($c1=~/and/){
$_=~s/$d0/<p><tt> $code{$d1}<\/tt><br><tt> $code{$d2}<\/tt><p>$d0/g;
}
elsif($c1=~/through|to/){
my@total;
while($d1 <= $d2){
push @total, $code{$d1};
$d1++
}
my$sum = join('<br>', @total);
$_=~s/$d0/<pre>$sum<\/pre><p>$d0/g;
}
elsif(!$d2){
$_=~s/$d0/<p><tt> $code{$d1}<\/tt><p>$d0/g;
}
}
}
print header;
print @MTV;
print end_html;
exit;
sub indexer{
my$c;
my$z = $merlyn . '/index.html';
my$dexter = get($z);
my$te = new HTML::TableExtract( headers => [qw(Column Listing Description)] );
$te->parse($dexter);
open (FILE,"> index.dat") or die "$!";
foreach my $ts ($te->table_states){
foreach my $row ($ts->rows){
chomp(@$row[0]);
chomp(@$row[2]);
@$row[0]=~s/col//o;
print FILE "@$row[0]\t@$row[2]\n";
$c++
}
}
close(FILE) or die "$!";
print header,start_html(-bgcolor=>'white',-text=>'black'),blockquote,p("$c Web Techniques columns indexed!<p><a href='$url'>Reload</a>");
exit
}
sub savecode{
open (FILE,"> $IN::codename") or die "$!";
print FILE $IN::codebody;
close(FILE) or die "$!";
print header,start_html(-bgcolor=>'white',-text=>'black'),blockquote,p("Code saved as <a href='$IN::codename'>$IN::codename</a> in same dir as <a href='$url'>this script</a>.");
exit
}
</code>