模組:劇集列表

local p = {}
local episodes = {}

local titleList = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
local localColSpan = 0
local localRowSpan = 0
local summaryRow = false

function episodes.headerRow(text, style, rowStyle, colspan, rowspan)
	local row = mw.html.create("th")
	
	if text ~= "" then
		localColSpan = localColSpan + 1
		row
		   :attr("colspan", (colspan or "1"))
		   :attr("rowspan", (rowspan or "1"))
		   :cssText("padding:.2em .5em;"..(rowStyle or "")..(style or ""))
		   :wikitext(text)
		return row
	end
	
	return
end

function episodes.baseSummaryRow(text, lineColor)
	local row = mw.html.create("td")
	
	if text ~= "" then
		row:attr("colspan", 30)
		   :css("padding", ".5em")
		   :css("text-align", "left")
		   :css("background", "white")
		   :css("white-space", "normal")
		   :css("border-bottom", ".3em solid "..(lineColor or "#aaa"))
		   :wikitext(text)
		return row
	end

	return
end

function episodes.titleRow(text, subtitle, nativeTitle, rowStyle, style, subtitleStyle, colspan, rowspan)
	local row = mw.html.create("td")
	
	if summaryRow then text = "<strong>"..text.."</strong>" end
	if subtitle ~= "" then text = text .. "<br /><small style=\"color:gray;" ..subtitleStyle.. "\"><em>" ..subtitle.. "</em></small>" end
	if nativeTitle ~= "" then text = text .. "<br />-{(" ..nativeTitle.. ")}-"end
	
	if text ~= "" then
		if summaryRow then rowspan = "1" end
		row:attr("colspan", colspan)
		   :attr("rowspan", (rowspan or "1"))
		   :css("text-align", "left")
		   :css("padding", ".2em .5em")
		   :css("line-height", "1.2")
		   :cssText((rowStyle or "")..(style or ""))
		   :wikitext(text)
		return row
	end
	
	return
end

function episodes.multipleTitleRow(text, subtitle, nativeTitle, style, rowStyle, subtitleStyle, colspan, rowspan)
	local row = mw.html.create("td")

	if summaryRow then text = "<strong>"..text.."</strong>" end
	if subtitle ~= "" then text = text .. "<br /><small style=\"color:gray;" ..subtitleStyle.. "\"><em>" ..subtitle.. "</em></small>" end
	if nativeTitle ~= "" then text = text .. "<br />-{(" ..nativeTitle.. ")}-"end
	
	if text ~= "" then
		if summaryRow then rowspan = "1" end
		row:addClass("Title-EX")
	       :attr("rowspan", (rowspan or "1"))
		   :attr("scope", "row")
		   :css("height", "2em")
		   :css("text-align", "left")
		   :css("padding", ".2em .5em")
		   :css("line-height", "1.2")
		   :cssText((rowStyle or "")..(style or ""))
		   :wikitext(text)
		return row
	end

	return
end

function episodes.chapterRow(text, style, rowStyle, lineColor)
	local row = mw.html.create("th")
	
	if text ~= "" then
		if (lineColor or "") ~= "" then row:css("border-bottom-color", lineColor) end
		row:attr("colspan", (colspan or "25"))
		   :cssText((rowStyle or "")..(style or ""))
		   :wikitext(text)
		return row
	end

	return
end

function episodes.baseRow(tableclass, class, text, style, rowStyle, colspan, rowspan)
	local row = mw.html.create(tableclass or "td")
	
	if text == "TBA" or text == "待公布" then
		text = "待公布"
		style = "background: #DDF; vertical-align: middle;"..(style == nil and style or "")
	elseif text == "TBD" or text == "待定" then
		text = "待定"
		style = "background: #FFD; vertical-align: middle;"..(style == nil and style or "")
	elseif text == "N/A" or text == "不適用" or text == "不适用" or text == "-" then
		if text ~= "-" then text = "不適用" end
		style = "background: var(--background-color-disabled-subtle, #ececec); color: var(--color-disabled, #2c2c2c); vertical-align: middle; font-size: smaller;"..(style == nil and style or "")
	end
	

	if text ~= "" then
		localColSpan = localColSpan + 1
		if summaryRow then rowspan = "1" end
		row
		   :addClass(class or "")
		   :attr("scope", "col")
		   :attr("colspan", (colspan or "1"))
		   :attr("rowspan", (rowspan or tostring(localRowSpan-1)))
		   :css("text-align", "center")
		   :cssText("padding:.2em .5em;"..(rowStyle or "")..(style or ""))
		   :wikitext(text)
		return row
	end

	return ""
end

function episodes.base(args)
	args = args or {}

	local base = mw.html.create("tr")
	local text = "td"
	
	if args.Chapter then
		base:node(episodes.chapterRow(args.Chapter, args.ChapterStyle, args.Style, args.ChapterColSpan))
		return base
	end

	if args.Summary ~= nil and args.Summary ~= "" and mw.title.getCurrentTitle().text ~= args.SublistOf then 
		summaryRow = true
		text = "th"
	end
	
	base
		:node(episodes.baseRow("th", "", (args.Header0 or ""), args.Header0Style, args.Style, args.Header0ColSpan, args.Header0RowSpan))
		:node(episodes.baseRow("th", "", (args.Header1 or ""), args.Header1Style, args.Style, args.Header1ColSpan, args.Header1RowSpan))
	
	if (args["TitleA"] or "") ~= "" then
		localRowSpan = localRowSpan + 1
		for i=1,6 do if (args["Title"..titleList[i]] or "") ~= "" then localRowSpan = localRowSpan + 1 end end
	else localRowSpan = 0 end

	if args["Number"] ~= "!INVISIBLE" then base:node(episodes.baseRow(text, "", args.Number, args.NumberStyle, args.Style, args.NumberColSpan, args.NumberRowSpan)) end
	
	if (args["TitleA"] or "") ~= "" then
		base:node(episodes.multipleTitleRow(args.TitleA, (args.SubtitleA or ""), (args.NativeTitleA or ""), (args.Style or ""), (args.TitleAStyle or ""), (args.SubtitleAStyle or ""), args.TitleAColSpan, args.TitleARowSpan))
	elseif args["Title"] ~= "!INVISIBLE" then
		base:node(episodes.titleRow(args.Title, (args.Subtitle or ""), (args.NativeTitle or ""), (args.TitleStyle or ""), (args.Style or ""), (args.SubtitleType or ""), args.TitleColSpan, args.TitleRowSpan))
	end

	for i=0,19 do
		base:node(episodes.baseRow("td", "", (args["Aux"..i] or ""), args["Aux"..i.."Style"], args.Style, args["Aux"..i.."ColSpan"], args["Aux"..i.."RowSpan"]))
		if mw.title.getCurrentTitle().text ~= args.SublistOf then
			if i == 19 then
				for j=16,19 do
					base:node(episodes.baseRow("td", "", (args["SubAux"..j] or ""), args["SubAux"..j.."Style"], args.Style, args["SubAux"..j.."ColSpan"], args["SubAux"..i.."RowSpan"]))
				end
			elseif i%5 == 0 then
				for j=0+6*((i-5)/5),5+6*((i-5)/5) do
					base:node(episodes.baseRow("td", "", (args["SubAux"..j] or ""), args["SubAux"..j.."Style"], args.Style, args["SubAux"..j.."ColSpan"], args["SubAux"..i.."RowSpan"]))
				end
			end
		end
	end

	if (args["TitleA"] or "") ~= "" then
		for i=2,6 do
			if (args["Title"..titleList[i]] or "") ~= "" then
				local title = "Title"..titleList[i]
				base:tag("tr"):node(episodes.multipleTitleRow(args[title], (args["Sub"..string.lower(title)] or ""), (args["Native"..title] or ""), (args[title.."Style"] or ""), args[title.."ColSpan"], args[title.."RowSpan"]))
			end
		end
	end
	
	if summaryRow then base:tag("tr"):node(episodes.baseSummaryRow(args.Summary, args.LineColor)) end
		
    return base
end

function episodes.header(args)
	args = args or {}
	
	local header = mw.html.create((args.ExcludeTableTag and "tr" or "table"))
	
	if not args.ExcludeTableTag then
		header
		  :addClass("wikitable")
		  :css("border-collapse", "collapse")
		  :css("border-spacing", "0")
		  :cssText(args.TableStyle)
	end
	
	if (args.Caption or "") ~= "" then
		header:tag("caption")
			  :cssText(args.CaptionStyle)
			  :wikitext(args.Caption)
	end
		  
	local headerRow = header:tag("tr")
	
	headerRow
		     :cssText("border-bottom: .3em solid " .. (args.LineColor or "#aaa"))
			 :node(episodes.headerRow((args.Header0 or ""), args.Header0Style, args.Style, args.Header0ColSpan, args.Header0RowSpan))
			 :node(episodes.headerRow((args.Header1 or ""), args.Header1Style, args.Style, args.Header1ColSpan, args.Header1RowSpan))
	if args["Number"] ~= "!INVISIBLE" then
		headerRow:node(episodes.headerRow((args.Number or "#"), args.NumberStyle, args.Style, args.NumberColSpan, args.NumberRowSpan))
	end
	if args["Title"] ~= "!INVISIBLE" then
		headerRow:node(episodes.headerRow((args.Title or "劇集標題"), args.TitleStyle, args.Style, args.TitleColSpan, args.NumberRowSpan))
	end
	for i=0,19 do
		headerRow:node(episodes.headerRow((args["Aux"..i] or ""), args["Aux"..i.."Style"], args.Style, args["Aux"..i.."ColSpan"], args["Aux"..i.."RowSpan"]))
		if mw.title.getCurrentTitle().text ~= args.SublistOf then
			if i == 19 then
				for j=16,19 do
					headerRow:node(episodes.headerRow((args["SubAux"..j] or ""), args["SubAux"..j.."Style"], args.Style, args["SubAux"..j.."ColSpan"], args["SubAux"..j.."RowSpan"]))
				end
			elseif i%5 == 0 then
				for j=0+6*((i-5)/5),5+6*((i-5)/5) do
					headerRow:node(episodes.headerRow((args["SubAux"..j] or ""), args["SubAux"..j.."Style"], args.Style, args["SubAux"..j.."ColSpan"], args["SubAux"..j.."RowSpan"]))
				end
			end
		end
	end
    return tostring(mw.ustring.gsub(tostring(header), "</table>", ""))
end

function p.mainHeader(frame)
    local args = require('Module:Arguments').getArgs(frame, {
		removeBlanks = false,
        wrappers = "Template:劇集列表/base/header"
    })
	return episodes.header(args)
end

function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame, {
		removeBlanks = false,
        wrappers = "Template:劇集列表/base"
    })
	return episodes.base(args)
end

return p