Module:Bibleverse

local p = {}

function p.main(frame) local defver   = 'nrsv' local version = mw.ustring.lower(trimArg(frame.args[3])) or defver site_tbl = { nrsv = 'oremus', kjv = 'wikisource', he = 'mechon_mamre', jps = 'mechon_mamre' } local site = site_tbl[version]

site_version_tbl = { mechon_mamre = { he = 'p/pt/pt', jps = 'e/et/et' },   wikisource = { kjv = 'King James', asv = 'American Standard' } }

if type(site_version_tbl[site]) == 'table' then local version = site_version_tbl[site][version] end urlpat_tbl = { oremus = 'http://bible.oremus.org/?passage=_book+s_chap:s_vers-e_chap:e_vers&version=_version', wikisource = 's:Bible (_version)/_book#s_chap:s_vers', mechon_mamre = 'http://www.mechon-mamre.org/_version_books_chap.htm#s_vers' }

local urlpat = urlpat_tbl[site]

local input_book = trimArg( frame.args[1] ) local std_book = mw.ustring.lower(mw.ustring.gsub(input_book, ' ', '_')) local book_aliases = { ['gen'] = 'genesis', ['exod'] = 'exodus', ['lev'] = 'leviticus', ['num'] = 'numbers', ['deut'] = 'deuteronomy', ['josh'] = 'joshua', ['judg'] = 'judges', ['ruth'] = 'ruth', ['1_sam'] = '1_samuel', ['2_sam'] = '2_samuel', ['1_kgs'] = '1_kings', ['2_kgs'] = '2_kings', ['1_chron'] = '1_chronicles', ['2_chron'] = '2_chronicles', ['ezra'] = 'ezra', ['neh'] = 'nehemiah', ['esther'] = 'esther', ['job'] = 'job', ['ps'] = 'psalms', ['prov'] = 'proverbs', ['eccles'] = 'ecclesiastes', ['song_of_sol'] = 'song_of_solomon', ['song_of_songs'] = 'song_of_solomon', ['isa'] = 'isaiah', ['jer'] = 'jeremiah', ['lam'] = 'lamentations', ['ezek'] = 'ezekiel', ['dan'] = 'daniel', ['hosea'] = 'hosea', ['joel'] = 'joel', ['amos'] = 'amos', ['obad'] = 'obadiah', ['jon'] = 'jonah', ['mic'] = 'micah', ['nah'] = 'nahum', ['hab'] = 'habakkuk', ['zeph'] = 'zephaniah', ['hag'] = 'haggai', ['zech'] = 'zechariah', ['mal'] = 'malachi', ['bar'] = 'baruch', ['ecclus'] = 'ecclesiasticus',	['ecclus'] = 'ecclesiasticus', ['wisdom of sirach'] = 'ecclesiasticus', ['sirach'] = 'ecclesiasticus', ['sir'] = 'ecclesiasticus', ['1esd'] = '1_esdras', ['2esd'] = '2_esdras', ['jth'] = 'judith', ['1macc'] = '1_maccabees', ['2macc'] = '2_maccabees', ['pr_of_man'] = 'prayer_of_manasseh', 	['prayer_of_manasses'] = 'prayer_of_manasseh', ['sus'] = 'susanna', ['tob'] = 'tobit', ['ws'] = 'wisdom_of_solomon', ['wisdom'] = 'wisdom_of_solomon', ['prayer_of_azariah_and_hymn_of_the_three_children'] = 'prayer_of_azariah', ['matt'] = 'matthew', ['mark'] = 'mark', ['luke'] = 'luke', ['john'] = 'john', ['acts_of_the_apostles'] = 'acts', ['rom'] = 'romans', ['1_cor'] = '1_corinthians', ['2_corinthians'] = '2_corinthians', ['gal'] = 'galatians', ['eph'] = 'ephesians', ['phil'] = 'philippians', ['col'] = 'colossians', ['1thess'] = '1_thessalonians', ['2thess'] = '2_thessalonians', ['1tim'] = '1_timothy', ['2tim'] = '2_timothy', ['titus'] = 'titus', ['philem'] = 'philemon', ['heb'] = 'hebrews', ['james'] = 'james', ['1_pet'] = '1_peter', ['2_pet'] = '2_peter', ['1_john'] = '1_john', ['2_john'] = '1_john', ['3_john'] = '1_john', ['jude'] = 'jude', ['rev'] = 'revelation',	['apocalypse'] = 'revelation', }	if valueExists(book_aliases, std_book) then book = std_book elseif book_aliases[std_book] ~= nil then book = book_aliases[std_book] else book = std_book error_cat = 'unknown book' end site_book_tbl = { wikisource = { ['prayer_of_azariah'] = 'Prayer_of_Azarias_and_Hymn_of_the_Three_Children' },       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 local book = site_book_tbl[site][book] or book end local book = titlecase(book)

local ref = trimArg(frame.args[2]) local split_ref   = mw.text.split(ref, '[-–—]') local s_ref       = mw.text.split(split_ref[1], '%p') local s_chap      = s_ref[1] local s_vers      = s_ref[2] local e_ref       = split_ref[2] or split_ref[1] local e_ref       = mw.text.split(e_ref, '%p') local e_chap       = e_ref[1] local e_vers       = e_ref[2] if site == 'mechon_mamre' then s_chap = string.format( '%x', s_chap/10 ) .. string.sub(string.format('%02d', s_chap), -1) end local url = mw.ustring.gsub(urlpat, '_book', book) 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, '_version', version) local text = trimArg(frame.args[4]) or input_book .. ' ' .. ref if site == 'wikisource' then fulllink =  .. text ..  else fulllink = '[' .. url .. ' ' .. text .. ']' end if error_cat == nil then return fulllink else return fulllink .. ' Category:' .. error_cat .. '' 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