| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: scripturefilter.inc,v 1.6 2007/05/21 16:40:51 smsimms Exp $
|
| 4 |
|
| 5 |
/*
|
| 6 |
* @file
|
| 7 |
* The scripturize functions that do the actual work of finding
|
| 8 |
* scripture references and turning them into links.
|
| 9 |
*/
|
| 10 |
|
| 11 |
define('DEFAULT_BIBLE_TRANSLATION', 'NIV');
|
| 12 |
|
| 13 |
function scripturize($text = '', $bible = DEFAULT_BIBLE_TRANSLATION) {
|
| 14 |
// skip everything within a hyperlink, a <pre> block, a <code> block, or a tag
|
| 15 |
// we skip tags because something like <img src="blah" alt="John 3:16"> should not be messed with
|
| 16 |
$anchor_regex = '<a\s+href.*?<\/a>';
|
| 17 |
$pre_regex = '<pre>.*<\/pre>';
|
| 18 |
$code_regex = '<code>.*<\/code>';
|
| 19 |
$tag_regex = '<(?:[^<>\s]*)(?:\s[^<>]*){0,1}>'; // $tag_regex='<[^>]+>';
|
| 20 |
$split_regex = "/((?:$anchor_regex)|(?:$pre_regex)|(?:$code_regex)|(?:$tag_regex))/i";
|
| 21 |
|
| 22 |
$parsed_text = preg_split($split_regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
|
| 23 |
$linked_text = '';
|
| 24 |
|
| 25 |
while (list($key, $value) = each($parsed_text)) {
|
| 26 |
if (preg_match($split_regex, $value)) {
|
| 27 |
$linked_text .= $value; // if it is an HTML element or within a link, just leave it as is
|
| 28 |
}
|
| 29 |
else {
|
| 30 |
$linked_text .= scripturizeAddLinks($value, $bible); // if it's text, parse it for Bible references
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
return $linked_text;
|
| 35 |
}
|
| 36 |
|
| 37 |
function scripturizeAddLinks($text = '', $bible = DEFAULT_BIBLE_TRANSLATION) {
|
| 38 |
$volume_regex = '1|2|3|I|II|III|1st|2nd|3rd|First|Second|Third';
|
| 39 |
|
| 40 |
$book_regex = 'Genesis|Exodus|Leviticus|Numbers|Deuteronomy|Joshua|Judges|Ruth|Samuel|Kings|Chronicles|Ezra|Nehemiah|Esther';
|
| 41 |
$book_regex .= '|Job|Psalms?|Proverbs?|Ecclesiastes|Songs? of Solomon|Song of Songs|Isaiah|Jeremiah|Lamentations|Ezekiel|Daniel|Hosea|Joel|Amos|Obadiah|Jonah|Micah|Nahum|Habakkuk|Zephaniah|Haggai|Zechariah|Malachi';
|
| 42 |
$book_regex .= '|Mat+hew|Mark|Luke|John|Acts?|Acts of the Apostles|Romans|Corinthians|Galatians|Ephesians|Phil+ippians|Colossians|Thessalonians|Timothy|Titus|Philemon|Hebrews|James|Peter|Jude|Revelations?';
|
| 43 |
|
| 44 |
// split these up from the Perl code because I want to be able to have an optional period at the end of just the abbreviations
|
| 45 |
|
| 46 |
$abbrev_regex = 'Gen|Ex|Exo|Lev|Num|Nmb|Deut?|Josh?|Judg?|Jdg|Rut|Sam|Ki?n|Chr(?:on?)?|Ezr|Neh|Est';
|
| 47 |
$abbrev_regex .= '|Jb|Psa?|Pr(?:ov?)?|Eccl?|Song?|Isa|Jer|Lam|Eze|Dan|Hos|Joe|Amo|Oba|Jon|Mic|Nah|Hab|Zeph?|Hag|Zech?|Mal';
|
| 48 |
$abbrev_regex .= '|Mat|Mr?k|Lu?k|Jh?n|Jo|Act|Rom|Cor|Gal|Eph|Col|Phi|The?|Thess?|Tim|Tit|Phile|Heb|Ja?m|Pe?t|Ju?d|Rev';
|
| 49 |
|
| 50 |
$book_regex = '(?:'. $book_regex .')|(?:'. $abbrev_regex .')\.?';
|
| 51 |
|
| 52 |
$verse_regex = "\d{1,3}(?::\d{1,3})?(?:\s?(?:[-&,]\s?\d+))*";
|
| 53 |
|
| 54 |
$translation_regex = 'NIV|NASB|AMP|NLT|KJV|ESV|CEV|NET|NKJV|KJ21|ASV|WE|YLT|DARBY|WYC|NIV-UK|TNIV|MSG|NIRV';
|
| 55 |
|
| 56 |
// note that this will be executed as PHP code after substitution thanks to the /e at the end!
|
| 57 |
|
| 58 |
$passage_regex = '/(?:('. $volume_regex .')\s)?('. $book_regex .')\s('. $verse_regex .')(?:\s?[,-]?\s?((?:'. $translation_regex .')|\s?\((?:'. $translation_regex .')\)))?/e';
|
| 59 |
|
| 60 |
$replacement_regex = "scripturizeLinkReference('\\0','\\1','\\2','\\3','\\4','$bible')";
|
| 61 |
|
| 62 |
$text=preg_replace($passage_regex, $replacement_regex, $text);
|
| 63 |
|
| 64 |
return $text;
|
| 65 |
}
|
| 66 |
|
| 67 |
function scripturizeLinkReference($reference='', $volume='', $book='', $verse='', $translation='', $user_translation='') {
|
| 68 |
if ($volume) {
|
| 69 |
$volume = str_replace('III', '3', $volume);
|
| 70 |
$volume = str_replace('II', '2', $volume);
|
| 71 |
$volume = str_replace('I', '1', $volume);
|
| 72 |
$volume = $volume{0}; // will remove st,nd,and rd (presupposes regex is correct)
|
| 73 |
}
|
| 74 |
|
| 75 |
if (!$translation) {
|
| 76 |
if (!$user_translation) {
|
| 77 |
$translation = DEFAULT_BIBLE_TRANSLATION;
|
| 78 |
}
|
| 79 |
else {
|
| 80 |
$translation = $user_translation;
|
| 81 |
}
|
| 82 |
}
|
| 83 |
else {
|
| 84 |
$translation = trim($translation, ' ()'); // strip out any parentheses that might have made it this far
|
| 85 |
}
|
| 86 |
|
| 87 |
// if necessary, just choose part of the verse reference to pass to the web interfaces
|
| 88 |
// they wouldn't know what to do with John 5:1-2, 5, 10-13 so I just give them John 5:1-2
|
| 89 |
// this doesn't work quite right with something like 1:5,6 - it gets chopped to 1:5 instead of converted to 1:5-6
|
| 90 |
if ($verse) {
|
| 91 |
$verse = strtok($verse, ',& ');
|
| 92 |
}
|
| 93 |
|
| 94 |
switch ($translation) {
|
| 95 |
case 'ESV':
|
| 96 |
// note: the ESV could actually support a mouseover reference
|
| 97 |
// we could pull it directly from their site and include it in the $title text
|
| 98 |
// http://www.gnpcb.org/esv/share/services/api/ for more info
|
| 99 |
$link = 'http://www.gnpcb.org/esv/search/?go=Go&q=';
|
| 100 |
$title = 'English Standard Version Bible';
|
| 101 |
$link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
|
| 102 |
break;
|
| 103 |
case 'NET':
|
| 104 |
$link = 'http://net.bible.org/passage.php?passage=';
|
| 105 |
$title = 'New English Translation';
|
| 106 |
$link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
|
| 107 |
break;
|
| 108 |
case 'TNIV':
|
| 109 |
$link = 'http://www.tniv.info/bible/passagesearch.php?passage_request=';
|
| 110 |
$title = 'Today\'s New International Version';
|
| 111 |
$link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
|
| 112 |
break;
|
| 113 |
default:
|
| 114 |
$link = "http://biblegateway.com/cgi-bin/bible?language=english&version=$translation&passage=";
|
| 115 |
$title = 'Bible Gateway';
|
| 116 |
$link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
|
| 117 |
break;
|
| 118 |
}
|
| 119 |
|
| 120 |
return $link;
|
| 121 |
}
|