Module:ChapterVariables

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

local p = {}

function p.getText(chapterText, subPage)
    if pcall(mw.loadData,('Module:ChapterVariables/' .. subPage)) then
        data = mw.loadData('Module:ChapterVariables/' .. subPage)
        if data[chapterText] then
            return data[chapterText]
        end
    end
    if pcall(mw.loadData,('Module:MagazineVariables/' .. subPage)) then
        data = mw.loadData('Module:MagazineVariables/' .. subPage)
        if data[chapterText] then
            return data[chapterText]
        end
    end
    return false
end

function p.main(frame)
    local chapterVariablesText = frame.args[1]
    local chapterVariablesTitle = ""
    local chapterVariablesResult = ""
    local variablesSubPage = 'data'
    if frame.args[2] ~= nil then
        variablesSubPage = 'data/' .. frame.args[2]
    end
    chapterVariablesTitle = p.getText(chapterVariablesText, variablesSubPage)

    -- Special case for CDDH chapters
    if string.find(chapterVariablesText, "CDDH Chapter") then
        local chapterNumber = string.match(chapterVariablesText, "%d+")
        if chapterNumber and chapterNumber ~= "16" then
            chapterVariablesTitle = "Chapter " .. chapterNumber
        end
    end

    if not chapterVariablesTitle then 
        return chapterVariablesText or ""
    end
    if frame.args[2] == 'ja' then
        chapterVariablesResult = frame:preprocess(chapterVariablesTitle)
    else
        chapterVariablesResult = chapterVariablesTitle
    end
    
    return chapterVariablesResult or chapterVariablesText or ""
end

return p