模块:Eval
来自东方活动维基
可在模块:Eval/doc创建此模块的帮助文档
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