Module:File

From Splatoon Fanon
Revision as of 15:01, 20 August 2025 by Lakelimbo (talk | contribs) (Created page with "--[[ This is an updated version of Inkipedia's File module, less bloated and easier to add features if needed. In fact, the way it works might as well be used as a base/generic module for other things. ]]-- local license = require("Module:File/Licenses") local f = {} local container = mw.html .create("table") :tag("tr") :tag("th") :attr({ ["colspan"] = 2 }) :css({ ["text-align"] = "center" }) :wikitext("File information") :done() :done() ---Will conv...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:File/doc

--[[
	This is an updated version of Inkipedia's File module, less bloated
	and easier to add features if needed.
	In fact, the way it works might as well be used as a base/generic module for
	other things.
]]--

local license = require("Module:File/Licenses")

local f = {}
local container = mw.html
	.create("table")
	:tag("tr")
	:tag("th")
	:attr({
		["colspan"] = 2
	})
	:css({
		["text-align"] = "center"
	})
	:wikitext("File information")
	:done()
	:done()

---Will convert a string with some delimiter into an iterable array,
---trimming spaces as well.
---The default delimiter is a comma (`",%s*"`), but it's possible to
---change to something else.
---
---Examples:
---		"foo,bar,baz"			-> { "foo", "bar", "baz" }
---		"green, Yellow   ,Blue"	-> { "green", "yellow", "blue" }
---
---@param str string
---@param delimiter string
---@return string[]
local function array_from_string(str, delimiter)
  delimiter = delimiter or ",%s*"
  
  if not str or #str == 0 then
    return nil
  end

  local result = {}
  local parts = mw.text.split(str, delimiter)
  
  for _, s in ipairs(parts) do
    table.insert(result, mw.text.trim(s))
  end

  return result
end

---Creates a row for an HTML table.
---Alongside with `rows.list()` down below, if it notices it's a one-dimensional
---table (array), likely from `array_from_string()`, it will split each element
---and add an appropriate space and comma to it.
---
---@param name string
---@content string | string[]
---@return table
local function table_row(name, content)
	local row = container
		:tag("tr")
		:tag("th")
		:wikitext(name)
		:done()
		:tag("td")

	if type(content) == "table" then
		for k, v in ipairs(content) do
			if k == #content then
				row:wikitext(v)
			else
				row:wikitext(v .. ", ")
			end
		end
	else
		row:wikitext(content)
	end

	row:done()
	return row
end

---Error message for some elements, if needed.
---
---@type message
---@return string
local function error_message(message)
	local span = mw.html.create("span")
		:css({
			["color"] = "red",
		})
		:wikitext(message)
		:done()
		
	return tostring(span)
end

---The list of elements to create a row.
---You can add any custom items with your own logic, as long as it returns
---`table_row()`.
---Even though on MW it's not necessary, it's good to adapt the parameter types
---if you happen to need something different other than args that have string
---values (e.g: `table<string, string | number>`).
---
---@type table<string, fun(content: string, args: table<string, string>)>
---@return table
local rows = {
    list = function(content, args)
        local lists = array_from_string(content)
        
        if not lists or #lists == 0 then
            return table_row(args.name, error_message(args.error))
        end
        
        for _, list in ipairs(lists) do
            container:wikitext("[[Category:" .. list .. " files]]")
        end
        
        return table_row(args.name, lists)
    end,
	text = function(content, args)
		local error = args.error or "Not provided"
		
		if not content or content == "" then
			content = "''" .. error .. "''"
		end
		
		table_row(args.name, content)
	end,
}

function f.main(frame)
	local param = frame.args
	
    rows.list(param.work, { name = "Work", error = "ERROR: invalid or empty work" })
    rows.text(param.description, { name ="Description" })
    rows.list(param.author, { name = "Author", error = "ERROR: invalid author" })
    rows.list(param.characters, { name = "Characters", error = "ERROR: invalid character" })
    rows.text(param.source, { name = "Source" })
    rows.text(license.get_license(param.license), { name = "License" })
    
    return tostring(container)
end

return f