2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/An196/Common

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

版权提示:该模块有位于其它平台的不同版本,可能有微小差异,不保证同步更新。同时,允许您搬运此模块。

简单输出库

这是一个简单的输出轮子。

不应当在页面内显式使用#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