Jump to content

Module:Sandbox/Izno

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 02:00, 18 May 2022 (escape box-shadow). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p.file() --frame
	
	local image = 'File:US 730.svg'
	local title = mw.title.new(image)
	
	local width = title.file.width
	local height = title.file.height
	
	return string.format('%s width: %d, height: %d', image, width, height)
end

function p.basic_iteration(frame)
	
	local pv = require('Module:If preview')
	local t = { 'a', 'b', 'c', 'd', }
	local t2 = { a = 'a', b = 'b', c = 'c', d = 'd' }
	local preview = ''
	for _, v in ipairs(t) do -- guarantees ordering
		preview = preview .. pv._warning({
			v
		})
	end
	for _, v in pairs(t2) do -- doesn't guarantee ordering
		preview = preview .. pv._warning({
			v
		})
	end
	
	return preview
end

local function has_navbar()
	return true
end

function p.addListStyles()
	local frame = mw.getCurrentFrame()
	-- TODO?: Should maybe take a table of classes for e.g. hnum, hwrap as above
	-- I'm going to do the stupid thing first though
	-- Also not sure hnum and hwrap are going to live in the same TemplateStyles
	-- as hlist
	local function _addListStyles(htmlclass, templatestyles)
		local class_args = { -- rough order of probability of use
			'bodyclass', 'listclass', 'aboveclass', 'belowclass', 'titleclass',
			'navboxclass', 'groupclass', 'titlegroupclass', 'imageclass'
		}
		local patterns = {
			'^' .. htmlclass .. '$',
			'%s' .. htmlclass .. '$',
			'^' .. htmlclass .. '%s',
			'%s' .. htmlclass .. '%s'
		}
		
		local found = false
		for _, arg in ipairs(class_args) do
			for _, pattern in ipairs(patterns) do
				if mw.ustring.find(frame.args[arg] or '', pattern) then
					found = true
					break
				end
			end
			if found then break end
		end
		if found then
			return frame:extensionTag{
				name = 'templatestyles', args = { src = templatestyles }
			}
		else
			return ''
		end
	end
	
	local hlist_styles = ''
	if not has_navbar() then
		hlist_styles = _addListStyles('hlist', 'Flatlist/styles.css')
	end
	local plainlist_styles = _addListStyles('plainlist', 'Plainlist/styles.css')
	
	return hlist_styles .. plainlist_styles
end

local msg = mw.message.newRawMessage

function p.message(frame)
	local messages = {}
	table.insert(messages, 'This is an inserted message')
	local msg1 = msg('This is a $1 message.', 'raw')
	local msg2 = msg('This is a $1 message of $2 quality.', '[[raw]]', '{{icon|fa}}')
	local msg3 = msg('This is a $1 message of $2 quality.', '[[raw]]', '{{icon|fa}}'):plain()
	if frame.args[1] then
		table.insert(messages, frame.args[1])
	end
	
	return -- tostring(messages) .. '\n\n' .. -- this prints string 'table'
		 table.concat(messages) .. tostring(msg1) .. '\n\n' .. tostring(msg2) .. '\n\n' .. tostring(msg3)
end

function p.output()
	local obj = mw.html.create()
	obj:wikitext(p.addListStyles())
	return obj
end

function p.tostringnil()
	return tostring(nil)
end

function p.remove_disallowed_css(s)
	
	local disallowed_css = {
		'border', -- border
		'border%-.+', -- border subproperties
		'background', -- background
		'background%-.+', -- background subproperties
		'box%-shadow', -- box-shadow
		'padding', -- padding
		'padding%-.+', -- padding subproperties
		
	}
	
	local split_rules = mw.text.split(s, ';')
	local super_split = {}
	for k, v in ipairs(split_rules) do
		split_rules[k] = mw.text.trim(v)
		super_split[k] = mw.text.split(v, ':')
	end
	for k, t in ipairs(super_split) do
		for _, v in ipairs(disallowed_css) do
			if string.match(mw.text.trim(t[1]), v) then
				table.remove(split_rules, k)
				break -- because we are done with the kth table
			end
		end
	end

	return table.concat(split_rules, ';')
end

return p