Module:Sandbox/An196/Common
< Module:Sandbox | An196
跳到导航
跳到搜索
版权提示:该模块有位于其它平台的不同版本,可能有微小差异,不保证同步更新。同时,允许您搬运此模块。
简单输出库
这是一个简单的输出轮子。
不应当在页面内显式使用#invoke调用,请在您的模块中使用下面的语句导入这个库。
- local out=require("Module:Sandbox/An196/Common")
使用
目前有以下函数:
out.str(obj)
-- 获取对象的字符串表示(似乎可以使用mw.logObject替代)out.write(data)
-- 不换行输出out.writeln(data)
-- 换行输出out.color(text, color)
-- 颜色输出:用带颜色的span包围text。out.create_main(base)
-- main函数装饰器,自动返回结果
使用main装饰器,您应该
- p.main = out.create_main(p.run)
- local out = {}
- local content = ""
- function out.str(obj)
- local text = ""
- if (type(obj)=="string") then
- text = obj
- elseif (type(obj)=="boolean") then
- if obj then
- text = "true"
- else
- text = "false"
- end
- elseif (type(obj)=="table") then
- text = "{"
- for k, v in pairs(obj) do
- text = text .. out.str(k) .. ":" .. out.str(v)
- end
- text = text .. "}"
- elseif (type(obj)=="number") then
- text = ""..obj
- else
- text = "<type '"..type(obj).."'>"
- end
- return text
- end
- function out.write(data)
- content = content .. out.str(data)
- end
- function out.writeln(data)
- content = content .. out.str(data) .. "<br>"
- end
- function out.color(text, color)
- return [[<span style="color:]] .. out.str(color) .. [[;">]]..out.str(text).."</span>"
- end
- function out.create_main(base)
- function warp(frame)
- base(frame)
- return frame:preprocess(content)
- end
- return warp
- end
- return out