Module:Term: Difference between revisions

From Space Station 14 Wiki
Aliser (talk | contribs)
moved to Module:Term, fixing links
Aliser (talk | contribs)
rewrite to use objects in terms json with multiple props
Line 3: Line 3:


local terms_json = mw.loadJsonData("Module:Term/terms.json")
local terms_json = mw.loadJsonData("Module:Term/terms.json")
local term_links_json = mw.loadJsonData("Module:Term/term links.json")


local function assert_not_nil(value, error_message)
local function assert_not_nil(value, error_message)
Line 17: Line 16:
-- =========================
-- =========================


-- Lookups term link from the json. Return nil if there's no definition for the term.
-- Lookups term from the json. If a term is defined, returns the object defining it, or `nil` otherwise.
local function lookup_term(term)
local function lookup_term(term)
return terms_json[string.lower(term)]
return terms_json[string.lower(term)]
end
-- Lookups term link from the json. Return nil if no link is defined for the term.
local function lookup_term_link(term)
return term_links_json[string.lower(term)]
end
end


Line 33: Line 27:
assert_not_nil(term, "failed to generate term: term not provided")
assert_not_nil(term, "failed to generate term: term not provided")
local term_definition = lookup_term(term)
local term_obj = lookup_term(term)
local term_link = lookup_term_link(term)
if not term_obj then
error("failed to lookup term: term '" .. term .. "' not defined. Make sure the term is defined in the terms JSON file")
end
local term_definition = term_obj["definition"]
local term_link = term_obj["link"]
local term_proper_name = term_obj["proper name"] or term
if term_definition == nil and term_link == nil then
if term_definition == nil and term_link == nil then
error("failed to generate term: no defintiion nor link found for term '" .. term .. "'")
error("failed to generate term: found term in the terms JSON file, but it doesn't have a definition nor a link'" .. term .. "'")
end
end
local term_text_result = term_link == nil and term or "[[" .. term_link .. "|" .. term .. "]]"
local term_wikitext = term_link == nil and term_proper_name or "[[" .. term_link .. "|" .. term_proper_name .. "]]"
if term_definition == nil then
if term_definition == nil then
-- no definition = return just the link
-- no definition = return just the link
return term_text_result
return term_wikitext
else
else
-- yes definition = generate a tooltip
-- yes definition = generate a tooltip
Line 50: Line 50:
title = 'Tooltip',
title = 'Tooltip',
args = {
args = {
term_link == nil and term or "[[" .. term_link .. "|" .. term .. "]]",
term_wikitext,
term_definition
term_definition
}
}

Revision as of 20:44, 14 March 2025

Module documentation
View or edit this documentation (about module documentation)
Uses JSON data
This module uses JSON data pages:

Implements {{Term}}.

Adding new terms

Go to the JSON file linked at the top of this page.

That file contains definitions for terms.

Term is defined with a lowercase key and an object value.

The object value has following format:

{
	"proper name": "[optional] proper term name, for example: LRP",
    "definition": "[optional] term definition",
    "link": "[optional] a page to link the term to",
}

Note that at least a definition or a link should be provided for a term.


local p = {}
local getArgs = require('Module:Arguments').getArgs

local terms_json = mw.loadJsonData("Module:Term/terms.json")

local function assert_not_nil(value, error_message)
    if value == nil then
        if error_message == nil then
            error("value is nil")
        else
            error(error_message)
        end
    end
end

-- =========================

-- Lookups term from the json. If a term is defined, returns the object defining it, or `nil` otherwise.
local function lookup_term(term)
	return terms_json[string.lower(term)]
end

-- Generates a term element with a tooltip and/or a link.
function p.main(frame)
	local args = getArgs(frame)
	local term = args[1]
	assert_not_nil(term, "failed to generate term: term not provided")
	
	local term_obj = lookup_term(term)
	if not term_obj then
		error("failed to lookup term: term '" .. term .. "' not defined. Make sure the term is defined in the terms JSON file")	
	end
	
	local term_definition = term_obj["definition"]
	local term_link = term_obj["link"]
	local term_proper_name = term_obj["proper name"] or term
	
	if term_definition == nil and term_link == nil then
		error("failed to generate term: found term in the terms JSON file, but it doesn't have a definition nor a link'" .. term .. "'")
	end
	
	local term_wikitext = term_link == nil and term_proper_name or "[[" .. term_link .. "|" .. term_proper_name .. "]]"
	
	if term_definition == nil then
		-- no definition = return just the link
		return term_wikitext
	else
		-- yes definition = generate a tooltip
		return frame:expandTemplate{
			title = 'Tooltip',
			args = {
				term_wikitext,
				term_definition
			}
		}
	end
end

return p