Module:Eval

From Moegirlpedia
Jump to: navigation, search
Template-info.svg Module Documentation  [View] [Edit] [History] [Refresh]

This module/template provides the possibility to parse almost any Wiki text almost anywhere.

Usage

Eval in the following format refers to:

  • Write #invoke:Eval when using modules;
  • Write Eval when using templates.

Expanding a template

{{ Eval | template | template name | parameter list... }}
Parameters
  • Template name: The name of the template to be expanded, without adding the Template:namespace prefix;
  • Parameter list: all parameters of the template. The format is:
    • parameter name = wikitext or wikitext;
    • When the parameter name is a number (or does not display and write [parameter name]=), the parameter value can write the parameter text of multiple templates to be expanded, such as [template parameter 1]|[template parameter 2]|…. Finally, the numbered parameters will be concatenated in order to become the anonymous parameter list of the template to be expanded;
    • When the parameter name is not a number, it will be equivalent to writing the parameter of the same name of the template to be expanded. These parameters will be concatenated after the anonymous parameter list.
local module = {}
local getArgs = require("Module:Arguments").getArgs

local preprocessNoWiki = function(wiki)
    wiki = mw.ustring.gsub(wiki, "(%$+)NOWIKI_([ES])", function(prefix, postfix)
        local length, tag = math.modf(mw.ustring.len(prefix) / 2)
        
        local escape = ""
        for i = 1, length do escape = escape .. "$" end
        if tag == 0 then
            tag = "NOWIKI_" .. postfix
        elseif postfix == "S" then
            tag = "<nowiki>"
        else
            tag = "</nowiki>"
        end
        
        return escape .. tag
    end)
    
    return wiki
end

function module.template(frame, args)
    args = args or getArgs(frame, { wrappers = "Template:Eval" })
    local parentTitle = tostring((frame:getParent() or frame):getTitle())
    local offset
    if parentTitle == "Template:Eval" then
        offset = 1
    else
        offset = 0
    end
    
    local tTitle = mw.text.trim(args[1 + offset] or "")
    if tTitle == "" then return nil end
    
    local tArgs = {}
    
    local indexes = {}
    for k, _ in pairs(args) do
        if type(k) == "number" then
            if k > 1 + offset then
                table.insert(indexes, k)
            end
        end
    end
    table.sort(indexes)
    for _, index in ipairs(indexes) do
        local wiki = args[index]
        wiki = mw.text.unstripNoWiki(wiki)
        wiki = mw.text.decode(wiki)
        table.insert(tArgs, preprocessNoWiki(wiki))
    end
    for k, v in pairs(args) do
        if type(k) ~= "number" then
            local wiki = v
            wiki = mw.text.unstripNoWiki(wiki)
            wiki = mw.text.decode(wiki)
            table.insert(tArgs, mw.ustring.format("%s=%s", k, preprocessNoWiki(wiki)))
        end
    end
    
    tArgs = table.concat(tArgs, "|")
    if mw.ustring.len(tArgs) > 0 then
        tArgs = "|" .. tArgs
    end
    return frame:preprocess(mw.ustring.format("{{ %s %s}}", tTitle, tArgs))
end

function module.wikitext(frame, args)
    args = args or frame.args
    local wiki = args[1] or ""
    wiki = mw.text.unstripNoWiki(wiki)
    wiki = mw.text.decode(wiki)
    return frame:preprocess(preprocessNoWiki(wiki))
end

function module.page(frame, args)
    args = args or frame.args
    local title = mw.title.new(mw.text.trim(args[1] or ""))
    if title ~= nil and title.exists and title.contentModel == "wikitext" then
        local content = title:getContent()
        return frame:preprocess(content)
    end
end

return module