Jump to content

Module:Mormonverse

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Eievie (talk | contribs) at 07:44, 26 February 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

--possible book inputs
local book_aliases = {
	['D&C'] = 'Doctrine and Covenants',
	['JS–H'] = 'Joseph Smith–History',
	['JS-H'] = 'Joseph Smith–History',
	['JS–M'] = 'Joseph Smith–Matthew',
	['JS-M'] = 'Joseph Smith–Matthew',
}

--these books only have one chapter, have to be handled differently
local no_chapters = {
	['Enos'] = true,
	['Jarom'] = true,
	['Omni'] = true,
	['Words of Mormon'] = true,
	['4 Nephi'] = true,
}

--which scripture a book is in
local book_scripture = {
--	['Book of Mormon'] = {
	['bofm'] = {
		'1 Nephi',
		'2 Nephi',
		'Jacob',
		'Enos',
		'Jarom',
		'Omni',
		'Words of Mormon',
		'Mosiah',
		'Alma',
		'Helaman',
		'3 Nephi',
		'4 Nephi',
		'Mormon',
		'Ether',
		'Moroni',
	},
	['dc-testament'] = {'dc'},
--	['Pearl of Great Price'] = {
	['pgp'] = {
		'Moses',
		'Abraham',
		'Joseph Smith–Matthew',
		'Joseph Smith–History',
		'Articles of Faith',
	},
}

--sets the website to be used for each version, lds_org is the fallback so don't define any for it
local site_tbl = {
	lds = 'lds_org',
	coc = 'coc_net', rlds = 'coc_net',
}

--pattern for the url of each site using _book etc.
-- (underscore then letters [a-z]) for variables
local urlpat_tbl = {
	wikisource = 's:Bible (_version)/_book#_schap:_svers',
	lds_org = 'https://www.churchofjesuschrist.org/study/scriptures/_book/_schap?lang=eng&id=_vrange#p_svers',
	coc_net = 'http://www.communityofchrist.net/Scriptures/'
}

--changes book name to use in url for each site, only if necessary
local site_book_tbl = {
	lds_org = {
		['1_Nephi']         = 'bofm/1-ne',
        ['2_Nephi']         = 'bofm/2-ne',
        ['Jacob']           = 'bofm/jacob',
        ['Enos']            = 'bofm/enos',
        ['Jarom']           = 'bofm/jarom',
        ['Omni']            = 'bofm/omni',
        ['Words_of_Mormon'] = 'bofm/w-of-m',
        ['Mosiah']          = 'bofm/mosiah',
        ['Alma']            = 'bofm/alma',
        ['Helaman']         = 'bofm/hel',
        ['3_Nephi']         = 'bofm/3-ne',
        ['4_Nephi']         = 'bofm/4-ne',
        ['Mormon']          = 'bofm/morm',
        ['Ether']           = 'bofm/ether',
        ['Moroni']          = 'bofm/moro',

        ['Doctrine_and_Covenants']  = 'dc-testament/dc',

        ['Moses']                = 'pgp/moses',
        ['Abraham']              = 'pgp/abr',
        ['Joseph_Smith–Matthew'] = 'pgp/js-m',
        ['Joseph_Smith–History'] = 'pgp/js-h',
        ['Articles_of_Faith']    = 'pgp/a-of-f',
    }
}

local function trimArg(text)
	if type(text) == 'string' then
		text = text:match('(%S.-)%s*$')  --trimmed text or nil if empty
	end
	return text
end

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

local function titlecase(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'] = true, ['an'] = true, ['the'] = true,
		['and'] = true, ['but'] = true, ['or'] = true, ['for'] = true,
		['nor'] = true, ['on'] = true, ['in'] = true, ['at'] = true, ['to'] = true,
		['from'] = true, ['by'] = true, ['of'] = true, ['up'] = true,
	}
	local words = mw.text.split(mw.text.trim(arg or ''), '_')
	for i, s in ipairs(words) do
		s = string.lower(s)
		if i > 1 then
			if not alwayslower[s] then
				s = mw.getContentLanguage():ucfirst(s)
			end
		else
			s = mw.getContentLanguage():ucfirst(s)
		end
		words[i] = s
	end
	return table.concat(words, '_')
end

function p.main(frame)
	local targs = frame:getParent().args
	local args = {}
	for _, param in ipairs({1, 2, 3, 4, 5, 'nobook', 'wlink'}) do
		args[param] = trimArg(targs[param])
	end
	local default_version = 'lds_org'
	local input_book = ''
	local ref = ''
	local version = ''
	local text = ''
	local wikilink = ''
	local mainspace = mw.title.getCurrentTitle():inNamespaces(0)
	if args[1] == nil or args[2] == nil or tonumber(args[1]) ~= nil then
		-- first argument is a numeric prefix and second is book name
		input_book = trimArg((args[1] or '') .. ' ' .. (args[2] or '')) or ''
		wikilink = trimArg((args[1] or 'Book of') .. ' ' .. (args[2] or '')) or ''
		ref = args[3] or ''
		version = mw.ustring.lower(args[4] or default_version)
		text = args[5] or trimArg((input_book .. ' ' .. ref))
	else
		-- first argument is the whole book name
		input_book = args[1] or ''
		wikilink = 'Book of ' .. args[1]
		ref = args[2] or ''
		version = mw.ustring.lower(args[3] or default_version)
		text = args[4] or ((book_aliases[input_book] or input_book) .. ' ' .. ref)
	end
	
	if args.nobook == 'yes' then
		text = ref
	end
	if args.wlink == 'yes' then
		text = ref
		wikilink = '[[' .. wikilink .. '|' .. input_book .. ']] '
	else
		wikilink = ''	
	end
	
	text = text:gsub('-', '–')  --change hyphens to en dashes (3:2-5 → 3:2–5)

	local errors = {}

	local book = book_aliases[input_book] or input_book;
	book = book:gsub('%p', ''):gsub(' ', '_')
	book = mw.ustring.lower(book)

--	local book_found = false
--	local standard = book:gsub('_', '')
--	for full_book, aliases in pairs(book_aliases) do
--		if standard == full_book:gsub('_', '') or valueExists(aliases, standard) then
--			book = full_book
--			book_found = true
--			break
--		end
--	end

	local site = site_tbl[version] or 'lds_org'    --fall back on lds_org

	local urlpat = urlpat_tbl[site]

	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]
	e_ref = mw.text.split(e_ref, '%p')
	for i, v in ipairs(s_ref) do s_ref[i] = v:gsub('%D', '') end  --remove any non-numeric character (such as f)
	for i, v in ipairs(e_ref) do e_ref[i] = v:gsub('%D', '') end

	local e_chap, e_vers, s_chap, s_vers
	local chapter_only = not s_ref[2]
	if no_chapters[book] then
		chapter_only = false
		s_chap = 1
		s_vers = s_ref[2] or s_ref[1] or 1   --verse 3 can be specified as "3" or "1:3"
		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
		if e_ref[2] or not s_ref[2] then     --chapter-chapter or chapter(:verse)?-chapter:verse
			e_chap = e_ref[1] or s_chap
		else                                 --chapter:verse-verse
			e_chap = s_chap
		end
		e_vers = e_ref[2] or e_ref[1] or s_vers
	end

	book = titlecase(book)  --title case looks better at oremus where they display the input

	--special case handling for components of the url
	if site == 'lds_org' then
--		string.lower(book)
		book = site_book_tbl[site][book] or book
	end
--	if type(site_book_tbl[site]) then --== 'table' then
--		book = site_book_tbl[site][book] or book
--	end

	local v_range
	if e_chap == s_chap and e_vers == s_vers then
		v_range = s_vers
	else
		v_range = s_vers .. '-' .. e_vers
	end

	local url = urlpat:gsub('_%l+', {  --get the components into the url
					_book      = book,
					_schap     = s_chap,
					_svers     = s_vers,
					_echap     = e_chap,
					_evers     = e_vers,
					_vrange    = v_range,
					_version   = version,
				})

	local fulllink
	if site == 'wikisource' then  --interwikilink
		fulllink = wikilink .. '[[' .. url .. '|' .. text .. ']]'
	else
		fulllink = wikilink .. '[' .. url .. ' ' .. text .. ']'
	end

	if mainspace then
		if (not book_found) then
			table.insert(errors, '<span style="color:red">Template:Mormonverse with invalid book</span>[[Category:Pages with Mormon scripture book errors]]')
		end
	end
	return fulllink .. table.concat(errors)
end

return p