Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Please sign up or log in to edit the wiki.
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local mArguments -- initialized lazily
local data = mw.loadJsonData('Module:ICE/data.json')
local p = {}

---Get citation
--- @param frame table - template arguments
--- @return string
function p.main(frame)
    mArguments = require('Module:Arguments')
    return p._main(mArguments.getArgs(frame), frame)
end

--- @param args table - template arguments
--- @param frame table - data
--- @return string
function p._main(args, frame)
    checkType('_main', 1, args, 'table')
    checkType('_main', 2, frame, 'table')

    local key = mw.text.trim(args[1] or "")
    if key == "" then 
        return "" 
    end
    
    -- lookup
    local book = data.citations[key]
    
    -- Fall back to stock number
    if not book then
        for _, entry in pairs(data.citations) do
            if entry.number and tostring(entry.number) == key then
                book = entry
                break
            end
        end
    end

    -- If still nothing is found, return the error
    if not book then
        return "<span class='error'>Unknown citation key or number: " .. key .. "</span>"
    end
    
    -- append number if it exists
    local publisherName = "[[Iron Crown Enterprises]]"
    if book.number and book.number ~= "" then
        publisherName = publisherName .. " #" .. book.number
    end
    
    local citeArgs = {
        author    = book.author or "",
        title     = book.title or "",
        date      = book.date or "",
        publisher = publisherName,
        chapter   = args.chapter or args[3] or "",
        page      = args.page or args[2] or "",
        pages     = args.pages or ""
    }
    
    return frame:expandTemplate{ title = 'cite book', args = citeArgs }
end

return p