Module:Param Parse: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with "--[=[ -- Standardized functions to parse Infobox params --]=] -- <nowiki> local p = {} -- Standardized "name" function function param.name(arg) return string.match(arg or '', '%S') and arg or nil end -- Standardized "has content" function function param.has_content(arg, default) -- Return arg if any non-whitespace character is found return string.match(arg or '', '%S') and arg or default end -- Create a standardized release function, since so many pages use it...")
 
(Fix image function bug for nil images)
 
(35 intermediate revisions by 4 users not shown)
Line 3: Line 3:
--]=]
--]=]
-- <nowiki>
-- <nowiki>
local Infobox = require('Module:Infobox')


local p = {}
local parse = {}


-- Generic functions --
-----------------------


-- Standardized "name" function
-- Standardized "has content" function
function param.name(arg)
function parse.has_content(arg)
-- Return arg if any non-whitespace character is found
return string.match(arg or '', '%S') and arg or nil
return string.match(arg or '', '%S') and arg or nil
end
end


-- Standardized numbers
function parse.number(num)
num = string.gsub(num or '',',','')
return tonumber(num)
end


-- Yes/No
-- Standardized "has content" function
local yes_no = {
function param.has_content(arg, default)
['Yes'] = 'true',
-- Return arg if any non-whitespace character is found
['No'] = 'false',
return string.match(arg or '', '%S') and arg or default
['N/A'] = 'NIL',
}
function parse.yes_no(text)
if yes_no[text] ~= nil then
return text
end
return nil
end
function parse.yes_no_smw(text)
if text == 'N/A' then
return nil
end
local bool = yes_no[text]
if bool ~= nil then
return bool
end
return nil
end
end


-- Premade Params --
--------------------


-- Standardized name function
-- Create a standardized release function, since so many pages use it
parse.name = {
-- Turns release and update into a single parameter
name = 'name',
function param.release_update(release, update)
func = parse.has_content,
if not Infobox.isDefined(release) then
smw_property = 'Name',
return nil
category_incomplete = 'Needs name',
}
-- Standardized examine function
parse.examine = {
name = 'examine',
func = parse.has_content,
smw_property = 'Examine',
category_incomplete = 'Needs examine',
}

-- Standardized image function
function parse.image_name(img)
if img then
return string.match(img, '%[%[(File:.+%.%w%w%w)|?%d*x?%d*p?x?%]%]')
end
end
return nil
if string.lower(release) == 'no' then
end
return 'N/A'
function parse.image_height(img, default)
if img then
return string.match(img, '%[%[File:.+%.%w%w%w|%d*x(%d*)px%]%]') or default
end
end
return default
if not Infobox.isDefined(update) then
end
return string.format('%s (Update unknown)',release)
function parse.image_width(img, default)
if img then
return string.match(img, '%[%[File:.+%.%w%w%w|(%d*)x?%d*px%]%]') or default
end
return default
end
function parse.image_func(img, default_width, default_height)
local name = parse.image_name(img)
local width = parse.image_width(img, default_width)
local height = parse.image_height(img, default_height)
local size = ''
if width and height then
size = '|'..width..'x'..height..'px'
elseif width then
size = '|'..width..'px'
elseif height then
size = '|x'..height..'px'
end
end
if string.lower(update) == 'no' then
if name then
return release
return '[['..name..size..']]'
end
end
return nil
return string.format('%s ([[Update:%s|Update]])', release, update)
end
function parse.image_smw(img)
return parse.image_name(img)
end
end


parse.image = {
name = 'image',
func = parse.image_func,
smw_property = 'Image',
smw_func = parse.image_smw,
empty = '[[Special:Upload|Please upload an image!]]',
category_incomplete = 'Needs image',
}
parse.image_size300 = {
name = 'image',
func = {name = parse.image_func, params = {Infobox.raw_param('image'), 300, 300}},
smw_property = 'Image',
smw_func = parse.image_smw,
empty = '[[Special:Upload|Please upload an image!]]',
category_incomplete = 'Needs image',
}



-- Standardized image function
-- Episode
function param.image(img)
local valid_episodes = {
if img and img:find('%S') then
['hopeport'] = 'Hopeport',
return img
['hopeforest'] = 'Hopeforest',
else
['mine of mantuban'] = 'Mine of Mantuban',
return nil
['crenopolis'] = 'Crenopolis'
}
function parse.episode_func(episode)
local valid_episode = valid_episodes[string.lower(episode or '')]
if valid_episode then
return '[[File:'..valid_episode..' episode icon.png|18px]] [['..valid_episode..']]'
end
return nil
end
function parse.episode_smw(episode)
local valid_episode = valid_episodes[string.lower(episode or '')]
if valid_episode then
return valid_episode
end
end
return nil
end
end
parse.episode = {
name = 'episode',
func = parse.episode_func,
smw_property = 'Episode',
smw_func = parse.episode_smw,
category_incomplete = 'Needs episode',
}


-- Premium
parse.premium = {
name = 'premium',
func = parse.yes_no,
smw_property = 'Premium',
smw_func = parse.yes_no_smw,
category_incomplete = 'Needs premium status',
}


-- Release (TODO)
-- Strip the brackets from the param
parse.release = {
function param.image_smw(img)
name = 'release',
local _img = string.match(img, "File:.-%.png")
func = parse.has_content,
return _img
}
end


-- Profession (TODO)
parse.profession = {
name = 'profession',
func = parse.has_content,
category_incomplete = 'Needs profession',
smw_property = 'Profession' -- TODO - Need to create property page
}


parse.variant = {
-- Standardized numbers
name = 'variant',
function param.numbers(num)
func = parse.has_content,
num = string.gsub(num or '',',','')
category_incomplete = 'Needs variant details',
return tonumber(num)
smw_property = 'Variant of' -- TODO - Need to create property page
end
}


parse.passive = {
return param
name = 'passive',
func = parse.yes_no,
smw_property = 'Passive',
smw_func = parse.yes_no_smw,
category_incomplete = 'Needs passiveness'
}


return parse
-- </nowiki>
-- </nowiki>

Latest revision as of 13:10, 28 June 2024

Module documentation
This documentation is transcluded from Module:Param Parse/doc. [edit] [history] [purge]
Module:Param Parse requires Module:Infobox.

These params and functions are used to validate and format information passed into an Infobox. See Module:Infobox for details and instructions on creating an Infobox. If the information is missing or not valid, the functions here should return nil.

Here is a summary of all the params:

param explanation
parse.name Title
parse.examine Examine info
parse.image [[File:IMAGE.png]]. Use the class infobox-image in the table
parse.image_size300 [[File:IMAGE.png|300x300px]] by default unless overridden. Use the class infobox-image in the table
parse.episode Episode name
parse.premium Premium (Yes, No, N/A)
parse.release TODO
parse.profession TODO
parse.variant TODO - Which category the subject is a variant of

Here are some general use functions that can be used for custom or new params:

function name explanation
has_content(arg) Returns arg if any non-whitespace character is found in the string.
number(num) Removes any commas and parses the string as a number
yes_no(text) Only accepts "Yes", "No" and "N/A"
yes_no_smw(text) Maps "Yes", "No" to true and false respectively

Creating New Params

Ideally, params should be defined here so that they can be reused between different Infoboxes. Here is how to create a new param:

parse.paramname = {
	name = <param>,
    func = <func>,
    empty = 'empty', -- optional
    category_never = 'category', -- optional
    category_partial = 'category', -- optional
    category_incomplete = 'category', -- optional
    category_complete = 'category', -- optional
    smw_property = 'Property', -- optional
    smw_func = <func>, -- optional
}


key value
name parameter name as used in the Template
func A function in Module:Param Parse to validate and process the Template argument. You can also use a local function, but this is not recommended.

If func is a function, will call func(Infobox.raw_param(name)):

	{name = <param>, func = <func>, ... },

If func is a table, it takes the following parameters:

	{name = <param>, func = { name = <func>, params = <func_params>}, ... },
name function in Module:Param Parse
params a list of parameters to pass the the function, in the form of constants, or Infobox.raw_param(name), Infobox.param(name), Infobox.smw_param(name)
empty (optional) text to display in the infobox if func returns nil; defaults to "? (edit)"
category_never (optional) category to add if func returns nil for all versions
category_partial (optional) category to add if func returns nil for some versions, but a value for other versions
category_incomplete (optional) category to add if func returns nil for at least 1 version (i.e. category_never and category_partial combined)
category_complete (optional) category to add if func returns a value for all versions
smw_property (optional) if this string is defined, the parameter will be saved into SMW
smw_func (optional) function to validate and process the Template argument to save into SMW. func is used by default if smw_func is not defined

--[=[
-- Standardized functions to parse Infobox params
--]=]
-- <nowiki>
local Infobox = require('Module:Infobox')

local parse = {}

-- Generic functions --
-----------------------

-- Standardized "has content" function
function parse.has_content(arg)
	-- Return arg if any non-whitespace character is found
	return string.match(arg or '', '%S') and arg or nil
end

-- Standardized numbers
function parse.number(num)
	num = string.gsub(num or '',',','')
	return tonumber(num)
end

-- Yes/No
local yes_no = {
	['Yes'] = 'true',
	['No'] = 'false',
	['N/A'] = 'NIL',
}
function parse.yes_no(text)
	if yes_no[text] ~= nil then
		return text
	end
	return nil
end
function parse.yes_no_smw(text)
	if text == 'N/A' then
		return nil
	end
	local bool = yes_no[text]
	if bool ~= nil then
		return bool
	end
	return nil
end

-- Premade Params --
--------------------

-- Standardized name function
parse.name = {
	name = 'name',
	func = parse.has_content,
	smw_property = 'Name',
	category_incomplete = 'Needs name',
}
-- Standardized examine function
parse.examine = {
	name = 'examine',
	func = parse.has_content,
	smw_property = 'Examine',
	category_incomplete = 'Needs examine',
}

-- Standardized image function
function parse.image_name(img)
	if img then
		return string.match(img, '%[%[(File:.+%.%w%w%w)|?%d*x?%d*p?x?%]%]')
	end
	return nil
end
function parse.image_height(img, default)
	if img then
		return string.match(img, '%[%[File:.+%.%w%w%w|%d*x(%d*)px%]%]') or default
	end
	return default
end
function parse.image_width(img, default)
	if img then
		return string.match(img, '%[%[File:.+%.%w%w%w|(%d*)x?%d*px%]%]') or default
	end
	return default
end
function parse.image_func(img, default_width, default_height)
	local name = parse.image_name(img)
	local width = parse.image_width(img, default_width)
	local height = parse.image_height(img, default_height)
	local size = ''
	if width and height then
		size = '|'..width..'x'..height..'px'
	elseif width then
		size = '|'..width..'px'
	elseif height then
		size = '|x'..height..'px'
	end
	if name then
		return '[['..name..size..']]'
	end
	return nil
end
function parse.image_smw(img)
	return parse.image_name(img)
end

parse.image = {
	name = 'image',
	func = parse.image_func,
	smw_property = 'Image',
	smw_func = parse.image_smw,
	empty = '[[Special:Upload|Please upload an image!]]',
	category_incomplete = 'Needs image',
}
parse.image_size300 = {
	name = 'image',
	func = {name = parse.image_func, params = {Infobox.raw_param('image'), 300, 300}},
	smw_property = 'Image',
	smw_func = parse.image_smw,
	empty = '[[Special:Upload|Please upload an image!]]',
	category_incomplete = 'Needs image',
}


-- Episode
local valid_episodes = {
	['hopeport'] = 'Hopeport',
	['hopeforest'] = 'Hopeforest',
	['mine of mantuban'] = 'Mine of Mantuban',
	['crenopolis'] = 'Crenopolis'
}
function parse.episode_func(episode)
	local valid_episode = valid_episodes[string.lower(episode or '')]
	if valid_episode then
		return '[[File:'..valid_episode..' episode icon.png|18px]] [['..valid_episode..']]'
	end
	return nil
end
function parse.episode_smw(episode)
	local valid_episode = valid_episodes[string.lower(episode or '')]
	if valid_episode then
		return valid_episode
	end
	return nil
end
parse.episode = {
	name = 'episode',
	func = parse.episode_func,
	smw_property = 'Episode',
	smw_func = parse.episode_smw,
	category_incomplete = 'Needs episode',
}

-- Premium
parse.premium = {
	name = 'premium',
	func = parse.yes_no,
	smw_property = 'Premium',
	smw_func = parse.yes_no_smw,
	category_incomplete = 'Needs premium status',
}

-- Release (TODO)
parse.release = {
	name = 'release',
	func = parse.has_content,
}

-- Profession (TODO)
parse.profession = {
	name = 'profession',
	func = parse.has_content,
	category_incomplete = 'Needs profession',
	smw_property = 'Profession' -- TODO - Need to create property page
}

parse.variant = {
	name = 'variant',
	func = parse.has_content,
	category_incomplete = 'Needs variant details',
	smw_property = 'Variant of' -- TODO - Need to create property page
}

parse.passive = {
	name = 'passive',
	func = parse.yes_no,
	smw_property = 'Passive',
	smw_func = parse.yes_no_smw,
	category_incomplete = 'Needs passiveness'
}

return parse
-- </nowiki>