Modul:link
Fra sv:Modul:link.
local lang = require("Modul:lang")
local export = {}
local m_pagename = mw.title.getCurrentTitle().text
function export.isValidLinkTarget(arg)
local str
if arg == mw.getCurrentFrame() then
local frame = arg
str = frame.args[1]
else
str = arg
end
local success, res = pcall(mw.title.new, str)
return not not res
end
function export.link(arg1, arg2)
local str
local lang_code
if arg1 == mw.getCurrentFrame() then
local frame = arg1
str = frame.args[1] or ""
lang_code = frame.args[2] or ""
else
str = arg1 or ""
lang_code = arg2 or ""
end
local language = lang.hasLanguage(lang_code) and lang.getLanguageUCFirst(lang_code)
while mw.ustring.find(str, "%[%[[^%[%]]*,[^%[%]]*%]%]") do
str = mw.ustring.gsub(str, "(%[%[[^%[%]]*),([^%[%]]*%]%])", "%1ESCAPE-COMA-EPACSE%2")
end
str = mw.ustring.gsub(str, "%&%#", "ESCAPE-AMPERSAND-HASH-EPACSE");
local strs = mw.text.split(str,"%s*,%s*")
local new_str = ""
for i,s in ipairs(strs) do
local is_special_string = s == "" or s == "-" or s == "?"
s = s == "-" and "–" or s
s = s == "" and " " or s
local should_add_brackets = not is_special_string and export.isValidLinkTarget(s)
local should_add_anchor = true --language and m_pagename ~= s and s ~= ("[[" .. m_pagename .. "]]")
if should_add_brackets then
if should_add_anchor then
s = "[[" .. lang.getEntryName(lang_code, s) .. "#" .. language .. "|" .. s .. "]]"
else
s = "[[" .. s .. "]]"
end
else
if should_add_anchor then
local matches1 = mw.ustring.gmatch(s, "%[%[([^%]:%|%#]+)#[^%]:%|]+%|[^%]:%|]+%]%]")
local matches2 = mw.ustring.gmatch(s, "%[%[([^%]:%|%#]+)%|[^%]:%|]+%]%]")
local matches3 = mw.ustring.gmatch(s, "%[%[([^%]:%|%#]+)%]%]")
for match in matches1 do
local entry_name = lang.getEntryName(lang_code, match)
s = mw.ustring.gsub( s, "%[%[([^%]:%|%#]+)#[^%]:%|]+(%|[^%]:%|]+)%]%]", ("[[" .. entry_name .. "ESCAPE-HASH-EPACSE" .. language .. "%2]]"),1)
end
for match in matches2 do
local entry_name = lang.getEntryName(lang_code, match)
s = mw.ustring.gsub( s, "%[%[([^%]:%|%#]+)(%|[^%]:%|]+)%]%]", ("[[" .. entry_name .. "#" .. language .. "%2]]"),1)
end
for match in matches3 do
local entry_name = lang.getEntryName(lang_code, match)
s = mw.ustring.gsub( s, "%[%[([^%]:%|%#]+)%]%]", ("[[" .. entry_name .. "#" .. language .. "|%1]]"),1)
end
end
end
new_str = new_str .. s
if i ~= #strs then
new_str = new_str .. ", "
end
end
new_str = mw.ustring.gsub(new_str, "ESCAPE%-AMPERSAND%-HASH%-EPACSE", "&#");
new_str = mw.ustring.gsub(new_str, "ESCAPE%-HASH%-EPACSE", "#");
new_str = mw.ustring.gsub(new_str, "ESCAPE%-COMA%-EPACSE", ",")
return new_str
end
return export