Module:Images

来自滚动的天空Wiki
文档图示 模块文档[查看] [编辑] [查看历史] [清除缓存]

本模块主要用于{{机关列表}}模板。

上述文档内容嵌入自Module:Images/doc编辑 | 历史
编者可以在本模块的沙盒创建 | 镜像和测试样例创建页面进行实验。
请将模块自身所属的分类添加在文档中。本模块的子页面
local p = {} 
local navbar =require("模块:Navbar")._navbar
local trim = mw.text.trim
-- 本模块用于创建关卡“机关”段落的内容。

local addNewline = require('module:others').addNewLine

local function join(data, index, num, value)
	num = tonumber(num)
	if (not num) or num<=0 then return end
	data[num] = data[num] or {}
	data[num][index] = value
end

function p.process(args)
	local config = {}
	local data = {}
	for k,v in pairs(args) do
		if type(k)=='string' then
			local index, num = k:match '^(%l+)(%d+)'
			if index == 'title'
			or index == 'image'
			or index == 'text'
			or index == 'section'
			or index == 'navbar' then
				--匹配识别这些前缀开头的带数字参数键加入data
				join(data, index, num, trim(v))
			elseif k == 'tag' or k == 'class' or k == 'nonavbar' then
				--识别特定参数,加入config
				config[k] = trim(v)
			end
		end
	end
	return data, config
end

function p.render(data, config)
	config = config or {}
	local tag = config.tag or 'h3'
	local class = config.class
	local keys = {}
	local root = mw.html.create 'div' --主体
		:addClass 'image-sections'
		:addClass(class)
	for k,v in require 'module:others'.visit(data) do 
		--考虑到断层参数的排序问题,先人工排序再迭代
		local section = root:tag 'div'
			:addClass 'image-section'
		if v.section then
			v.title, v.text = v.section:match("===(.-)===(.+)")
			v.text = v.text or v.section
			--将section直接拆成标题和文本
		end
		if v.title then --渲染标题
			section:tag(tag)
				:addClass 'image-section-title'
				:wikitext(v.title)
		end
		if v.navbar ~= "0" and config.nonavbar == "0" then
			-- nonavbar设为0且对应navbar不为0才渲染导航栏。
			-- 默认的链接页面同title。不是合法页面名会报错。
			v._nb = v.navbar or v.title
			v.nb = navbar{title=':' .. v._nb, style="display:inline-block"}
			section:wikitext(v.nb)
		end
		if v.image then --未来或将废除,直接并入文本部分
			section:tag 'div'
				:addClass 'image-section-image'
				:wikitext(v.image)
		end
		if v.text then --渲染文本部分
			section:tag 'div'
				:addClass 'image-section-text hantsect'
				:wikitext(addNewline(v.text))
		end
		
	end
	return root
end

function p._images(args)
	if type(args) ~= 'table' then return end
	return p.render(p.process(args))
end

function p.main(frame)
	return p._images(frame.args)
end

function p.images(frame)
	return p._images(frame:getParent().args)
end

return p