Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Merlyn Technique Viewer enhances the Web Techniques Perl Columns of Randal L. Schwartz (aka merlyn). On his home page he says of these articles,

"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."

A better solution
  1. Code description interleaved with source code.
  2. Code listed in a form for easy editing and saving.

    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.

    Here's a live demo.

    Update: Now uses HTML::TableExtract to resist subtle changes in the source files.
    #!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.co +m/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 r +an 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 listin +gs from their source locations, modifies the contents, and displays the results. An index d +ata 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 lis +tings (@oops) which choke MTV.<br>They are diplayed without enhanceme +nt.</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 listin +g",-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 t +hen only the source code is published at <a href='http://web.stonehen +ge.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' valu +e='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><mer +lyn>$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\+v +iewer">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 Descript +ion)] ); $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 }

    In reply to MTV by epoptai

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



  3. Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  4. Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  5. Please read these before you post! —
  6. 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
  7. Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  8. Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  9. See Writeup Formatting Tips and other pages linked from there for more info.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2014-09-02 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls