模块:Gloss:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
| (未显示同一用户的8个中间版本) | |||
| 第1行: | 第1行: | ||
local utils_str = require("Module:Utils/String") | local utils_str = require("Module:Utils/String") | ||
local unlines = utils_str.unlines | |||
local trim = utils_str.trim | |||
local concat = table.concat | |||
local gloss = {} | local gloss = {} | ||
| 第20行: | 第24行: | ||
else | else | ||
t[#t+1] = text | t[#t+1] = text | ||
end | |||
end | |||
local function append_styled_paragraph(t, style) | |||
if style ~= nil then | |||
t[#t+1] = "<p style=\"" | |||
t[#t+1] = style | |||
t[#t+1] = "\">" | |||
else | |||
t[#t+1] = "<p>" | |||
end | end | ||
end | end | ||
gloss.show = function(frame) | gloss.show = function(frame) | ||
local | local texts = unlines(frame.args[1]) | ||
local | local tips_raw = frame.args[2] | ||
local styles = unlines(frame.args.styles or "") | |||
local mode = frame.args.mode | |||
local t = {} | local t = {} | ||
local i = 1 | local i = 1 | ||
local | if tips_raw == nil or tips_raw == "" then | ||
for j = 1, #texts do | |||
append_styled_paragraph(t, styles[j]) | |||
t[#t+1] = trim(texts[j]) | |||
t[#t+1] = "</p>" | |||
end | |||
return frame:preprocess(concat(t)) | |||
end | |||
local tips = unlines(tips_raw, "\n;") | |||
if mode == nil or mode == "" or mode == "空格" then | if mode == nil or mode == "" or mode == "空格" then | ||
for word in string.gmatch( | for j = 1, #texts do | ||
append_styled_paragraph(t, styles[j]) | |||
local prev_i = i | |||
i = | for word in string.gmatch(texts[j], "([^ ]+)") do | ||
if word ~= "" then | |||
format_tooltip(t, word, tips[i]) | |||
i = i + 1 | |||
end | |||
t[#t+1] = " " | |||
end | |||
if prev_i ~= i then | |||
t[#t] = "</p>" | |||
else | |||
t[#t+1] = "</p>" | |||
end | end | ||
end | end | ||
elseif mode == "分隔符" then | elseif mode == "分隔符" then | ||
local delimiter = frame.args.delimiter | local delimiter = frame.args.delimiter | ||
| 第45行: | 第78行: | ||
delimiter = ";" | delimiter = ";" | ||
end | end | ||
for word in string.gmatch( | for j = 1, #texts do | ||
append_styled_paragraph(t, styles[j]) | |||
local prev_i = i | |||
for word in string.gmatch(texts[j], "([^"..delimiter.."]+)") do | |||
if word == " " then | |||
t[#t+1] = " " | |||
elseif word ~= "" then | |||
format_tooltip(t, word, tips[i]) | |||
i = i + 1 | |||
end | |||
end | end | ||
t[#t+1] = "</p>" | |||
end | end | ||
end | end | ||
return frame:preprocess( | return frame:preprocess(concat(t)) | ||
end | end | ||
return gloss | return gloss | ||
2025年10月29日 (三) 23:04的最新版本
此模块的文档可以在模块:Gloss/doc创建
local utils_str = require("Module:Utils/String")
local unlines = utils_str.unlines
local trim = utils_str.trim
local concat = table.concat
local gloss = {}
local function split(text, delimiters)
local parts = {}
for part in string.gmatch(text, "([^"..delimiters.."]+)") do
parts[#parts+1] = part
end
return parts
end
local function format_tooltip(t, text, tip)
if tip ~= "" and tip ~= nil then
t[#t+1] = "{{#tip-text:"
t[#t+1] = text
t[#t+1] = "|"
t[#t+1] = tip
t[#t+1] = "}}"
else
t[#t+1] = text
end
end
local function append_styled_paragraph(t, style)
if style ~= nil then
t[#t+1] = "<p style=\""
t[#t+1] = style
t[#t+1] = "\">"
else
t[#t+1] = "<p>"
end
end
gloss.show = function(frame)
local texts = unlines(frame.args[1])
local tips_raw = frame.args[2]
local styles = unlines(frame.args.styles or "")
local mode = frame.args.mode
local t = {}
local i = 1
if tips_raw == nil or tips_raw == "" then
for j = 1, #texts do
append_styled_paragraph(t, styles[j])
t[#t+1] = trim(texts[j])
t[#t+1] = "</p>"
end
return frame:preprocess(concat(t))
end
local tips = unlines(tips_raw, "\n;")
if mode == nil or mode == "" or mode == "空格" then
for j = 1, #texts do
append_styled_paragraph(t, styles[j])
local prev_i = i
for word in string.gmatch(texts[j], "([^ ]+)") do
if word ~= "" then
format_tooltip(t, word, tips[i])
i = i + 1
end
t[#t+1] = " "
end
if prev_i ~= i then
t[#t] = "</p>"
else
t[#t+1] = "</p>"
end
end
elseif mode == "分隔符" then
local delimiter = frame.args.delimiter
if delimiter == nil or delimiter == "" then
delimiter = ";"
end
for j = 1, #texts do
append_styled_paragraph(t, styles[j])
local prev_i = i
for word in string.gmatch(texts[j], "([^"..delimiter.."]+)") do
if word == " " then
t[#t+1] = " "
elseif word ~= "" then
format_tooltip(t, word, tips[i])
i = i + 1
end
end
t[#t+1] = "</p>"
end
end
return frame:preprocess(concat(t))
end
return gloss