FANDOM


-- <nowiki>
local p = {}
local scriptTypes = require ("Module:Scriptlist/types")
local html = mw.html
--% Entry point that returns formatted list of javascript
--: (string) a listing of all javascript
function p.main()
    return showScripts()
end
--% Creates a list of all scripts
--: (string) a list of all scripts
function showScripts()
    -- local scriptData = mw.loadData("Module:Scriptlist/data")
    local scriptOutput = {}
    local scriptData = loadScriptsDpl()
    local scriptListDisplay = ""
    local cat = ""
 
    for _, scriptDetails in pairs(scriptData) do
        cat = scriptDetails.category
        if scriptTypes[cat:lower()] then
            scriptOutput[cat] = scriptOutput[cat] or html.create("ul")
            scriptOutput[cat]:
                node(formatElement(scriptDetails))
        else
            scriptOutput["general"] = scriptOutput["general"] 
                or html.create("ul") 
            scriptOutput["general"]
                :node(formatElement(scriptDetails))
        end
    end
 
    for heading, list in pairs(scriptOutput) do
        scriptListDisplay = scriptListDisplay .. 
            tostring(p.createHeadings(heading)) .. 
            tostring(list)
    end
 
    return scriptListDisplay
end
--% Creates headings for the type of script
--@ scriptType (string) Type of script, e.g. "User tools"
--: (string)  headings for type of script
function p.createHeadings(scriptType)
    local heading = html.create("h2")
    local lang = mw.language.new("en")
 
    if scriptTypes[scriptType:lower()] or scriptType == "general" then
        heading:wikitext(lang:ucfirst(scriptType))
    end
 
    return heading
end
--% Formats each script element in the page
--@ details (table) Table containing script metadata
--: (string)  A formatted li element
function formatElement(details)
    local separator = " — "
    local scriptNode = html.create("li")
    local content = (details.name or "") .. separator ..
        (details.description or "") 
 
    scriptNode:wikitext(content)
 
    return scriptNode:done()
end
--% Creates a dpl query and processes it
--: (table) Contains a key-value consisting of script name and description
function loadScriptsDpl()
    local dplCode = [[{{#dpl:
        | category = JavaScript
        | format   = ,<scriptdata>%PAGE%<dataseparator/>,</scriptdata>,
        | mode = userformat
        | suppresserrors = true
        | allowcachedresults = true
        | include={Infobox JavaScript}:Description:cat}}]]
    local dplResults = mw.getCurrentFrame():preprocess(dplCode)
    mw.incrementExpensiveFunctionCount()
 
    return p.extractDplData(dplResults)
end
--% Extracts results from dpl and puts it in a table
--@ dplResult (string) Contains a formatted list with script content
--: (table) An array with key-values and name, category, description, etc
function p.extractDplData(dplResult)
    local capturePattern = "<scriptdata>(.-)<dataseparator/>(.-)</scriptdata>"
    local scriptTable = {}
    local scriptCategory = ""
    local scriptDesc = ""
 
    for scriptName, scriptData in string.gmatch(dplResult, capturePattern) do
        scriptDesc, scriptCategory  = scriptData:match("(.*)\n|(.*)")
        table.insert(scriptTable, {name = scriptName, 
            category = scriptCategory,
            description  =  scriptDesc or scriptData 
        })
    end
 
    return scriptTable
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.