Module:Block value: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
m KnightMiner moved page Module:Hardness to Module:Block value without leaving a redirect: Better name reflecting new usage. It will be broken for a little bit while I rename it, since modules do not support redirects
m add support for alternative value modules
Line 1: Line 1:
local p = {}
local p = {}
p.hardness = function( f )
p.value = function( f )
local args = f
local args = require( 'Module:ProcessArgs' ).norm( f.args or f )
if f == mw.getCurrentFrame() then
args = f:getParent().args
end
local block = mw.text.trim( args[1] or '' ):lower()
local block = mw.text.trim( args[1] or '' ):lower()
local type = args.type


-- Most of these transforms are unnecessary, but are kept for compatibility with original template
-- Most of these transforms are unnecessary, but are kept for compatibility with original template
Line 30: Line 28:
:gsub( "['%(%)%-%s]+", '' )
:gsub( "['%(%)%-%s]+", '' )
local value = mw.loadData( [[Module:Hardness values]] )[block]
local value = mw.loadData( 'Module:' .. type .. ' values' )[block]
local category = ''
local category = ''
if not value then
if not value then
value = '[[Template:Hardness values#Missing value|?]]'
value = '[[Template:' .. type .. ' values#Missing value|?]]'
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
if not args.nocat and title.namespace == 0 and not title.isSubpage then
if not args.nocat and title.namespace == 0 and not title.isSubpage then
category = '[[Category:Missing hardness]]'
category = '[[Category:Missing ' .. type:lower() .. ']]'
end
end
end
end

Revision as of 15:41, 23 December 2014

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

local p = {}
p.value = function( f )
	local args = require( 'Module:ProcessArgs' ).norm( f.args or f )
	local block = mw.text.trim( args[1] or '' ):lower()
	local type = args.type

	-- Most of these transforms are unnecessary, but are kept for compatibility with original template
	
	-- Strip trailing "s" on everything but these
	local keepS = {
		glass = true,
		steps = true, stairs = true,
		bars = true,
		cactus = true,
		leaves = true,
		grass = true,
		potatoes = true
	}
	if not keepS[block:match( '%w+$' )] then
		block = block:gsub( 's$', '' )
	end
	
	-- Other transforms
	block = block
		:gsub( 'wooden', 'wood' )
		:gsub( 'mossy', 'moss' )
		:gsub( 'steps', 'stairs' )
		:gsub( "['%(%)%-%s]+", '' )
	
	local value = mw.loadData( 'Module:' .. type .. ' values' )[block]
	local category = ''
	if not value then
		value = '[[Template:' .. type .. ' values#Missing value|?]]'
		local title = mw.title.getCurrentTitle()
		if not args.nocat and title.namespace == 0 and not title.isSubpage then
			category = '[[Category:Missing ' .. type:lower() .. ']]'
		end
	end
	return value .. category
end
return p