Dokumentationen för denna modul kan skapas på Modul:Skalman/grammar-link/dok /test
Modul:Skalman/grammar-link/dok
local lk = require("Modul:link")
local lang = require("Modul:lang")
local export = {}
local function link(str, link_type, lang_code, pos_code, mode)
-- Kod för att hantera mode=export i den här funktionen
local new_str = ""
local magic_characters_set = "%*%+%-%?]";
local is_standalone = not not pos_code and pos_code ~= ""
local should_include_anchor = not not lang_code and lang_code ~= ""
local function trimLeft(s)
return mw.ustring.gsub(s, "^%s*", "")
end
local function escapeMagicCharacters(s)
return string.gsub(s, magic_characters_set, "%%%1")
end
local function addSpanTagsForEachLink(s)
for link_str in mw.ustring.gmatch(s, "%]*%]%]") do
local link_str_with_span_tag = '<span class="' .. link_type .. '">' .. link_str .. '</span>'
local escaped_link_str = escapeMagicCharacters(link_str)
s = mw.ustring.gsub(s, escaped_link_str, link_str_with_span_tag)
end
return s
end
local function isSpecialCase(s)
return not not (s == " " or s == "–" or s == "?")
end
local function maybeLink(s)
local contains_no_space = not mw.ustring.find(s, " ")
local should_try_to_add_link = link_type ~= "no-link" and contains_no_space
local should_add_spans = link_type ~= "link-only" and not isSpecialCase(s)
if should_try_to_add_link then
if should_include_anchor then
s = lk.link(s, lang_code)
else
s = lk.link(s)
end
end
if should_add_spans then
s = addSpanTagsForEachLink(s)
end
s = s == "-" and "–" or s
return s
end
for word_str in mw.ustring.gmatch(str, '(+)') do
word_str = trimLeft(word_str)
word_str = maybeLink(word_str)
new_str = new_str .. (new_str == "" and "" or ", ") .. word_str
end
new_str = new_str == "" and " " or new_str
if is_standalone then
new_str = '<span class="grammar-inline" data-lang="' .. lang_code .. '" data-h3="' .. pos_code .. '">' .. new_str .. '</span>'
end
return new_str
end
function export.link(arg, link_type, lang_code, pos_code)
if arg == mw.getCurrentFrame() then
return link(
frame.args or "",
frame.args,
frame.args,
frame.args,
"wikitext"
)
else
return link(
arg or "",
link_type,
lang_code,
pos_code,
"wikitext"
)
end
end
function export.exportLink(arg, link_type)
if arg == mw.getCurrentFrame() then
return link(
frame.args or "",
frame.args,
nil,
nil,
"export"
)
else
return link(
arg or "",
link_type,
nil,
nil,
"export"
)
end
end
return export