Module:Term: Difference between revisions
From Space Station 14 Wiki
Created page with "local p = {} local getArgs = require('Module:Arguments').getArgs local terms_json = mw.loadJsonData("Module:Terms/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 local function lookup_term(term) return terms_json[string.lower(term)] end function p.main(frame) local args = getArgs(frame..." |
No edit summary |
||
Line 3: | Line 3: | ||
local terms_json = mw.loadJsonData("Module:Terms/terms.json") | local terms_json = mw.loadJsonData("Module:Terms/terms.json") | ||
local term_links_json = mw.loadJsonData("Module:Terms/term links.json") | |||
local function assert_not_nil(value, error_message) | local function assert_not_nil(value, error_message) | ||
Line 14: | Line 15: | ||
end | end | ||
-- ========================= | |||
-- Lookups term link from the json. Return nil if there's no definition for the term. | |||
local function lookup_term(term) | local function lookup_term(term) | ||
return terms_json[string.lower(term)] | return terms_json[string.lower(term)] | ||
end | 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 | |||
-- Generates a term element with a tooltip and/or a link. | |||
function p.main(frame) | function p.main(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local term = args[1] | local term = args[1] | ||
assert_not_nil(term, "failed to | assert_not_nil(term, "failed to generate term: term not provided") | ||
local term_definition = lookup_term(term) | |||
local term_link = lookup_term_link(term) | |||
if term_definition == nil and term_link == nil then | |||
error("failed to generate term: no defintiion nor link found for term '" .. term .. "'") | |||
end | |||
local term_text_result = term_link == nil and term or "[[" .. term_link .. "|" .. term .. "]]" | |||
return | if term_definition == nil then | ||
-- no definition = return just the link | |||
return term_text_result | |||
else | |||
-- yes definition = generate a tooltip | |||
return frame:expandTemplate{ | |||
title = 'Tooltip', | |||
args = { | |||
term_link == nil and term or "[[" .. term_link .. "|" .. term .. "]]", | |||
term_definition | |||
} | |||
} | |||
end | |||
end | end | ||
return p | return p |
Revision as of 03:46, 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:Terms/terms.json")
local term_links_json = mw.loadJsonData("Module:Terms/term links.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 link from the json. Return nil if there's no definition for the term.
local function lookup_term(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
-- 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_definition = lookup_term(term)
local term_link = lookup_term_link(term)
if term_definition == nil and term_link == nil then
error("failed to generate term: no defintiion nor link found for term '" .. term .. "'")
end
local term_text_result = term_link == nil and term or "[[" .. term_link .. "|" .. term .. "]]"
if term_definition == nil then
-- no definition = return just the link
return term_text_result
else
-- yes definition = generate a tooltip
return frame:expandTemplate{
title = 'Tooltip',
args = {
term_link == nil and term or "[[" .. term_link .. "|" .. term .. "]]",
term_definition
}
}
end
end
return p