local p = {}
local getArgs = require('Module:Arguments').getArgs
local State = {
pick = nil,
color = nil,
css = nil,
url = nil,
isNextLast = false,
parts = {}
}
function State:reset()
self.pick = nil
self.color = nil
self.css = nil
self.url = nil
end
local function processSpecialPrefix(value)
if value == "\\end" then
return false, "end"
end
local firstChar = value:sub(1, 1)
if firstChar == "-" then
local secondChar = value:sub(2, 2)
if secondChar == "@" then
local prefix = value:sub(3, 6)
if prefix == "css-" then
State.css = value:sub(7)
return true
elseif prefix == "url-" then
State.url = value:sub(7)
return true
end
elseif secondChar == "#" then
State.color = value:sub(3)
return true
else
State.pick = value:sub(2)
return true
end
end
return false
end
local function generateStyle()
local style = ""
if State.color then
style = style .. "color:" .. State.color .. ";"
end
if State.css then
style = style .. State.css .. ";"
end
return style
end
local function generateLink(value, args, isLast)
local linkText = ""
local displayText = State.pick or value
if State.url then
linkText = State.url
else
local prefix = args.prefix or ""
local suffix = args.suffix or ""
local colon = args.c or "%3A"
linkText = prefix .. colon .. value .. suffix
end
local style = generateStyle()
local link
if style ~= "" then
link = string.format("[[%s|<span style=\"%s\">%s</span>]]",
linkText, style, displayText)
else
link = string.format("[[%s|%s]]", linkText, displayText)
end
return link, isLast
end
function p._main(args)
local resultParts = {}
local params = {}
for i = 1, 100 do
local value = args[i]
if not value then break end
table.insert(params, value)
end
for i = 1, #params do
local value = params[i]
if value == "end" then
State.isNextLast = true
else
local isSpecial, escapedValue = processSpecialPrefix(value)
if escapedValue then
value = escapedValue
isSpecial = false
end
if not isSpecial then
local isLast = State.isNextLast
local link, lastFlag = generateLink(value, args, isLast)
table.insert(resultParts, {
link = link,
isLast = isLast
})
State:reset()
State.isNextLast = false
if isLast then
break
end
end
end
end
if #resultParts == 0 then
return ""
end
local splitter = args.split or " • "
local result = ""
for i, part in ipairs(resultParts) do
result = result .. part.link
if not part.isLast and i < #resultParts then
result = result .. splitter
end
end
return result
end
function p.main(frame)
local args = getArgs(frame, {
parentFirst = true,
})
return p._main(args)
end
return p