Module:ConvertLink:修订间差异

来自Arcaea中文维基
(Module:沙盒/Economy666/CL)
(尝试优化)
 
(未显示同一用户的5个中间版本)
第1行: 第1行:
local CALv2 = mw.loadJsonData 'Template:ComplexArtistsList.json'
local DL = mw.loadJsonData 'User:Economy666/DesignersList.json'
local CDL = DL.complex
local SDL = DL.simple
local p = {}
local p = {}


-- local mad = require 'Module:AnotherData'
-- local mad = require 'Module:AnotherData'


local memory = {}


local NotJaArtist = {['旅人E']=''}
local function getMemory(enType, name)
if memory[enType] then
if memory[enType][name] then
return memory[enType][name]
end
else
memory[enType] = {}
end
end


local function link(l, d)
local function link(l, d)
第21行: 第25行:
local function formatLangText(text)
local function formatLangText(text)
if not text then return text end
if not text then return text end
if NotJaArtist[text] then return text end
    return mw.ustring.gsub(
    return mw.ustring.gsub(
  text, '[一-龠ぁ-ゔァ-ヴー々〆〤ヶ]+',
  text, '[一-龠ぁ-ゔァ-ヴー々〆〤ヶ]+',
第28行: 第31行:
end
end


local function langLink(theType, __link, display)
local function langLink(theType, __link, display, noForm)
if display then
local dis = display or __link
return link(theType..'列表#' .. __link, formatLangText(display))
local disForm = noForm and dis or formatLangText(dis)
else
return link(theType..'列表#' .. __link, disForm)
return link(theType..'列表#' .. __link, formatLangText(__link))
end
end
end


第41行: 第42行:
local display_lang =  data.zh and data.display or formatLangText(data.display)
local display_lang =  data.zh and data.display or formatLangText(data.display)
if data.link then
if data.link then
artist = artist .. langLink(theType, data.link, data.display)
artist = artist .. langLink(theType, data.link, display_lang, true)
else
else
artist = artist .. (display_lang or '')
artist = artist .. (display_lang or '')
第78行: 第79行:
end
end


local function defaultSDL(text)
local CALv2
local theType = ' 谱师'
local function findArtist(artistName)
local temp_text = text
CALv2 = CALv2 or mw.loadJsonData 'Template:ComplexArtistsList.json'
local link_placeholders = {}
local placeholder_count = 0
local artist_list = CALv2[artistName]
for _,data in ipairs(SDL) do
local theType = '曲师'
local dis = data.display
if artist_list then
local name = data.link
if artist_list.__FullData__ then
if string.find(temp_text, dis) then
artist = fullCL(theType, artist_list.__FullData__)
local link_text = langLink(theType, name, dis)
else
local placeholder = "__LINK_PLACEHOLDER_" .. placeholder_count .. "__"
artist = defaultCL(theType, artistName, artist_list)
link_placeholders[placeholder] = link_text
temp_text = string.gsub(temp_text, dis, placeholder)
placeholder_count = placeholder_count + 1
end
end
else
artist = langLink(theType, artistName)
end
end
if placeholder_count == 0 then return text end
return artist
   local formatted_text = formatLangText(temp_text)
   for placeholder, link_text in pairs(link_placeholders) do
     formatted_text = string.gsub(formatted_text, placeholder, link_text)
   end
   return formatted_text
end
end


function p.artistLink(artistName)
function p.artistLink(artistName)
第110行: 第104行:
-- 注意事项:json转义符,如曲师名称有 " 需要写成 \"(点名Gram(DJ Genki));
-- 注意事项:json转义符,如曲师名称有 " 需要写成 \"(点名Gram(DJ Genki));
--      lua匹配转义符,CALv2使用匹配简化结构,注意默认格式下value即显示部分(冒号右侧) . * 等符号需要在之前添加%转义
--      lua匹配转义符,CALv2使用匹配简化结构,注意默认格式下value即显示部分(冒号右侧) . * 等符号需要在之前添加%转义
if artistName then artistName=tostring(artistName) else return end
if artistName then artistName=tostring(artistName) else return end
local artist = getMemory('artist', artistName)
if artist then return artist end
artist = findArtist(artistName)
memory.artist[artistName] = artist
return artist
end
local DL,CDL,SDL
local dInit,sdlInit
local function designerInit()
DL = mw.text.jsonDecode(mw.title.new('Template:DesignersList.json'):getContent())
CDL = DL.complex
SDL = DL.simple
dInit = true
end
local function SDL_Init()
table.sort(SDL,
function(a,b)
return #(a.display)<#(b.display)
end)
sdlInit = true
end
local function findDesigner(designerName, songid)
if not dInit then designerInit() end
local theType = '谱师'
local artist_list = CALv2[artistName]
local function defaultSDL(text)
local theType = '曲师'
if not sdlInit then SDL_Init() end
if artist_list then
local temp_text = text
if artist_list.__FullData__ then
local text_len = #text
artist = fullCL(theType, artist_list.__FullData__)
local link_placeholders = {}
else
local placeholder_count = 0
artist = defaultCL(theType, artistName, artist_list)
for _,data in ipairs(SDL) do
local dis = data.display
local name = data.link
if string.find(temp_text, dis) then
local link_text = langLink(theType, name, dis)
local placeholder = "__LINK_PLACEHOLDER_" .. placeholder_count .. "__"
link_placeholders[placeholder] = link_text
temp_text = string.gsub(temp_text, dis, placeholder)
text_len = text_len - #dis
placeholder_count = placeholder_count + 1
if text_len < #dis then break end
end
-- temp_text = temp_text .. '__'..text_len
end
end
else
if placeholder_count == 0 then return text end
artist = langLink(theType, artistName)
   local formatted_text = formatLangText(temp_text)
   for placeholder, link_text in pairs(link_placeholders) do
     formatted_text = string.gsub(formatted_text, placeholder, link_text)
   end
   return formatted_text
end
end
return artist
end
 
local specialSong = DL.special
function p.designerLink(designerName, songid)
-- songid参数可选,用于最终区分"其他未确认谱师名义"与"与主线剧情相关的特殊名义"
--  不填默认归为"其他未确认谱师名义"
if designerName then designerName=tostring(designerName) else return end
local designer
local designer
local theType = '谱师'
local designer_list = CDL[designerName]
local designer_list = CDL[designerName]
if designer_list then
if designer_list then
第147行: 第176行:
designerName = designerName==''and ' ' or designerName
designerName = designerName==''and ' ' or designerName
if songid then
if songid then
if specialSong[songid] then
if DL.special[songid] then
return langLink(theType,'与主线剧情相关的特殊名义', designerName)
return langLink(theType,'与主线剧情相关的特殊名义', designerName)
end
end
end
end
return langLink(theType, '其他未确认谱师名义', designerName)
return langLink(theType, '其他未确认谱师名义', designerName)
end
function p.designerLink(designerName, songid)
-- songid参数可选,用于最终区分"其他未确认谱师名义"与"与主线剧情相关的特殊名义"
--  不填默认归为"其他未确认谱师名义"
if designerName then designerName=tostring(designerName) else return end
local designer = getMemory('designer', designerName)
if designer then return designer end
local designer = findDesigner(designerName, songid)
memory.designer[designerName] = designer
return designer
end
end


第163行: 第203行:
-- true  NS版曲包
-- true  NS版曲包
-- false 移动版曲包
-- false 移动版曲包
if packName then packName=tostring(packName) else return end
local class = 'pack_'..(ns and 'ns' or 'mobile')..(short and '_short' or '')
local pack = getMemory(class, packName)
if pack then return pack end
local discrete_pack = {['Arcaea']='曲包', ['Memory Archive']=''}
local discrete_pack = {['Arcaea']='曲包', ['Memory Archive']=''}
第187行: 第231行:
end
end
local pack = link(packLink, packDisplay)
local pack = link(packLink, packDisplay)
memory[class][packName] = pack
return pack
return pack
end
end


function p.artistLinkFrame(frame)
local lang, VT
   local name
local function findVer(ver, date)
   if frame and frame.args[1] then 
lang = lang or mw.language.getContentLanguage()
     name = frame.args[1]
-- VT = VT or mw.loadJsonData 'Template:VersionTime.json'
   else 
VT = VT or mw.text.jsonDecode(mw.title.new('Template:VersionTime.json'):getContent())
     return ''
local ymd = tonumber(lang:formatDate('ymd', '@' .. date)) - 1
   end
local vt
   local al = p.artistLink(name)
if VT[ver] then vt=VT[ver] else return ver, ymd end
   return al
local dates = vt.time
local version = vt.ver
if #dates ~= #version then return ver,ymd end
local l, r = 1, #dates
while l < r do
local m = math.floor((l + r) / 2)
if dates[m] < ymd then
l = m + 1
else
r = m
end
end
return ver.. '.' ..version[l], dates[l]
end
local function dateConvert(date)
if not date then return nil end
local dateStr = tostring(date)
local yy = string.sub(dateStr,1,2)
local mm = string.sub(dateStr,3,4)
local dd = string.sub(dateStr,5,6)
return '20'..string.format("%02d/%02d/%02d", yy, mm, dd % 100)
end
end


function p.packLinkFrame(frame)
function p.fullVersionNum(ver, date)
   if frame then
local verClass = math.floor(date/100)
   local name = frame.args['name'] or nil
local verData = getMemory('ver', verClass)
   local short = frame.args['short'] or nil
if verData then return verData[1], verData[2], 'Memory' end
   local ns = frame.args['ns'] or nil
verData={}
   return p.packLink(name, short, ns)
verData[1], verData[2] = findVer(ver, date)
   else
verData[2] = dateConvert(verData[2])
   return 'none'
memory.ver[verClass] = verData
   end
return verData[1], verData[2]
end
end


return p
return p

2024年10月25日 (五) 14:29的最新版本

可在Module:ConvertLink/doc创建此模块的帮助文档

local p = {}

-- local mad = require 'Module:AnotherData'

local memory = {}

local function getMemory(enType, name)
	if memory[enType] then
		if memory[enType][name] then
			return memory[enType][name]
		end
	else
		memory[enType] = {}
	end
end

local function link(l, d)
    if d then
        return '[[' .. l .. '|' .. d .. ']]'
    else
        return '[[' .. l .. ']]'
    end
end

local function formatLangText(text)
	if not text then return text end
    return mw.ustring.gsub(
	   text, '[一-龠ぁ-ゔァ-ヴー々〆〤ヶ]+',
    	function(v) return ('<span lang="ja">-{%s}-</span>'):format(v) end
	)
end

local function langLink(theType, __link, display, noForm)
	local dis = display or __link
	local disForm = noForm and dis or formatLangText(dis)
	return link(theType..'列表#' .. __link, disForm)
end

local function fullCL(theType, __FullData__)
	local artist = ''
	for _, data in ipairs(__FullData__) do
		local display_lang =  data.zh and data.display or formatLangText(data.display)
		if data.link then
			artist = artist .. langLink(theType, data.link, display_lang, true)
		else
			artist = artist .. (display_lang or '')
		end
	end
	return artist
end

local function defaultCL(theType, text, artist_list)
	local temp_text = text
	local link_placeholders = {}
	local placeholder_count = 0
	for theLink, display in pairs(artist_list) do
		if type(display) == 'string' then
			local link_text = langLink(theType, theLink, display)
			local placeholder = "__LINK_PLACEHOLDER_" .. placeholder_count .. "__"
			link_placeholders[placeholder] = link_text
			temp_text = string.gsub(temp_text, display, placeholder)
			placeholder_count = placeholder_count + 1
		elseif type(display)=='table' then
			for _,dis in pairs(display) do
				local link_text = langLink(theType, theLink, dis)
				local placeholder = "__LINK_PLACEHOLDER_" .. placeholder_count .. "__"
				link_placeholders[placeholder] = link_text
				temp_text = string.gsub(temp_text, dis, placeholder)
				placeholder_count = placeholder_count + 1
			end
    	end
	end
	if placeholder_count == 0 then return text end
    local formatted_text = formatLangText(temp_text)
    for placeholder, link_text in pairs(link_placeholders) do
        formatted_text = string.gsub(formatted_text, placeholder, link_text)
    end
    return formatted_text
end

local CALv2
local function findArtist(artistName)
	CALv2 = CALv2 or mw.loadJsonData 'Template:ComplexArtistsList.json'
	
	local artist_list = CALv2[artistName]
	local theType = '曲师'
	if artist_list then
		if artist_list.__FullData__ then
			artist = fullCL(theType, artist_list.__FullData__)
		else
			artist = defaultCL(theType, artistName, artist_list)
		end
	else
		artist = langLink(theType, artistName)
	end
	return artist
end

function p.artistLink(artistName)
	-- 将songlist中纯文本形式的曲师名称转换为带一个或多个链接的曲师名称,同时进行日文转换
	-- 输入:曲师名称(需要与songlist内完全一致,或仅存在空格差异)(不要手打!!)
	-- 返回:带链接的曲师名称
	-- 若预期返回多个链接,实际返回一个链接;或曲师使用了马甲未能正确识别,请在 Template:ComplexArtistsList.json 添加新规则
	-- 注意事项:json转义符,如曲师名称有 " 需要写成 \"(点名Gram(DJ Genki));
	--           lua匹配转义符,CALv2使用匹配简化结构,注意默认格式下value即显示部分(冒号右侧) . * 等符号需要在之前添加%转义
	if artistName then artistName=tostring(artistName) else return end
	local artist = getMemory('artist', artistName)
	if artist then return artist end
	artist = findArtist(artistName)
	memory.artist[artistName] = artist
	return artist
end

local DL,CDL,SDL
local dInit,sdlInit

local function designerInit()
	DL = mw.text.jsonDecode(mw.title.new('Template:DesignersList.json'):getContent())
	CDL = DL.complex
	SDL = DL.simple
	dInit = true
end

local function SDL_Init()
	table.sort(SDL,
			function(a,b)
				return #(a.display)<#(b.display)
			end)	
	sdlInit = true
end

local function findDesigner(designerName, songid)
	if not dInit then designerInit() end
	local theType = '谱师'
	
	local function defaultSDL(text)
		if not sdlInit then SDL_Init() end
		local temp_text = text
		local text_len = #text
		local link_placeholders = {}
		local placeholder_count = 0
		for _,data in ipairs(SDL) do
			local dis = data.display
			local name = data.link
			if string.find(temp_text, dis) then
				local link_text = langLink(theType, name, dis)
				local placeholder = "__LINK_PLACEHOLDER_" .. placeholder_count .. "__"
				link_placeholders[placeholder] = link_text
				temp_text = string.gsub(temp_text, dis, placeholder)
				text_len = text_len - #dis
				placeholder_count = placeholder_count + 1
				if text_len < #dis then break end
			end
			-- temp_text = temp_text .. '__'..text_len
		end
		if placeholder_count == 0 then return text end
	    local formatted_text = formatLangText(temp_text)
	    for placeholder, link_text in pairs(link_placeholders) do
	        formatted_text = string.gsub(formatted_text, placeholder, link_text)
	    end
	    return formatted_text
	end
	
	local designer
	local designer_list = CDL[designerName]
	if designer_list then
		if designer_list.__FullData__ then
			designer = fullCL(theType, designer_list.__FullData__)
		else
			designer = defaultCL(theType, designerName, designer_list)
		end
	else
		designer = defaultSDL(designerName)
	end
	if designer ~= designerName then return designer end
	designerName = designerName==''and ' ' or designerName
	if songid then
		if DL.special[songid] then
			return langLink(theType,'与主线剧情相关的特殊名义', designerName)
		end
	end
	return langLink(theType, '其他未确认谱师名义', designerName)
end

function p.designerLink(designerName, songid)
	-- songid参数可选,用于最终区分"其他未确认谱师名义"与"与主线剧情相关的特殊名义"
	--   不填默认归为"其他未确认谱师名义"
	if designerName then designerName=tostring(designerName) else return end
	local designer = getMemory('designer', designerName)
	if designer then return designer end
	local designer = findDesigner(designerName, songid)
	memory.designer[designerName] = designer
	return designer
end


function p.packLink(packName, short, ns)
	-- 曲包名称转换成链接
	-- short
		-- true  简短(x Collaboration y)
		-- false 完整(x Collaboration - Collaboration y)
	-- ns  适用于Arcaea和Memory Archive曲包
		-- true  NS版曲包
		-- false 移动版曲包
	if packName then packName=tostring(packName) else return end
	local class = 'pack_'..(ns and 'ns' or 'mobile')..(short and '_short' or '')
	local pack = getMemory(class, packName)
	if pack then return pack end
	
	local discrete_pack = {['Arcaea']='曲包', ['Memory Archive']=''}
	-- 分离双平台页的曲包,value为后缀附加内容: '曲包'->' (xx版曲包)' ; ''->' (xx版)'
	
	local cc = 'Collaboration %- Collaboration'
	local ccl = 'Collaboration#Collaboration'
	local linkFlag = ''
	if discrete_pack[packName] then
		if ns then
			linkFlag = (' (NS版%s)'):format(discrete_pack[packName])
		else
			linkFlag = (' (移动版%s)'):format(discrete_pack[packName])
		end
	end
	
	local packLink = string.gsub(packName, cc, ccl) .. linkFlag
	local packDisplay
	if short then
		local ccd = 'Collaboration'
		packDisplay = string.gsub(packName, cc, ccd)
	else
		packDisplay = packName
	end
	local pack = link(packLink, packDisplay)
	
	memory[class][packName] = pack
	return pack
end

local lang, VT
local function findVer(ver, date)
	lang = lang or mw.language.getContentLanguage()
	-- VT = VT or mw.loadJsonData 'Template:VersionTime.json'
	VT = VT or mw.text.jsonDecode(mw.title.new('Template:VersionTime.json'):getContent())
	local ymd = tonumber(lang:formatDate('ymd', '@' .. date)) - 1
	local vt
	if VT[ver] then vt=VT[ver] else return ver, ymd end
	local dates = vt.time
	local version = vt.ver
	if #dates ~= #version then return ver,ymd end
	local l, r = 1, #dates
	while l < r do
		local m = math.floor((l + r) / 2)
		if dates[m] < ymd then
			l = m + 1
		else
			r = m
		end
	end
	return ver.. '.' ..version[l], dates[l]
end
local function dateConvert(date)
	if not date then return nil end
	local dateStr = tostring(date)
	local yy = string.sub(dateStr,1,2)
	local mm = string.sub(dateStr,3,4)
	local dd = string.sub(dateStr,5,6)
	return '20'..string.format("%02d/%02d/%02d", yy, mm, dd % 100)
end

function p.fullVersionNum(ver, date)
	local verClass = math.floor(date/100)
	local verData = getMemory('ver', verClass)
	if verData then return verData[1], verData[2], 'Memory' end
	verData={}
	verData[1], verData[2] = findVer(ver, date)
	verData[2] = dateConvert(verData[2])
	memory.ver[verClass] = verData
	return verData[1], verData[2]
end

return p