Retreiving ESV Passages From References

Introduction

I often find that i want to to reference a passage of Scripture in my blog. Though i sometimes use just a verse reference for brevity (e.g. "see Col 3:16"), i prefer to include the actual text so the reader doesn't have to go somewhere else (and risk never coming back!). It's not so hard to go find the text, copy the content, paste it in, and then format it appropriately ... but why tolerate this when the ESV web service and some Perl can make it completely trivial (Perl programmers call this Laziness as a Virtue)?

The basic approach is to look up verse references, parse the resulting text, do some simple reformatting, and then print it out. From there i grab it and paste it into whatever interface i'm using for blogging (usually, the Radio Userland outliner). The ESV web service produces a rich XML format with perhaps a little more than what you'd normally want for these purposes. I set some query parameters to leave most of it out, and then do some simple tree surgery on the results with the XML::Twig module.

I/O Format

You run this program from a DOS prompt. The verse references are provided on the command line, and you can string them together with commas, like so:

perl ref2text.pl -ref Col.3.16,Acts.20.31

Note the periods between the book name and chapter: this prevents the simple-minded command line parsing from breaking them up, and also happens to conform to the OSIS verse reference style that i prefer anyway.

As queried in this code, the text comes back looking like this:

<div class="esv"><div class="esv-text"><p>Let the word of Christ dwell in you richly, teaching and admonishing one another in all wisdom, singing psalms and hymns and spiritual songs, with thankfulness in your hearts to God.</p></div></div>

I replace it with this format which is what i generally use for blogging.

<blockquote class="esv">"<span class="esv-text"><span>Let the word of Christ dwell in you richly, teaching and admonishing one another in all wisdom, singing psalms and hymns and spiritual songs, with thankfulness in your hearts to God.</span></span>" (<a href="http://www.gnpcb.org/esv/search/?q=Col.3.16">Col.3.16</a>)</blockquote>

Using a blockquote and quotes helps make the Word stand out. I append a hyperlink to the ESV site, both to make it easy to look at the context, and also as a courtesy to them for making this possible. Here's how it looks as rendered:

"Let the word of Christ dwell in you richly, teaching and admonishing one another in all wisdom, singing psalms and hymns and spiritual songs, with thankfulness in your hearts to God." (Col.3.16)

Details

The code can be downloaded here. You should rename it from ref2text.txt to ref2text.pl. To run this hack, you'll need a copy of Perl (for which ActiveState is by far the best source), and the inscrutable yet irreplacable XML::Twig module. To modify the output format to suit your own purposes, look at the twig_handlers. If you were working in a different environment, you might find it easier to do this via XSLT.

For querying the ESV web service, you need an access key, which the ESV folks make freely available, but you have to ask. Using the key here ("TEST" ) will produce slightly garbled text, which is a nice reminder to get your own key!

A more complex application of this general approach is described here.

Credits

None of this would be possible if the folks at Good News Publishers hadn't seen fit to make their ESV translation available as a web service, which most other contemporary Bible publishers are unwilling to do. They deserve your appreciation and support.