模組: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