Module:Show RandomSelection options

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Documentation for this module may be created at Module:Show RandomSelection options/doc

local p = {}

local getArgs = require('Module:Arguments')

local function getArgs(frame)
	-- Fetches the arguments from the parent frame. Whitespace is trimmed and
	-- blanks are removed.
	mArguments = require('Module:Arguments')
	return mArguments.getArgs(frame, {parentOnly = true})
end

function p.main(frame)

	local args = getArgs(frame)
	local template = args[1]
	local mode = args['mode'] or 'list'
	local pre = args['pre'] or false
	
	if template ~= nil then
		-- analogous to {{msgnw:{{{1}}}}}
		local str = mw.ustring.gsub(mw.text.decode(frame:preprocess { text = '{{msgnw:' .. template .. '}}' }), '<noinclude>.-</noinclude>', '')
		
		if mode == 'list' then
			-- make <choose>...</choose> into divided columns
			str = mw.ustring.gsub(str, '.-<choose.->%s*(.-)%s*</choose>.*', '\n{{div col}}\n%1\n{{div col end}}')
			-- replace <option>...</option> with bullets
			str = mw.ustring.gsub(str, '%s*<option.->%s*(.-)%s*</option>%s*', '\n* %1')
		elseif mode == 'gallery' then
			-- make <choose>...</choose> into <gallery>...</gallery>
			str = mw.ustring.gsub(str, '.-<choose.->%s*(.-)%s*</choose>.*', '\n<gallery>%1</gallery>')
			-- replace <option>...</option> with images
			str = mw.ustring.gsub(str, '%s*<option.->.-%[+%s*([^]|]*).-</option>%s*', '%1\n')
		elseif mode == 'table' then
			-- make <choose>...</choose> into table
			str = mw.ustring.gsub(str, '.-<choose.->%s*(.-)%s*</choose>.*', '\n{| class="wikitable"\n! Weight\n! Result%1\n|}')
			-- replace <option>...</option> with table cell
			str = mw.ustring.gsub(str, '%s*<option weight="?(%d+)"?>%s*(.-)%s*</option>%s*', '\n|-\n| %1 || %2\n')
			str = mw.ustring.gsub(str, '%s*<option.->%s*(.-)%s*</option>%s*', '\n|-\n| 1 || %1\n')
		elseif mode == 'raw' then
			-- unwrap <choose>...</choose>
			str = mw.ustring.gsub(str, '</?choose.->', '')
			-- unwrap <option>...</option>
			str = mw.ustring.gsub(str, '</?option.->', '')
		end
		-- render
		if pre == 'true' then
			return frame:preprocess { text = '<pre>' .. str .. '</pre>' }
		else
			return frame:preprocess { text = str }
		end
		
	end
end

return p