45,645
edits
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 | 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 | 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 | 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 | local function normalize_single_tag(tag) | ||
local expansion = export.lookup_shortcut(tag | 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 | ||
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 | 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 | 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 | 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 | ||
table.insert(normtags, normalize_single_tag(component)) | |||
table.insert(normtags, normalize_single_tag(component | |||
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 | 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) | ||
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 | ||
table.insert(normtags, normalize_multipart_component(single_tag, | table.insert(normtags, normalize_multipart_component(single_tag, | ||
recombine_multitags | 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 | function export.normalize_tags(tags, recombine_multitags) | ||
local ntags = {} | local ntags = {} | ||
for _, tag in ipairs(tags) do | for _, tag in ipairs(tags) do | ||
-- 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 | 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 | ||
table.insert(ntags, normalize_tag(t, recombine_multitags)) | |||
table.insert(ntags, normalize_tag(t, recombine_multitags | |||
end | end | ||
else | else | ||
table.insert(ntags, normalize_tag(tag, recombine_multitags | table.insert(ntags, normalize_tag(tag, recombine_multitags)) | ||
end | end | ||
end | end |