Module:CharTree/zh-hant: Difference between revisions
From Elwiki
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
local p = {} | local p = {} | ||
local get_char_list = require('Module:CharList').main | local get_char_list = require('Module:CharList').main | ||
-- 假設的 split 函數(如果 Module:CommonFunctions 未提供) | |||
local function split(str, delimiter) | |||
local result = {} | |||
for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do | |||
table.insert(result, match) | |||
end | |||
return result | |||
end | |||
-- 假設的 indexOf 函數(如果 Module:CommonFunctions 未提供) | |||
local function indexOf(value, tbl) | |||
for i, v in ipairs(tbl) do | |||
if v == value then | |||
return true | |||
end | |||
end | |||
return false | |||
end | |||
-- Main process | -- Main process | ||
function p.main(frame) | function p.main(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local char_outer = mw.html.create('div'):addClass('character-banner-outer'):attr('data-lang', args.lang) | local char_outer = mw.html.create('div'):addClass('character-banner-outer'):attr('data-lang', args.lang) | ||
local char_banner = mw.html.create('div'):addClass('character-banner-wrap border-color tone-2') | local char_banner = mw.html.create('div'):addClass('character-banner-wrap border-color tone-2') | ||
local char_list = get_char_list(frame, args.lang) | local char_list = get_char_list(frame, args.lang) | ||
local lang = | |||
-- 規範化 lang 格式,確保始終以斜杠開頭 | |||
local lang = args.lang and ('/' .. args.lang) or '' | |||
local is_localized = lang and lang ~= '' | local is_localized = lang and lang ~= '' | ||
local char_order = char_list[1] | local char_order = char_list[1] | ||
local not_exist = char_list[3] | local not_exist = char_list[3] | ||
local char_list = char_list[2] | local char_list = char_list[2] | ||
-- 日誌:檢查 lang 的值 | |||
mw.log("lang: " .. tostring(lang)) | |||
mw.log("args.lang: " .. tostring(args.lang)) | |||
function split_lang(str) | function split_lang(str) | ||
local lang_l = is_localized and lang or '' | local lang_l = is_localized and lang or '' | ||
return | if not lang_l or lang_l == '' then | ||
return {str, str} | |||
end | |||
-- 清理 lang_l 的格式,移除前導斜杠以匹配無斜杠的情況 | |||
local clean_lang = lang_l:gsub('^/', '') | |||
if string.find(str, lang_l .. '|') or string.find(str, clean_lang .. '|') then | |||
local split_result = split(str, lang_l .. '|') | |||
if #split_result == 0 then | |||
split_result = split(str, clean_lang .. '|') | |||
end | |||
return split_result | |||
elseif string.find(str, lang_l) or string.find(str, clean_lang) then | |||
-- 處理僅包含語言標籤的情況,例如 Elsword/zh-hant | |||
local actual_name = str:gsub(lang_l, ''):gsub(clean_lang, '') | |||
return {actual_name, actual_name} | |||
end | |||
return {str, str} | |||
end | end | ||
function str_localized(str) | function str_localized(str) | ||
return string.find(str, | if not str or not lang or lang == '' then | ||
return false | |||
end | |||
-- 檢查是否包含 {lang}| 或僅包含 {lang} | |||
return string.find(str, lang .. '|') or string.find(str, lang:gsub('^/', '')) | |||
end | end | ||
local base_arg = args[1] or 'Elsword | local base_arg = args[1] or 'Elsword' | ||
local base_name = str_localized(base_arg) and split_lang(base_arg)[1] or base_arg | local base_name = str_localized(base_arg) and split_lang(base_arg)[1] or base_arg | ||
local char_tree = mw.html.create('div'):addClass('char-banner-tree') | local char_tree = mw.html.create('div'):addClass('char-banner-tree') | ||
local out = '' | local out = '' | ||
local char, file_name, link, nopath, nopathnojob, entry, image_arg, icon_str | local char, file_name, link, nopath, nopathnojob, entry, image_arg, icon_str | ||
for char_iter=1, #char_order, 1 do | |||
for char_iter = 1, #char_order, 1 do | |||
local base, jobs = char_order[char_iter], {{}, {}, {}} | local base, jobs = char_order[char_iter], {{}, {}, {}} | ||
local base_name = split_lang(base)[1] or base | local base_name = split_lang(base)[1] or base | ||
local tree = mw.html.create('div'):addClass('char-banner-tree'):attr('data-base', base_name:gsub('/', '')):attr('data-display-base', split_lang(base)[2] or base) | local tree = mw.html.create('div'):addClass('char-banner-tree'):attr('data-base', base_name:gsub('/', '')):attr('data-display-base', split_lang(base)[2] or base) | ||
for path_iter=1, 4, 1 do | for path_iter = 1, 4, 1 do | ||
local path = char_list[base][path_iter] | local path = char_list[base][path_iter] | ||
for job_iter=1, 3, 1 do | for job_iter = 1, 3, 1 do | ||
local job = path[job_iter] | local job = path[job_iter] | ||
local out_table = jobs[job_iter] | local out_table = jobs[job_iter] | ||
Line 48: | Line 93: | ||
end | end | ||
end | end | ||
end | end | ||
for job_iter, job_name in ipairs(jobs) do | for job_iter, job_name in ipairs(jobs) do | ||
for path_iter, path_name in ipairs(job_name) do | for path_iter, path_name in ipairs(job_name) do | ||
-- 日誌:檢查 path_name | |||
mw.log("path_name: " .. tostring(path_name)) | |||
-- Foreign languages | -- Foreign languages | ||
local icon_link_char_name = path_name | local icon_link_char_name = path_name or 'Unknown' | ||
local normal_link_char_name = path_name | local normal_link_char_name = path_name or 'Unknown' | ||
if str_localized(path_name) then | if str_localized(path_name) then | ||
local name_split = split_lang(path_name) | local name_split = split_lang(path_name) | ||
icon_link_char_name = name_split[1]:gsub(':', '') | icon_link_char_name = (name_split[1] or 'Unknown'):gsub(':', ''):gsub(lang or '', ''):gsub(lang:gsub('^/', '') or '', '') | ||
path_name = name_split[2] | path_name = name_split[2] or name_split[1] or 'Unknown' | ||
normal_link_char_name = name_split[1] .. lang | normal_link_char_name = (name_split[1] or 'Unknown') .. lang | ||
else | |||
icon_link_char_name = path_name:gsub(':', ''):gsub(lang or '', ''):gsub(lang:gsub('^/', '') or '', '') | |||
end | end | ||
icon_link_char_name | |||
-- 日誌:檢查處理後的變數 | |||
mw.log("icon_link_char_name: " .. tostring(icon_link_char_name)) | |||
mw.log("normal_link_char_name: " .. tostring(normal_link_char_name)) | |||
local icon_str = 'Icon - ' | local icon_str = 'Icon - ' | ||
local link = '|link=' .. normal_link_char_name | local link = '|link=' .. normal_link_char_name | ||
Line 73: | Line 126: | ||
end | end | ||
local icon_with_link = '[[File:' .. icon_str .. | -- 清理 icon_link_char_name 中的非法字符,確保文件名正確 | ||
local clean_icon_name = icon_link_char_name:gsub('[^%w%s]', '') | |||
local icon_with_link = '[[File:' .. icon_str .. clean_icon_name .. '.png' .. link .. ']]' | |||
local img = tree:tag('div'):addClass('char-banner-tree-image'):attr('data-class-name', path_name):wikitext(icon_with_link) | local img = tree:tag('div'):addClass('char-banner-tree-image'):attr('data-class-name', path_name):wikitext(icon_with_link) | ||
-- 日誌:檢查最終的 icon_with_link | |||
mw.log("icon_with_link: " .. tostring(icon_with_link)) | |||
if unreleased then | if unreleased then | ||
Line 99: | Line 157: | ||
local select_banner = mw.html.create('div'):addClass('character-banner-select tone-8 border-color') | local select_banner = mw.html.create('div'):addClass('character-banner-select tone-8 border-color') | ||
for k, v in ipairs(char_order) do | for k, v in ipairs(char_order) do | ||
local display_name = v | local display_name = v | ||
local actual_name = v | local actual_name = v | ||
if str_localized(v) then | if str_localized(v) then | ||
if actual_name == 'Lu/Ciel' then actual_name = 'LuCiel (Lu)' end | local split_result = split_lang(v) | ||
local base_icon = mw.html.create('div'):attr('data-base', actual_name):attr('data-display-base', display_name or actual_name):wikitext('[[File: Icon - ' .. actual_name .. '.png]]') | actual_name = split_result[1] | ||
display_name = split_result[2] | |||
end | |||
if actual_name == 'Lu/Ciel' then | |||
actual_name = 'LuCiel (Lu)' | |||
end | |||
local base_icon = mw.html.create('div'):attr('data-base', actual_name):attr('data-display-base', display_name or actual_name):wikitext('[[File:Icon - ' .. actual_name .. '.png]]') | |||
select_banner:node(base_icon) | select_banner:node(base_icon) | ||
end | end | ||
Line 110: | Line 174: | ||
return styles .. tostring(char_outer) | return styles .. tostring(char_outer) | ||
end | end | ||
return p | return p |
Revision as of 04:15, 22 April 2025
Documentation for this module may be created at Module:CharTree/zh-hant/doc
require('Module:CommonFunctions')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local get_char_list = require('Module:CharList').main
-- 假設的 split 函數(如果 Module:CommonFunctions 未提供)
local function split(str, delimiter)
local result = {}
for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do
table.insert(result, match)
end
return result
end
-- 假設的 indexOf 函數(如果 Module:CommonFunctions 未提供)
local function indexOf(value, tbl)
for i, v in ipairs(tbl) do
if v == value then
return true
end
end
return false
end
-- Main process
function p.main(frame)
local args = getArgs(frame)
local char_outer = mw.html.create('div'):addClass('character-banner-outer'):attr('data-lang', args.lang)
local char_banner = mw.html.create('div'):addClass('character-banner-wrap border-color tone-2')
local char_list = get_char_list(frame, args.lang)
-- 規範化 lang 格式,確保始終以斜杠開頭
local lang = args.lang and ('/' .. args.lang) or ''
local is_localized = lang and lang ~= ''
local char_order = char_list[1]
local not_exist = char_list[3]
local char_list = char_list[2]
-- 日誌:檢查 lang 的值
mw.log("lang: " .. tostring(lang))
mw.log("args.lang: " .. tostring(args.lang))
function split_lang(str)
local lang_l = is_localized and lang or ''
if not lang_l or lang_l == '' then
return {str, str}
end
-- 清理 lang_l 的格式,移除前導斜杠以匹配無斜杠的情況
local clean_lang = lang_l:gsub('^/', '')
if string.find(str, lang_l .. '|') or string.find(str, clean_lang .. '|') then
local split_result = split(str, lang_l .. '|')
if #split_result == 0 then
split_result = split(str, clean_lang .. '|')
end
return split_result
elseif string.find(str, lang_l) or string.find(str, clean_lang) then
-- 處理僅包含語言標籤的情況,例如 Elsword/zh-hant
local actual_name = str:gsub(lang_l, ''):gsub(clean_lang, '')
return {actual_name, actual_name}
end
return {str, str}
end
function str_localized(str)
if not str or not lang or lang == '' then
return false
end
-- 檢查是否包含 {lang}| 或僅包含 {lang}
return string.find(str, lang .. '|') or string.find(str, lang:gsub('^/', ''))
end
local base_arg = args[1] or 'Elsword'
local base_name = str_localized(base_arg) and split_lang(base_arg)[1] or base_arg
local char_tree = mw.html.create('div'):addClass('char-banner-tree')
local out = ''
local char, file_name, link, nopath, nopathnojob, entry, image_arg, icon_str
for char_iter = 1, #char_order, 1 do
local base, jobs = char_order[char_iter], {{}, {}, {}}
local base_name = split_lang(base)[1] or base
local tree = mw.html.create('div'):addClass('char-banner-tree'):attr('data-base', base_name:gsub('/', '')):attr('data-display-base', split_lang(base)[2] or base)
for path_iter = 1, 4, 1 do
local path = char_list[base][path_iter]
for job_iter = 1, 3, 1 do
local job = path[job_iter]
local out_table = jobs[job_iter]
if job == nil and jobs[1][path_iter] ~= nil then
job = jobs[1][path_iter]
end
if job ~= nil then
table.insert(out_table, job)
end
end
end
for job_iter, job_name in ipairs(jobs) do
for path_iter, path_name in ipairs(job_name) do
-- 日誌:檢查 path_name
mw.log("path_name: " .. tostring(path_name))
-- Foreign languages
local icon_link_char_name = path_name or 'Unknown'
local normal_link_char_name = path_name or 'Unknown'
if str_localized(path_name) then
local name_split = split_lang(path_name)
icon_link_char_name = (name_split[1] or 'Unknown'):gsub(':', ''):gsub(lang or '', ''):gsub(lang:gsub('^/', '') or '', '')
path_name = name_split[2] or name_split[1] or 'Unknown'
normal_link_char_name = (name_split[1] or 'Unknown') .. lang
else
icon_link_char_name = path_name:gsub(':', ''):gsub(lang or '', ''):gsub(lang:gsub('^/', '') or '', '')
end
-- 日誌:檢查處理後的變數
mw.log("icon_link_char_name: " .. tostring(icon_link_char_name))
mw.log("normal_link_char_name: " .. tostring(normal_link_char_name))
local icon_str = 'Icon - '
local link = '|link=' .. normal_link_char_name
local unreleased = indexOf(normal_link_char_name, not_exist)
if unreleased then
icon_str = ''
icon_link_char_name = 'Dunno'
link = '|link=#!'
end
-- 清理 icon_link_char_name 中的非法字符,確保文件名正確
local clean_icon_name = icon_link_char_name:gsub('[^%w%s]', '')
local icon_with_link = '[[File:' .. icon_str .. clean_icon_name .. '.png' .. link .. ']]'
local img = tree:tag('div'):addClass('char-banner-tree-image'):attr('data-class-name', path_name):wikitext(icon_with_link)
-- 日誌:檢查最終的 icon_with_link
mw.log("icon_with_link: " .. tostring(icon_with_link))
if unreleased then
img:attr('data-unreleased', 'true')
end
end
end
char_banner:node(tree)
out = not_exist
end
local styles = frame:preprocess('{{#css:MediaWiki:Character-Banner.css}}')
local header = frame:expandTemplate{
title = 'CharHeader',
args = {
'Elsword',
'Elsword' .. lang,
'tone-8',
force_title = split_lang(char_order[1])[2]
}
}
local select_banner = mw.html.create('div'):addClass('character-banner-select tone-8 border-color')
for k, v in ipairs(char_order) do
local display_name = v
local actual_name = v
if str_localized(v) then
local split_result = split_lang(v)
actual_name = split_result[1]
display_name = split_result[2]
end
if actual_name == 'Lu/Ciel' then
actual_name = 'LuCiel (Lu)'
end
local base_icon = mw.html.create('div'):attr('data-base', actual_name):attr('data-display-base', display_name or actual_name):wikitext('[[File:Icon - ' .. actual_name .. '.png]]')
select_banner:node(base_icon)
end
char_outer:wikitext(header):node(char_banner:node(select_banner))
return styles .. tostring(char_outer)
end
return p