Module:BibleQuote: Difference between revisions

From BibleVerseWiki
No edit summary
No edit summary
Line 6: Line 6:


local function removePilcrow(verse)
local function removePilcrow(verse)
     return (string.gsub(verse, "^<span class=\"pilcrow\">¶</span>[%W]-%w", "P"))
     return (string.gsub(verse, "^<span class=\"pilcrow\">¶</span>%W-%w", "P"))
end
end



Revision as of 02:28, 12 December 2023

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

local p = {} --p stands for package

local function verseNamespace()
    return "Template"
end

local function removePilcrow(verse)
    return (string.gsub(verse, "^<span class=\"pilcrow\">¶</span>%W-%w", "P"))
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 expandTemplate(title, ...)
--     return mw.getCurrentFrame():expandTemplate { title = title };
-- end

local function preProcess(text)
    return mw.getCurrentFrame():preprocess(text);
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 function renderVerses(verseSpec, showVerseNumber, showPilcrow, singleSpace, doubleSpace, indent)
    local _, _, book, chapter, startVerse, stopVerse =
        string.find(verseSpec, "%s-(.+)%s+(%d+):(%d+)-(%d+)")
    local verses = {}
    local function renderVerse(verseName, verseNumber)
        local wikiText = mw.title.new(verseName, verseNamespace()):getContent()
        local function addVerseNumber(wikiText)
            return (string.gsub(wikiText, "^", "<sup>'''" .. verseNumber .. "'''</sup> "))
        end
        local process = {}
        if not showPilcrow then table.insert(process, removePilcrow) end
        if showVerseNumber 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
        if doubleSpace then
            table.insert(process, addDoubleSpace)
        elseif singleSpace then
            table.insert(process, addSingleSpace)
        end
        if #process > 0 then return compose(unpack(process))("") end
        return " "
    end
    if stopVerse == nil then return renderVerse(verseSpec, startVerse) end
    for i = startVerse, stopVerse, 1 do
        table.insert(verses, renderVerse(book .. " " .. chapter .. ":" .. i, i))
    end
    if indent then return addIndent(table.concat(verses, renderConcat())) end
    return table.concat(verses, renderConcat())
end

local function addChapterNumber(verse, number)
    return (string.gsub(verse, "^", ""))
end

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

return p