Module:form of: Difference between revisions

2,305 bytes removed ,  22 March 2021
no edit summary
No edit summary
No edit summary
Line 148: Line 148:
table.insert(parts, "</span>")
table.insert(parts, "</span>")
return table.concat(parts)
return table.concat(parts)
end
-- Add tracking category for PAGE when called from {{inflection of}} or
-- similar TEMPLATE. The tracking category linked to is
-- [[Template:tracking/inflection of/PAGE]].
local function infl_track(page)
require("Module:debug").track("inflection of/" ..
-- avoid including links in pages (may cause error)
page:gsub("%[", "("):gsub("%]", ")"):gsub("|", "!"))
end
end


Line 192: Line 182:
-- If the expansion is a string and is different from the tag, track it if
-- If the expansion is a string and is different from the tag, track it if
-- DO_TRACK is true.
-- DO_TRACK is true.
function export.lookup_shortcut(tag, do_track)
function export.lookup_shortcut(tag)
-- If there is HTML or a link in the tag, return it directly; don't try
-- If there is HTML or a link in the tag, return it directly; don't try
-- to look it up, which will fail.
-- to look it up, which will fail.
Line 212: Line 202:
if not expansion then
if not expansion then
return tag
return tag
end
-- Maybe track the expansion if it's not the same as the raw tag.
if do_track and expansion ~= tag and type(expansion) == "string" then
infl_track("tag/" .. tag)
end
end
return expansion
return expansion
Line 242: Line 228:
-- Normalize a single tag, which may be a shortcut but should not be a
-- Normalize a single tag, which may be a shortcut but should not be a
-- multipart tag, a multipart-tag shortcut or a list-tag shortcut.
-- multipart tag, a multipart-tag shortcut or a list-tag shortcut.
local function normalize_single_tag(tag, do_track)
local function normalize_single_tag(tag)
local expansion = export.lookup_shortcut(tag, do_track)
local expansion = export.lookup_shortcut(tag)
if type(expansion) ~= "string" then
if type(expansion) ~= "string" then
error("Tag '" .. tag .. "' is a list-tag shortcut, which is not allowed here")
error("Tag '" .. tag .. "' is a list-tag shortcut, which is not allowed here")
end
end
tag = expansion
tag = expansion
if not export.lookup_tag(tag) and do_track then
-- If after all expansions and normalizations we don't recognize
-- the canonical tag, track it.
infl_track("unknown")
infl_track("unknown/" .. tag)
end
return tag
return tag
end
end
Line 264: Line 245:
-- otherwise, it will always be a string, and multiple tags will be
-- otherwise, it will always be a string, and multiple tags will be
-- represented as canonical-form tags joined by ":".
-- represented as canonical-form tags joined by ":".
local function normalize_multipart_component(tag, recombine_tags, do_track)
local function normalize_multipart_component(tag, recombine_tags)
-- If there is HTML or a link in the tag, don't try to split on colon.
-- If there is HTML or a link in the tag, don't try to split on colon.
-- A colon may legitimately occur in either one, and we don't want
-- A colon may legitimately occur in either one, and we don't want
Line 277: Line 258:
-- We allow list-tag shortcuts inside of multipart tags, e.g.
-- We allow list-tag shortcuts inside of multipart tags, e.g.
-- '1s//3p'. Check for this now.
-- '1s//3p'. Check for this now.
tag = export.lookup_shortcut(tag, do_track)
tag = export.lookup_shortcut(tag)
if type(tag) == "table" then
if type(tag) == "table" then
-- We found a list-tag shortcut; treat as if colon-separated.
-- We found a list-tag shortcut; treat as if colon-separated.
components = tag
components = tag
else
else
return normalize_single_tag(tag, do_track)
return normalize_single_tag(tag)
end
end
end
end
local normtags = {}
local normtags = {}
for _, component in ipairs(components) do
for _, component in ipairs(components) do
if do_track then
table.insert(normtags, normalize_single_tag(component))
-- There are multiple components; track each of the individual
-- raw tags.
infl_track("tag/" .. component)
end
table.insert(normtags, normalize_single_tag(component, do_track))
end
end


Line 308: Line 284:
-- be a string, and multipart tags will be represented as canonical-form tags
-- be a string, and multipart tags will be represented as canonical-form tags
-- joined by "//" and/or ":".
-- joined by "//" and/or ":".
local function normalize_tag(tag, recombine_multitags, do_track)
local function normalize_tag(tag, recombine_multitags)
-- We don't check for links or HTML before splitting on //, which we
-- We don't check for links or HTML before splitting on //, which we
-- don't expect to occur in links or HTML. Doing it this way allows for
-- don't expect to occur in links or HTML. Doing it this way allows for
Line 323: Line 299:
local split_tags = rsplit(tag, "//", true)
local split_tags = rsplit(tag, "//", true)
if #split_tags == 1 then
if #split_tags == 1 then
local retval = normalize_multipart_component(tag, recombine_multitags,
local retval = normalize_multipart_component(tag, recombine_multitags)
do_track)
if type(retval) == "table" then
if type(retval) == "table" then
-- The user gave a tag like '1:s', i.e. with colon but without
-- The user gave a tag like '1:s', i.e. with colon but without
Line 335: Line 310:
local normtags = {}
local normtags = {}
for _, single_tag in ipairs(split_tags) do
for _, single_tag in ipairs(split_tags) do
if do_track then
-- If the tag was a multipart tag, track each of individual raw tags.
infl_track("tag/" .. single_tag)
end
table.insert(normtags, normalize_multipart_component(single_tag,
table.insert(normtags, normalize_multipart_component(single_tag,
recombine_multitags, do_track))
recombine_multitags))
end
end
if recombine_multitags then
if recombine_multitags then
Line 363: Line 334:
-- If RECOMBINE_TAGS is given, multipart tags will be represented in string
-- If RECOMBINE_TAGS is given, multipart tags will be represented in string
-- form, i.e. as canonical-form tags joined by "//" and/or ":".
-- form, i.e. as canonical-form tags joined by "//" and/or ":".
function export.normalize_tags(tags, recombine_multitags, do_track)
function export.normalize_tags(tags, recombine_multitags)
-- We track usage of shortcuts, normalized forms and (in the case of
-- multipart tags or list tags) intermediate forms. For example,
-- if the tags 1s|mn|gen|indefinite are passed in, we track the following:
-- [[Template:tracking/inflection of/tag/1s]]
-- [[Template:tracking/inflection of/tag/1]]
-- [[Template:tracking/inflection of/tag/s]]
-- [[Template:tracking/inflection of/tag/first-person]]
-- [[Template:tracking/inflection of/tag/singular]]
-- [[Template:tracking/inflection of/tag/mn]]
-- [[Template:tracking/inflection of/tag/m//n]]
-- [[Template:tracking/inflection of/tag/m]]
-- [[Template:tracking/inflection of/tag/n]]
-- [[Template:tracking/inflection of/tag/masculine]]
-- [[Template:tracking/inflection of/tag/neuter]]
-- [[Template:tracking/inflection of/tag/gen]]
-- [[Template:tracking/inflection of/tag/genitive]]
-- [[Template:tracking/inflection of/tag/indefinite]]
local ntags = {}
local ntags = {}
for _, tag in ipairs(tags) do
for _, tag in ipairs(tags) do
if do_track then
-- Track the raw tag.
infl_track("tag/" .. tag)
end
-- Expand the tag, which may generate a new tag (either a
-- Expand the tag, which may generate a new tag (either a
-- fully canonicalized tag, a multipart tag, or a list of tags).
-- fully canonicalized tag, a multipart tag, or a list of tags).
tag = export.lookup_shortcut(tag, do_track)
tag = export.lookup_shortcut(tag)
if type(tag) == "table" then
if type(tag) == "table" then
for _, t in ipairs(tag) do
for _, t in ipairs(tag) do
if do_track then
table.insert(ntags, normalize_tag(t, recombine_multitags))
-- If the tag expands to a list of raw tags, track each of
-- those.
infl_track("tag/" .. t)
end
table.insert(ntags, normalize_tag(t, recombine_multitags,
do_track))
end
end
else
else
table.insert(ntags, normalize_tag(tag, recombine_multitags,
table.insert(ntags, normalize_tag(tag, recombine_multitags))
do_track))
end
end
end
end