Module:table of contents: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local export = {}
local export = {}


local function makeUrl(title, queryType, from)
local m_languages = require("Module:languages")
local title = mw.title.getCurrentTitle()
 
local function makeUrl(queryType, target)
if title and title.fullUrl then
if title and title.fullUrl then
if from then
if target then
if queryType then
if queryType then
local query = { [queryType] = from }
local query = {[queryType] = target}
return title:fullUrl(query, "https")
return title:fullUrl(query, "https")
else
else
Line 15: Line 17:
end
end


local function link(title, queryType, from, display, script)
local function link(queryType, target, display, lang_code, script_code)
local url = makeUrl(title, queryType, from)
local url = makeUrl(queryType, target)
if url then
if url then
local ext_link = "[" .. url .. " " .. ( display or from ) .. "]"
local ext_link = "[" .. url .. " " .. (display or target) .. "]"
if script then
ext_link = mw.html.create("span"):wikitext(ext_link)
return '<span class="' .. script .. '">' .. ext_link .. "</span>"
ext_link = lang_code and ext_link:attr("lang", lang_code) or ext_link
else
ext_link = script_code and ext_link:addClass(script_code) or ext_link
return ext_link
return tostring(ext_link)
end
end
end
end
end
Line 29: Line 30:
function export.show(frame)
function export.show(frame)
local output = {}
local output = {}
local queryType
local params = {
local params = {
[1] = { list = true },
[1] = {list = true},
["subcat"] = { type = "boolean" },
["lang"] = {type = "string"},
["subcat"] = {type = "boolean"},
["top"] = {},
["top"] = {},
["CopticDisplay"] = {}, -- for Coptic
}
}
local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
local title = mw.title.getCurrentTitle()
local lang = args.lang and m_languages.getByCode(args.lang) or nil
local froms = args[1]
local targets = args[1]
local display = {}
local displays = {}
local all_linktext = {}
for i, from in pairs(froms) do
for i, char in ipairs(targets) do
if mw.ustring.match(from, ":") then
if char:find(":") then
local from, linktext = mw.ustring.match(from, "([^:]+):(.+)")
local target, display = char:match("([^:]+):(.+)")
if from then
if target then
froms[i] = from
targets[i] = lang and (lang:makeSortKey(target)) or target
display[i] = linktext
displays[i] = display
table.insert(all_linktext, linktext)
else
else
error('Parameter ' .. i .. ' is badly formatted. It should contain a key to use in the link, a colon, and then the displayed text.')
error('Parameter ' .. i .. ' is badly formatted. It should contain a key to use in the link, a colon, and then the displayed text.')
end
end
else
targets[i] = lang and (lang:makeSortKey(char)) or char
displays[i] = char
end
end
end
end
local all_linktext = table.concat(all_linktext)
local script_code
local script = require "Module:scripts".findBestScriptWithoutLang(all_linktext)
if lang then
mw.log(all_linktext, script)
script_code = lang:findBestScript(table.concat(displays)):getCode()
elseif table.concat(displays) ~= "" then
if args.CopticDisplay then
script_code = require("Module:scripts").findBestScriptWithoutLang(table.concat(displays)):getCode()
for character in mw.ustring.gmatch(args.CopticDisplay, ".") do
table.insert(display, character)
end
end
end
local subcat = args.subcat
local subcat, queryType = args.subcat
if subcat then
if subcat then
queryType = "subcatfrom"
queryType = "subcatfrom"
Line 77: Line 75:
if args.top then
if args.top then
local link = link(title, nil, args.top, nil)
local link = link(nil, args.top)
table.insert(output, link)
if targets and targets[1] then
table.insert(output, " – ")
end
end
for i, char in ipairs(targets) do
local link = link(queryType, char, displays[i], lang and lang:getCode() or nil, script_code)
table.insert(output, link)
table.insert(output, link)
end
end
if args.top and froms and froms[1] then
if not lang then
table.insert(output, " ")
table.insert(output, "[[Category:User:Theknightwho/table of contents]]")
end
end
for i, from in ipairs(froms) do
return table.concat(output, " ")
local link = link(title, queryType, from, display[i], script)
end
 
function export.full(frame)
local params = {
[1] = {list = true},
["lang"] = {type = "string", required = true},
["subcat"] = {type = "boolean"}
}
local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
local lang = m_languages.getByCode(args.lang)
local lang_code = lang:getCode()
local targets = args[1]
local displays = {}
table.insert(output, link)
local script_code = lang:findBestScript(table.concat(args[1])):getCode()
local subcat, queryType = args.subcat
if subcat then
queryType = "subcatfrom"
else
queryType = "from"
end
local output = mw.html.create("table")
:attr("id", "toc")
:addClass("toc plainlinks")
:attr("summary", "Contents")
:attr("cellpadding", "5")
:attr("border", "yes")
:css("text-align", "center")
:tag("tr")
:tag("th")
:attr("colspan", #args[1])
:wikitext("Contents (" .. link(nil, "Top") .. ")")
:allDone()
local row = output:tag("tr")
local cell = {}
for i, char in ipairs(args[1]) do
local target = lang:makeSortKey(char)
table.insert(cell, link(queryType, target, char, lang_code, script))
end
cell = row:tag("td"):wikitext(table.concat(cell, " "))
:attr("colspan", #args[1])
for i, char1 in ipairs(args[1]) do
local row = output:tag("tr")
:css("text-align", "center")
for i, char2 in ipairs(args[1]) do
local cell = row:tag("td")
local display = char1 .. char2
if lang:hasDottedDotlessI() then
display = display
:gsub("İ", "i")
:gsub("I", "ı")
end
display = display:ulower()
local target = lang:makeSortKey(display)
cell = cell:wikitext(link(queryType, target, display, lang_code, script))
end
end
local row = output:tag("tr")
local cell = {}
for i = 0, 9 do
local display = tostring(i)
local target = lang:makeSortKey(display)
table.insert(cell, link(queryType, target, display, lang_code, script))
end
end
cell = row:tag("td"):wikitext(table.concat(cell, " "))
:attr("colspan", #args[1])
return table.concat(output, " ")
return tostring(output)
end
end


return export
return export

Revision as of 11:15, 6 August 2024



local export = {}

local m_languages = require("Module:languages")
local title = mw.title.getCurrentTitle()

local function makeUrl(queryType, target)
	if title and title.fullUrl then
		if target then
			if queryType then
				local query = {[queryType] = target}
				return title:fullUrl(query, "https")
			else
				return title:fullUrl(nil, "https")
			end
		end
	end
end

local function link(queryType, target, display, lang_code, script_code)
	local url = makeUrl(queryType, target)
	if url then
		local ext_link = "[" .. url .. " " .. (display or target) .. "]"
		ext_link = mw.html.create("span"):wikitext(ext_link)
		ext_link = lang_code and ext_link:attr("lang", lang_code) or ext_link
		ext_link = script_code and ext_link:addClass(script_code) or ext_link
		return tostring(ext_link)
	end
end

function export.show(frame)
	local output = {}
	
	local params = {
		[1] = {list = true},
		["lang"] = {type = "string"},
		["subcat"] = {type = "boolean"},
		["top"] = {},
	}
	
	local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
	
	local lang = args.lang and m_languages.getByCode(args.lang) or nil
	
	local targets = args[1]
	local displays = {}
	
	for i, char in ipairs(targets) do
		if char:find(":") then
			local target, display = char:match("([^:]+):(.+)")
			if target then
				targets[i] = lang and (lang:makeSortKey(target)) or target
				displays[i] = display
			else
				error('Parameter ' .. i .. ' is badly formatted. It should contain a key to use in the link, a colon, and then the displayed text.')
			end
		else
			targets[i] = lang and (lang:makeSortKey(char)) or char
			displays[i] = char
		end
	end
	
	local script_code
	if lang then
		script_code = lang:findBestScript(table.concat(displays)):getCode()
	elseif table.concat(displays) ~= "" then
		script_code = require("Module:scripts").findBestScriptWithoutLang(table.concat(displays)):getCode()
	end
	
	local subcat, queryType = args.subcat
	if subcat then
		queryType = "subcatfrom"
	else
		queryType = "from"
	end
	
	if args.top then
		local link = link(nil, args.top)
		table.insert(output, link)
		if targets and targets[1] then
			table.insert(output, " – ")
		end
	end
	
	for i, char in ipairs(targets) do
		local link = link(queryType, char, displays[i], lang and lang:getCode() or nil, script_code)
		table.insert(output, link)
	end
	
	if not lang then
		table.insert(output, "[[Category:User:Theknightwho/table of contents]]")
	end
	
	return table.concat(output, " ")
end

function export.full(frame)
	local params = {
		[1] = {list = true},
		["lang"] = {type = "string", required = true},
		["subcat"] = {type = "boolean"}
	}
	
	local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
	
	local lang = m_languages.getByCode(args.lang)
	local lang_code = lang:getCode()
	
	local targets = args[1]
	local displays = {}
		
	local script_code = lang:findBestScript(table.concat(args[1])):getCode()
	
	local subcat, queryType = args.subcat
	if subcat then
		queryType = "subcatfrom"
	else
		queryType = "from"
	end
	
	local output = mw.html.create("table")
		:attr("id", "toc")
		:addClass("toc plainlinks")
		:attr("summary", "Contents")
		:attr("cellpadding", "5")
		:attr("border", "yes")
		:css("text-align", "center")
		:tag("tr")
			:tag("th")
				:attr("colspan", #args[1])
				:wikitext("Contents (" .. link(nil, "Top") .. ")")
				:allDone()
	
	local row = output:tag("tr")
	local cell = {}
	for i, char in ipairs(args[1]) do
		local target = lang:makeSortKey(char)
		table.insert(cell, link(queryType, target, char, lang_code, script))
	end
	cell = row:tag("td"):wikitext(table.concat(cell, " "))
		:attr("colspan", #args[1])
	
	for i, char1 in ipairs(args[1]) do
		local row = output:tag("tr")
			:css("text-align", "center")
		for i, char2 in ipairs(args[1]) do
			local cell = row:tag("td")
			local display = char1 .. char2
			if lang:hasDottedDotlessI() then
				display = display
					:gsub("İ", "i")
					:gsub("I", "ı")
			end
			display = display:ulower()
			local target = lang:makeSortKey(display)
			cell = cell:wikitext(link(queryType, target, display, lang_code, script))
		end
	end
	
	local row = output:tag("tr")
	local cell = {}
	for i = 0, 9 do
		local display = tostring(i)
		local target = lang:makeSortKey(display)
		table.insert(cell, link(queryType, target, display, lang_code, script))
	end
	cell = row:tag("td"):wikitext(table.concat(cell, " "))
		:attr("colspan", #args[1])
	
	return tostring(output)
end

return export