打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Module:Eye color

来自OGAS数据中枢

此模块的文档可以在Module:Eye color/doc创建

local p = {}
local getArgs = require('Module:Arguments').getArgs
local splitString = require('Module:Split2')

-- 繁简转换映射表
local tradToSimple = {
  ["異"] = "异", 
  ["淺"] = "浅", 
  ["濃"] = "浓",
  ["鈷"] = "钴",
  ["銀"] = "银",
  ["緋"] = "绯",
  ["紅"] = "红",
  ["藍"] = "蓝",
  ["綠"] = "绿",
  ["緑"] = "绿",
  ["靑"] = "青",
  ["黃"] = "黄",
  ["黒"] = "黑", 
  ["亞"] = "亚",
  ["漸"] = "渐",
  ["變"] = "变"
}

-- 标准化处理函数
local function normalizeText(text)
  if not text or text == "" then 
    return text
  end
    
  -- 繁简转换
  for trad, simple in pairs(tradToSimple) do
    text = text:gsub(trad, simple)
  end
    
  -- 移除"瞳"和"色"后缀
  text = text:gsub("瞳$", ""):gsub("色$", "")
    return text
  end

-- 从后向前匹配颜色分类函数
local function getColorCategory(color)
  -- 特殊类型处理
  if color == "异色" or color == "彩虹" or color == "渐变" then
    return color .. "瞳"
  end
    
 -- 只保留必要的复合颜色映射
  local colorKeywords = {
    -- 需要特殊处理的复合颜色
    {"异", "异色瞳"},
    {"彩", "彩虹瞳"},
    -- 粉
    {"洋红", "粉瞳"},
    {"粉红", "粉瞳"},
    {"桃红", "粉瞳"},
    -- 蓝
    {"蓝绿", "蓝瞳"}, 
    {"蓝灰", "蓝瞳"}, 
    -- 紫
    {"紫蓝", "紫瞳"},
    {"雪青", "紫瞳"},
    {"紫红", "紫瞳"},
    -- 绿
    {"绿棕", "绿瞳"},
    {"绿褐", "绿瞳"},
    {"青绿", "绿瞳"},
    {"青灰", "绿瞳"},
    {"翡翠", "绿瞳"},
    -- 金
    {"亚麻", "金瞳"}, 
    {"琥珀", "金瞳"},
    -- 橙
    {"桔黄", "橙瞳"},
    {"橘黄", "橙瞳"},
    {"橙褐", "橙瞳"}, 
    -- 棕
    {"亚麻棕", "棕瞳"},
    {"棕黑", "棕瞳"}, 
    {"棕黄", "棕瞳"}, 
    {"酒红", "棕瞳"},
    {"咖啡", "棕瞳"},
    -- 灰
    {"银白", "灰瞳"}
  }
    
  -- 从后向前检查每个关键词
  for i = #colorKeywords, 1, -1 do
    local keyword = colorKeywords[i]
    if color:find(keyword[1]) then
      return keyword[2]
    end
  end
    
  return nil
end

function p._main(args, frame)
  local input = args[1] or ''
  local ft = args.ft 

  local eyeParts = splitString.split(input, '$', false)
  local eyeColor = eyeParts.parts[1] or ''
  local eyeNote = eyeParts.parts[2] or ''
    
  -- 标准化处理
  eyeColor = normalizeText(eyeColor)
    
  -- 截取第一个"["之前的内容
  local bracketPos = mw.ustring.find(eyeColor, "%[")
  if bracketPos then
    eyeColor = mw.text.trim(mw.ustring.sub(eyeColor, 1, bracketPos - 1))
  end

  -- 特殊类型处理
  local specialTypes = {
    ["异色"] = true, 
    ["彩虹"] = true, 
    ["渐变"] = true
  }
  
  if specialTypes[eyeColor] then
    local categoryTemplate = frame:expandTemplate{title = "ArticleCategory", args = {eyeColor .. "瞳"}}
    return string.format("[[%s瞳|%s瞳]]%s%s", eyeColor, eyeColor, eyeNote, categoryTemplate)
  end

  -- 移除修饰词
  local modifiers = {
    ["浅"] = true, 
    ["深"] = true,
    ["亮"] = true,
    ["暗"] = true, 
    ["浓"] = true,
    ["淡"] = true, 
    ["黑"] = true, 
    ["灰"] = true, 
    ["银"] = true, 
    ["碧"] = true, 
    ["墨"] = true, 
    ["玫"] = true,
    ["土"] = true, 
    ["水"] = true, 
    ["翠"] = true,
    ["钴"] = true
  }

  local coreColor = eyeColor
  local firstChar = mw.ustring.sub(eyeColor, 1, 1)
  if modifiers[firstChar] and mw.ustring.len(eyeColor) > 1 then
    coreColor = mw.ustring.sub(eyeColor, 2)
  end

  -- 获取颜色类别
  local category = getColorCategory(coreColor)

  if category then
    local displayText = eyeColor .. "瞳"
    local pageName = category
        
    if category == "黑瞳" then
          pageName = "黑瞳(瞳色)"
    end

    local categoryTemplate = frame:expandTemplate{title = "ArticleCategory", args = {category}}
    return string.format("[[%s|%s]]%s%s", pageName, displayText, eyeNote, categoryTemplate)
  else
    -- 基础颜色映射(优先级最低)
    local baseColors = {
      ["粉"] = "粉瞳", 
      ["红"] = "红瞳", 
      ["紫"] = "紫瞳", 
      ["蓝"] = "蓝瞳",
      ["绿"] = "绿瞳", 
      ["金"] = "金瞳", 
      ["橙"] = "橙瞳",
      ["棕"] = "棕瞳", 
      ["黑"] = "黑瞳", 
      ["灰"] = "灰瞳",
      ["白"] = "白瞳"
    }
        
    -- 尝试使用最后一个字符匹配基础颜色
    local lastChar = mw.ustring.sub(coreColor, -1)
    local charMap = {
      ["桃"] = "粉", 
      ["绯"] = "红",
      ["赤"] = "红",
      ["青"] = "蓝",
      ["碧"] = "蓝", 
      ["靛"] = "紫",
      ["黄"] = "金",
      ["米"] = "金",
      ["桔"] = "橙",
      ["橘"] = "橙",
      ["栗"] = "棕", 
      ["茶"] = "棕",
      ["褐"] = "棕",
      ["银"] = "灰",
    }
        
    local mappedChar = charMap[lastChar] or lastChar
    local testCategory = baseColors[mappedChar]
        
    if testCategory then
      local displayText = eyeColor .. "瞳"
      local pageName = testCategory
            
      if testCategory == "黑瞳" then
        pageName = "黑瞳(瞳色)"
      end

      local categoryTemplate = frame:expandTemplate{title = "ArticleCategory", args = {testCategory}}
      return string.format("[[%s|%s]]%s%s", pageName, displayText, eyeNote, categoryTemplate)
    else
      if ft then
        return eyeColor .. "瞳" .. eyeNote
      else
        local categoryTemplate = frame:expandTemplate{title = "ArticleCategory", args = {"错误瞳色"}}
        return eyeColor .. "瞳" .. eyeNote .. categoryTemplate
      end
    end
  end
end

function p.main(frame)
  local args = getArgs(frame, {
      parentFirst = true,
  })
  return p._main(args, frame)
end

return p