#!perl use strict; use vars qw($opt_usage $opt_index $opt_debug $queries $lastref); use XML::Twig; use LWP::Simple; #MAIN { process_options(); $queries = 0; $lastref = ""; #print "\n"; my $indextwig= XML::Twig->new( twig_roots => { 'item' => \&do_item } ); $indextwig->parsefile($opt_index); #print "
\n"; print STDERR "Queries performed: $queries\n"; } sub do_item { my ($twig, $elt)= @_; my $ref = $elt->{'att'}->{'ref'}; my $fullref = $ref; $fullref =~ s/[a-z]$//; unless ($fullref eq $lastref) { my $plainref = $fullref; $plainref =~ s/\./ /; $plainref =~ s/\./:/; #formatted for query and href my $esvref = $plainref; $esvref =~ s/ /\+/; # formatted for HTML my $htmlref = $plainref; $htmlref =~ s/ / /; my $description = $elt->{'att'}->{'description'}; my $implicithtml = ""; if ($elt->{'att'}->{'implicit'}) { $implicithtml = "(implicit) "; } my $comment = $elt->first_child; my $placement; if ($comment) { $placement = $comment->{'att'}->{'placement'}; } #print "$htmlref"; print "

[$htmlref] $implicithtml"; if ($placement eq "before") { print "(", $comment->text, ") "; } # use the reference to retrieve the verse content my $text = query_passage($esvref); my $esv=XML::Twig->new(twig_handlers => { p => sub { $_->set_gi('span'); }, div => sub { $_->set_gi('span'); }, br => sub { $_->delete; }}, keep_encoding => 1); $esv->parse($text); $esv->flush; # flush the end of the document if ($placement eq "after") { print " (", $comment->text, ")"; } #print "\n"; print "

\n"; } $lastref = $fullref; $twig->purge; # frees the memory } sub query_passage { my ($passage) = @_; # this is for testing only: you have to get your own key and put it here my $key = "TEST"; my $options = "action=doPassageQuery&include-verse-numbers=false&include-passage-references=false&include-footnotes=false&include-headings=false&include-subheadings=false&include-short-copyright=false"; $queries++; return get("http://www.gnpcb.org/esv/share/get/?key=$key&passage=$passage&$options"); } sub process_options { $opt_usage = 0; $opt_debug = 0; $opt_index = ""; # Parse the command line options use Getopt::Long; &GetOptions("usage|help|?", "index=s", # unadvertised "debug"); if ($opt_usage) {print usage(); exit 0;} unless (-e $opt_index) { die "Can't open $opt_index\n".usage(); } } sub usage { return "usage: index2html.pl [-index file.xml] Read an input file in 'index' format, connect to the ESV web service to download the verse content for each item, merge in additional index file content and write to stdout. "; }