Module:links: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
 
(42 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local export = {}
local export = {}


function export.make_link(args)
function export.language_link(data)
if args["link_type"] ~= "mention" and args["link_type"] ~= "list" then
if not data.term then
error("Link type must be specified")
error("language link data must contain term")
end
end
if not args["code"]  then
local link = data.term
error("Language code must be specified")
local alt = data.alt or data.term
end
local anchor = data.anchor
local word = args["word"]
local display = args["display"]
local plaenk_alt = mw.loadData("Module:lnk-pro-morph/data").alt
local text = args["text"]
local pos = args["pos"]
if data.language then
local language = require("Module:languages").get_by_code(args["code"])
local link = ""
if data.language.code == "lnk-pro" and plaenk_alt[link] then link = plaenk_alt[link] end
if link_type == "mention" then
if type(language.link) == "string" then
if data.hypo then alt = "**" .. alt end
link = link .. "[[" .. language.link .. "|" .. language.name .. "]] "
if data.language.proto then
if not data.nostar and not data.hypo then alt = "*" .. alt end -- * is an asterisk but sometimes the asterisk gets mistaken for wiki formatting
link = "Appendix:" .. data.language.name .. "/" .. data.term
if anchor then link = link .. "#" .. data.language.name .. anchor end
else
else
link = link .. language.name .. " "
local jump = data.language.name
if anchor then jump = data.language.name .. anchor end
link = link .. "#" .. string.gsub(jump, " ", "_")
end
end
end
end
local link_format = (link_type == "mention" and "''") or ""
local link_proto = (language.proto and ("Appendix:" .. string.gsub(language.name, " ", "_") .. "/")) or ""
local term = mw.ustring.match(mw.ustring.gsub(link, " ", " "),"([^%#]+)")
local link_display_proto = (language.proto and "*") or ""
local link_display = display or word
if term == mw.title.getCurrentTitle().text and not data.nobold then
link = link .. link_format .. "[[" .. link_proto .. word .. "#" .. string.gsub(language.name, " ", "_") .. "|" .. link_display_proto .. link_display .. "]]" .. link_format
return "<b>" .. alt .. "</b>"
if type(text) == "string" or type(pos) == "string" then
elseif mw.ustring.match(term, "^[%?%-–—]+$") or mw.ustring.match(term, ":[%?%-–—]+$") or data.nolink then
link = link .. " ("
return alt
if type(text) == "string" then
else
link = link .. "“" .. text .. "”" .. ((type(pos) == "string" and ", ") or "")
return "[[" .. link .. "|" .. alt .. "]]"
end
if type(pos) == "string" then
link = link .. pos
end
link = link .. ")"
end
end
return link
end
end


function export.make_list_link(args)
function export.link_extras(data)
args["link_type"] = "list"
if not (data.pos or data.gloss) then return nil end
return export.make_link(args)
local out = {}
if data.gloss then table.insert(out, '“' .. data.gloss .. '”') end
if data.pos then table.insert(out, data.pos) end
return table.concat(out, ", ")
end
end


function export.make_mention_link(args)
function export.full_link(data, face)
args["link_type"] = "mention"
local link = export.language_link(data)
return export.make_link(args, "mention")
link = require("Module:formatting").wrap_face(link, data.language, face or "nil")
end
local extras = export.link_extras(data)
 
if extras then
local function make_from_frame(frame, link_type)
extras = " (" .. extras .. ")"
local args = (frame:getParent() and frame:getParent().args) or frame.args
end
return export.make_link{
local prefix
["link_type"] = link_type,
if data.language and data.showlanguage then
["code"] = args[1],
prefix = "[[" .. data.language.link .. "|" .. data.language.name .. "]] "
["word"] = args[2],
end
["display"] = args[3] or args["d"] or args["display"],
return (prefix or "") .. link .. (extras or "")
["text"] = args[4] or args["t"] or args["text"],
["pos"] = args["pos"]
}
end
 
function export.list(frame)
return make_from_frame(frame, "list")
end
 
function export.mention(frame)
return make_from_frame(frame, "mention")
end
end


return export
return export

Latest revision as of 16:04, 31 May 2024

Documentation for this module may be created at Module:links/documentation

local export = {}

function export.language_link(data)
	if not data.term then
		error("language link data must contain term")
	end
	local link = data.term
	local alt = data.alt or data.term
	local anchor = data.anchor
	
	local plaenk_alt = mw.loadData("Module:lnk-pro-morph/data").alt
	
	if data.language then
	
		if data.language.code == "lnk-pro" and plaenk_alt[link] then link = plaenk_alt[link] end
		
		if data.hypo then alt = "&#42;&#42;" .. alt end
		
		if data.language.proto then
			if not data.nostar and not data.hypo then alt = "&#42;" .. alt end -- &#42; is an asterisk but sometimes the asterisk gets mistaken for wiki formatting
			link = "Appendix:" .. data.language.name .. "/" .. data.term
			if anchor then link = link .. "#" .. data.language.name .. anchor end
		else
			local jump = data.language.name
			if anchor then jump = data.language.name .. anchor end
			link = link .. "#" .. string.gsub(jump, " ", "_")
		end
		
	end
	
	local term = mw.ustring.match(mw.ustring.gsub(link, "&nbsp;", " "),"([^%#]+)")
	
	if term == mw.title.getCurrentTitle().text and not data.nobold then
		return "<b>" .. alt .. "</b>"
	elseif mw.ustring.match(term, "^[%?%-–—]+$") or mw.ustring.match(term, ":[%?%-–—]+$") or data.nolink then
		return alt
	else
		return "[[" .. link .. "|" .. alt .. "]]"
	end
end

function export.link_extras(data)
	if not (data.pos or data.gloss) then return nil end
	local out = {}
	if data.gloss then table.insert(out, '“' .. data.gloss .. '”') end
	if data.pos then table.insert(out, data.pos) end
	return table.concat(out, ", ")
end

function export.full_link(data, face)
	local link = export.language_link(data)
	link = require("Module:formatting").wrap_face(link, data.language, face or "nil")
	local extras = export.link_extras(data)
	if extras then
		extras = " (" .. extras .. ")"
	end
	local prefix
	if data.language and data.showlanguage then
		prefix = "[[" .. data.language.link .. "|" .. data.language.name .. "]] "
	end
	return (prefix or "") .. link .. (extras or "")
end

return export