Module:Tpp

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 = ""
	-- if not args.text and not args[2] then args[1] is assumed to be the text, but args.title will always be the title.
	args.text = args.text or args[2]
	local notitle = false
	local notext = false
	if not args.text then
		notitle = true
		args.text = args[1]
	end
	if not args.text then
		notext = true
	end
	if not notitle then
		args.title = args.title or args[1]
	end
	args["end"] = args["end"] or args.e
	args.start = args.start or args.st -- s used to be taken
	args.quote = args.quote or args.q
	if args.title and args.title ~= "" then
		res = res .. '<div class="ws-poem-tpp-title">' .. args.title .. '</div>'
	end
	if args.quote then
		res = res ..'<div style="margin-bottom:1em">' .. _ppoem({args.quote}) .. '</div>'
	end
	if args.text then
		if args.quote 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(args.text, j+1, #args.text) -- I manually added dhrs after quotes, removing them for backwards compatibility
			end
		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"  or k == "style" 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
			local marginleft = 0
			local t = mw.text.split(v, "\n", true)
			local parsed = {}
			for a, b in pairs(t) do
				if string.sub(b, 1, 2) == "^>" and #parsed > 1 then
					table.insert(parsed, '<span style="visibility:hidden">' .. parsed[#parsed-1] .. '</span>' .. string.sub(b, 3, #b))
				else if string.sub(b, 1, 2) == "+>" and #parsed > 0 then
					table.insert(parsed, '<span style="visibility:hidden">' .. parsed[#parsed] .. '</span>' .. string.sub(b, 3, #b))
				else if string.sub(b, 1, 1) == "+" and string.sub(b, 3, 3) == ">" and tonumber(string.sub(b, 2, 2)) and #parsed > tonumber(string.sub(b, 2, 2)) then
					table.insert(parsed, '<span style="visibility:hidden">' .. parsed[#parsed-tonumber(string.sub(b, 2, 2))] .. '</span>' .. string.sub(b, 4, #b))
				else 
					local i, j = mw.ustring.find(b, "^=+$")
					if j then
						table.insert(parsed, "")
						for i=1,#b-2,1 do
							table.insert(parsed, '<span style="visibility:hidden>&nbsp;</span>')
						end
						table.insert(parsed, "")
					else
						local i, j = mw.ustring.find(b, '^;+', 0)
						if j then
							table.insert(parsed, '<span style="margin-left:-' .. tostring(j) .. 'em">' ..  mw.ustring.sub(b, j+1, #b) .. "</span>")
							marginleft = j
						else
							table.insert(parsed, b)
						end
					end
				end
				end 
				end
			end
			newArgs[1] = table.concat(parsed, "\n")
			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
			if (not args.style or not mw.ustring.find(args.style, "padding-left", 0)) and marginleft ~= 0 then
				newArgs.style = "padding-left:" .. tostring(marginleft) .. "em"
			else
				newArgs.style = args.style
			end
			res = res .. _ppoem(newArgs)
			if k ~= #poems then
				res = res .. '<div style="margin-bottom:1em"></div>'
			end
		end
	end

	return res
end

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

return p