Module:LDSverse explained

-- This module implements the lion's share of the newly-revised-as-of-August-2020 Template:LDS.local p =

-- TODO:-- Check the verse parameter to make sure it's a number (sometimes users -- mistakenly include the en dash and ending verse number in this -- parameter, e.g.,). If it isn't a number, -- default to citing to the chapter as a whole?-- Take out the remaining error statements and handle errors more gracefully.-- (As of September 2020, many have already been removed, so that's good.)-- Functions with error statements left in them:-- getFullBookName-- handleOD-- getStandardWork (only throws an error if pre-conditions not met)-- getLectureOrdinal (only called with pcall)

--

local bomBooks =

local dcBooks =

local lofBooks =

local pogpBooks =

local bibleBooks =

-- This table holds the titles of the Standard Works themselves, as found on WSlocal standardWorks =

local wsBaseURL = "" .. displayTextParam .. " 1" elseif decNumber

"2" then return "" .. displayTextParam .. " 2" else -- decNumber wasn't 1 or 2 (and could even be nil) if decNumber then error("No such Official Declaration: " .. decNumber, 0) else error("No official declaration number provided", 0) end endend

-- for when the user has provided either no parameters or just one (the display -- text for the wikilink)local function handleFewParams(displayTextParam) if not displayTextParam then -- no parameters provided to the template at all return wsBaseURL .. wsStandardWorks .. "|LDS Standard Works]]" end -- otherwise same link but displaying the text they provided return wsBaseURL .. wsStandardWorks .. "|" .. displayTextParam .. "]]"end

local function removePeriods(arg) if arg then return string.gsub(arg, "%.", "") else return nil endend

-- this function removes leading and trailing spaces via regex magiclocal function trimSpaces(arg) if arg then return string.match(arg, "^%s*(.-)%s*$") else return nil endend

-- local function extractParams(args):-- Extracts and returns the parameters from the table of args passed in-- Includes logic to handle the case where a user forgets to pass in bookParamlocal function extractParams(args) local displayTextParam = trimSpaces(args[1]) local bookParam = removePeriods(trimSpaces(args[2])) -- take out periods and spaces local chapterParam = trimSpaces(args[3]) local verseParam = trimSpaces(args[4]) local endVerseParam = trimSpaces(args[5]) -- the 6th and 7th parameters, for footnotes and cross-references, have no -- use on Wikisource (and don't seem to work even on CoCJ.org, but whatever) -- so they are ignored local endVerseFlag = trimSpaces(args[8]) -- handle the common mistake where someone forgets the second parameter -- altogether (e.g.,). -- This is done by testing whether the second parameter, which should be the -- name/abbrev. of the book (e.g., 1ne), is instead a number. In that case -- we will try assuming that the params should just be "shifted" over 1 -- and use the display text as the bookParam and go from there, fingers crossed. if tonumber(args[2]) then -- the second parameter is a number (tonumber returns nil if there is any text in its argument) bookParam = removePeriods(trimSpaces(args[1])) -- use the display text and hope it's the full name of the book (or a valid abbreviation)! chapterParam = trimSpaces(args[2]) verseParam = trimSpaces(args[3]) endVerseParam = trimSpaces(args[4]) endVerseFlag = trimSpaces(args[7]) end return displayTextParam, bookParam, chapterParam, verseParam, endVerseParam, endVerseFlagend

-- pre-condition: lectureNum is non-nillocal function getLectureOrdinal(lectureNum) if lectureNum

"1" then return "First" elseif lectureNum

"2" then return "Second" elseif lectureNum

"3" then return "Third" elseif lectureNum

"4" then return "Fourth" elseif lectureNum

"5" then return "Fifth" elseif lectureNum

"6" then return "Sixth" elseif lectureNum

"7" then return "Seventh" end error("Lecture on Faith number <" .. lectureNum .. "> does not exist (should be between 1 and 7)", 0)end

-- [=======[ ...oooOOOOOOOooo... ]

] ---- [===========[ MAIN FUNCTION ]

=] -- -- [=======[ ...oooOOOOOOOooo... ]

] --

-- function p.main(frame)-- This function returns a wikitext link to the cited LDS scripture on Wikisource-- (except for Official Declaration 2, which is copyrighted, so this returns a-- link to CoJC.org for that)function p.main(frame) local args = frame:getParent.args -- the args to the template that invokes this module local displayTextParam, bookParam, chapterParam, verseParam, endVerseParam, endVerseFlag = extractParams(args) local wikiText = "" if not bookParam then --fewer than two parameters provided to the template return handleFewParams(displayTextParam) end local fullBookName = getFullBookName(bookParam) local standardWork = getStandardWork(fullBookName) if fullBookName

"Official Declaration" then return handleOD(displayTextParam, chapterParam) end wikiText = wsBaseURL .. standardWork -- start of the wikilink text -- Special case for JST Matthew (safe to do since we've already determined -- which Standard Work we're linking to) if fullBookName

"JST Matthew" then fullBookName = "Matthew" end -- Special case for sections of the Doctrine and Covenants if fullBookName

"The Doctrine and Covenants" then if chapterParam then fullBookName = "Section " .. chapterParam else fullBookName = nil -- The user has not provided a chapter (section) so this is treated as wanting to cite the D&C generally end end -- Special case for Lectures on Faith if fullBookName

"Lectures on Faith" then if chapterParam then local status, ordinal = pcall(getLectureOrdinal, chapterParam) if status then fullBookName = "Lecture " .. ordinal else fullBookName = nil -- just cite to LoF generally chapterParam = chapterParam .. " (invalid)" -- so user knows there was a problem mw.log(ordinal) -- print error message to debug console (?) end else fullBookName = nil -- The user has not provided a chapter (lecture) so this is treated as wanting to cite the LoF generally end end if fullBookName then wikiText = wikiText .. "/" .. fullBookName if chapterParam then wikiText = wikiText .. buildBookmark(fullBookName, chapterParam, verseParam) end end wikiText = wikiText .. "|" wikiText = wikiText .. buildFinalDisplayText(displayTextParam, chapterParam, verseParam, endVerseParam, endVerseFlag) wikiText = wikiText .. "]]" -- DON'T FORGET!!! return wikiTextend

-- like the name says, this just counts the number of keys (aka indexes) in a tablelocal function countKeys(t) local i = 0 for k,v in pairs(t) do i = i+1 end return iend

-- returns a (collapsed by default) wikitable listing all the aliases for every book-- designed for use on the template documentation pagefunction p.getBookAliasesWikiTable local wikiTable = class="wikitable sortable mw-collapsible mw-collapsed" |+ class="nowrap" |LDS Template Book Name Aliases ! Standard Work !! Book !! Aliases ]] for sw, bookList in pairs(standardWorks) do wikiTable = wikiTable .. '|-\n|rowspan="' .. countKeys(bookList) .. '"|' .. sw .. "\n" for book, aliases in pairs(bookList) do wikiTable = wikiTable .. "| " .. book .. "\n| " .. table.concat(aliases, ", ") .. "\n|-\n" end end wikiTable = wikiTable .. "|}" return wikiTableend

return p