• Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:萌点

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

简介

本模块用于实现Template:萌点的主要功能:将输入的萌点参数按照相应的转换规则,转换为带格式的链接并添加分类。本模块使用的具体对应规则请见Module:萌点/data

此文档主要面向需要对模块本身进行修改的编辑者。若需要增加新的链接/分类对应规则,请于Module:萌点/data处进行修改。

模块结构

依赖模块

  • Module:Arguments - 获取参数
  • Module:萌点/data - 萌点链接/分类转换规则数据
  • Module:Split2 - 用于字符串分割

核心函数

p.trim(x)

  • 用于去除萌点名称末尾的括号内容
    • 使用正则表达式 pattern = "[%s]*[(][^))\n]*[)][%s]*$"
    • 示例: "女王(身份)" → "女王"

p.proc(str1, control, frame)

  • 应用文本格式化控制
  • 支持的格式控制符:
控制符 效果 示例
黑幕/heimu 黑幕效果 文本
del 删除线 文本
加粗/b 加粗 文本

p.proc2(str)

  • 用于确定萌点的链接部分(即“[[A|B]]”中“A”的部分)
  • 处理优先级:
    1. data.link_rules.direct_mapping (直接映射规则)
    2. data.link_rules.suffix_mapping (是否需要添加后缀)
    3. data.link_rules.special_professions (职业类型萌属性单独区分,后加“(萌属性)”)
    4. 若不符合上述规则,默认返回原始字符串

p.proc3(name, rawname, link, frame)

  • 用于生成萌点分类
  • 处理规则:
    1. 检查是否需要添加“需要检查”维护分类(当原始名称包含括号,但不在“免检”范围时)
    2. 特殊规则处理:
      • 能力者类萌点自动添加“者”后缀(但于data.ability_exceptions处排除一些以“能力”结尾的萌属性)
      • 使用data.category_rules.direct_mappingdata.category_rules.suffix_mapping进行匹配
    3. 最终通过ac模板生成分类

p._main(args, frame)

  • 用于主处理逻辑
  • 参数格式:
  • 输入萌点支持三种格式:
    1. 呆毛 → 基本格式
    2. 眼镜,黑幕 → 带格式控制
    3. 黑色过膝袜,黑丝,del → 自定义显示名称+格式
  • 处理流程:
    1. 用逗号/中文逗号分割参数
    2. 应用链接规则(proc2)和格式(proc)
    3. 添加分类(proc3)
    4. 若有多个萌点参数,则用顿号连接

需要注意的是,如果某个参数由三部分组成(即“link, name, control”),那么虽然分类会按照对应规则进行处理,但条目名及链接名不会进行处理。这是由于本模块认为用户在此种情形下希望自行决定链接转换方法,而不是按照已有规则进行转换。

local p = {}
local getArgs = require('Module:Arguments').getArgs
--local data = mw.loadJsonData('User:Liliaceae/MDJSON')

local data = mw.loadData('Module:萌点/data')
local splitString = require('Module:Split2')
--local stringx = require('Module:String')

function p.trim(x)
    --local pattern = "^(%s*)(.*)(%(.*%))(%s*)$"
    local pattern = "[%s]*[(][^))\n]*[)][%s]*$"
    local result, _ = mw.ustring.gsub(x, pattern, "", 1)
    return result
end
function p.proc(str1, control, frame)
	local ret = ''
	if control == '黑幕' or control == 'heimu' then
		ret = frame:expandTemplate{title = "黑幕", args = {str1}}
	elseif control == 'del' then
		ret = '<del>' .. str1 .. '</del>'
	elseif control == '加粗' or control == 'b' then
		ret = "'''" .. str1 .. "'''"
	else
		ret = str1
	end
	return ret
end
function p.proc2(str)
	--Link Rules
	--Part 1: Directly Map
	if data.link_rules.direct_mapping[str] ~= nil then
		return data.link_rules.direct_mapping[str]
	end
	if data.link_rules.suffix_mapping[str] ~= nil then
		return str .. "(" .. data.link_rules.suffix_mapping[str] .. ")"
	end
	if data.link_rules.special_professions[str] ~= nil then
		return str .. "(萌属性)"
	end	
	return "0"
end

function p.proc3(name, rawname, link, frame)
	--Cat Rules
	ret = ""
	--Need Check
	test1 = p.trim(rawname)
	--if data.skip_check[link] == nil and test1 ~= rawname then
	--	local title1 = mw.title.new(link)
	--	if title1.exists == true then
	--		ret = ret .. frame:expandTemplate{title = "ac", args = {"需要检查的萌点模板使用"}}
	--	end
	--end
	if test1 == name and link ~= name and data.skip_check[link] ~= nil or data.category_rules.use_link_value[name] ~= nil then -- 直接处理
		return ret .. frame:expandTemplate{title = "ac", args = {link}}
	end
	-- 能力者
	if mw.ustring.match(name, "能力", 1) ~= nil then
		local flag = 0
		for _, k in ipairs(data.ability_exceptions.patterns) do
			local result = mw.ustring.match(name, k, 1)
			if result ~= nil then
				flag = 1
			end
		end
		if flag == 0 then
			return ret .. frame:expandTemplate{title = "ac", args = {link .. "者"}}
		end
	end
	-- 两类特殊处理
	if data.special_handling.preserve_suffix[link] ~= nil then
		return frame:expandTemplate{title = "ac", args = {link}}
	end
	-- 直接对应
	if data.category_rules.direct_mapping[name] ~= nil then
		return ret .. frame:expandTemplate{title = "ac", args = {data.category_rules.direct_mapping[name]}}
	end
	if data.category_rules.suffix_mapping[name] ~= nil then
		return ret .. frame:expandTemplate{title = "ac", args = {name .. "(" .. data.category_rules.suffix_mapping[name] .. ")"}}
	end
	-- 默认规则
	local trim0 = p.trim(link)
	return ret .. frame:expandTemplate{title = "ac", args = {trim0}}
end
function p._main(args, frame)
	local arg = args
	code = ''
	counter = 0
	for index, moe in pairs(arg) do
		--Link Rules
		local done1 = 0
		local needSJ = 0
		if counter > 0 then
			code = code .. '、'
		end
		--local result = mw.ustring.find( moe, "_N", 1)
		--if result == 1 then
		--	result, _ = mw.ustring.gsub(moe, "_N", "", 1)
		--	code = code .. frame:expandTemplate{title = "User:Liliaceae/T:Moe", args = {result}}
		--	done1 = 1
		--end
		if done1 ~= 1 then 
			local moe1 = splitString.split(moe, '[,,]', 1)
			local moe2 = moe1.parts
			local link = ""
			local name = ""
			local rawname = ""
			if moe1.count == 1 or moe1.count == 2 then
				local procres = p.proc2(moe2[1])
				rawname = moe2[1]
				name = p.trim(moe2[1])
				if procres ~= "0" then
					link = procres
					procres = procres .. "|" .. name
				else
					if moe2[1] ~= name then
						procres = moe2[1] .. "|" .. name
					else
						procres = moe2[1]
					end
					link = moe2[1]
				end
				if data.special_handling.preserve_suffix[moe2[1]] ~= nil then
					link = moe2[1]
					name = moe2[1]
					procres = moe2[1]
				end
				local todo = '[[' .. procres .. ']]'
				if moe1.count == 2 then
					local todo2 = p.proc(todo, moe2[2], frame)
					if todo2 ~= todo then
						todo = todo2
					else
						needSJ = 1
					end
				end
				if needSJ ~= 1 then
					local cat = p.proc3(name, rawname, link, frame)
					code = code .. todo .. cat
				end
			end
			if moe1.count == 3 or needSJ == 1 then
				rawname = moe2[1]
				local procres = p.proc2(moe2[1])
				if procres == "0" then
					procres = moe2[1]
				end
				link = procres
				name = moe2[2]
				todo = '[[' .. procres .. '|' .. moe2[2] .. ']]'
				if needSJ ~= 1 then
					todo = p.proc(todo, moe2[3], frame)
				end
				local cat = p.proc3(name, rawname, link, frame)
				code = code .. todo .. cat
			end
			--Cat Rules
		end
		counter = counter + 1
	end
    return code
end

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

return p