มอดูล:Storm categories/demo

--
-- This module handles demonstration and list of possible values for all storm
-- category templates! Please test this module on มอดูล:Storm categories/demo/doc
-- before publishing to avoid errros. Thank you!
--
local colorRatio = require("มอดูล:Color contrast")._ratio
local TableTools = require("มอดูล:TableTools")
local stormcats = require("มอดูล:Storm categories")
local cats = require("มอดูล:Storm categories/categories").cats
local colors = require("มอดูล:Storm categories/colors").colors
local icons = require("มอดูล:Storm categories/icons").icons
local p = {}
local customKeys = {}
for k, v in pairs(colors) do
	table.insert(customKeys, k)
end
for k, v in pairs(icons) do
	table.insert(customKeys, k)
end
TableTools.removeDuplicates(customKeys)
function tableEmpty(_table)
	for k, v in pairs(_table) do
		return false
	end
	return true
end
--- Generates and renderds the demo table.
-- @param frame The Scribunto frame.
function p.demo(frame)
	local plain = (frame.args"plain" or frame:getParent().args"plain" or "") ~= ""
	local verbose = (frame.args"verbose" or frame:getParent().args"verbose" or "") ~= ""
	
	local legend = setmetatable({}, { refgroupname = "" })
	local errors = setmetatable({}, { refgroupname = "E" })
	local warnings = setmetatable({}, { refgroupname = "W" })
	
	--- Builds the entire reflist
	-- @param _group The group to build for. Uses `legend` by default.
	function buildReflist(_group)
		group = _group or legend
		return frame:expandTemplate{ title = 'reflist', args = {
			group = getmetatable(group)"refgroupname"
		} }
	end
	
	--- Returns a single 
				name = 'ref',
				content = mw.ustring.gsub(details, "%%File:(+)^%*%%", ":File:%1"),
				args = { 
					name = hash, 
					group = getmetatable(group)"refgroupname"
				}
			}
		end
		
		-- 
		return plain and "" or frame:extensionTag{ name = 'ref', args = { 
			name = hash,
			group = getmetatable(group)"refgroupname"
		} }
	end
	
	--- Checks for color contrast issues and tags accordingly.
	-- Returns the refernce tag, so this should be used in conjunction with an
	-- HTML node's :wikitext function.
	function contrastCheck(color)
		local catColorBlackRatio = colorRatio({ "#" .. color, "black" })
		local catColorLinkRatio = colorRatio({ "#" .. color, "#0645ad" })
		local catColorVisitedLinkRatio = colorRatio({ "#" .. color, "#0b0080" })
		
		local finalWikitext = ""
		if catColorBlackRatio == "?" or catColorLinkRatio == "?" or catColorVisitedLinkRatio == "?" then
			finalWikitext = finalWikitext ..
				createLegend("This color must be a hexadecimal color.", errors)
		else
			if catColorBlackRatio = 7 and aaa or (contrast >= 4.5 and aa or nc)
		end
		
		return mw.html.create("td")
			:attr("data-sort-value", math.min(catColorBlackRatio))
			:wikitext(
				"#" .. color .. (extra or "") .. contrastCheck(color) .. (verbose and (""
				.. tostring(
					mw.html.create("abbr")
						:attr("title", "Contrast to black")
						:wikitext("CTB")
				) .. ": " .. string.format("%.2f", catColorBlackRatio) .. " (" .. contrastLevel(catColorBlackRatio) .. ")"
				.. tostring(
					mw.html.create("abbr")
						:attr("title", "Contrast to links")
						:wikitext("CTL")
				) .. ": " .. string.format("%.2f", catColorLinkRatio) .. " (" .. contrastLevel(catColorLinkRatio) .. ")"
				.. tostring(
					mw.html.create("abbr")
						:attr("title", "Contrast to visited links")
						:wikitext("CTVL")
				) .. ": " .. string.format("%.2f", catColorVisitedLinkRatio) .. " (" .. contrastLevel(catColorVisitedLinkRatio) .. ")") or "")
			)
	end
	
	local categoryTable = mw.html.create("table")
		:addClass("wikitable")
		:addClass("sortable")
		:attr("style", "width: 100%")
		
	categoryTable
		:node(
			mw.html.create("tr")
				:node(mw.html.create("th"):wikitext("Icon")
					:attr("class", "unsortable")
					:attr("rowspan", "2")
					:css("width", "0"))
				:node(mw.html.create("th"):wikitext("ID")
					:attr("rowspan", "2"))
				:node(mw.html.create("th"):wikitext("Name")
					:attr("colspan", "2"))
				:node(mw.html.create("th"):wikitext("Color")
					:attr("rowspan", "2")
					:attr("colspan", "2"))
				:node(mw.html.create("th"):wikitext("Sortkey")
					:attr("colspan", "2"))
		):node(
			mw.html.create("tr")
				:node(mw.html.create("th"):wikitext("Basin"))
				:node(mw.html.create("th"):wikitext("Name"))
				:node(mw.html.create("th"):wikitext("Basin"))
				:node(mw.html.create("th"):wikitext("Sortkey")
					:attr("data-sort-type", "number"))
		)
	
	for name, cat in TableTools.sortedPairs(cats) do
		local rows = { mw.html.create("tr") }
		local row = rows1
		
		local actualIcon = stormcats._icon(name)
		local icon = mw.html.create("td")
			:wikitext(actualIcon)
		if cat"icon" ~= nil and actualIcon ~= cat"icon" then
			icon:wikitext(
				createLegend("Overriden from original icon (" .. cat"icon" .. ")")
			)
		end
			
		local id = mw.html.create("td")
			:wikitext(name)
		local actualColor = stormcats._color(name)
		local colorPreview = mw.html.create("td")
			:attr("style", "background-color: #" .. actualColor .. "; padding: 0; width: 1.8em")
		local color = colorInfo(
			actualColor,
			verbose,
			actualColor ~= cat"color" and createLegend(
				"Overriden from original color ( #"
				.. cat"color"
				.. ")"
			) or ""
		):css("width", "0"):css("white-space", "nowrap")
		
		local sortkeyCategory = mw.html.create("td")
			:attr("data-sort-value", cat"sortkey")
		local sortkey = mw.html.create("td")
			:attr("data-sort-value", cat"sortkey")
			:wikitext(cat"sortkey")
			
		if cat"sortkey" N/A"
					))
				:node(mw.html.create("td"):wikitext(name))
			
			local color = stormcats._color(name, true)
			-- Add more conditions eventually
			if color ~= nil then
				row
					:node(mw.html.create("td")
						:attr("colspan", "2")
						:wikitext("not available"))
					:node(mw.html.create("td")
						:attr("style", "background-color: #" .. color .. "; padding: 0; width: 1.8em"))
					:node(colorInfo(color, verbose))
						:css("width", "0")
						:css("white-space", "nowrap")
					:node(mw.html.create("td")
						:attr("colspan", "2")
						:wikitext("not available"))
			else
				row
					:node(mw.html.create("td")
						:attr("colspan", "6")
						:wikitext("not available"))
			end
			categoryTable:node(row)
		end
	end
	
	out = ""
	if not plain and not tableEmpty(errors) then
		out = out
			.. tostring(mw.html.create("h4"):wikitext("Error"))
		    .. tostring(mw.html.create("p"):wikitext("This table contains errors than need to be addressed immediately, as it may cause errors on a large amount of pages."))
		    .. buildReflist(errors)
	end
	if not plain and not tableEmpty(legend) then
		out = out
			.. tostring(mw.html.create("h4"):wikitext("ตำนาน"))
		    .. buildReflist(legend)
	end
	out = out .. tostring(categoryTable)
	if not plain and not tableEmpty(warnings) then
		out = out
			.. tostring(mw.html.create("h4"):wikitext("คำเตือน"))
		    .. tostring(mw.html.create("p"):wikitext("ตารางนี้ประกอบด้วยคำเตือนที่ควรได้รับการแก้ไข โปรดทราบว่าคำเตือนบางข้อไม่สามารถแก้ไขให้ครบถ้วนได้หากไม่มีการเปลี่ยนแปลงที่ต้องได้รับความเห็นชอบจากทั้งสองฝ่าย."))
		    .. buildReflist(warnings)
	end
	
	return mw.text.trim(out)
end
return p