Editing Module:Param Parse
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 3: | Line 3: | ||
--]=] |
--]=] |
||
-- <nowiki> |
-- <nowiki> |
||
local Infobox = require('Module:Infobox') |
|||
local |
local param = {} |
||
-- Generic functions -- |
|||
----------------------- |
|||
-- Standardized " |
-- Standardized "name" function |
||
function |
function param.name(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 |
|||
-- Standardized "has content" function |
|||
-- Yes/No |
|||
function param.has_content(arg, default) |
|||
local yes_no = { |
|||
-- Return arg if any non-whitespace character is found |
|||
['Yes'] = 'true', |
|||
return string.match(arg or '', '%S') and arg or default |
|||
['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 |
end |
||
-- Premade Params -- |
|||
-------------------- |
|||
-- Create a standardized release function, since so many pages use it |
|||
-- Standardized name function |
|||
-- Turns release and update into a single parameter |
|||
parse.name = { |
|||
function param.release_update(release, update) |
|||
name = 'name', |
|||
if not Infobox.isDefined(release) then |
|||
func = parse.has_content, |
|||
return nil |
|||
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 |
end |
||
if string.lower(release) == 'no' then |
|||
return nil |
|||
return 'N/A' |
|||
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 |
end |
||
if not Infobox.isDefined(update) then |
|||
return default |
|||
return string.format('%s (Update unknown)',release) |
|||
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 |
end |
||
if |
if string.lower(update) == 'no' then |
||
return |
return release |
||
end |
end |
||
return string.format('%s ([[Update:%s|Update]])', release, update) |
|||
return nil |
|||
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 |
|||
function param.image(img) |
|||
-- Episode |
|||
if img and img:find('%S') then |
|||
local valid_episodes = { |
|||
return img |
|||
['hopeport'] = 'Hopeport', |
|||
else |
|||
['hopeforest'] = 'Hopeforest', |
|||
return nil |
|||
['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 |
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', |
|||
} |
|||
-- Strip the brackets from the param |
|||
-- Release (TODO) |
|||
function param.image_smw(img) |
|||
parse.release = { |
|||
local _img = string.match(img, "File:.-%.png") |
|||
name = 'release', |
|||
return _img |
|||
func = parse.has_content, |
|||
end |
|||
} |
|||
-- Profession (TODO) |
|||
parse.profession = { |
|||
name = 'profession', |
|||
func = parse.has_content, |
|||
category_incomplete = 'Needs profession', |
|||
smw_property = 'Profession' -- TODO - Need to create property page |
|||
} |
|||
-- Standardized numbers |
|||
parse.variant = { |
|||
function param.numbers(num) |
|||
name = 'variant', |
|||
num = string.gsub(num or '',',','') |
|||
func = parse.has_content, |
|||
return tonumber(num) |
|||
category_incomplete = 'Needs variant details', |
|||
end |
|||
smw_property = 'Variant of' -- TODO - Need to create property page |
|||
} |
|||
return param |
|||
parse.passive = { |
|||
name = 'passive', |
|||
func = parse.yes_no, |
|||
smw_property = 'Passive', |
|||
smw_func = parse.yes_no_smw, |
|||
category_incomplete = 'Needs passiveness' |
|||
} |
|||
return parse |
|||
-- </nowiki> |
-- </nowiki> |