Module:BibleQuote

From BibleVerseWiki
Revision as of 00:40, 4 January 2024 by Kelvin (talk | contribs)

Documentation for this module may be created at Module:BibleQuote/doc

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs

local function verseNamespace()
    return "VerseTemplate"
end

local function removePilcrow(verse)
    return (string.gsub(verse, "^<span class=\"pilcrow\">¶</span>%s*", ""))
end

local function addSingleSpace(verse)
    return (string.gsub(verse, "^", "<br>"))
end

local function addDoubleSpace(verse)
    return (string.gsub(verse, "^", "<br><br>"))
end

local function addIndent(verse)
    return (string.gsub(verse, "^", "&nbsp;&nbsp;&nbsp;&nbsp;"))
end

local function compose(...)
    local chain = { ... }
    local function recurse(i, ...)
        if i == #chain then return chain[i](...) end
        return recurse(i + 1, chain[i](...))
    end
    return function(...) return recurse(1, ...) end
end

local anchorNames = {
    ["Genesis"] = "a",
    ["Exodus"] = "b",
    ["Leviticus"] = "c",
    ["Numbers"] = "d",
    ["Deuteronomy"] = "e",
    ["Joshua"] = "f",
    ["Judges"] = "g",
    ["Ruth"] = "h",
    ["1 Samuel"] = "i",
    ["2 Samuel"] = "j",
    ["1 Kings"] = "k",
    ["2 Kings"] = "l",
    ["1 Chronicles"] = "m",
    ["2 Chronicles"] = "n",
    ["Ezra"] = "o",
    ["Nehemiah"] = "P",
    ["Esther"] = "q",
    ["Job"] = "r",
    ["Psalm"] = "s",
    ["Proverbs"] = "t",
    ["Ecclesiastes"] = "u",
    ["Song of Solomon"] = "v",
    ["Isaiah"] = "w",
    ["Jeremiah"] = "x",
    ["Lamentations"] = "y",
    ["Ezekiel"] = "z",
    ["Daniel"] = "A",
    ["Hosea"] = "B",
    ["Joel"] = "C",
    ["Amos"] = "D",
    ["Obadiah"] = "E",
    ["Jonah"] = "F",
    ["Micah"] = "G",
    ["Nahum"] = "H",
    ["Habakkuk"] = "I",
    ["Zephaniah"] = "J",
    ["Haggai"] = "K",
    ["Zechariah"] = "L",
    ["Malachi"] = "M",
    ["Matthew"] = "N",
    ["Mark"] = "O",
    ["Luke"] = "P",
    ["John"] = "Q",
    ["Acts"] = "R",
    ["Romans"] = "S",
    ["1 Corinthians"] = "T",
    ["2 Corinthians"] = "U",
    ["Galatians"] = "V",
    ["Ephesians"] = "W",
    ["Philippians"] = "X",
    ["Colossians"] = "Y",
    ["1 Thessalonians"] = "Z",
    ["2 Thessalonians"] = "1",
    ["1 Timothy"] = "2",
    ["2 Timothy"] = "3",
    ["Titus"] = "4",
    ["Philemon"] = "5",
    ["Hebrews"] = "6",
    ["James"] = "7",
    ["1 Peter"] = "8",
    ["2 Peter"] = "9",
    ["1 John"] = "10",
    ["2 John"] = "11",
    ["3 John"] = "12",
    ["Jude"] = "13",
    ["Revelation"] = "0"
}

local function renderVerses(verseSpec, showVerseNumber, showPilcrow, singleSpace, doubleSpace, indent)
    local book, chapter, startVerse, stopVerse
    if string.find(verseSpec, "-") then
        _, _, book, chapter, startVerse, stopVerse =
            string.find(verseSpec, "%s-(.+)%s+(%d+):(%d+)-(%d+)")
    else
        _, _, book, chapter, startVerse =
            string.find(verseSpec, "%s-(.+)%s+(%d+):(%d+)")
    end
    local function contextLink()
        return anchorNames[book] .. "-" .. chapter .. "-" .. startVerse
    end
    local function blockquote(quote, verse, contextLink)
        return "<blockquote>" .. quote .. "<div style=\"text-align: right\">" ..
            "<span class=\"navigation-not-searchable\">'''[[Bible#" ..
            contextLink .. "|" .. string.gsub(verse, "%s", "&nbsp;") ..
            "]]'''</span></div></blockquote>"
    end
    local function renderVerse(verseName, verseNumber)
        local wikiText = mw.title.new(verseName, verseNamespace()):getContent()
        if not wikiText then error("Invalid Verse Number: " .. verseSpec) end
        local function addVerseNumber(wikiText)
            if verseNumber == 1 then
                return (string.gsub(wikiText, "^", "<big>'''" .. chapter .. "'''</big> "))
            end
            return (string.gsub(wikiText, "^", "<sup>'''" .. verseNumber .. "'''</sup> "))
        end
        local process = {}
        if not showPilcrow then table.insert(process, removePilcrow) end
        if stopVerse then table.insert(process, addVerseNumber) end
        if #process > 0 then return compose(unpack(process))(wikiText) end
        return wikiText
    end
    local function renderConcat()
        local process = {}
        if indent then table.insert(process, addIndent) end
        table.insert(process, addDoubleSpace)
        if #process > 0 then return compose(unpack(process))("") end
        return " "
    end
    local function renderQuote(verses)
        if indent then return addIndent(table.concat(verses, renderConcat())) end
        return table.concat(verses, renderConcat())
    end
    local verses = {}
    for i = startVerse, (stopVerse and stopVerse or startVerse) do
        table.insert(verses, renderVerse(book .. " " .. chapter .. ":" .. i, i))
    end
    return blockquote(renderQuote(verses), verseSpec, contextLink())
end

function p.standard(frame)
    local args = getArgs(frame)
    local stringToBoolean = { ["true"] = true, ["false"] = false }
    return renderVerses(
        args[1]
        , stringToBoolean[args.showVerseNumber]
        , stringToBoolean[args.showPilcrow]
        , stringToBoolean[args.singleSpace]
        , stringToBoolean[args.doubleSpace]
        , stringToBoolean[args.indent]
    )
end

return p