Module:glossary: Difference between revisions

From Linguifex
Jump to navigation Jump to search
(Created page with "local export = {} local gsub = mw.ustring.gsub function format_def (term, definition) local anchor = gsub(term, "%[%[([^%]]+)%]%]", "%1") -- Remove wikilinks anchor = gsub...")
 
No edit summary
Line 26: Line 26:
function export.link(frame)
function export.link(frame)
local args = frame:getParent().args
local args = frame:getParent().args
for k, v in pairs(args) do
if not (k == 1 or k == 2) then
require "Module:debug".track "glossary/invalid argument"
require "Module:debug".track("glossary/invalid argument/" .. k)
mw.log("invalid argument in {{glossary}}: " .. k)
end
end
local anchor, text = args[1] or "", args[2]
local anchor, text = args[1] or "", args[2]
Line 42: Line 35:
local lower_anchor = anchor:lower()
local lower_anchor = anchor:lower()
local data = mw.loadData("Module:glossary/data")
return "[[wikt:Appendix:Glossary#" .. anchor .. "|" .. (text or anchor) .. "]]"
if data[anchor] then
return "[[wikt:Appendix:Glossary#" .. anchor .. "|" .. (text or anchor) .. "]]"
else
local link = "[[wikt:Appendix:Glossary#" .. lower_anchor .. "|" .. (text or anchor) .. "]]"
if data[lower_anchor] or lower_anchor:find "^%s*$" then
return link
else
mw.log("The anchor " .. lower_anchor
.. (lower_anchor ~= anchor and " or " .. anchor or "")
.. " does not exist in Appendix:Glossary.")
return link
end
end
end
end


return export
return export

Revision as of 16:33, 24 March 2021



local export = {}

local gsub = mw.ustring.gsub

function format_def (term, definition)
	local anchor = gsub(term, "%[%[([^%]]+)%]%]", "%1") -- Remove wikilinks
	anchor = gsub(anchor, "^%w+:", "") -- Remove interwiki prefixes
	
	return "; <span id=\""..anchor.."\">"..term.."</span>\n: "..definition
end

function export.def (frame)
	local params = {
		[1] = { required = true, default = "", },
		[2] = { required = true, default = "", },
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local term = args[1]
	local definition = args[2]
	
	return format_def (term, definition)
end

function export.link(frame)
	local args = frame:getParent().args
	
	local anchor, text = args[1] or "", args[2]
	if text and text:match "^%s*$" then
		text = nil
	end
	
	-- This won't work if the initial letter is non-ASCII.
	local lower_anchor = anchor:lower()
	
	return "[[wikt:Appendix:Glossary#" .. anchor .. "|" .. (text or anchor) .. "]]"
end

return export