Module:links: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:


--[=[
--[=[
[[Unsupported titles]], pages with high memory usage,
[[Unsupported titles]] and pages with high
extraction modules and part-of-speech names are listed
memory usage are listed at [[Module:links/data]].
at [[Module:links/data]].
 
Other modules used:
Other modules used:
[[Module:script utilities]]
[[Module:script utilities]]
Line 11: Line 10:
[[Module:languages]] and its submodules
[[Module:languages]] and its submodules
[[Module:gender and number]]
[[Module:gender and number]]
[[Module:utilities]]
[[Module:string]]
]=]
]=]


-- These are prefixed with u to avoid confusion with the default string methods
-- These are prefixed with u to avoid confusion with the default string methods
-- of the same name.
-- of the same name.
local concat = table.concat
local usub = mw.ustring.sub
local encode = mw.text.encode
 
local find = mw.ustring.find
local table_insert = table.insert
local get_entities = require("Module:utilities").get_entities
local table_concat = table.concat
local gsub = mw.ustring.gsub
 
local insert = table.insert
local ignore_cap = {
local lower = mw.ustring.lower
["ko"] = true,
local split = mw.text.split
}
local toNFC = mw.ustring.toNFC
local trim = mw.text.trim
local unstrip = mw.text.unstrip


local function escape(text, str)
local phonetic_extraction = {
local rep
["th"] = "Module:th",
repeat
["km"] = "Module:km",
text, rep = text:gsub("\\\\(\\*" .. str .. ")", "\5%1")
}
until rep == 0
return (text:gsub("\\" .. str, "\6"))
end


local function unescape(text, str)
local pos_tags = {
return (text
["a"] = "adjective",
:gsub("\5", "\\")
["adv"] = "adverb",
:gsub("\6", str))
["int"] = "interjection",
end
["n"] = "noun",
["pron"] = "pronoun",
["v"] = "verb",
["vi"] = "intransitive verb",
["vt"] = "transitive verb",
["vti"] = "transitive and intransitive verb",
}


-- Trim only if there are non-whitespace characters.
local unsupported_titles
local function cond_trim(text)
function export.getLinkPage(target, lang)
-- Include all conventional whitespace + zero-width space.
unsupported_titles = unsupported_titles or mw.loadData("Module:links/data").unsupported_titles
if find(text, "[^%s​]") then
if unsupported_titles[target] then
text = trim(text, "%s​")
return "Unsupported titles/" .. unsupported_titles[target]
end
end
return text
end


local ignore_cap
local pos_tags
function export.getLinkPage(target, lang, sc, plain)
if not target then
return nil
end
-- Remove bold, italics, soft hyphens, strip markers and HTML tags.
target = target
:gsub("('*)'''(.-'*)'''", "%1%2")
:gsub("('*)''(.-'*)''", "%1%2")
:gsub("­", "")
target = unstrip(target)
:gsub("<[^<>]+>", "")
-- Check if the target is an interwiki link.
if target:match(":") and target ~= ":" then
local m_utildata = mw.loadData("Module:utilities/data")
-- If this is an a link to another namespace or an interwiki link, ensure there's an initial colon and then return what we have (so that it works as a conventional link, and doesn't do anything weird like add the term to a category.)
local prefix = target:gsub("^:*(.-):.*", lower)
if m_utildata.namespaces[prefix] or m_utildata.interwikis[prefix] then
return ":" .. target:gsub("^:+", ""), nil, {}
end
-- Convert any escaped colons
target = target:gsub("\\:", ":")
end
-- Check if the term is reconstructed and remove any asterisk. Otherwise, handle the escapes.
local reconstructed, escaped
if not plain then
target, reconstructed = target:gsub("^%*(.)", "%1")
end
target, escaped = target:gsub("^(\\-)\\%*", "%1*")
if not require("Module:utilities").check_object("script", true, sc) or sc:getCode() == "None" then
sc = lang:findBestScript(target)
end
-- Remove carets if they are used to capitalize parts of transliterations (unless they have been escaped).
if (not sc:hasCapitalization()) and sc:isTransliterated() and target:match("%^") then
target = escape(target, "^")
:gsub("%^", "")
target = unescape(target, "^")
end
-- Get the entry name for the language.
target = lang:makeEntryName(target, sc)
-- If the link contains unexpanded template parameters, then don't create a link.
-- If the link contains unexpanded template parameters, then don't create a link.
if target:find("{{{") then
if target:find("{{{") then
return nil
return nil
end
end
 
if target:sub(1, 1) == ":" or target:sub(1, 2) == "w:" or target:sub(1, 10) == "wikipedia:" then
return target
end
 
-- Remove diacritics from the page name
target = lang:makeEntryName(target)
 
if target:sub(1, 1) == "/" then
if target:sub(1, 1) == "/" then
return ":" .. target
return ":" .. target
elseif target:find("^Reconstruction:") then
return target
-- Link to appendix for reconstructed terms and terms in appendix-only languages. Plain links interpret * literally, however.
elseif reconstructed == 1 then
if lang:getNonEtymologicalCode() == "und" then
return nil
else
target = "Reconstruction:" .. lang:getNonEtymologicalName() .. "/" .. target
end
-- Reconstructed languages and substrates require an initial *.
elseif lang:hasType("reconstructed") or lang:getFamilyCode() == "qfa-sub" then
local check, m_utildata = target:match("^:*([^:]*):"), mw.loadData("Module:utilities/data")
check = check and lower(check)
if m_utildata.interwikis[check] or m_utildata.namespaces[check] then
return target
else
error("The specified language " .. lang:getCanonicalName()
.. " is unattested, while the given word is not marked with '*' to indicate that it is reconstructed.")
end
elseif lang:hasType("appendix-constructed") then
target = "Appendix:" .. lang:getNonEtymologicalName() .. "/" .. target
end
end


return target, escaped > 0
return target
end
end


-- Make a link from a given link's parts
-- Make a language-specific link from given link's parts
local function makeLink(link, lang, sc, id, allow_self_link, isolated, plain)
local function makeLangLink(link, lang, id, allow_self_link)
-- Convert percent encoding to plaintext.
-- Temporary tracking code
link.target = mw.uri.decode(link.target, "PATH")
local langCode = lang:getCode()
link.fragment = link.fragment and mw.uri.decode(link.fragment, "PATH")
if langCode == "se" or langCode == "sia" or langCode:find("^sm[ajns]$")
or langCode:find("^sj[dektu]$") then
if link.display and link.display:find("'") then
elseif link.target and link.target:find("'") then
end
end
 
-- Find fragments (when link didn't come from parseLink).
-- Find fragments (when link didn't come from parseLink).
-- Prevents {{l|en|word#Etymology 2|word}} from linking to [[word#Etymology 2#English]].
-- Prevents {{l|en|word#Etymology 2|word}} from linking to [[word#Etymology 2#English]].
-- # can be escaped as \#.
if link.fragment == nil then
if link.target then
-- Replace numeric character references with the corresponding character (&#29; → '),
link.target = escape(link.target, "#")
-- as they contain #, which causes the numeric character reference to be
if link.fragment == nil then
-- misparsed (wa'a → wa&#29;a → pagename wa&, fragment 29;a).
-- Replace numeric character references with the corresponding character (&#29; → '),
link.target = link.target:gsub("&#(%d+);",
-- as they contain #, which causes the numeric character reference to be
function(number) return mw.ustring.char(tonumber(number)) end)
-- misparsed (wa'a → wa&#29;a → pagename wa&, fragment 29;a).
local first, second = link.target:match("^([^#]+)#(.+)$")
link.target = get_entities(link.target)
if first then
local first, second = link.target:match("^([^#]+)#(.+)$")
link.target, link.fragment = first, second
if first then
link.target, link.fragment = first, second
end
end
end
link.target = unescape(link.target, "#")
link.fragment = link.fragment and unescape(link.fragment, "#")
end
end
 
-- If there is no display form, then create a default one.
-- If there is no display form, then create a default one
if not link.display then
if not link.display then
link.display = link.target
link.display = link.target
-- Strip the prefix from the displayed form
-- TODO: other interwiki links?
if link.display:sub(1, 1) == ":" and not mw.loadData("Module:links/data").unsupported_titles[link.display] then
link.display = link.display:sub(2) -- remove colon from beginning
else
local prefix = link.display:match("^([^:]+):")
local prefixes = {
w = true,
wikipedia = true,
}
if prefixes[prefix] then
link.display = link.display:sub(#prefix + 2) -- remove prefix plus colon
end
end
end
end
local display_is_target = link.display == link.target
-- Process the target
-- Process the target
local escaped
link.target = link.nocont and link.target:gsub("^%*", "Reconstruction:" .. lang:getCanonicalName() .. "/") or link.target
link.target, escaped = export.getLinkPage(link.target, lang, sc, plain)
link.target = export.getLinkPage(link.target, lang)
 
-- If the display is the target and the reconstruction * has been escaped, remove the escaping backslash.
if display_is_target and escaped then
link.display = link.display:gsub("\\([^\\]*%*)", "%1", 1)
end
-- Process the display form.
link.display = lang:makeDisplayText(link.display, sc, not display_is_target)
if not link.target then
if not link.target then
return link.display
return link.display
Line 185: Line 125:
-- and linking to the same page hasn't been turned on, then return a "self-link"
-- and linking to the same page hasn't been turned on, then return a "self-link"
-- like the software does.
-- like the software does.
if (not (allow_self_link or id)) and link.target == mw.title.getCurrentTitle().prefixedText then
if not (allow_self_link or id) and link.target:gsub("^:", "") == mw.title.getCurrentTitle().prefixedText then
return tostring(mw.html.create("strong")
return "<strong class=\"selflink\">" .. link.display .. "</strong>"
:addClass("selflink")
:wikitext(link.display))
end
end


-- Add fragment. Do not add a section link to "Undetermined", as such sections do not exist and are invalid. TabbedLanguages handles links without a section by linking to the "last visited" section, but adding "Undetermined" would break that feature. For localized prefixes that make syntax error, please use the format: ["xyz"] = true.
--[[
local prefix, lower_prefix = link.target:match("^:*([^:]+):")
Add fragment
Do not add a section link to "Undetermined", as such sections do not exist and are invalid.
local m_utildata
TabbedLanguages handles links without a section by linking to the "last visited" section,
if prefix then
but adding "Undetermined" would break that feature.
lower_prefix = lower(prefix)
For localized prefixes that make syntax error, please use the format: ["xyz"] = true,
m_utildata = mw.loadData("Module:utilities/data")
]]
end
local prefix = link.target:match("^:?([^:]+):")
local prefixes = {
if not (m_utildata and m_utildata.interwikis[lower_prefix]) then
w = true,
wikipedia = true,
if (not link.fragment) and lang:getNonEtymologicalCode() ~= "und" then
Category = true,
}
 
if not prefixes[prefix] then
 
if not link.fragment and lang:getCode() ~= "und" then
if id then
if id then
link.fragment = require("Module:senseid").anchor(lang, id)
link.fragment = require("Module:utilities").make_id(lang, id)
elseif not (link.target:find("^Appendix:") or link.target:find("^Reconstruction:") or plain) then
elseif not mw.ustring.find(link.target, "^Appendix:")
link.fragment = lang:getNonEtymologicalName()
and not mw.ustring.find(link.target, "^Reconstruction:") then
link.fragment = lang:getCanonicalName()
end
end
elseif plain and id then
link.fragment = id
end
end
-- This allows linking to pages like [[sms:a]] without it being treated weirdly.
link.target = link.target:gsub(":", "&#x3a;")
end
end
 
if isolated then
link.display = mw.loadData("Module:links/data").display_change[link.display] or link.display
end
link.target = link.target:gsub("^(:?)(.*)", function(m1, m2)
return m1 .. encode(m2, "#%%&+/:<=>@[\\%]_{|}")
end)
link.fragment = link.fragment and encode(link.fragment, "#%%&+/:<=>@[\\%]_{|}")
return "[[" .. (link.nocont and "wikt:" or "Contionary:") .. link.target .. "#" .. lang:getCanonicalName() .. "|" .. link.display .. "]]"
return "[[" .. (link.nocont and "wikt:" or "Contionary:") .. link.target .. "#" .. lang:getCanonicalName() .. "|" .. link.display .. "]]"
end
end
Line 230: Line 166:
local link = { target = linktext }
local link = { target = linktext }
local first, second = link.target:match("^([^|]+)|(.+)$")
local first, second = link.target:match("^([^|]+)|(.+)$")
-- Prevent characters whose HTML entities are unsupported titles from being incorrectly recognised as the entity if they are in a link being re-parsed (e.g. "&" becomes "&amp;" when returned, but "&amp;" is also an unsupported title. If "&" is given as a link which is then re-parsed, we don't want it to be perceived as "&amp;".)
if link.target:match("&[^;]+;") then
local unsupported_titles = mw.loadData("Module:links/data").unsupported_titles
if unsupported_titles[second] and unsupported_titles[second] ~= first then
link.target = get_entities(link.target)
first, second = link.target:match("^([^|]+)|(.+)$")
end
end


if first then
if first then
Line 245: Line 172:
else
else
link.display = link.target
link.display = link.target
end
-- There's no point in processing these, as they aren't real links.
local target_lower = link.target:lower()
for _, falsePositive in ipairs({"category", "cat", "file", "image"}) do
if target_lower:match("^" .. falsePositive .. ":") then return nil end
end
end


Line 259: Line 180:
link.fragment = second
link.fragment = second
else
else
-- So that makeLink does not look for a fragment again
-- So that makeLangLink does not look for a fragment again
link.fragment = false
link.fragment = false
end
end
Line 266: Line 187:
end
end


-- Find embedded links and ensure they link to the correct section.
local function process_embedded_links(text, data, allow_self_link, plain)
-- Process the non-linked text.
text = data.lang:makeDisplayText(text, data.sc[1], true)
-- If the text begins with * and another character, then act as if each link begins with *. However, don't do this if the * is contained within a link at the start. E.g. `|*[[foo]]` would set allReconstructed to true, while `|[[*foo]]` would not.
local allReconstructed = false
if not plain then
if require("Module:utilities").get_plaintext(text:gsub("%[%[.-%]%]", ".")):match("^*.") then
allReconstructed = true
end
-- Otherwise, handle any escapes.
text = text:gsub("^(\\-)\\%*", "%1*")
end
if data.alt then
mw.log("(from Module:links)", "text with embedded wikilinks:", text,
"ignored alt:", data.alt, "lang:", data.lang:getNonEtymologicalCode())
end
if data.id then
mw.log("(from Module:links)", "text with embedded wikilinks:", text,
"ignored id:", data.id, "lang:", data.lang:getNonEtymologicalCode())
end
local function processLink(space1, linktext, space2)
local capture = "[[" .. linktext .. "]]"
linktext = get_entities(linktext)
local link = parseLink(linktext)
--Return unprocessed false positives untouched (e.g. categories).
if not link then return capture end
if allReconstructed and not link.target:find("^%*") then
link.target = "*" .. link.target
end
linktext = makeLink(link, data.lang, data.sc, data.id, allow_self_link, false, plain)
:gsub("^%[%[", "\3")
:gsub("%]%]$", "\4")
return space1 .. linktext .. space2
end
-- Use chars 1 and 2 as temporary substitutions, so that we can use charsets. These are converted to chars 3 and 4 by processLink, which means we can convert any remaining chars 1 and 2 back to square brackets (i.e. those not part of a link).
text = text
:gsub("%[%[", "\1")
:gsub("%]%]", "\2")
-- If the script uses ^ to capitalize transliterations, make sure that any carets preceding links are on the inside, so that they get processed with the following text.
if text:match("%^") and not data.sc:hasCapitalization() and data.sc:isTransliterated() then
text = escape(text, "^")
:gsub("%^\1", "\1%^")
text = unescape(text, "^")
end
text = text:gsub("\1(%s*)([^\1\2]-)(%s*)\2", processLink)
-- Remove the extra * at the beginning of a language link if it's immediately followed by a link whose display begins with * too.
if allReconstructed then
text = text:gsub("^%*\3([^|\1-\4]+)|%*", "\3%1|*")
end
return (text
:gsub("[\1\3]", "[[")
:gsub("[\2\4]", "]]"))
end
local function handle_redundant_wikilink(text, alt)
local temp = text:match("^%[%[(.-)%]%]$")
if not temp then
return text, alt
end
local temp_lower = temp:lower()
for _, falsePositive in ipairs({"category", "cat", "file", "image"}) do
if temp_lower:match("^" .. falsePositive .. ":") then
return text, alt
end
end
-- Note: it's possible for "[[" or "]]" to be uninvolved in links, so we need to check for both individually (e.g. "[[aaa]] bb]]" would not have a redundant wikilink).
if temp and not (temp:find("%[%[") or temp:find("%]%]")) then
text, alt = temp:match("^([^|]+)|?(.-)$")
if alt == "" then
alt = nil
end
end
return text, alt
end


--[==[Creates a basic link to the given term. It links to the language section (such as <code>==English==</code>), but it does not add language and script wrappers, so any code that uses this function should call the <code class="n">[[Module:script utilities#tag_text|tag_text]]</code> from [[Module:script utilities]] to add such wrappers itself at some point.
-- Creates a basic wikilink to the given term. If the text already contains
The first argument, <code class="n">data</code>, may contain the following items, a subset of the items used in the <code class="n">data</code> argument of <code class="n">full_link</code>. If any other items are included, they are ignored.
-- links, these are replaced with links to the correct section.
{ {
term = entry_to_link_to,
alt = link_text_or_displayed_text,
lang = language_object,
id = sense_id,
} }
; <code class="n">term</code>
: Text to turn into a link. This is generally the name of a page. The text can contain wikilinks already embedded in it. These are processed individually just like a single link would be. The <code class="n">alt</code> argument is ignored in this case.
; <code class="n">alt</code> (''optional'')
: The alternative display for the link, if different from the linked page. If this is {{code|lua|nil}}, the <code class="n">text</code> argument is used instead (much like regular wikilinks). If <code class="n">text</code> contains wikilinks in it, this argument is ignored and has no effect. (Links in which the alt is ignored are tracked with the tracking template {{whatlinkshere|tracking=links/alt-ignored}}.)
; <code class="n">lang</code>
: The [[Module:languages#Language objects|language object]] for the term being linked. If this argument is defined, the function will determine the language's canonical name (see [[Template:language data documentation]]), and point the link or links in the <code class="n">term</code> to the language's section of an entry, or to a language-specific senseid if the <code class="n">id</code> argument is defined.
; <code class="n">id</code> (''optional'')
: Sense id string. If this argument is defined, the link will point to a language-specific sense id ({{ll|en|identifier|id=HTML}}) created by the template {{temp|senseid}}. A sense id consists of the language's canonical name, a hyphen (<code>-</code>), and the string that was supplied as the <code class="n">id</code> argument. This is useful when a term has more than one sense in a language. If the <code class="n">term</code> argument contains wikilinks, this argument is ignored. (Links in which the sense id is ignored are tracked with the tracking template {{whatlinkshere|tracking=links/id-ignored}}.)
The second argument is as follows:
; <code class="n">allowSelfLink</code>
: If {{code|lua|true}}, the function will also generate links to the current page. The default ({{code|lua|false}}) will not generate a link but generate a bolded "self link" instead.
The following special options are processed for each link (both simple text and with embedded wikilinks):
* The target page name will be processed to generate the correct entry name. This is done by the [[Module:languages#makeEntryName|makeEntryName]] function in [[Module:languages]], using the <code class="n">entry_name</code> replacements in the language's data file (see [[Template:language data documentation]] for more information). This function is generally used to automatically strip dictionary-only diacritics that are not part of the normal written form of a language.
* If the text starts with <code class="n">*</code>, then the term is considered a reconstructed term, and a link to the Reconstruction: namespace will be created. If the text contains embedded wikilinks, then <code class="n">*</code> is automatically applied to each one individually, while preserving the displayed form of each link as it was given. This allows linking to phrases containing multiple reconstructed terms, while only showing the * once at the beginning.
* If the text starts with <code class="n">:</code>, then the link is treated as "raw" and the above steps are skipped. This can be used in rare cases where the page name begins with <code class="n">*</code> or if diacritics should not be stripped. For example:
** {{temp|l|en|*nix}} links to the nonexistent page [[Reconstruction:English/nix]] (<code class="n">*</code> is interpreted as a reconstruction), but {{temp|l|en|:*nix}} links to [[*nix]].
** {{temp|l|sl|Franche-Comté}} links to the nonexistent page [[Franche-Comte]] (<code>é</code> is converted to <code>e</code> by <code class="n">makeEntryName</code>), but {{temp|l|sl|:Franche-Comté}} links to [[Franche-Comté]].]==]
function export.language_link(data, allow_self_link)
function export.language_link(data, allow_self_link)
if type(data) ~= "table" then
if type(data) ~= "table" then
error("The first argument to the function language_link must be a table. See Module:links/documentation for more information.")
error("The first argument to the function language_link must be a table. See Module:links/documentation for more information.")
-- Nothing to process, return nil.
elseif not (data.term or data.alt) then
return nil
end
end
 
local text = data.term
local text = data.term
 
data.sc = data.sc or data.lang:findBestScript(text)
if ignore_cap[data.lang:getCode()] and text then
ignore_cap = ignore_cap or mw.loadData("Module:links/data").ignore_cap
if (ignore_cap[data.lang:getCode()] or ignore_cap[data.lang:getNonEtymologicalCode()]) and text then
text = text:gsub("%^", "")
text = text:gsub("%^", "")
end
end
 
-- Do we have a redundant wikilink? If so, remove it.
-- If the text begins with * and another character,
if text then
-- then act as if each link begins with *
text, data.alt = handle_redundant_wikilink(text, data.alt)
local allReconstructed = false
 
if text:find("^*.") then
allReconstructed = true
end
end
 
-- Do we have embedded wikilinks?
-- Do we have embedded wikilinks?
if text and text:find("%[%[.-%]%]") then
if text:find("[[", nil, true) then
text = process_embedded_links(text, data, allow_self_link)
 
-- If not, make a link using the parameters.
if data.alt then
else
mw.log("(from Module:links)", "text with embedded wikilinks:", text,
text = text and cond_trim(text)
"ignored alt:", data.alt, "lang:", data.lang:getCode())
data.alt = data.alt and cond_trim(data.alt)
end
text = makeLink({ target = text, display = data.alt }, data.lang, data.sc, data.id, allow_self_link, true)
 
end
if data.id then
mw.log("(from Module:links)", "text with embedded wikilinks:", text,
return text
"ignored id:", data.id, "lang:", data.lang:getCode())
end
end
 
-- Begins and ends with a wikilink tag
if text:find("^%[%[(.+)%]%]$") then
-- There are no [ ] in between.
-- This makes the wikilink tag redundant.
if text:find("^%[%[[^%[%]]+%]%]$") then
else
local temp = text:gsub("^%[%[(.+)%]%]$", "%1")
temp = temp:gsub("%]%], %[%[", "|")
end
end
 
text = text:gsub("%[%[([^%]]+)%]%]",
function(linktext)
local link = parseLink(linktext)
 
if allReconstructed then
link.target = "*" .. link.target
end
 
return makeLangLink(link, data.lang, data.id, allow_self_link)
end)


function export.plain_link(data, allow_self_link)
-- Remove the extra * at the beginning if it's immediately followed
if type(data) ~= "table" then
-- by a link whose display begins with * too
error("The first argument to the function language_link must be a table. See Module:links/documentation for more information.")
if allReconstructed then
-- Nothing to process, return nil.
text = text:gsub("^%*%[%[([^|%]]+)|%*", "[[%1|*")
elseif not (data.term or data.alt) then
end
return nil
-- Only have alt, just return it.
elseif not data.term then
return data.alt
end
local text = data.term
if (not data.lang) or data.lang:getNonEtymologicalCode() ~= "und" then
data.lang = require("Module:languages").getByCode("und")
end
data.sc = data.sc or require("Module:scripts").findBestScriptWithoutLang(text)
-- Do we have a redundant wikilink? If so, remove it.
if text then
text, data.alt = handle_redundant_wikilink(text, data.alt)
end
-- Do we have embedded wikilinks?
if text:find("%[%[.-%]%]") then
text = process_embedded_links(text, data, allow_self_link, true)
-- If not, make a link using the parameters.
else
else
text = cond_trim(text)
-- There is no embedded wikilink, make a link using the parameters.
data.alt = data.alt and cond_trim(data.alt)
text = makeLangLink({ target = text, display = data.alt, nocont = data.nocont}, data.lang, data.id, allow_self_link)
text = makeLink({ target = text, display = data.alt }, data.lang, data.sc, data.id, allow_self_link, true, true)
end
end
return text
end


--[==[Replace any links with links to the correct section, but don't link the whole text if no embedded links are found. Returns the display text form.]==]
function export.embedded_language_links(data, allow_self_link)
if type(data) ~= "table" then
error("The first argument to the function language_link must be a table. See Module:links/documentation for more information.")
end
local text = data.term
data.sc = data.sc or data.lang:findBestScript(text)
-- Do we have embedded wikilinks?
if text:find("%[%[.-%]%]") then
text = process_embedded_links(text, data, allow_self_link)
else
-- If there are no embedded wikilinks, return the display text.
text = cond_trim(text)
text = (data.lang:makeDisplayText(text, data.sc, true))
end
return text
return text
end
end
Line 479: Line 265:
elseif itemType == "tr" then
elseif itemType == "tr" then
if face == "term" then
if face == "term" then
tag = { '<span lang="' .. lang:getNonEtymologicalCode() .. '" class="tr mention-tr Latn">',
tag = { '<span lang="' .. lang:getCode() .. '" class="tr mention-tr Latn">',
'</span>' }
'</span>' }
else
else
tag = { '<span lang="' .. lang:getNonEtymologicalCode() .. '" class="tr Latn">', '</span>' }
tag = { '<span lang="' .. lang:getCode() .. '" class="tr Latn">', '</span>' }
end
end
elseif itemType == "ts" then
elseif itemType == "ts" then
-- \226\129\160 = word joiner (zero-width non-breaking space) U+2060
tag = { '<span class="ts mention-ts Latn">/', '/</span>' }
tag = { '<span class="ts mention-ts Latn">/\226\129\160', '\226\129\160/</span>' }
elseif itemType == "pos" then
elseif itemType == "pos" then
tag = { '<span class="ann-pos">', '</span>' }
tag = { '<span class="ann-pos">', '</span>' }
Line 501: Line 286:
end
end


--[==[Formats the annotations that are displayed with a link created by {{code|lua|full_link}}. Annotations are the extra bits of information that are displayed following the linked term, and include things such as gender, transliteration, gloss and so on.
-- Format the annotations (things following the linked term)
* The first argument is a table possessing some or all of the following keys:
*:; <code class="n">genders</code>
*:: Table containing a list of gender specifications in the style of [[Module:gender and number]].
*:; <code class="n">tr</code>
*:: Transliteration.
*:; <code class="n">gloss</code>
*:: Gloss that translates the term in the link, or gives some other descriptive information.
*:; <code class="n">pos</code>
*:: Part of speech of the linked term. If the given argument matches one of the templates in [[:Category:Part of speech tags]], then call that to show a part-of-speech tag. Otherwise, just show the given text as it is.
*:; <code class="n">lit</code>
*:: Literal meaning of the term, if the usual meaning is figurative or idiomatic.
*:Any of the above values can be omitted from the <code class="n">info</code> argument. If a completely empty table is given (with no annotations at all), then an empty string is returned.
* The second argument is a string. Valid values are listed in [[Module:script utilities/data]] "data.translit" table.]==]
function export.format_link_annotations(data, face)
function export.format_link_annotations(data, face)
local output = {}
local output = {}
Line 520: Line 292:
-- Interwiki link
-- Interwiki link
if data.interwiki then
if data.interwiki then
insert(output, data.interwiki)
table_insert(output, data.interwiki)
end
end


Line 529: Line 301:


if data.genders and #data.genders > 0 then
if data.genders and #data.genders > 0 then
local m_gen = require("Module:gender and number")
local m_gen = require("Module:getn")
insert(output, "&nbsp;" .. m_gen.format_list(data.genders, data.lang))
table_insert(output, "&nbsp;" .. m_gen.format_list(data.genders, data.lang))
end
end


Line 536: Line 308:


-- Transliteration and transcription
-- Transliteration and transcription
if data.tr and data.tr[1] or data.ts and data.ts[1] then
if data.tr or data.ts then
local kind
local kind
if face == "term" then
if face == "term" then
Line 544: Line 316:
end
end


if data.tr[1] and data.ts[1] then
if data.tr and data.ts then
insert(annotations,
table_insert(annotations,
require("Module:script utilities").tag_translit(data.tr[1], data.lang, kind)
require("Module:script utilities").tag_translit(data.tr, data.lang, kind)
.. " " .. export.mark(data.ts[1], "ts"))
.. " " .. export.mark(data.ts, "ts"))
elseif data.ts[1] then
elseif data.ts then
insert(annotations, export.mark(data.ts[1], "ts"))
table_insert(annotations, export.mark(data.ts, "ts"))
else
else
insert(annotations,
table_insert(annotations,
require("Module:script utilities").tag_translit(data.tr[1], data.lang, kind))
require("Module:script utilities").tag_translit(data.tr, data.lang, kind))
end
end
end
end
Line 558: Line 330:
-- Gloss/translation
-- Gloss/translation
if data.gloss then
if data.gloss then
insert(annotations, export.mark(data.gloss, "gloss"))
table_insert(annotations, export.mark(data.gloss, "gloss"))
end
end


Line 568: Line 340:
end
end


pos_tags = pos_tags or mw.loadData("Module:links/data").pos_tags
table_insert(annotations, export.mark(pos_tags[data.pos] or data.pos, "pos"))
insert(annotations, export.mark(pos_tags[data.pos] or data.pos, "pos"))
end
end


-- Literal/sum-of-parts meaning
-- Literal/sum-of-parts meaning
if data.lit then
if data.lit then
insert(annotations, "literally " .. export.mark(data.lit, "gloss"))
table_insert(annotations, "literally " .. export.mark(data.lit, "gloss"))
end
end


if #annotations > 0 then
if #annotations > 0 then
insert(output, " " .. export.mark(concat(annotations, ", "), "annotations"))
table_insert(output, " " .. export.mark(table_concat(annotations, ", "), "annotations"))
end
end


return concat(output)
return table_concat(output)
end
end


-- Add any left or right qualifiers or references to a formatted term. `data` is the object specifying the term, which
-- A version of {{l}} or {{m}} that can be called from other modules too
-- should optionally contain:
function export.full_link(data, face, allow_self_link, no_check_redundant_translit)
-- * left qualifiers in `q` (an array of strings or a single string); an empty array or blank string will be ignored;
-- * right qualifiers in `qq` (an array of strings or a single string); an empty array or blank string will be ignored;
-- * references in `refs`, an array either of strings (formatted reference text) or objects containing fields `text`
--  (formatted reference text) and optionally `name` and/or `group`.
-- `formatted` is the formatted version of the term itself.
function export.add_qualifiers_and_refs_to_term(data, formatted)
local left_qualifiers, right_qualifiers
local reftext
 
left_qualifiers = data.q and #data.q > 0 and data.q
if left_qualifiers then
left_qualifiers = require("Module:qualifier").format_qualifier(left_qualifiers) .. " "
end
 
right_qualifiers = data.qq and #data.qq > 0 and data.qq
if right_qualifiers then
right_qualifiers = " " .. require("Module:qualifier").format_qualifier(right_qualifiers)
end
if data.refs and #data.refs > 0 then
local refs = {}
for _, ref in ipairs(data.refs) do
if type(ref) ~= "table" then
ref = {text = ref}
end
local refargs
if ref.name or ref.group then
refargs = {name = ref.name, group = ref.group}
end
table.insert(refs, mw.getCurrentFrame():extensionTag("ref", ref.text, refargs))
end
reftext = table.concat(refs)
end
 
if left_qualifiers then
formatted = left_qualifiers .. formatted
end
if reftext then
formatted = formatted .. reftext
end
if right_qualifiers then
formatted = formatted .. right_qualifiers
end
 
return formatted
end
 
 
--[==[Creates a full link, with annotations (see <code class="n">[[#format_link_annotations|format_link_annotations]]</code>), in the style of {{temp|l}} or {{temp|m}}.
The first argument, <code class="n">data</code>, must be a table. It contains the various elements that can be supplied as parameters to {{temp|l}} or {{temp|m}}:
{ {
term = entry_to_link_to,
alt = link_text_or_displayed_text,
lang = language_object,
sc = script_object,
id = sense_id,
genders = { "gender1", "gender2", ... },
tr = transliteration,
ts = transcription,
gloss = gloss,
pos = part_of_speech_tag,
lit = literal_translation,
accel = {accelerated_creation_tags},
interwiki = interwiki,
q = { "left_qualifier1", "left_qualifier2", ...} or "left_qualifier",
qq = { "right_qualifier1", "right_qualifier2", ...} or "right_qualifier",
refs = { "formatted_ref1", "formatted_ref2", ...} or { {text = "text", name = "name", group = "group"}, ... },
} }
Any one of the items in the <code class="n">data</code> table may be {{code|lua|nil}}, but an error will be shown if neither <code class="n">term</code> nor <code class="n">alt</code> nor <code class="n">tr</code> is present.
Thus, calling {{code|lua|2=full_link{ term = term, lang = lang, sc = sc } }}, where <code class="n">term</code> is an entry name, <code class="n">lang</code>  is a [[Module:languages#Language objects|language object]] from [[Module:languages]], and <code class="n">sc</code> is a [[Module:scripts#Script objects|script object]] from [[Module:scripts]], will give a plain link similar to the one produced by the template {{temp|l}}, and calling {{code|lua|2=full_link( { term = term, lang = lang, sc = sc }, "term" )}} will give a link similar to the one produced by the template {{temp|m}}.
The function will:
* Try to determine the script, based on the characters found in the term or alt argument, if the script was not given.
* Call <code class="n">[[#language_link|language_link]]</code> on the term or alt forms, to remove diacritics in the page name, process any embedded wikilinks and create links to Reconstruction or Appendix pages when necessary.
* Call <code class="n">[[Module:script utilities#tag_text]]</code> to add the appropriate language and script tags to the term, and to italicize terms written in the Latin script if necessary. Accelerated creation tags, as used by [[WT:ACCEL]], are included.
* Generate a transliteration, based on the alt or term arguments, if the script is not Latin and no transliteration was provided.
* Add the annotations (transliteration, gender, gloss etc.) after the link.
* If <code class="n">show_qualifiers</code> is specified, left and right qualifiers and references will be displayed. (This is for compatibility reasons, since a fair amount of code stores qualifiers and/or references in these fields and displays them itself, expecting {{code|lua|full_link()}} to ignore them.]==]
function export.full_link(data, face, allow_self_link, show_qualifiers)
-- Prevent data from being destructively modified.
local data = require("Module:table").shallowcopy(data)
if type(data) ~= "table" then
if type(data) ~= "table" then
error("The first argument to the function full_link must be a table. "
error("The first argument to the function full_link must be a table. "
Line 671: Line 362:
end
end


local terms = {true}
-- Generate multiple forms if applicable.
for _, param in ipairs{"term", "alt"} do
if type(data[param]) == "string" and data[param]:find("//") then
data[param] = escape(data[param], "//")
data[param] = split(data[param], "//") or {}
for i, subparam in ipairs(data[param]) do
data[param][i] = unescape(subparam, "//")
if subparam == "" then
data[param][i] = nil
end
end
elseif type(data[param]) == "string" and not (type(data.term) == "string" and data.term:find("//")) then
data[param] = data.lang:generateForms(data[param])
else
data[param] = {}
end
end
for _, param in ipairs{"sc", "tr", "ts"} do
data[param] = {data[param]}
end
for _, param in ipairs{"term", "alt", "sc", "tr", "ts"} do
for i in pairs(data[param]) do
terms[i] = true
end
end
-- Create the link
-- Create the link
local output = {}
local output = {}
Line 706: Line 367:
local link = ""
local link = ""
local annotations
local annotations
local phonetic_extraction = mw.loadData("Module:links/data").phonetic_extraction
phonetic_extraction = phonetic_extraction[data.lang:getCode()] or phonetic_extraction[data.lang:getNonEtymologicalCode()]
for i in ipairs(terms) do
-- Is there any text to show?
if (data.term[i] or data.alt[i]) then
-- Try to detect the script if it was not provided
local best = data.lang:findBestScript(data.alt[i] or data.term[i])
if not data.sc[i] then
data.sc[i] = best
end
-- If using a discouraged character sequence, add to maintenance category
if data.sc[i]:hasNormalizationFixes() == true then
if (data.term[i] and data.sc[i]:fixDiscouragedSequences(toNFC(data.term[i])) ~= toNFC(data.term[i])) or (data.alt[i] and data.sc[i]:fixDiscouragedSequences(toNFC(data.alt[i])) ~= toNFC(data.alt[i])) then
insert(categories, "Pages using discouraged character sequences")
end
end


local class = ""
--local m_utilities = require("Module:utilities")
 
-- Is there any text to show?
if (data.term or data.alt) and data.term ~= "-" then
-- Try to detect the script if it was not provided
if not data.sc then
data.sc = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
else
-- Track uses of sc parameter
local best = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
end
 
local class = ""


-- Encode certain characters to avoid various delimiter-related issues at various stages. We need to encode < and >
local function encode_accel_param(prefix, param)
-- because they end up forming part of CSS class names inside of <span ...> and will interfere with finding the end
-- This is decoded again by [[WT:ACCEL]].
-- of the HTML tag. I first tried converting them to URL encoding, i.e. %3C and %3E; they then appear in the URL as
return param and prefix .. param:gsub("%%", "."):gsub(" ", "_") or ""
-- %253C and %253E, which get mapped back to %3C and %3E when passed to [[Module:accel]]. But mapping them to &lt;
end
-- and &gt; somehow works magically without any further work; they appear in the URL as < and >, and get passed to
-- [[Module:accel]] as < and >. I have no idea who along the chain of calls is doing the encoding and decoding. If
-- someone knows, please modify this comment appropriately!
local encode_accel_char_map = {
["%"] = ".",
[" "] = "_",
["<"] = "&lt;",
[">"] = "&gt;",
}
local function encode_accel_param_chars(param)
local retval = param:gsub("[% <>]", encode_accel_char_map) -- discard second return value
return retval
end


local function encode_accel_param(prefix, param)
if data.accel then
if not param then
local form = data.accel.form and data.accel.form .. "-form-of" or ""
return ""
local gender = encode_accel_param("gender-", data.accel.gender)
end
local pos = encode_accel_param("pos-", data.accel.pos)
if type(param) == "table" then
local translit = encode_accel_param("transliteration-", data.accel.translit)
local filled_params = {}
local lemma = encode_accel_param("origin-", data.accel.lemma)
-- There may be gaps in the sequence, especially for translit params.
local lemma_translit = encode_accel_param("origin_transliteration-", data.accel.lemma_translit)
local maxindex = 0
local no_store = data.accel.no_store and "form-of-nostore" or ""
for k, v in pairs(param) do
if type(k) == "number" and k > maxindex then
maxindex = k
end
end
for i=1,maxindex do
filled_params[i] = param[i] or ""
end
-- [[Module:accel]] splits these up again.
param = table.concat(filled_params, "*~!")
end
-- This is decoded again by [[WT:ACCEL]].
return prefix .. encode_accel_param_chars(param)
end


if data.accel then
local accel =
local form = data.accel.form and encode_accel_param_chars(data.accel.form) .. "-form-of" or ""
form .. " " ..
local gender = encode_accel_param("gender-", data.accel.gender)
gender .. " " ..
local pos = encode_accel_param("pos-", data.accel.pos)
pos .. " " ..
local translit = encode_accel_param("transliteration-",
translit .. " " ..
data.accel.translit or (data.tr[i] ~= "-" and data.tr[i] or nil))
lemma .. " " ..
local target = encode_accel_param("target-", data.accel.target)
lemma_translit .. " " ..
local lemma = encode_accel_param("origin-", data.accel.lemma)
no_store .. " "
local lemma_translit = encode_accel_param("origin_transliteration-", data.accel.lemma_translit)
local no_store = data.accel.no_store and "form-of-nostore" or ""


local accel =
class = "form-of lang-" .. data.lang:getCode() .. " " .. accel
form .. " " ..
end
gender .. " " ..
pos .. " " ..
translit .. " " ..
target .. " " ..
lemma .. " " ..
lemma_translit .. " " ..
no_store .. " "


class = "form-of lang-" .. data.lang:getNonEtymologicalCode() .. " " .. accel
-- Only make a link if the term has been given, otherwise just show the alt text without a link
end
link = require("Module:script utilities").tag_text(
data.term and export.language_link(data, allow_self_link)
-- Only make a link if the term has been given, otherwise just show the alt text without a link
or data.alt, data.lang, data.sc, face, class)
local term_data = {term = data.term[i], alt = data.alt[i], lang = data.lang, sc = data.sc[i], id = data.id, genders = data.genders, tr = data.tr[i], ts = data.ts[i], gloss = data.gloss, pos = data.pos, lit = data.lit, accel = data.accel, interwiki = data.interwiki}
else
link = require("Module:script utilities").tag_text(
--[[ No term to show.
data.term[i] and export.language_link(term_data, allow_self_link)
Is there at least a transliteration we can work from? ]]
or data.alt[i], data.lang, data.sc[i], face, class)
link = require("Module:script utilities").request_script(data.lang, data.sc)
if not data.sc then
data.sc = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
else
else
--[[ No term to show.
-- Track uses of sc parameter
Is there at least a transliteration we can work from? ]]
local best = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
link = require("Module:script utilities").request_script(data.lang, data.sc[i])
end
if link == "" or link == "-" or link == "?" or not data.tr or data.tr == "-" then
-- No link to show, and no transliteration either. Show a term request.
local category = ""


if (link == "" or (not data.tr[i]) or data.tr[i] == "-") and data.lang:getFamilyCode() ~= "qfa-sub" then
if mw.title.getCurrentTitle().nsText ~= "Template" then
-- No link to show, and no transliteration either. Show a term request (unless it's a substrate, as they rarely take terms).
table_insert(categories, "[[Category:" .. data.lang:getCanonicalName() .. " term requests]]")
if mw.title.getCurrentTitle().nsText ~= "Template" then
insert(categories, data.lang:getNonEtymologicalName() .. " term requests")
end
link = "<small>[Term?]</small>"
end
end
link = "<small>[Term?]</small>"
end
end
insert(output, link)
if i < #terms then insert(output, "<span class=\"Zsym mention\" style=\"font-size:100%;\">/</span>") end
end
end
-- TODO: Currently only handles the first transliteration, pending consensus on how to handle multiple translits for multiple forms, as this is not always desirable (e.g. traditional/simplified Chinese).
if data.tr[1] == "" or data.tr[1] == "-" then
data.tr[1] = nil


elseif phonetic_extraction then
table_insert(output, link)
local m_phonetic = require(phonetic_extraction)
 
data.tr[1] = data.tr[1] or m_phonetic.getTranslit(export.remove_links(data.alt[1] or data.term[1]))
if data.tr == "" or data.tr == "-" then
data.tr = nil
 
elseif phonetic_extraction[data.lang:getCode()] then
local m_phonetic = require(phonetic_extraction[data.lang:getCode()])
data.tr = data.tr or m_phonetic.getTranslit(export.remove_links(data.term))


elseif (data.term[1] or data.alt[1]) and data.sc[1]:isTransliterated() then
elseif (data.term or data.alt) and not data.sc:getCode():find("Lati?n") then


-- Try to generate a transliteration, unless transliteration has been supplied and data.no_check_redundant_translit is
-- Try to generate a transliteration, unless transliteration has been supplied and either
-- given. (Checking for redundant transliteration can use up significant amounts of memory so we don't want to do it
-- no_check_redundant_translit is given or we are in a high-memory entry. (Checking for redundant
-- if memory is tight. `no_check_redundant_translit` is currently set when called ultimately from
-- transliteration can use up significant amounts of memory so we don't want to do it if memory
-- is tight. `no_check_redundant_translit` is currently set when called ultimately from
-- {{multitrans|...|no-check-redundant-translit=1}}.)
-- {{multitrans|...|no-check-redundant-translit=1}}.)
if not (data.tr[1] and data.no_check_redundant_translit) then
if not data.tr then
local text = data.alt[1] or data.term[1]
local automated_tr = data.lang:transliterate(export.remove_links(data.alt or data.term), data.sc)
if not data.lang:link_tr() then
 
text = export.remove_links(text, true)
if automated_tr then
end
local manual_tr = data.tr
local automated_tr, tr_categories
automated_tr, data.tr_fail, tr_categories = data.lang:transliterate(text, data.sc[1])
if automated_tr or data.tr_fail then
local manual_tr = data.tr[1]


if manual_tr then
if manual_tr then
if (export.remove_links(manual_tr) == export.remove_links(automated_tr)) and (not data.tr_fail) then
if manual_tr == automated_tr then
insert(categories, "Terms with redundant transliterations")
table_insert(categories,
insert(categories, "Terms with redundant transliterations/" .. data.lang:getNonEtymologicalCode())
"[[Category:Terms with redundant transliterations]]"
elseif not data.tr_fail then
.. "[[Category:Terms with redundant transliterations/" .. data.lang:getCode() .. "]]")
else
-- Prevents Arabic root categories from flooding the tracking categories.
-- Prevents Arabic root categories from flooding the tracking categories.
if mw.title.getCurrentTitle().nsText ~= "Category" then
if mw.title.getCurrentTitle().nsText ~= "Category" then
insert(categories, "Terms with manual transliterations different from the automated ones")
table_insert(categories,
insert(categories, "Terms with manual transliterations different from the automated ones/" .. data.lang:getNonEtymologicalCode())
"[[Category:Terms with manual transliterations different from the automated ones]]"
.. "[[Category:Terms with manual transliterations different from the automated ones/" .. data.lang:getCode() .. "]]")
end
end
end
end
Line 855: Line 474:


if (not manual_tr) or data.lang:overrideManualTranslit() then
if (not manual_tr) or data.lang:overrideManualTranslit() then
data.tr[1] = automated_tr
data.tr = automated_tr
for _, category in ipairs(tr_categories) do
insert(categories, category)
end
end
end
end
end
end
end
end
end
 
-- Link to the transliteration entry for languages that require this
-- Link to the transliteration entry for languages that require this
if data.tr[1] and data.lang:link_tr() and not (data.tr[1]:match("%[%[(.-)%]%]") or data.tr_fail) then
if data.tr and data.lang:link_tr() then
data.tr[1] = export.language_link{lang = data.lang, term = data.tr[1]}
data.tr = require("Module:script utilities").tag_text(
elseif data.tr[1] and not (data.lang:link_tr() or data.tr_fail) then
export.language_link({ lang = data.lang, term = data.tr, nocont = data.nocont}, allow_self_link),
-- Remove the pseudo-HTML tags added by remove_links.
data.lang, data.sc, face, class)
data.tr[1] = data.tr[1]:gsub("</?link>", "")
--data.tr = export.language_link { lang = data.lang, term = data.tr, nocont = data.nocont}
elseif data.tr then
data.tr = "''" .. data.tr .. "''"
end
end
if data.tr[1] and gsub(data.tr[1], "[%s%p]", ""):len() == 0 then data.tr[1] = nil end
insert(output, export.format_link_annotations(data, face))
categories = #categories > 0 and require("Module:utilities").format_categories(categories, data.lang, "-", nil, nil, data.sc) or ""


output = concat(output)
table_insert(output, export.format_link_annotations(data, face))
if show_qualifiers then
 
output = export.add_qualifiers_and_refs_to_term(data, output)
return table_concat(output) .. table_concat(categories)
end
return output .. categories
end
end


--[==[Replaces all wikilinks with their displayed text, and removes any categories. This function can be invoked either from a template or from another module.
--[[ Strips links: deletes category links,
-- Strips links: deletes category links, the targets of piped links, and any double square brackets involved in links (other than file links, which are untouched). If `tag` is set, then any links removed will be given pseudo-HTML tags, which allow the substitution functions in [[Module:languages]] to properly subdivide the text in order to reduce the chance of substitution failures in modules which scrape pages like [[Module:zh-translit]].
the targets of piped links,
-- FIXME: This is quite hacky. We probably want this to be integrated into [[Module:languages]], but we can't do that until we know that nothing is pushing pipe linked transliterations through it for languages which don't have link_tr set.
and all double square brackets. ]]
* <code><nowiki>[[page|displayed text]]</nowiki></code> &rarr; <code><nowiki>displayed text</nowiki></code>
function export.remove_links(text)
* <code><nowiki>[[page and displayed text]]</nowiki></code> &rarr; <code><nowiki>page and displayed text</nowiki></code>
* <code><nowiki>[[Category:English lemmas|WORD]]</nowiki></code> &rarr; ''(nothing)'']==]
function export.remove_links(text, tag)
if type(text) == "table" then
if type(text) == "table" then
text = text.args[1]
text = text.args[1]
Line 898: Line 506:
return ""
return ""
end
end
text = text
:gsub("%[%[", "\1")
:gsub("%]%]", "\2")


-- Parse internal links for the display text.
text = mw.ustring.gsub(text, "%[%[Category:[^|%]]-|?[^|%]]-%]%]", "")
text = text:gsub("(\1)([^\1\2]-)(\2)",
text = text:gsub("%[%[[^|%]]-|", "")
function(c1, c2, c3)
text = text:gsub("%[%[", "")
-- Don't remove files.
text = text:gsub("%]%]", "")
for _, falsePositive in ipairs({"file", "image"}) do
if c2:lower():match("^" .. falsePositive .. ":") then return c1 .. c2 .. c3 end
end
-- Remove categories completely.
for _, falsePositive in ipairs({"category", "cat"}) do
if c2:lower():match("^" .. falsePositive .. ":") then return "" end
end
-- In piped links, remove all text before the pipe, unless it's the final character (i.e. the pipe trick), in which case just remove the pipe.
c2 = c2:match("^[^|]*|(.+)") or c2:match("([^|]+)|$") or c2
if tag then
return "<link>" .. c2 .. "</link>"
else
return c2
end
end)
text = text
:gsub("\1", "[[")
:gsub("\2", "]]")


return text
return text
end
function export.english_links(text)
local lang = require("Module:languages").getByCode("en")
-- Parentheses around function call to remove second return value, the
-- number of replacements.
return (text:gsub("%[%[([^%]]+)%]%]",
function(linktext)
local link = parseLink(linktext)
return makeLangLink(link, lang, nil, true, false)
end))
end
function export.light_link(data)
local language_names = mw.loadData("Module:languages/code to canonical name")
local script_codes = mw.loadData("Module:scripts/codes")
if data.langCode then
data.langName = language_names[data.langCode] or error('The language code "' .. data.langCode .. '" is not recognized.')
else
error('Language code is required.')
end
if not data.term then
error('Term to link to is required.')
end
if data.scCode then
if not script_codes[data.scCode] then
error('The script code "' .. data.sc .. '" is not recognized.')
end
else
error("The function light_link requires a script code.")
end
local fragment
if data.id then
fragment = data.langName .. "-" .. mw.uri.encode(data.id, "WIKI")
else
fragment = data.langName
end
return table_concat {
'<span class="', data.scCode, '" lang="', data.langCode,
'">[[', data.term, "#", fragment, "|", (data.alt or data.term), "]]</span>"
}
end
end


--[=[
--[=[
This decodes old section encodings.
For example, Norwegian_Bokm.C3.A5l → Norwegian_Bokmål. 0xC3 and 0xA5 are the
For example, Norwegian_Bokm.C3.A5l → Norwegian_Bokmål.
hexadecimal-base representation of the two bytes used to encode the character
It isn't picky about whether the section encodings represent the UTF-8 encoding
å in the UTF-8 encoding:
of a real Unicode character, so it will mangle section names that contain
11000011 10100101
a period followed by two uppercase hex characters. At least such section names
are probably pretty rare.


Wiktionary adds an additional id="" attribute for sections
Note that the bytes used to represent a character are actually different from
using a legacy encoding, if it is different from the modern minimally modified attribute.
the Unicode codepoint. For å, the codepoint is 0xE5. The bits (digits) that
It is like percent encoding (URI or URL encoding) except with "." instead of "%".
actually spell the codepoint are found in the brackets: 110[00011] 10[100101].
See [[mw:Manual:$wgFragmentMode]] and the code that does the encoding at
For further explanation, see [[w:UTF-8#Description]].
https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/7bf779524ab1fd8e1d74f79ea4840564d48eea4d/includes/parser/Sanitizer.php#893
]=]
]=]


Line 963: Line 592:
link = link:gsub("_", " ")
link = link:gsub("_", " ")


local numberSigns = select(2, link:gsub("#", ""))
local numberSigns = require("Module:string").count(link, "#")


if numberSigns > 1 then
if numberSigns > 1 then

Latest revision as of 22:26, 30 January 2024



local export = {}

--[=[
	[[Unsupported titles]] and pages with high
	memory usage are listed at [[Module:links/data]].

	Other modules used:
		[[Module:script utilities]]
		[[Module:scripts]]
		[[Module:languages]] and its submodules
		[[Module:gender and number]]
		[[Module:utilities]]
		[[Module:string]]
]=]

-- These are prefixed with u to avoid confusion with the default string methods
-- of the same name.
local usub = mw.ustring.sub

local table_insert = table.insert
local table_concat = table.concat

local ignore_cap = {
	["ko"] = true,
}

local phonetic_extraction = {
	["th"] = "Module:th",
	["km"] = "Module:km",
}

local pos_tags = {
	["a"] = "adjective",
	["adv"] = "adverb",
	["int"] = "interjection",
	["n"] = "noun",
	["pron"] = "pronoun",
	["v"] = "verb",
	["vi"] = "intransitive verb",
	["vt"] = "transitive verb",
	["vti"] = "transitive and intransitive verb",
}

local unsupported_titles
function export.getLinkPage(target, lang)
	unsupported_titles = unsupported_titles or mw.loadData("Module:links/data").unsupported_titles
	if unsupported_titles[target] then
		return "Unsupported titles/" .. unsupported_titles[target]
	end

	-- If the link contains unexpanded template parameters, then don't create a link.
	if target:find("{{{") then
		return nil
	end

	if target:sub(1, 1) == ":" or target:sub(1, 2) == "w:" or target:sub(1, 10) == "wikipedia:" then
		return target
	end

	-- Remove diacritics from the page name
	target = lang:makeEntryName(target)

	if target:sub(1, 1) == "/" then
		return ":" .. target
	end

	return target
end

-- Make a language-specific link from given link's parts
local function makeLangLink(link, lang, id, allow_self_link)
	-- Temporary tracking code
	local langCode = lang:getCode()
	if langCode == "se" or langCode == "sia" or langCode:find("^sm[ajns]$")
			or langCode:find("^sj[dektu]$") then
		if link.display and link.display:find("'") then
		elseif link.target and link.target:find("'") then
		end
	end

	-- Find fragments (when link didn't come from parseLink).
	-- Prevents {{l|en|word#Etymology 2|word}} from linking to [[word#Etymology 2#English]].
	if link.fragment == nil then
		-- Replace numeric character references with the corresponding character (&#29; → '),
		-- as they contain #, which causes the numeric character reference to be
		-- misparsed (wa'a → wa&#29;a → pagename wa&, fragment 29;a).
		link.target = link.target:gsub("&#(%d+);",
			function(number) return mw.ustring.char(tonumber(number)) end)
		local first, second = link.target:match("^([^#]+)#(.+)$")
		if first then
			link.target, link.fragment = first, second
		end
	end

	-- If there is no display form, then create a default one
	if not link.display then
		link.display = link.target

		-- Strip the prefix from the displayed form
		-- TODO: other interwiki links?
		if link.display:sub(1, 1) == ":" and not mw.loadData("Module:links/data").unsupported_titles[link.display] then
			link.display = link.display:sub(2) -- remove colon from beginning
		else
			local prefix = link.display:match("^([^:]+):")
			local prefixes = {
				w = true,
				wikipedia = true,
			}

			if prefixes[prefix] then
				link.display = link.display:sub(#prefix + 2) -- remove prefix plus colon
			end
		end
	end
	
	-- Process the target
	link.target = link.nocont and link.target:gsub("^%*", "Reconstruction:" .. lang:getCanonicalName() .. "/") or link.target
	link.target = export.getLinkPage(link.target, lang)

	if not link.target then
		return link.display
	end

	-- If the target is the same as the current page and there is no sense id
	-- and linking to the same page hasn't been turned on, then return a "self-link"
	-- like the software does.
	if not (allow_self_link or id) and link.target:gsub("^:", "") == mw.title.getCurrentTitle().prefixedText then
		return "<strong class=\"selflink\">" .. link.display .. "</strong>"
	end

	--[[
		Add fragment
		Do not add a section link to "Undetermined", as such sections do not exist and are invalid.
		TabbedLanguages handles links without a section by linking to the "last visited" section,
		but adding "Undetermined" would break that feature.
		For localized prefixes that make syntax error, please use the format: ["xyz"] = true,
	]]
	local prefix = link.target:match("^:?([^:]+):")
	local prefixes = {
		w = true,
		wikipedia = true,
		Category = true,
	}

	if not prefixes[prefix] then

		if not link.fragment and lang:getCode() ~= "und" then
			if id then
				link.fragment = require("Module:utilities").make_id(lang, id)
			elseif not mw.ustring.find(link.target, "^Appendix:")
					and not mw.ustring.find(link.target, "^Reconstruction:") then
				link.fragment = lang:getCanonicalName()
			end
		end

		-- This allows linking to pages like [[sms:a]] without it being treated weirdly.
		link.target = link.target:gsub(":", "&#x3a;")
	end

	return "[[" .. (link.nocont and "wikt:" or "Contionary:") .. link.target .. "#" .. lang:getCanonicalName() .. "|" .. link.display .. "]]"
end


-- Split a link into its parts
local function parseLink(linktext)
	local link = { target = linktext }
	local first, second = link.target:match("^([^|]+)|(.+)$")

	if first then
		link.target = first
		link.display = second
	else
		link.display = link.target
	end

	first, second = link.target:match("^(.+)#(.+)$")

	if first then
		link.target = first
		link.fragment = second
	else
		-- So that makeLangLink does not look for a fragment again
		link.fragment = false
	end

	return link
end


-- Creates a basic wikilink to the given term. If the text already contains
-- links, these are replaced with links to the correct section.
function export.language_link(data, allow_self_link)
	if type(data) ~= "table" then
		error("The first argument to the function language_link must be a table. See Module:links/documentation for more information.")
	end

	local text = data.term

	if ignore_cap[data.lang:getCode()] and text then
		text = text:gsub("%^", "")
	end

	-- If the text begins with * and another character,
	-- then act as if each link begins with *
	local allReconstructed = false

	if text:find("^*.") then
		allReconstructed = true
	end

	-- Do we have embedded wikilinks?
	if text:find("[[", nil, true) then

		if data.alt then
			mw.log("(from Module:links)", "text with embedded wikilinks:", text,
				"ignored alt:", data.alt, "lang:", data.lang:getCode())
		end

		if data.id then
			mw.log("(from Module:links)", "text with embedded wikilinks:", text,
				"ignored id:", data.id, "lang:", data.lang:getCode())
		end

		-- Begins and ends with a wikilink tag
		if text:find("^%[%[(.+)%]%]$") then
			-- There are no [ ] in between.
			-- This makes the wikilink tag redundant.
			if text:find("^%[%[[^%[%]]+%]%]$") then
			else
				local temp = text:gsub("^%[%[(.+)%]%]$", "%1")
				temp = temp:gsub("%]%], %[%[", "|")
			end
		end

		text = text:gsub("%[%[([^%]]+)%]%]",
			function(linktext)
				local link = parseLink(linktext)

				if allReconstructed then
					link.target = "*" .. link.target
				end

				return makeLangLink(link, data.lang, data.id, allow_self_link)
			end)

		-- Remove the extra * at the beginning if it's immediately followed
		-- by a link whose display begins with * too
		if allReconstructed then
			text = text:gsub("^%*%[%[([^|%]]+)|%*", "[[%1|*")
		end
	else
		-- There is no embedded wikilink, make a link using the parameters.
		text = makeLangLink({ target = text, display = data.alt, nocont = data.nocont}, data.lang, data.id, allow_self_link)
	end

	return text
end

function export.mark(text, itemType, face, lang)
	local tag = { "", "" }

	if itemType == "gloss" then
		tag = { '<span class="mention-gloss-double-quote">“</span><span class="mention-gloss">',
			'</span><span class="mention-gloss-double-quote">”</span>' }
	elseif itemType == "tr" then
		if face == "term" then
			tag = { '<span lang="' .. lang:getCode() .. '" class="tr mention-tr Latn">',
				'</span>' }
		else
			tag = { '<span lang="' .. lang:getCode() .. '" class="tr Latn">', '</span>' }
		end
	elseif itemType == "ts" then
		tag = { '<span class="ts mention-ts Latn">/', '/</span>' }
	elseif itemType == "pos" then
		tag = { '<span class="ann-pos">', '</span>' }
	elseif itemType == "annotations" then
		tag = { '<span class="mention-gloss-paren annotation-paren">(</span>',
			'<span class="mention-gloss-paren annotation-paren">)</span>' }
	end

	if type(text) == "string" then
		return tag[1] .. text .. tag[2]
	else
		return ""
	end
end

-- Format the annotations (things following the linked term)
function export.format_link_annotations(data, face)
	local output = {}

	-- Interwiki link
	if data.interwiki then
		table_insert(output, data.interwiki)
	end

	-- Genders
	if type(data.genders) ~= "table" then
		data.genders = { data.genders }
	end

	if data.genders and #data.genders > 0 then
		local m_gen = require("Module:getn")
		table_insert(output, "&nbsp;" .. m_gen.format_list(data.genders, data.lang))
	end

	local annotations = {}

	-- Transliteration and transcription
	if data.tr or data.ts then
		local kind
		if face == "term" then
			kind = face
		else
			kind = "default"
		end

		if data.tr and data.ts then
			table_insert(annotations,
				require("Module:script utilities").tag_translit(data.tr, data.lang, kind)
				.. " " .. export.mark(data.ts, "ts"))
		elseif data.ts then
			table_insert(annotations, export.mark(data.ts, "ts"))
		else
			table_insert(annotations,
				require("Module:script utilities").tag_translit(data.tr, data.lang, kind))
		end
	end

	-- Gloss/translation
	if data.gloss then
		table_insert(annotations, export.mark(data.gloss, "gloss"))
	end

	-- Part of speech
	if data.pos then
		-- debug category for pos= containing transcriptions
		if data.pos:find("/[^><]*/") then
			data.pos = data.pos .. "[[Category:links likely containing transcriptions in pos]]"
		end

		table_insert(annotations, export.mark(pos_tags[data.pos] or data.pos, "pos"))
	end

	-- Literal/sum-of-parts meaning
	if data.lit then
		table_insert(annotations, "literally " .. export.mark(data.lit, "gloss"))
	end

	if #annotations > 0 then
		table_insert(output, " " .. export.mark(table_concat(annotations, ", "), "annotations"))
	end

	return table_concat(output)
end

-- A version of {{l}} or {{m}} that can be called from other modules too
function export.full_link(data, face, allow_self_link, no_check_redundant_translit)
	if type(data) ~= "table" then
		error("The first argument to the function full_link must be a table. "
			.. "See Module:links/documentation for more information.")
	end

	-- Create the link
	local output = {}
	local categories = {}
	local link = ""
	local annotations

	--local m_utilities = require("Module:utilities")

	-- Is there any text to show?
	if (data.term or data.alt) and data.term ~= "-" then
		-- Try to detect the script if it was not provided
		if not data.sc then
			data.sc = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
		else
			-- Track uses of sc parameter
			local best = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
		end

		local class = ""

		local function encode_accel_param(prefix, param)
			-- This is decoded again by [[WT:ACCEL]].
			return param and prefix .. param:gsub("%%", "."):gsub(" ", "_") or ""
		end

		if data.accel then
			local form = data.accel.form and data.accel.form .. "-form-of" or ""
			local gender = encode_accel_param("gender-", data.accel.gender)
			local pos = encode_accel_param("pos-", data.accel.pos)
			local translit = encode_accel_param("transliteration-", data.accel.translit)
			local lemma = encode_accel_param("origin-", data.accel.lemma)
			local lemma_translit = encode_accel_param("origin_transliteration-", data.accel.lemma_translit)
			local no_store = data.accel.no_store and "form-of-nostore" or ""

			local accel =
				form .. " " ..
				gender .. " " ..
				pos .. " " ..
				translit .. " " ..
				lemma .. " " ..
				lemma_translit .. " " ..
				no_store .. " "

			class = "form-of lang-" .. data.lang:getCode() .. " " .. accel
		end

		-- Only make a link if the term has been given, otherwise just show the alt text without a link
		link = require("Module:script utilities").tag_text(
			data.term and export.language_link(data, allow_self_link)
			or data.alt, data.lang, data.sc, face, class)
	else
		--[[	No term to show.
				Is there at least a transliteration we can work from?	]]
		link = require("Module:script utilities").request_script(data.lang, data.sc)
		
		if not data.sc then
			data.sc = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
		else
			-- Track uses of sc parameter
			local best = require("Module:scripts").findBestScript(data.alt or data.term, data.lang)
		end
		
		if link == "" or link == "-" or link == "?" or not data.tr or data.tr == "-" then
			-- No link to show, and no transliteration either. Show a term request.
			local category = ""

			if mw.title.getCurrentTitle().nsText ~= "Template" then
				table_insert(categories, "[[Category:" .. data.lang:getCanonicalName() .. " term requests]]")
			end

			link = "<small>[Term?]</small>"
		end
	end

	table_insert(output, link)

	if data.tr == "" or data.tr == "-" then
		data.tr = nil

	elseif phonetic_extraction[data.lang:getCode()] then
		local m_phonetic = require(phonetic_extraction[data.lang:getCode()])
		data.tr = data.tr or m_phonetic.getTranslit(export.remove_links(data.term))

	elseif (data.term or data.alt) and not data.sc:getCode():find("Lati?n") then

		-- Try to generate a transliteration, unless transliteration has been supplied and either
		-- no_check_redundant_translit is given or we are in a high-memory entry. (Checking for redundant
		-- transliteration can use up significant amounts of memory so we don't want to do it if memory
		-- is tight. `no_check_redundant_translit` is currently set when called ultimately from
		-- {{multitrans|...|no-check-redundant-translit=1}}.)
		if not data.tr then
			local automated_tr = data.lang:transliterate(export.remove_links(data.alt or data.term), data.sc)

			if automated_tr then
				local manual_tr = data.tr

				if manual_tr then
					if manual_tr == automated_tr then
						table_insert(categories,
							"[[Category:Terms with redundant transliterations]]"
									.. "[[Category:Terms with redundant transliterations/" .. data.lang:getCode() .. "]]")
					else
						-- Prevents Arabic root categories from flooding the tracking categories.
						if mw.title.getCurrentTitle().nsText ~= "Category" then
							table_insert(categories,
								"[[Category:Terms with manual transliterations different from the automated ones]]"
										.. "[[Category:Terms with manual transliterations different from the automated ones/" .. data.lang:getCode() .. "]]")
						end
					end
				end

				if (not manual_tr) or data.lang:overrideManualTranslit() then
					data.tr = automated_tr
				end
			end
		end
	end

	-- Link to the transliteration entry for languages that require this
	if data.tr and data.lang:link_tr() then
		data.tr = require("Module:script utilities").tag_text(
			export.language_link({ lang = data.lang, term = data.tr, nocont = data.nocont}, allow_self_link),
			data.lang, data.sc, face, class)
		--data.tr = export.language_link { lang = data.lang, term = data.tr, nocont = data.nocont}
	elseif data.tr then
		data.tr = "''" .. data.tr .. "''"
	end

	table_insert(output, export.format_link_annotations(data, face))

	return table_concat(output) .. table_concat(categories)
end

--[[	Strips links: deletes category links,
		the targets of piped links,
		and all double square brackets.			]]
function export.remove_links(text)
	if type(text) == "table" then
		text = text.args[1]
	end

	if not text or text == "" then
		return ""
	end

	text = mw.ustring.gsub(text, "%[%[Category:[^|%]]-|?[^|%]]-%]%]", "")
	text = text:gsub("%[%[[^|%]]-|", "")
	text = text:gsub("%[%[", "")
	text = text:gsub("%]%]", "")

	return text
end

function export.english_links(text)
	local lang = require("Module:languages").getByCode("en")

	-- Parentheses around function call to remove second return value, the
	-- number of replacements.
	return (text:gsub("%[%[([^%]]+)%]%]",
		function(linktext)
			local link = parseLink(linktext)
			return makeLangLink(link, lang, nil, true, false)
		end))
end

function export.light_link(data)
	local language_names = mw.loadData("Module:languages/code to canonical name")
	local script_codes = mw.loadData("Module:scripts/codes")

	if data.langCode then
		data.langName = language_names[data.langCode] or error('The language code "' .. data.langCode .. '" is not recognized.')
	else
		error('Language code is required.')
	end

	if not data.term then
		error('Term to link to is required.')
	end

	if data.scCode then
		if not script_codes[data.scCode] then
			error('The script code "' .. data.sc .. '" is not recognized.')
		end
	else
		error("The function light_link requires a script code.")
	end

	local fragment
	if data.id then
		fragment = data.langName .. "-" .. mw.uri.encode(data.id, "WIKI")
	else
		fragment = data.langName
	end

	return table_concat {
		'<span class="', data.scCode, '" lang="', data.langCode,
		'">[[', data.term, "#", fragment, "|", (data.alt or data.term), "]]</span>"
	}
end

--[=[
	For example, Norwegian_Bokm.C3.A5l → Norwegian_Bokmål. 0xC3 and 0xA5 are the
	hexadecimal-base representation of the two bytes used to encode the character
	å in the UTF-8 encoding:
		11000011 10100101

	Note that the bytes used to represent a character are actually different from
	the Unicode codepoint. For å, the codepoint is 0xE5. The bits (digits) that
	actually spell the codepoint are found in the brackets: 110[00011] 10[100101].
	For further explanation, see [[w:UTF-8#Description]].
]=]

-- The character class %x should not be used, as it includes the characters a-f,
-- which do not occur in these anchor encodings.
local capitalHex = "[0-9A-F]"

local function decodeAnchor(anchor)
	return (anchor:gsub("%.(" .. capitalHex .. capitalHex .. ")",
		function(hexByte)
			return string.char(tonumber(hexByte, 16))
		end))
end

function export.section_link(link)
	if type(link) ~= "string" then
		error("The first argument to section_link was a " .. type(link) .. ", but it should be a string.")
	end

	link = link:gsub("_", " ")

	local numberSigns = require("Module:string").count(link, "#")

	if numberSigns > 1 then
		error("The section link should only contain one number sign (#).")
	end

	link = mw.uri.decode(link, "WIKI")
	local page, section = link:match("^([^#]*)#(.+)$")
	if page == "" then
		page = nil
	end

	if section then
		section = decodeAnchor(section)

		-- URI-encode (percent-encode) section to allow square brackets and
		-- other dodgy characters in section name.
		-- If not percent-encoded, they prevent the parser from creating a link.
		-- Decode percent-encoding in the displayed text
		if page then
			return "[[" .. page .. "#" .. mw.uri.encode(section, "WIKI")
				.. "|" .. page .. " §&nbsp;" .. section .. "]]"
		else
			return "[[#" .. mw.uri.encode(section, "WIKI")
				.. "|§&nbsp;" .. section .. "]]"
		end
	else
		error("The function “section_link” could not find a number sign marking a section name.")
	end
end

return export