--[=[ 此模块包含[[少女前线]]相关模板使用的工具函数。 ]=] local ugsub = mw.ustring.gsub local format = string.format local utils = {} --- 为星级添加颜色。 function utils.colored_rarity(frame) local arg = assert(frame.args[1], '未提供参数') local rarity_color = { '#865c7d', '#777777', '#33566f', '#7b813f', '#a7753b', '#ad4229' } -- 3 -> '<span style="...">★★★</span>' -- 9 -> nil local function make_colored_rarity(rarity) local color = rarity_color[rarity] if not color then return nil end return format('<span style="color:%s">%s</span>', color, ('★'):rep(rarity)) end -- '3' -> <span style="...">★★★</span> -- '9' -> '9' local function colorize_number(str) return make_colored_rarity(tonumber(str)) or str end -- '★★★' -> <span style="...">★★★</span> -- '★★★★★★★★★' -> '★★★★★★★★★' local function colorize_star(start, str, stop) return make_colored_rarity(stop - start) or str end local out = arg:match('^%s*(.-)%s*$') -- 去首尾空白 out = out:gsub('%s*→%s*', ' → '):gsub('%s*->%s*', ' → ') -- 调整箭头 out = ugsub(out, '()(★+)()', colorize_star) out = out:gsub('%d+', colorize_number) return out end return utils