Jump to content

Module:Custom rule

From Wikisource

require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local error_message = require('Module:Error')['error']

local segment_shortcuts = mw.loadData('Module:Custom rule/segments')

local function custom_rule_segment(args)
	local name
	if args[1] then
		name = segment_shortcuts[args[1]] or args[1]
	end
	local size = args[2]
	if not name or not size then
		return error_message({'[[Module:Custom rule]] error: could not interpret rule segment'})
	end
	return '[[File:Rule Segment - ' .. name .. ' - ' .. size .. 'px.svg|middle|link=|alt=]]'
end

function p._custom_rule(args)
	local rule
	if yesno(args.inline or false) then
		rule = mw.html.create('span')
	else
		rule = mw.html.create('div')
	end
	
	rule:addClass('wst-custom-rule ' .. (args.class or ''))
	
	if args.style then
		rule:attr('style', args.style)
	end
	
	local segments = {}
	for i, v in ipairs(args) do
		if i % 2 == 1 then
			table.insert(segments, custom_rule_segment({args[i], args[i + 1]}))
		end
	end
	rule:wikitext(table.concat(segments))
	
	return tostring(rule)
end

function p.custom_rule(frame)
	local args = getArgs(frame)
	return p._custom_rule(args)
end

return p