Module:form of/functions: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
--[=[ | --[=[ | ||
DISPLAY_HANDLERS is a list of one or more functions that provide special | DISPLAY_HANDLERS is a list of one or more functions that provide special handling for multipart tags. Each function | ||
handling for multipart tags. Each function takes formerly a single argument | takes formerly a single argument (the multipart tag), now temporarily two arguments (the multipart tag and the join | ||
(the multipart tag), now temporarily two arguments (the multipart tag and | strategy, which can be nil to use the default), and should either return the formatted display text or nil to check the | ||
the join strategy, which can be nil to use the default), and should either | next handler. If no handlers apply, there is a default handler that appropriately formats most multipart tags. | ||
return the formatted display text or nil to check the next handler. If no | |||
handlers apply, there is a default handler that appropriately formats most | |||
multipart tags. | |||
CAT_FUNCTIONS is a map from function names to functions of a single argument, | CAT_FUNCTIONS is a map from function names to functions of a single argument, as described in [[Module:form of/cats]]. | ||
as described in [[Module:form of/cats]]. There are two types of functions: | There are two types of functions: condition handlers (which return true or false) and spec handlers (which return a | ||
condition handlers (which return true or false) and spec handlers (which | specification, as described in [[Module:form of/cats]]). We need to put the functions here rather than in | ||
return a specification, as described in [[Module:form of/cats]]). We need to | [[Module:form of/cats]] because that module is loaded using mw.loadData(), which can't directly handle functions. | ||
put the functions here rather than in [[Module:form of/cats]] because that | |||
module is loaded using mw.loadData(), which can't directly handle functions. | |||
]=] | ]=] | ||
| Line 27: | Line 22: | ||
local function wrap_in_inflection_of_sep(text) | local function wrap_in_inflection_of_sep(text) | ||
return '<span>' .. text .. '</span>' | return '<span class="inflection-of-sep">' .. text .. '</span>' | ||
end | end | ||
| Line 42: | Line 37: | ||
return seq[1] .. wrap_in_inflection_of_sep(" and ") .. seq[2] | return seq[1] .. wrap_in_inflection_of_sep(" and ") .. seq[2] | ||
else | else | ||
local comma = '<span>,</span>' | local comma = '<span class="serial-comma">,</span>' | ||
local conj = '<span> and </span>' | local conj = '<span class="serial-and"> and </span>' | ||
return table.concat(seq, wrap_in_inflection_of_sep(", "), 1, length - 1) .. | return table.concat(seq, wrap_in_inflection_of_sep(", "), 1, length - 1) .. | ||
wrap_in_inflection_of_sep(comma .. conj) .. seq[length] | wrap_in_inflection_of_sep(comma .. conj) .. seq[length] | ||
| Line 72: | Line 67: | ||
error("Unrecognized multipart join strategy: " .. strategy) | error("Unrecognized multipart join strategy: " .. strategy) | ||
end | end | ||
return '<span>' .. retval .. '</span>' | return '<span class="inflection-of-conjoined">' .. retval .. '</span>' | ||
end | end | ||
| Line 93: | Line 88: | ||
strategy == "and" and "-]]" or "]]" | strategy == "and" and "-]]" or "]]" | ||
if tag == "first-person" then | if tag == "first-person" then | ||
table.insert(els, "[[Appendix:Glossary#first person|first" .. suffix) | table.insert(els, "[[wikt:Appendix:Glossary#first person|first" .. suffix) | ||
elseif tag == "second-person" then | elseif tag == "second-person" then | ||
table.insert(els, "[[Appendix:Glossary#second person|second" .. suffix) | table.insert(els, "[[wikt:Appendix:Glossary#second person|second" .. suffix) | ||
elseif tag == "third-person" then | elseif tag == "third-person" then | ||
table.insert(els, "[[Appendix:Glossary#third person|third" .. suffix) | table.insert(els, "[[wikt:Appendix:Glossary#third person|third" .. suffix) | ||
else | else | ||
return nil | return nil | ||
| Line 107: | Line 102: | ||
return export | return export | ||
Latest revision as of 13:19, 12 January 2025
- The following documentation is located at Module:form of/functions/doc.[edit]
- Useful links: root page • root page's subpages • links • transclusions • testcases • sandbox
--[=[
DISPLAY_HANDLERS is a list of one or more functions that provide special handling for multipart tags. Each function
takes formerly a single argument (the multipart tag), now temporarily two arguments (the multipart tag and the join
strategy, which can be nil to use the default), and should either return the formatted display text or nil to check the
next handler. If no handlers apply, there is a default handler that appropriately formats most multipart tags.
CAT_FUNCTIONS is a map from function names to functions of a single argument, as described in [[Module:form of/cats]].
There are two types of functions: condition handlers (which return true or false) and spec handlers (which return a
specification, as described in [[Module:form of/cats]]). We need to put the functions here rather than in
[[Module:form of/cats]] because that module is loaded using mw.loadData(), which can't directly handle functions.
]=]
local export = {}
function export.multipart_join_strategy()
-- Recognized values:
-- "and": Join with "and", using commas for 3 or more items
-- "en-dash": Join with – (en-dash)
-- "slash": Join with / (slash)
return "slash"
end
local function wrap_in_inflection_of_sep(text)
return '<span class="inflection-of-sep">' .. text .. '</span>'
end
-- This is a local version of serialCommaJoin from [[Module:table]] that
-- implements it a bit differently.
local function join_multiparts_with_serial_comma(seq)
local length = #seq
if length == 0 then
return ""
elseif length == 1 then
return seq[1] -- nothing to join
elseif length == 2 then
return seq[1] .. wrap_in_inflection_of_sep(" and ") .. seq[2]
else
local comma = '<span class="serial-comma">,</span>'
local conj = '<span class="serial-and"> and </span>'
return table.concat(seq, wrap_in_inflection_of_sep(", "), 1, length - 1) ..
wrap_in_inflection_of_sep(comma .. conj) .. seq[length]
end
end
function export.join_multiparts(parts, joiner)
-- Display the elements of a multipart tag. Currently we use "and",
-- with commas when then are three or more elements, of the form
-- "foo, bar, baz and bat"; but we are seriously considering switching
-- to en-dash, e.g. "foo–bar–baz–bat". Arguably,
-- dative–ablative masculine–feminine–neuter plural
-- looks better then
-- dative and ablative masculine, feminine and neuter plural
-- and
-- first–second–third-person singular present subjunctive
-- looks better than
-- first-, second- and third-person singular present subjunctive
local strategy = joiner or export.multipart_join_strategy()
local retval
if strategy == "and" then
retval = join_multiparts_with_serial_comma(parts)
elseif strategy == "en-dash" then
retval = table.concat(parts, wrap_in_inflection_of_sep("–"))
elseif strategy == "slash" then
retval = table.concat(parts, wrap_in_inflection_of_sep("/"))
else
error("Unrecognized multipart join strategy: " .. strategy)
end
return '<span class="inflection-of-conjoined">' .. retval .. '</span>'
end
export.cat_functions = {}
export.display_handlers = {}
-- Display handler to clean up display of multiple persons by omitting
-- redundant "person" in all but the last element. For example, the tag
-- "123" maps to "1//2//3", which in turn gets displayed as (approximately)
-- "first-, second- and third-person" (with appropriate glossary links, and
-- appropriate spans marking the serial comma).
table.insert(export.display_handlers,
function(tags, joiner)
local els = {}
local numtags = #tags
local strategy = joiner or export.multipart_join_strategy()
for i, tag in ipairs(tags) do
local suffix = i == numtags and "-person]]" or
strategy == "and" and "-]]" or "]]"
if tag == "first-person" then
table.insert(els, "[[wikt:Appendix:Glossary#first person|first" .. suffix)
elseif tag == "second-person" then
table.insert(els, "[[wikt:Appendix:Glossary#second person|second" .. suffix)
elseif tag == "third-person" then
table.insert(els, "[[wikt:Appendix:Glossary#third person|third" .. suffix)
else
return nil
end
end
return export.join_multiparts(els, joiner)
end
)
return export