Module:BibleQuote: Difference between revisions
No edit summary  | 
				No edit summary  | 
				||
| Line 20: | Line 20: | ||
     return (string.gsub(verse, "^", "    "))  |      return (string.gsub(verse, "^", "    "))  | ||
end  | end  | ||
-- local function expandTemplate(title, ...)  | -- local function expandTemplate(title, ...)  | ||
--     return mw.getCurrentFrame():expandTemplate { title = title };  | --     return mw.getCurrentFrame():expandTemplate { title = title };  | ||
| Line 38: | Line 37: | ||
end  | end  | ||
local function renderVerses(verseSpec, showVerseNumber, showPilcrow)  | local function renderVerses(verseSpec, showVerseNumber, showPilcrow, singleSpace, doubleSpace, indent)  | ||
     local _, _, book, chapter, startVerse, stopVerse =  |      local _, _, book, chapter, startVerse, stopVerse =  | ||
         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)  | ||
         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 50: | Line 49: | ||
         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 #process > 0 then return compose(unpack(process))(wikiText) end  | |||
        return preProcess(wikiText)  | |||
    end  | |||
    local function renderConcat()  | |||
        local process = {}  | |||
         if doubleSpace then  |          if doubleSpace then  | ||
             table.insert(process, addDoubleSpace)  |              table.insert(process, addDoubleSpace)  | ||
| Line 56: | Line 60: | ||
         end  |          end  | ||
         if indent then table.insert(process, addIndent) end  |          if indent then table.insert(process, addIndent) end  | ||
         if #process > 0 then return compose(unpack(process))(  |          if #process > 0 then return compose(unpack(process))("") end  | ||
         return   |          return " "  | ||
     end  |      end  | ||
     if stopVerse == nil then return renderVerse(verseSpec, startVerse) end  |      if stopVerse == nil then return renderVerse(verseSpec, startVerse) end  | ||
| Line 63: | Line 67: | ||
         table.insert(verses, renderVerse(book .. " " .. chapter .. ":" .. i, i))  |          table.insert(verses, renderVerse(book .. " " .. chapter .. ":" .. i, i))  | ||
     end  |      end  | ||
     return table.concat(verses,   |      return table.concat(verses, renderConcat())  | ||
end  | end  | ||
Revision as of 01:58, 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, "^", "    "))
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 preProcess(wikiText)
    end
    local function renderConcat()
        local process = {}
        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))("") 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
    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 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