Module:Bibleverse

local p = {}

function p.main(frame) local pframe = frame:getParent args = {} for i, v in ipairs(pframe.args) do args[i] = trimArg(v) end local default_version = 'nrsv'                               --default to the nrsv if args[1] == nil or args[2] == nil or tonumber(args[1]) ~= nil then   --case where first argument is a numeric prefix and second is book name input_book = trimArg((args[1] or ) .. ' ' .. (args[2] or )) ref = args[3] or '' version = mw.ustring.lower(args[4] or default_version) text = args[5] or trimArg((input_book .. ' ' .. ref)) else input_book = args[1] or ''                                   --case where first argument is the whole book name ref = args[2] or '' version = mw.ustring.lower(args[3] or default_version) text = trimArg(args[4]) or (input_book .. ' ' .. ref) end site_tbl = {                                                   --sets the website to be used for each version, biblegateway is the fallback so don't define any for it    nrsv = 'oremus', nrsvae = 'oremus', kjv = 'wikisource', asv = 'wikisource', he = 'mechon_mamre', jps = 'mechon_mamre', niv = 'biblica', nivuk = 'biblica', amp = 'biblica', ab = 'biblica', nasb = 'biblica', msg = 'biblica', nlt = 'biblica', nirv = 'biblica', nab = 'usccb', bb = 'bible_hub', bbc = 'bible_hub', bbl = 'bible_hub', opb = 'bible_hub', esv = 'esv_bible' } local site = site_tbl[version] or 'biblegateway'                --fall back on biblegateway because it has so many versions urlpat_tbl = {                                               --pattern for the url of each site, use _book, v_range, etc. for variables oremus = 'http://bible.oremus.org/?passage=_book+v_range&version=_version', wikisource = 's:Bible (_version)/_book#s_chap:s_vers', mechon_mamre = 'http://www.mechon-mamre.org/_version_books_chap.htm#s_vers', biblica = 'http://www.biblica.com/en-us/bible/online-bible/?osis=_version:_book.v_range', usccb = 'http://www.usccb.org/bible/_book.s_chap:s_vers', biblegateway = 'http://www.biblegateway.com/passage/?search=_book%20v_range&version=_version', bible_hub = 'http://_version/_book/s_chap-s_vers.htm', esv_bible = 'http://www.esvbible.org/_book+s_chap:s_vers', } local urlpat = urlpat_tbl[site]

local std_book = mw.ustring.gsub(input_book, '%p', '') local std_book = mw.ustring.gsub(std_book, ' ', '_') local std_book = mw.ustring.lower(std_book)

book_aliases = {                                        --possible book inputs, based on Chicago Manual ['genesis'] = {'gen', 'gn'},                                  --old testament/tanakh ['exodus'] ={'exod', 'ex'}, ['leviticus'] = {'levit', 'lev'}, ['numbers'] = {'num', 'nm'}, ['deuteronomy'] = {'deut', 'dt'}, ['joshua'] = {'josh', 'jo'}, ['judges'] = {'judg', 'jgs'}, ['ruth'] = {'ru'}, ['1_samuel'] = {'1_sam', '1_sm'}, ['2_samuel'] = {'2_sam', '2_sm'}, ['1_kings'] = {'1_kgs'}, ['2_kings'] = {'2_kgs'}, ['1_chronicles'] = {'1_chron', '1_chr'}, ['2_chronicles'] = {'2_chron', '2_chr'}, ['ezra'] = {'ezr'}, ['nehemiah'] = {'neh'}, ['esther'] = {'est'}, ['job'] = {'jb'}, ['psalms'] = {'ps', 'pss', 'psalm'}, ['proverbs'] = {'prov', 'prv'}, ['ecclesiastes'] = {'eccles', 'eccl', 'qoheleth'}, ['song_of_solomon'] = {'song_of_sol', 'song_of_songs', 'song', 'sg', 'canticles', 'canticle_of_canticles'}, ['isaiah'] = {'isa', 'is'}, ['jeremiah'] = {'jer'}, ['lamentations'] = {'lam'}, ['ezekiel'] = {'ezek', 'ez'}, ['daniel'] = {'dan', 'dn'}, ['hosea'] = {'hos'}, ['joel'] = {'jl'}, ['amos'] = {'am'}, ['obadiah'] = {'obad', 'ob'}, ['jonah'] = {'jon'}, ['micah'] = {'mic'}, ['nahum'] = {'nah', 'na'}, ['habakkuk'] = {'hab', 'hb'}, ['zephaniah'] = {'zeph', 'zep'}, ['haggai'] = {'hag', 'hg'}, ['zechariah'] = {'zech', 'zec'}, ['malachi'] = {'mal'}, ['baruch'] = {'bar'},                                           --apocrypha/deuterocanon ['ecclesiasticus'] = {'ecclus', 'wisdom of sirach', 'sirach', 'sir'}, ['1_esdras'] = {'1_esd'}, ['1_esdras'] = {'1_esd'}, ['judith'] = {'jth', 'jdt'}, ['1_maccabees'] = {'1_macc', '1_mc'}, ['2_maccabees'] = {'2_macc', '2_mc'}, ['3_maccabees'] = {'3_macc', '3_mc'}, ['4_maccabees'] = {'4_macc', '4_mc'}, ['prayer_of_manasseh'] = {'pr_of_man', 'prayer_of_manasses'}, ['susanna'] = {'sus'}, ['tobit'] = {'tob', 'tb'}, ['wisdom_of_solomon'] = {'ws', 'wisdom', 'wis_of_sol'}, ['prayer_of_azariah'] = {'prayer_of_azariah_and_hymn_of_the_three_children'}, ['rest_of_esther'] = {'additions_to_esther'}, ['bel_and_the_dragon'] = {'bel'}, ['epistle_of_jeremiah'] = {'letter_of_jeremiah'}, ['matthew'] = {'matt', 'mt'},                                       --new testament ['mark'] = {'mk'}, ['luke'] = {'lk'}, ['john'] = {'jn'}, ['acts'] = {'acts_of_the_apostles'}, ['romans'] = {'rom'}, ['1_corinthians'] = {'1_cor'}, ['2_corinthians'] = {'2_cor'}, ['galatians'] = {'gal'}, ['ephesians'] = {'eph'}, ['philippians'] = {'phil'}, ['colossians'] = {'col'}, ['1_thessalonians'] = {'1_thess', '1_thes'}, ['2_thessalonians'] = {'2_thess', '2_thes'}, ['1_timothy'] = {'1_tim', '1_tm'}, ['2_timothy'] = {'2_tim', '2_tm'}, ['titus'] = {'ti'}, ['philemon'] = {'philem', 'phlm'}, ['hebrews'] = {'heb'}, ['james'] = {'jas'}, ['1_peter'] = {'1_pet', '1_pt'}, ['2_peter'] = {'2_pet', '2_pt'}, ['1_john'] = {'1_jn'}, ['2_john'] = {'2_jn'}, ['3_john'] = {'3_jn'}, ['jude'] = {'jude'}, ['revelation'] = {'rev', 'apocalypse', 'apoc', 'rv'} }   for full_books, alias_tbls in pairs(book_aliases) do      if std_book == full_books then											--is the book already standardized? book = std_book elseif valueExists(book_aliases[full_books], std_book) then            --check to see if it exists as a value in each table book = full_books else book = std_book                                             --if not in the table, use the input book error_cat = 'Pages with Bible book errors'                  --but add a tracking category so i can check on it      end end no_chaps = {'obadiah', 'philemon', '2_john', '3_john', 'jude'}--these books only have one chapter, have to be handled differently

local split_ref = mw.text.split(ref, '[-–—]')                   --split the ref into the part before and after the dash/hyphen local s_ref = mw.text.split(split_ref[1], '%p')               --any punctuation can be used to separate chapter from verse local e_ref = split_ref[2] or split_ref[1] local e_ref = mw.text.split(e_ref, '%p') for i, v in ipairs(s_ref) do s_ref[i] = trimArg(v) end for i, v in ipairs(e_ref) do e_ref[i] = trimArg(v) end if valueExists(no_chaps, book) then s_chap = 1 s_vers = s_ref[1] or s_ref[2] or 1 e_chap = 1 e_vers = e_ref[2] or e_ref[1] or 1 else s_chap = s_ref[1] or 1 s_vers = s_ref[2] or 1 e_chap = e_ref[1] or s_chap e_vers = e_ref[2] or s_vers end

site_book_tbl = {                                               --changes book name to use in url for each site, only if necessary mechon_mamre = { ['genesis'] = '01', ['exodus'] = '02', ['leviticus'] = '03', ['numbers'] = '04', ['deuteronomy'] = '05', ['joshua'] = '06', ['judges'] = '07', ['1_samuel'] = '08a', ['2_samuel'] = '08b', ['1_kings'] = '09a', ['2_kings'] = '09b', ['isaiah'] = '10', ['jeremiah'] = '11', ['ezekiel'] = '12', ['hosea'] = '13', ['joel'] = '14', ['amos'] = '15', ['obadiah'] = '16', ['jonah'] = '17', ['micah'] = '18', ['nahum'] = '19', ['habakkuk'] = '20', ['zephaniah'] = '21', ['haggai'] = '22', ['zechariah'] = '23', ['malachi'] = '24', ['1_chronicles'] = '25a', ['2_chronicles'] = '25b', ['psalms'] = '26', ['job'] = '27', ['proverbs'] = '28', ['ruth'] = '29', ['song_of_solomon'] = '30', ['ecclesiastes'] = '31', ['lamentations'] = '32', ['esther'] = '33', ['daniel'] = '34', ['ezra'] = '35a', ['nehemiah'] = '35b' } }  if type(site_book_tbl[site]) == 'table' then book = site_book_tbl[site][book] or book end

local book = titlecase(book)                                   --put book in title case, only because it looks better at oremus where they display the input

if site == 'mechon_mamre' then                               --special case handling of aspects of the components of the url s_chap = string.format( '%x', s_chap/10 ) .. string.sub(string.format('%02d', s_chap), -1) elseif (site == 'oremus') or (site == 'esv_bible') then book = mw.ustring.gsub(book, '_', '+') elseif site == 'bible_hub' then book = string.lower(book) end site_version_tbl = {                                           --changes to the version name to be used in urls, only if necessary mechon_mamre = { he = 'p/pt/pt', jps = 'e/et/et' },   wikisource = { kjv = 'King James', asv = 'American Standard' },   bible_hub = { bb = 'biblebrowser.com', bbc = 'biblecommenter.com', bbl = 'scripturetext.com', opb = 'bible.cc'} }

if type(site_version_tbl[site]) == 'table' then version = site_version_tbl[site][version] end if s_ref[2] == nil then                                       --case of chapter range only v_range = s_chap ..'–' .. e_chap else v_range = s_chap ..':' .. s_vers ..'–' .. e_chap ..':' .. e_vers   --case of chapter and verse range end local url = mw.ustring.gsub(urlpat, '_book', book)           --get the components into the urls local url = mw.ustring.gsub(url, 's_chap', s_chap) local url = mw.ustring.gsub(url, 's_vers', s_vers) local url = mw.ustring.gsub(url, 'e_chap', e_chap) local url = mw.ustring.gsub(url, 'e_vers', e_vers) local url = mw.ustring.gsub(url, 'v_range', v_range) local url = mw.ustring.gsub(url, '_version', version) if site == 'wikisource' then                                   --interwikilink fulllink =  .. text ..  else fulllink = '[' .. url .. ' ' .. text .. ']' end if error_cat == nil then return fulllink else return fulllink .. ' ' end end

function trimArg(arg) if arg == '' or arg == ' ' or arg == nil then return nil else return mw.text.trim(arg) end end

function valueExists(tbl, value) for k,v in pairs(tbl) do   if value == v then return true end end return false end

titlecase = function ( arg ) -- http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html -- recommended by The U.S. Government Printing Office Style Manual: -- 'Capitalize all words in titles of publications and documents, -- except a, an, the, at, by, for, in, of, on, to, up, and, as, but, or, and nor.' local alwayslower = {['a'] = 1, ['an'] = 1, ['the'] = 1, ['and'] = 1, ['but'] = 1, ['or'] = 1, ['for'] = 1, ['nor'] = 1, ['on'] = 1, ['in'] = 1, ['at'] = 1, ['to'] = 1, ['from'] = 1, ['by'] = 1, ['of'] = 1, ['up'] = 1 } local res = '' local s = mw.text.trim(arg or '') local words = mw.text.split( s, '_') for i, s in ipairs(words) do       s = string.lower(s) if(i > 1) then if(alwayslower[s] ~= 1) then s = mw.getContentLanguage:ucfirst(s) end else s = mw.getContentLanguage:ucfirst(s) end words[i] = s   end return table.concat(words, '_') end

return p