Module:links: Difference between revisions

Jump to navigation Jump to search
1,403 bytes added ,  13 October 2025
no edit summary
No edit summary
No edit summary
Line 10: Line 10:
[[Module:scripts]]
[[Module:scripts]]
[[Module:languages]] and its submodules
[[Module:languages]] and its submodules
[[Module:gender and number]]
[[Module:getn]]
]=]
]=]


Line 295: Line 295:


local pos_tags
local pos_tags
function export.get_link_page(target, lang, sc, plain)
--[==[
Given a link target as passed to `full_link()`, get the actual page that the target refers to. This removes
bold, italics, strip markets and HTML; calls `makeEntryName()` for the language in question; converts targets
beginning with `*` to the Reconstruction namespace; and converts appendix-constructed languages to the Appendix
namespace. Returns up to three values: the page, the original target minus any anti-asterisk !!, and a third
value `true` if the target had a backslash-escaped * in it.
]==]
function export.get_link_page_with_auto_display(target, lang, sc, plain)
local orig_target = target
 
if not target then
if not target then
return nil
return nil
Line 303: Line 312:
if target:sub(1, 1) == ":" then
if target:sub(1, 1) == ":" then
return target:sub(2)
-- FIXME, the auto_display (second return value) should probably remove the colon
return target:sub(2), orig_target
end
end
Line 316: Line 326:
load_data("Module:data/interwikis")[prefix]
load_data("Module:data/interwikis")[prefix]
) then
) then
return target
return target, orig_target
end
end
end
end


-- Check if the term is reconstructed and remove any asterisk. Otherwise, handle the escapes.
-- Check if the term is reconstructed and remove any asterisk. Also check for anti-asterisk (!!).
local reconstructed, escaped
-- Otherwise, handle the escapes.
local reconstructed, escaped, anti_asterisk
if not plain then
if not plain then
target, reconstructed = target:gsub("^%*(.)", "%1")
target, reconstructed = target:gsub("^%*(.)", "%1")
if reconstructed == 0 then
target, anti_asterisk = target:gsub("^!!(.)", "%1")
if anti_asterisk == 1 then
-- Remove !! from original. FIXME! We do it this way because the call to remove_formatting() above
-- may cause non-initial !! to be interpreted as anti-asterisks. We should surely move the
-- remove_formatting() call later.
orig_target = orig_target:gsub("^!!", "")
end
end
end
end
target, escaped = target:gsub("^(\\-)\\%*", "%1*")
target, escaped = target:gsub("^(\\-)\\%*", "%1*")
Line 350: Line 370:
if lang:getFullCode() == "und" then
if lang:getFullCode() == "und" then
return nil
return nil
elseif lang:hasType("conlang") then
target = "*" .. target
elseif not lang:hasType("conlang") then
target = "Reconstruction:" .. lang:getFullName() .. "/" .. target
end
end
target = "Reconstruction:" .. lang:getFullName() .. "/" .. target
if not lang:hasType("conlang") then
target = "wikt:Reconstruction:" .. lang:getFullName() .. "/" .. target
end
-- Reconstructed languages and substrates require an initial *.
-- Reconstructed languages and substrates require an initial *.
elseif lang:hasType("reconstructed") or lang:getFamilyCode() == "qfa-sub" then
elseif anti_asterisk ~= 1 and (lang:hasType("reconstructed") or lang:getFamilyCode() == "qfa-sub") then
--error("The specified language " .. lang:getCanonicalName() .. " is unattested, while the given term does not begin with '*' to indicate that it is reconstructed.")
error("The specified language " .. lang:getCanonicalName()
if lang:hasType("conlang") then
.. " is unattested, while the given term does not begin with '*' to indicate that it is reconstructed.")
target = "*" .. target
elseif not lang:hasType("conlang") then
target = "Reconstruction:" .. lang:getFullName() .. "/" .. target
end
elseif lang:hasType("appendix-constructed") then
elseif lang:hasType("appendix-constructed") then
target = "Appendix:" .. lang:getFullName() .. "/" .. target
target = "wikt:Appendix:" .. lang:getFullName() .. "/" .. target
else
else
target = target
target = target
end
end
 
target = (lang:hasType("conlang") and "Contionary:" or "wikt:") .. target
return target, orig_target, escaped > 0
end
return target, escaped > 0
 
function export.get_link_page(target, lang, sc, plain)
local target, auto_display, escaped = export.get_link_page_with_auto_display(target, lang, sc, plain)
return target, escaped
end
end


Line 388: Line 405:
link.target, link.fragment = get_fragment(link.target)
link.target, link.fragment = get_fragment(link.target)
end
end
-- Process the target
local auto_display, escaped
link.target, auto_display, escaped = export.get_link_page_with_auto_display(link.target, lang, sc, plain)


-- Create a default display form.
-- Create a default display form.
local auto_display = link.target
-- If the target is "" then it's a link like [[#English]], which refers to the current page.
-- If the target is "" then it's a link like [[#English]], which refers to the current page.
if auto_display == "" then
if auto_display == "" then
auto_display = load_data("Module:headword/data").pagename
auto_display = load_data("Module:headword/data").pagename
end
end
-- Process the target
local escaped
link.target, escaped = export.get_link_page(link.target, lang, sc, plain)


-- If the display is the target and the reconstruction * has been escaped, remove the escaping backslash.
-- If the display is the target and the reconstruction * has been escaped, remove the escaping backslash.
Line 485: Line 501:
link.fragment = link.fragment and encode_entities(remove_formatting(link.fragment), "#%&+/:<=>@[\\]_{|}")
link.fragment = link.fragment and encode_entities(remove_formatting(link.fragment), "#%&+/:<=>@[\\]_{|}")
if lang:hasType("reconstructed") and not lang:hasType("conlang") then
link.display = "*" .. link.display
link.display = link.display:gsub("%*%*", "*")
link.display = link.display:gsub("%*%?", "")
end
return "[[" .. link.target:gsub("^[^:]", ":%0") .. (link.fragment and "#" .. link.fragment or "") .. "|" .. link.display .. "]]"
return "[[" .. link.target:gsub("^[^:]", ":%0") .. (link.fragment and "#" .. link.fragment or "") .. "|" .. link.display .. "]]"
end
end
Line 566: Line 575:
end
end


if all_reconstructed and not link.target:match("^%*") then
if all_reconstructed then
link.target = "*" .. link.target
if link.target:find("^!!") then
-- Check for anti-asterisk !! at the beginning of a target, indicating that a reconstructed term
-- wants a part of the term to link to a non-reconstructed term, e.g. Old English
-- {{ang-noun|m|head=*[[!!Crist|Cristes]] [[!!mæsseǣfen]]}}.
link.target = link.target:sub(3)
-- Also remove !! from the display, which may have been copied from the target (as in mæsseǣfen in
-- the example above).
link.display = link.display:gsub("^!!", "")
elseif not link.target:match("^%*") then
link.target = "*" .. link.target
end
end
end


Line 1,062: Line 1,081:
-- Create the link
-- Create the link
local output = {}
local output = {}
local id, no_alt_ast, srwc, accel = data.id, data.no_alt_ast, data.suppress_redundant_wikilink_cat, data.accel
local id, no_alt_ast, srwc, accel, nevercalltr = data.id, data.no_alt_ast, data.suppress_redundant_wikilink_cat, data.accel, data.never_call_transliteration_module


for i in ipairs(terms) do
for i in ipairs(terms) do
Line 1,111: Line 1,130:
-- simple_link can return nil, so check if a link has been generated.
-- simple_link can return nil, so check if a link has been generated.
if link then
if link then
link = tag_text(link, lang, data.sc[i], face, get_class(lang, data.tr[i], accel))
-- Add "nowrap" class to prefixes in order to prevent wrapping after the hyphen
local nowrap = ""
local display_term = data.alt[i] or data.term[i]
if display_term and (sub(display_term, 1, 1) == "-" or mw.ustring.sub(display_term, 1, 1) == "־") then -- "sub" does not work for the Hebrew-script hyphen
nowrap = " nowrap"
end
link = tag_text(link, lang, data.sc[i], face, get_class(lang, data.tr[i], accel) .. nowrap)
else
else
--[[ No term to show.
--[[ No term to show.
Line 1,143: Line 1,169:


elseif (data.term[1] or data.alt[1]) and data.sc[1]:isTransliterated() then
elseif (data.term[1] or data.alt[1]) and data.sc[1]:isTransliterated() then
-- Track whenever there is manual translit. The categories below like 'terms with redundant transliterations'
-- aren't sufficient because they only work with reference to automatic translit and won't operate at all in
-- languages without any automatic translit, like Persian and Hebrew.
if data.tr[1] then
local full_code = lang:getFullCode()
end


-- Try to generate a transliteration.
if not nevercalltr then
local text = data.alt[1] or data.term[1]
-- Try to generate a transliteration.
if not lang:link_tr(data.sc[1]) then
local text = data.alt[1] or data.term[1]
text = export.remove_links(text, true)
if not lang:link_tr(data.sc[1]) then
end
text = export.remove_links(text, true)
 
end
local automated_tr, tr_categories
automated_tr, data.tr_fail, tr_categories = lang:transliterate(text, data.sc[1])
local automated_tr, tr_categories
 
automated_tr, data.tr_fail, tr_categories = lang:transliterate(text, data.sc[1])
if automated_tr or data.tr_fail then
local manual_tr = data.tr[1]
if automated_tr or data.tr_fail then
 
local manual_tr = data.tr[1]
if manual_tr then
if (export.remove_links(manual_tr) == export.remove_links(automated_tr)) and (not data.tr_fail) then
if manual_tr then
insert(cats, lang:getFullName() .. " terms with redundant transliterations")
if (export.remove_links(manual_tr) == export.remove_links(automated_tr)) and (not data.tr_fail) then
elseif not data.tr_fail then
insert(cats, lang:getFullName() .. " terms with redundant transliterations")
-- Prevents Arabic root categories from flooding the tracking categories.
elseif not data.tr_fail then
if NAMESPACE ~= 14 then -- Category:
-- Prevents Arabic root categories from flooding the tracking categories.
insert(cats, lang:getFullName() .. " terms with non-redundant manual transliterations")
if NAMESPACE ~= 14 then -- Category:
insert(cats, lang:getFullName() .. " terms with non-redundant manual transliterations")
end
end
end
end
end
end
if (not manual_tr) or lang:overrideManualTranslit(data.sc[1]) then
if (not manual_tr) or lang:overrideManualTranslit(data.sc[1]) then
data.tr[1] = automated_tr
data.tr[1] = automated_tr
for _, category in ipairs(tr_categories) do
for _, category in ipairs(tr_categories) do
insert(cats, category)
insert(cats, category)
end
end
end
end
end

Navigation menu