Module:BibleQuote: Difference between revisions

From BibleVerseWiki
No edit summary
No edit summary
Line 9: Line 9:
end
end


local function expandTemplate(title, ...)
local function addSingleSpace(verse)
     return mw.getCurrentFrame():expandTemplate { title = title };
     return (string.gsub(verse, "^", "<br>"))
end
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)
local function preProcess(text)
Line 30: Line 42:
         string.find(verseSpec, "%s-(.+)%s+(%d+):(%d+)-(%d+)")
         string.find(verseSpec, "%s-(.+)%s+(%d+):(%d+)-(%d+)")
     local verses = {}
     local verses = {}
     local function renderVerse(verseName, verseNumber)
     local function renderVerse(verseName, verseNumber, singleSpace, doubleSpace, indent)
         local wikiText = mw.title.new(verseName, verseNamespace()):getContent()
         local wikiText = mw.title.new(verseName, verseNamespace()):getContent()
         local function addVerseNumber(wikiText)
         local function addVerseNumber(wikiText)
Line 38: Line 50:
         if not showPilcrow then table.insert(process, removePilcrow) end
         if not showPilcrow then table.insert(process, removePilcrow) end
         if showVerseNumber then table.insert(process, addVerseNumber) end
         if showVerseNumber then table.insert(process, addVerseNumber) end
        if doubleSpace then
            table.insert(process, addDoubleSpace)
        elseif singleSpace then
            table.insert(process, addSingleSpace)
        end
        if indent then table.insert(process, addIndent) end
         if #process > 0 then return compose(unpack(process))(wikiText) end
         if #process > 0 then return compose(unpack(process))(wikiText) end
         return preProcess(wikiText)
         return preProcess(wikiText)
Line 58: Line 76:
         , stringToBoolean[frame.args.showVerseNumber]
         , stringToBoolean[frame.args.showVerseNumber]
         , stringToBoolean[frame.args.showPilcrow]
         , stringToBoolean[frame.args.showPilcrow]
        , stringToBoolean[frame.args.singleSpace]
        , stringToBoolean[frame.args.doubleSpace]
        , stringToBoolean[frame.args.indent]
     )
     )
end
end


return p
return p

Revision as of 01:50, 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>%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 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)
    local _, _, book, chapter, startVerse, stopVerse =
        string.find(verseSpec, "%s-(.+)%s+(%d+):(%d+)-(%d+)")
    local verses = {}
    local function renderVerse(verseName, verseNumber, singleSpace, doubleSpace, indent)
        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 doubleSpace then
            table.insert(process, addDoubleSpace)
        elseif singleSpace then
            table.insert(process, addSingleSpace)
        end
        if indent then table.insert(process, addIndent) end
        if #process > 0 then return compose(unpack(process))(wikiText) end
        return preProcess(wikiText)
    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
    return table.concat(verses, " ")
end

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

function p.standard(frame)
    local stringToBoolean = { ["true"] = true, ["false"] = false }
    return 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