Module:Tpp/sandbox

From Wikisource
Jump to navigation Jump to search
local p = {}

--[=[TODO:
	use mw.html since it exists
]=]

local _ppoem = require("Module:Ppoem")._ppoem
local getArgs = require("Module:Arguments").getArgs

function p._tpp(args)
	local res = ""
	args.text = args.text or args[2]
	args.title = args.title or args[1]
	if not args.text then
		args.text = args[1]
		args.title = nil
	end
	args.size = args.size or args.s
	args["end"] = args["end"] or args.e
	args.start = args.start or args.st -- s was taken
	args.quote = args.quote or args.q
	if args.title then
		res = res .. '<div class="wst-center tiInherit">'
		local sizes  = 
		{
			['s']='<span style="font-size: 83%">',
			['smaller']='<span style="font-size: 83%">',
			["l"]='<span style="font-size: 120%">',
			["larger"]='<span style="font-size: 120%">',
			["n"]='<span>',
			["normal"]='<span>'
		}
		local found = false
		for k, v in pairs(sizes) do
			if args.size == k and not found then
				res = res .. v
				found = true
			end
		end
		if not found then
			res = res .. sizes.larger
		end
		res = res .. args.title .. '</span></div><div class="wst-tpp-small-break"><span style="visibility:hidden">&nbsp;</span></div>' -- that div is for spacing after title
	end
	if args.quote then
		res = res .. _ppoem({args.quote}) .. '<div class="wst-tpp-small-break"><span style="visibility:hidden">&nbsp;</span></div>' -- spacing after quote
	end
	if args.text then
		local i, j
		i, j = mw.ustring.find(args.text, '^[ \n\t]*<div class="wst%-dhr[^<]->&nbsp;</div>', 0) --dhr
		if not j then
			i, j = mw.ustring.find(args.text, '^[ \n\t]*<span class="wst%-dhr[^<]->&nbsp;</span>', 0) --dhri
		end
		if j then
			args.text = mw.ustring.sub(arg.text, j+1, #args.text) -- I manually added dhrs after quotes, removing them for backwards compatibility
		end
		local newArgs = {}
		for k, v in pairs(args) do
			if k == "text" or k == "title" or k == 1  or k == "quote" or k == "q" or k == "e" or k == "s" or k == "size" or k == "st" or k == 2 or k == "start" or k == "end" then
				-- do nothing
			else
				newArgs[k] = v -- pass on every other parameter (ppoem's got a lot of them)
			end
		end
		local poems = mw.text.split(args.text, ">><<\n", true)
		for k, v in pairs(poems) do
			newArgs[1] = v
			if k == 1 then
				newArgs.start = args.start
			else
				newArgs.start = nil
			end
			if k == #poems then
				newArgs["end"] = args["end"]
			else
				newArgs["end"] = nil
			end
			res = res .. _ppoem(newArgs)
			if k ~= #poems then
				res = res .. '<div class="wst-dhr" style="visibility:hidden">&nbsp;</div>'
			end
		end
	end

	return res
end

function p.tpp(frame)
	local args = getArgs(frame)
	return p._tpp(args)
end

return p