Module:Check for unknown parameters and Module:List: Difference between pages

From Frontierpedia, the Microsoft Agent encyclopedia
(Difference between pages)
en>Frietjes
(blank check)
 
en>Mr. Stradivarius
(simplify the type-property-checking code)
 
Line 1: Line 1:
-- This module may be used to compare the arguments passed to the parent
-- This module outputs different kinds of lists. At the moment, bulleted, unbulleted,
-- with a list of arguments, returning a specified result if an argument is
-- horizontal, ordered, and horizontal ordered lists are supported.
-- not on the list
 
local p = {}
local p = {}


local function trim(s)
local htmlBuilder = require('Module:HtmlBuilder')
return s:match('^%s*(.-)%s*$')
end


local function isnotempty(s)
local function getListItem(data, style, itemStyle, itemValue)
return s and trim(s) ~= ''
if not data then
return c
end
local item = htmlBuilder.create('li')
item
.cssText(style)
.cssText(itemStyle)
.wikitext(data)
if(itemValue) then
item.attr('value',itemValue)
end
return tostring(item)
end
end


function p.check (frame)
local function getArgNums(args)
local args = frame.args
-- Returns an array containing the keys of all positional arguments
local pargs = frame:getParent().args
-- that contain data (i.e. non-whitespace values).
local ignoreblank = isnotempty(frame.args['ignoreblank'])
local nums = {}
local checkpos = isnotempty(frame.args['checkpositional'])
local knownargs = {}
local unknown = 'Found _VALUE_, '
local preview = nil
 
local res = {}
local regexps = {}
local comments = {}
local commentstr = ''
local ispreview = frame:preprocess( "{{REVISIONID}}" ) == "" and 1 or 0
 
-- create the list of known args, regular expressions, and the return string
for k, v in pairs(args) do
for k, v in pairs(args) do
if type(k) == 'number' then
if type(k) == 'number' and
v = trim(v)
k >= 1 and  
knownargs[v] = 1
math.floor(k) == k and  
elseif k == 'unknown' and ispreview == 0 then
mw.ustring.match(v, '%S') then
unknown = v
table.insert(nums, k)
elseif k == 'preview' and ispreview == 1 then
preview = v
elseif k:find('^regexp[1-9][0-9]*$') then
table.insert(regexps, '^' .. v .. '$')
end
end
end
end
if preview and preview ~= '' then preview = '<div class="hatnote" style="color:red"><strong>Warning:</strong> ' .. preview .. ' (this message is shown only in preview).</div>' end
table.sort(nums)
if ispreview == 1 and preview then unknown = preview end
return nums
end
-- adds one result to the output tables
 
local function addresult(k)
function p.makeList(listType, args)
if k == '' then
-- This is the main function to be called from other Lua modules.
-- Fix odd bug for | = which gets stripped to the empty string and
-- First, get the list items.
-- breaks category links
local listItems = {}
k = ' '
local argNums = getArgNums(args)
for i, num in ipairs(argNums) do
local item = getListItem(
args[num],
args.item_style or args.li_style, -- li_style is included for backwards compatibility. item_style was included to be easier to understand for non-coders.
args['item_style' .. tostring(num)] or args['li_style' .. tostring(num)],
args['item_value' .. tostring(num)]
)
table.insert(listItems, item)
end
if #listItems == 0 then
return ''
end
-- Build the list html.
local root = htmlBuilder.create('div')
if listType == 'horizontal' or listType == 'horizontal_ordered' then
root.addClass('hlist')
elseif listType == 'unbulleted' then
root.addClass('plainlist')
end
root.addClass(args.class)
if listType == 'horizontal' or listType == 'horizontal_ordered' then
local indent = tonumber(args.indent)
indent = tostring((indent and indent * 1.6) or 0)
root.css('margin-left', indent .. 'em')
end
-- Detect if the type attribute specified by args.type is using an invalid value, and if so try passing it as a list-style-type CSS property.
local listStyleType = args.list_style_type or args['list-style-type']
local listType = args['type']
if listType and not listType:match('^%s*[1AaIi]%s*$') then
if not listStyleType then
listStyleType = listType
listType = nil
end
end
local r = unknown:gsub('_VALUE_', k)
table.insert(res, r)
table.insert(comments, '"' .. k .. '"')
end
end
root.cssText(args.style)
local list = root.tag((listType == 'ordered' or listType == 'horizontal_ordered') and 'ol' or 'ul')
list
.attr('start', args.start)
.attr('type', listType)
.css('list-style-type', listStyleType)
.cssText(args.list_style or args.ul_style or args.ol_style) -- ul_style and ol_style are included for backwards compatibility. No distinction is made for ordered or unordered lists.
.wikitext(table.concat(listItems))
return tostring(root)
end


-- loop over the parent args, and make sure they are on the list
local function makeWrapper(listType)
for k, v in pairs(pargs) do
return function(frame)
if type(k) == 'string' and knownargs[k] == nil then
local origArgs
local knownflag = false
if frame == mw.getCurrentFrame() then
for i, regexp in ipairs(regexps) do
origArgs = frame:getParent().args
if mw.ustring.match(k, regexp) then
for k, v in pairs(frame.args) do
knownflag = true
origArgs = frame.args
break
break
end
end
end
if not knownflag and ( not ignoreblank or isnotempty(v) ) then
else
k = mw.ustring.gsub(k, '[^%w\-_ ]', '?')
origArgs = frame
addresult(k)
end
local args = {}
for k, v in pairs(origArgs) do
if type(k) == 'number' or v ~= '' then
args[k] = v
end
end
elseif checkpos and
type(k) == 'number' and
knownargs[tostring(k)] == nil and
( not ignoreblank or isnotempty(v) )
then
addresult(k)
end
end
return p.makeList(listType, args)
end
end
end
local funcNames = {'bulleted', 'unbulleted', 'horizontal', 'ordered', 'horizontal_ordered'}


if #comments > 0 then
for _, funcName in ipairs(funcNames) do
commentstr = '<!-- Module:Check for unknown parameters results: ' ..
p[funcName] = makeWrapper(funcName)
table.concat(comments, ', ') .. '-->'
end
return table.concat(res) .. commentstr
end
end


return p
return p

Revision as of 17:42, 23 November 2013

Documentation for this module may be created at Module:List/doc

-- This module outputs different kinds of lists. At the moment, bulleted, unbulleted,
-- horizontal, ordered, and horizontal ordered lists are supported.

local p = {}

local htmlBuilder = require('Module:HtmlBuilder')

local function getListItem(data, style, itemStyle, itemValue)
	if not data then
		return c
	end
	local item = htmlBuilder.create('li')
	item
		.cssText(style)
		.cssText(itemStyle)
		.wikitext(data)
	if(itemValue) then
		item.attr('value',itemValue)
	end
	return tostring(item)
end

local function getArgNums(args)
	-- Returns an array containing the keys of all positional arguments
	-- that contain data (i.e. non-whitespace values).
	local nums = {}
	for k, v in pairs(args) do
		if type(k) == 'number' and 
			k >= 1 and 
			math.floor(k) == k and 
			mw.ustring.match(v, '%S') then
				table.insert(nums, k)
		end
	end
	table.sort(nums)
	return nums
end

function p.makeList(listType, args)
	-- This is the main function to be called from other Lua modules.
	-- First, get the list items.
	local listItems = {}
	local argNums = getArgNums(args)
	for i, num in ipairs(argNums) do
		local item = getListItem(
			args[num],
			args.item_style or args.li_style, -- li_style is included for backwards compatibility. item_style was included to be easier to understand for non-coders.
			args['item_style' .. tostring(num)] or args['li_style' .. tostring(num)],
			args['item_value' .. tostring(num)]
		)
		table.insert(listItems, item)
	end
	if #listItems == 0 then
		return ''
	end
	-- Build the list html.
	local root = htmlBuilder.create('div')
	if listType == 'horizontal' or listType == 'horizontal_ordered' then
		root.addClass('hlist')
	elseif listType == 'unbulleted' then
		root.addClass('plainlist')
	end
	root.addClass(args.class)
	if listType == 'horizontal' or listType == 'horizontal_ordered' then
		local indent = tonumber(args.indent)
		indent = tostring((indent and indent * 1.6) or 0)
		root.css('margin-left', indent .. 'em')
	end
	-- Detect if the type attribute specified by args.type is using an invalid value, and if so try passing it as a list-style-type CSS property.
	local listStyleType = args.list_style_type or args['list-style-type']
	local listType = args['type']
	if listType and not listType:match('^%s*[1AaIi]%s*$') then
		if not listStyleType then
			listStyleType = listType
			listType = nil
		end
	end
	root.cssText(args.style)
	local list = root.tag((listType == 'ordered' or listType == 'horizontal_ordered') and 'ol' or 'ul')
	list
		.attr('start', args.start)
		.attr('type', listType)
		.css('list-style-type', listStyleType)
		.cssText(args.list_style or args.ul_style or args.ol_style) -- ul_style and ol_style are included for backwards compatibility. No distinction is made for ordered or unordered lists.
		.wikitext(table.concat(listItems))
	return tostring(root)
end

local function makeWrapper(listType)
	return function(frame)
		local origArgs
		if frame == mw.getCurrentFrame() then
			origArgs = frame:getParent().args
			for k, v in pairs(frame.args) do
				origArgs = frame.args
				break
			end
		else
			origArgs = frame
		end
		
		local args = {}
		for k, v in pairs(origArgs) do
			if type(k) == 'number' or v ~= '' then
				args[k] = v
			end
		end
		return p.makeList(listType, args)
	end
end

local funcNames = {'bulleted', 'unbulleted', 'horizontal', 'ordered', 'horizontal_ordered'}

for _, funcName in ipairs(funcNames) do
	p[funcName] = makeWrapper(funcName)
end

return p