/[drupal]/contributions/modules/scripturefilter/scripturefilter.inc
ViewVC logotype

Diff of /contributions/modules/scripturefilter/scripturefilter.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.6, Mon May 21 16:40:51 2007 UTC revision 1.7, Fri Feb 13 22:40:30 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
 /* $Id$ */  
2    
3  define('DEFAULT_BIBLE_TRANSLATION','NIV');  // $Id: scripturefilter.inc,v 1.6 2007/05/21 16:40:51 smsimms Exp $
4    
5  function scripturize ($text = '',$bible = DEFAULT_BIBLE_TRANSLATION){  /*
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      // 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      // 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>';      $anchor_regex = '<a\s+href.*?<\/a>';
# Line 12  function scripturize ($text = '',$bible Line 19  function scripturize ($text = '',$bible
19      $tag_regex = '<(?:[^<>\s]*)(?:\s[^<>]*){0,1}>'; // $tag_regex='<[^>]+>';      $tag_regex = '<(?:[^<>\s]*)(?:\s[^<>]*){0,1}>'; // $tag_regex='<[^>]+>';
20      $split_regex = "/((?:$anchor_regex)|(?:$pre_regex)|(?:$code_regex)|(?:$tag_regex))/i";      $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);      $parsed_text = preg_split($split_regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
23      $linked_text = '';      $linked_text = '';
24    
25      while (list($key,$value) = each($parsed_text)) {      while (list($key, $value) = each($parsed_text)) {
26          if (preg_match($split_regex,$value)) {          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            $linked_text .= $value; // if it is an HTML element or within a link, just leave it as is
28          } else {          }
29              $linked_text .= scripturizeAddLinks($value,$bible); // if it's text, parse it for Bible references          else {
30          }            $linked_text .= scripturizeAddLinks($value, $bible); // if it's text, parse it for Bible references
31            }
32      }      }
33    
34      return $linked_text;      return $linked_text;
35  }  }
36    
37  function scripturizeAddLinks($text = '',$bible = DEFAULT_BIBLE_TRANSLATION) {  function scripturizeAddLinks($text = '', $bible = DEFAULT_BIBLE_TRANSLATION) {
38      $volume_regex = '1|2|3|I|II|III|1st|2nd|3rd|First|Second|Third';      $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';      $book_regex  = 'Genesis|Exodus|Leviticus|Numbers|Deuteronomy|Joshua|Judges|Ruth|Samuel|Kings|Chronicles|Ezra|Nehemiah|Esther';
# Line 39  function scripturizeAddLinks($text = '', Line 47  function scripturizeAddLinks($text = '',
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';      $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';      $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.')\.?';      $book_regex = '(?:'. $book_regex .')|(?:'. $abbrev_regex .')\.?';
51    
52      $verse_regex="\d{1,3}(?::\d{1,3})?(?:\s?(?:[-&,]\s?\d+))*";      $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';      $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!      // 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';      $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')";      $replacement_regex = "scripturizeLinkReference('\\0','\\1','\\2','\\3','\\4','$bible')";
61    
62      $text=preg_replace($passage_regex,$replacement_regex,$text);      $text=preg_replace($passage_regex, $replacement_regex, $text);
63    
64      return $text;      return $text;
65  }  }
66    
67  function scripturizeLinkReference($reference='',$volume='',$book='',$verse='',$translation='',$user_translation='') {  function scripturizeLinkReference($reference='', $volume='', $book='', $verse='', $translation='', $user_translation='') {
68      if ($volume) {      if ($volume) {
69         $volume = str_replace('III','3',$volume);         $volume = str_replace('III', '3', $volume);
70         $volume = str_replace('II','2',$volume);         $volume = str_replace('II', '2', $volume);
71         $volume = str_replace('I','1',$volume);         $volume = str_replace('I', '1', $volume);
72         $volume = $volume{0}; // will remove st,nd,and rd (presupposes regex is correct)         $volume = $volume{0}; // will remove st,nd,and rd (presupposes regex is correct)
73      }      }
74    
75     if(!$translation) {     if (!$translation) {
76           if (!$user_translation) {           if (!$user_translation) {
77               $translation = DEFAULT_BIBLE_TRANSLATION;               $translation = DEFAULT_BIBLE_TRANSLATION;
78           } else {           }
79             else {
80               $translation = $user_translation;               $translation = $user_translation;
81           }           }
82     } else {     }
83         $translation = trim($translation,' ()'); // strip out any parentheses that might have made it this far     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     // 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     // 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     // 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) {     if ($verse) {
91         $verse = strtok($verse,',& ');         $verse = strtok($verse, ',& ');
92     }     }
93    
94     switch ($translation) {     switch ($translation) {
# Line 88  function scripturizeLinkReference($refer Line 98  function scripturizeLinkReference($refer
98          // http://www.gnpcb.org/esv/share/services/api/ for more info          // http://www.gnpcb.org/esv/share/services/api/ for more info
99               $link = 'http://www.gnpcb.org/esv/search/?go=Go&q=';               $link = 'http://www.gnpcb.org/esv/search/?go=Go&q=';
100               $title = 'English Standard Version Bible';               $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));               $link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
102               break;               break;
103          case 'NET':          case 'NET':
104               $link = 'http://net.bible.org/passage.php?passage=';               $link = 'http://net.bible.org/passage.php?passage=';
105               $title = 'New English Translation';               $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));               $link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
107               break;               break;
108          case 'TNIV':          case 'TNIV':
109               $link = 'http://www.tniv.info/bible/passagesearch.php?passage_request=';               $link = 'http://www.tniv.info/bible/passagesearch.php?passage_request=';
110               $title = 'Today\'s New International Version';               $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));               $link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
112               break;               break;
113          default:          default:
114               $link = "http://biblegateway.com/cgi-bin/bible?language=english&version=$translation&passage=";               $link = "http://biblegateway.com/cgi-bin/bible?language=english&version=$translation&passage=";
115               $title = 'Bible Gateway';               $title = 'Bible Gateway';
116               $link = sprintf('<a href="%s%s" title="%s">%s</a>',$link,htmlentities(urlencode(trim("$volume $book $verse"))),$title,trim($reference));               $link = sprintf('<a href="%s%s" title="%s">%s</a>', $link, htmlentities(urlencode(trim("$volume $book $verse"))), $title, trim($reference));
117               break;               break;
118      }      }
119    
120      return $link;      return $link;
121  }  }
   
 ?>  

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.2