模块:Lyrics
来自永雏塔菲百科
更多操作
此模块的文档可以在模块:Lyrics/doc创建
local p = {}
local function trim(s)
if not s then return "" end
return mw.text.trim(s)
end
local function truthy(v)
if not v then return false end
v = mw.ustring.lower(trim(v))
return v == "1" or v == "yes" or v == "true" or v == "y" or v == "是"
end
local function escape(s)
return mw.text.nowiki(s or "")
end
local function sanitizeColor(c)
c = trim(c)
if c == "" then
return nil
end
-- 只允许常见十六进制颜色,避免 CSS 注入
if mw.ustring.match(c, "^#%x%x%x$") or
mw.ustring.match(c, "^#%x%x%x%x%x%x$") or
mw.ustring.match(c, "^#%x%x%x%x%x%x%x%x$") then
return c
end
return nil
end
local function parseRoleColors(text)
local map = {}
local list = {}
text = text or ""
text = text:gsub("\r\n", "\n"):gsub("\r", "\n")
for rawLine in mw.text.gsplit(text, "\n", true) do
local line = trim(rawLine)
if line ~= "" then
local name, color = mw.ustring.match(line, "^([^=::]+)%s*[=::]%s*(.+)$")
if name and color then
name = trim(name)
color = sanitizeColor(color)
if name ~= "" and color then
map[name] = color
table.insert(list, {
name = name,
color = color
})
end
end
end
end
return map, list
end
local function expectedCellCount(showJp, showRomaji, showZh)
local n = 0
if showJp then n = n + 1 end
if showRomaji then n = n + 1 end
if showZh then n = n + 1 end
return n
end
-- 支持假名标注:
-- 《漢字|かな》
-- 也兼容 《漢字|かな》
local function renderRuby(text)
text = text or ""
local out = {}
local pos = 1
while true do
local s1, e1, base1, rt1 = mw.ustring.find(text, "《([^《》||]+)[||]([^《》]+)》", pos)
if not s1 then
table.insert(out, escape(mw.ustring.sub(text, pos)))
break
end
table.insert(out, escape(mw.ustring.sub(text, pos, s1 - 1)))
table.insert(out,
"<ruby><rb>" ..
escape(base1) ..
"</rb><rt>" ..
escape(rt1) ..
"</rt></ruby>"
)
pos = e1 + 1
end
return table.concat(out)
end
local function removeRubyMarkup(text)
text = text or ""
-- 去掉 《漢字|かな》 或 《漢字|かな》 的假名部分,只保留漢字
text = mw.ustring.gsub(text, "《([^《》||]+)[||]([^《》]+)》", "%1")
return text
end
local function splitLine(line, sep)
local cells = mw.text.split(line, sep, true)
for i, v in ipairs(cells) do
cells[i] = trim(v)
end
return cells
end
-- Opt-in inline highlights: 【颜色名|文字】, using the existing safe color map.
local function renderInline(text, roleColorMap, render)
text = text or ""
local out = {}
local pos = 1
while true do
local first, last, role, value = mw.ustring.find(text, "【([^【】|]+)|([^【】]+)】", pos)
if not first then
table.insert(out, render(mw.ustring.sub(text, pos)))
break
end
table.insert(out, render(mw.ustring.sub(text, pos, first - 1)))
local color = roleColorMap[trim(role)]
if color then
table.insert(out, tostring(mw.html.create("span")
:addClass("lyrics-inline-color")
:attr("style", "color:" .. color .. ";")
:wikitext(render(value))))
else
table.insert(out, render(mw.ustring.sub(text, first, last)))
end
pos = last + 1
end
return table.concat(out)
end
function p.render(frame)
local args = frame:getParent() and frame:getParent().args or frame.args
local data = args.data or args[1] or ""
local sep = args.sep or "||"
local title = args.title
local note = args.note
local colors = args.colors or args.roleColors or ""
local mode = trim(args.mode or "")
local showRomaji = args.showRomaji == nil or truthy(args.showRomaji)
local showZh = args.showZh == nil or truthy(args.showZh)
local showFuri = args.showFuri == nil or truthy(args.showFuri)
local showJp = true
if mode == "ja-ro-zh" then
showJp = true
showRomaji = true
showZh = true
elseif mode == "ja-zh" then
showJp = true
showRomaji = false
showZh = true
elseif mode == "ja" then
showJp = true
showRomaji = false
showZh = false
elseif mode == "zh" then
showJp = false
showRomaji = false
showZh = true
end
local roleColorMap, roleColorList = parseRoleColors(colors)
local useInlineColors = truthy(args.inlineColors)
local function renderCell(text, japanese)
local render = escape
if japanese then
if showFuri then
render = renderRuby
else
text = removeRubyMarkup(text)
end
end
if useInlineColors then
return renderInline(text, roleColorMap, render)
end
return render(text)
end
local jpLabel = args.jpLabel or "日文"
local romajiLabel = args.romajiLabel or "罗马音"
local zhLabel = args.zhLabel or "中文"
local root = mw.html.create("div")
:addClass("lyrics-box")
:addClass(args.class or "")
if mode ~= "" then
root:addClass("lyrics-mode-" .. mode)
end
if title and trim(title) ~= "" then
root:tag("div")
:addClass("lyrics-title")
:wikitext(escape(title))
end
if note and trim(note) ~= "" then
root:tag("div")
:addClass("lyrics-note")
:wikitext(escape(note))
end
if #roleColorList > 0 then
local legend = root:tag("div")
:addClass("lyrics-color-legend")
legend:tag("div")
:addClass("lyrics-color-legend-title")
:wikitext("颜色说明")
local items = legend:tag("div")
:addClass("lyrics-color-legend-items")
for _, item in ipairs(roleColorList) do
local one = items:tag("span")
:addClass("lyrics-color-legend-item")
one:tag("span")
:addClass("lyrics-color-dot")
:attr("style", "background-color:" .. item.color .. ";")
one:tag("span")
:addClass("lyrics-color-name")
:wikitext(escape(item.name))
one:tag("span")
:addClass("lyrics-color-code")
:wikitext(escape(item.color))
end
end
local header = root:tag("div"):addClass("lyrics-header")
if showJp then
header:tag("div")
:addClass("lyrics-cell lyrics-jp")
:wikitext(escape(jpLabel))
end
if showRomaji then
header:tag("div")
:addClass("lyrics-cell lyrics-romaji")
:wikitext(escape(romajiLabel))
end
if showZh then
header:tag("div")
:addClass("lyrics-cell lyrics-zh")
:wikitext(escape(zhLabel))
end
local body = root:tag("div"):addClass("lyrics-body")
data = data:gsub("\r\n", "\n"):gsub("\r", "\n")
for rawLine in mw.text.gsplit(data, "\n", true) do
local line = trim(rawLine)
if line ~= "" then
local cells = splitLine(line, sep)
local jp = ""
local romaji = ""
local zh = ""
local role = ""
local roleColor = nil
local expected = expectedCellCount(showJp, showRomaji, showZh)
local offset = 0
-- 新格式:角色@@日文@@罗马音@@中文
-- 旧格式:日文@@罗马音@@中文
-- 只有第一列能在 colors / roleColors 中找到颜色时,才把第一列当作角色名
if #cells >= expected + 1 then
local possibleRole = cells[1] or ""
if possibleRole ~= "" and roleColorMap[possibleRole] then
role = possibleRole
roleColor = roleColorMap[possibleRole]
offset = 1
end
end
if mode == "zh" then
zh = cells[1 + offset] or ""
elseif showJp and showRomaji and showZh then
jp = cells[1 + offset] or ""
romaji = cells[2 + offset] or ""
zh = cells[3 + offset] or ""
elseif showJp and showZh then
jp = cells[1 + offset] or ""
zh = cells[2 + offset] or ""
elseif showJp and showRomaji then
jp = cells[1 + offset] or ""
romaji = cells[2 + offset] or ""
elseif showJp then
jp = cells[1 + offset] or ""
elseif showZh then
zh = cells[1 + offset] or ""
end
local row = body:tag("div")
:addClass("lyrics-row")
if role ~= "" then
row:attr("data-role", role)
end
if roleColor then
row:addClass("lyrics-has-role-color")
:attr("style", "--lyrics-role-color:" .. roleColor .. ";")
end
if showJp then
row:tag("div")
:addClass("lyrics-cell lyrics-jp")
:wikitext(renderCell(jp, true))
end
if showRomaji then
row:tag("div")
:addClass("lyrics-cell lyrics-romaji")
:wikitext(renderCell(romaji, false))
end
if showZh then
row:tag("div")
:addClass("lyrics-cell lyrics-zh")
:wikitext(renderCell(zh, false))
end
end
end
return tostring(root)
end
return p