local specialDelims = {'/', '、', '\\', '/', '·'}
-- Trim function
function trim2(s)
return (s:gsub("^[%z\128-\255%s]+", ""))
end
-- Function to check if a string contains Chinese characters
local function containsChinese(str)
for i = 1, #str do
local c = string.byte(str, i)
if c >= 0x4E00 and c <= 0x9FFF then
return true
end
end
return false
end
-- Function to check if a string contains English letters
local function containsEnglish(str)
for i = 1, #str do
local c = string.byte(str, i)
if (c >= 0x41 and c <= 0x5A) or (c >= 0x61 and c <= 0x7A) then
return true
end
end
return false
end
-- Test the functions
--local str1 = "夜夜夜夜 (Demo)"
--local str2 = "Demo (English)"
--print("String 1 contains Chinese: ", containsChinese(str1)) -- Output: true
--print("String 1 contains English: ", containsEnglish(str1)) -- Output: true
--print("String 2 contains Chinese: ", containsChinese(str2)) -- Output: false
--print("String 2 contains English: ", containsEnglish(str2)) -- Output: true
local p = {}; --All Lua modules on Wikipedia must begin by defining a variable
--that will hold their externally accessible functions.
--Such variables can have whatever name you want and may
--also contain various data as well as functions.
function p.addCat(frame) -- Add another function
local cat = frame.args[1] -- To access arguments passed to a module, use `frame.args`
-- `frame.args[1]` refers to the first unnamed parameter
-- given to the module
local ret = ''
if string.match(cat, '国语') then
ret = ret .. '[[Category:国语歌词]]'
end
if string.match(cat, '粤语') then
ret = ret .. '[[Category:粤语歌词]]'
end
if string.match(cat, '闽南语') or string.match(cat, '台语') then
ret = ret .. '[[Category:闽南语歌词]]'
end
if string.match(cat, '英语') then
ret = ret .. '[[Category:Lyrics in English]]'
end
if string.match(cat, '日语') then
ret = ret .. '[[Category:日本語の歌詞]]'
end
if string.match(cat, '韩语') then
ret = ret .. '[[Category:한국어 가사]]'
end
if string.match(cat, '音乐') then
ret = ret .. '[[Category:Instrumental]]'
end
if ret == '' then
ret = '[[Category:单曲歌词]] [[Category:Song Lyrics]]'
end
return ret -- `..` concatenates strings. This will return a customized
-- greeting depending on the name given, such as "Hello, Fred!"
end
function p.addSongTitle(frame)
local songtitle = frame.args[1] or ''
if songtitle == '' then
local pagename = tostring(mw.title.getCurrentTitle())
if string.match(pagename, '%(') then
local pattern = "^(.-)%s*%([^%(%)]*%)$" -- Find the position of the last set of parentheses
--local pattern = ".*%(([^()]+)%)$" -- This pattern captures the last set of parentheses and its content
songtitle = string.match(pagename, pattern)
else
songtitle = pagename
end
end
return songtitle
end
function p.addISRC(frame) -- Add another function
local cat = frame.args[1] -- To access arguments passed to a module, use `frame.args`
-- `frame.args[1]` refers to the first unnamed parameter
-- given to the module
local ret = cat
if string.match(cat, '{{ISRC') then
elseif string.match(cat, '/') then
ret = '<span class="ISRC">' .. cat .. '</span>'
else
end
return ret
end
function p.addLinkRef(str, t)
local ret = ''
if t == '' then
if string.match(str, "\127") then
str = string.gsub(str, "\127'", "]]\127'")
ret = '[[' .. str
else
ret = '[[' .. str .. ']]'
end
elseif string.match(t, "#") then
if string.match(str, "\127") then
local str0 = string.match(str, "(.-)\127") -- remove string after DEL
str = string.gsub(str, "\127'", "]]\127'")
ret = '[[' .. str0 .. t .. '|'.. str
else
ret = '[[' .. str .. t .. '|'.. str .. ']]'
end
else
if string.match(str, "\127") then
local str0 = string.match(str, "(.-)\127") -- remove string after DEL
str = string.gsub(str, "\127'", "]]\127'")
ret = '[[' .. str0 .. ' (' .. t .. ')|'.. str
else
ret = '[[' .. str .. ' (' .. t .. ')|'.. str .. ']]'
end
end
return ret
end
function p.addCatRef(str, t)
local ret = ''
if t == '' then return ret end
if string.match(str, "`UNIQ--") then
str = string.match(str, "(.-)\127") -- remove string after DEL
end
ret = '[[Category:' .. str .. '|' .. t .. ']]'
return ret
end
function p.addCatRCs(frame) -- 自动增加唱片公司标签
local rcstr = frame.args[1] or ''
local altype = frame.args[2] or '专辑'
if (altype == '') then altype = '专辑' end
local ret = rcstr
if (rcstr == '') then
return ret
else
local match = ''
for i, d in ipairs(specialDelims) do
match = string.match(rcstr, specialDelims[i]) or match --or string.match(artist, "/") or ''
end
if match == '' then
ret = p.addCatRef(rcstr, altype)
else -- 多厂牌
local rcs = {}
rcs = p.getMultiRCs(frame, match)
for i, a in ipairs(rcs) do
rcs[i] = p.addCatRef(a, altype)
end
ret = table.concat(rcs, " ")
end
end
return ret
end
function p.getMultiRCs(frame, delim)
-- Split the input string by the delimiter " / "
local segments = {}
local rcstr = frame.args[1] or ''
delim = delim or ''
rcstr = string.gsub(rcstr, delim, "/")
for segment in string.gmatch(rcstr, '([^/]+)') do
-- Remove leading and trailing whitespace
segment = segment:match("^%s*(.-)%s*$")
-- Format each segment
table.insert(segments, segment)
end
return segments
end
function p.addRCLink(frame) -- 自动添加唱片公司/厂牌链接
local ret = ''
local rc = frame.args[1] or ''
if rc == '' or (string.match(rc, '%{') or string.match(rc,'%[')) then
return rc
end
local rctype = frame.args[2] or ''
local match = ''
for i, d in ipairs(specialDelims) do
match = string.match(rc, specialDelims[i]) or match --or string.match(artist, "/") or ''
end
if match == '' then
ret = p.addLinkRef(rc, rctype)
else -- 多厂牌
local rcs = {}
rcs = p.getMultiRCs(frame, match)
for i, a in ipairs(rcs) do
rcs[i] = p.addLinkRef(a, rctype)
end
ret = table.concat(rcs, " · ")
end
return ret
end
function p.renderSpotify(frame) -- Add spotify frame
local uri = frame.args[1] or ''
local height = frame.args.h or '160'
if height == '' then height = '160' end
local width = frame.args.w or '270'
if width == '' then width = '270' end
local float = frame.args[2] or 'right'
local ret = '<div>' --'<div style="float: ' .. float .. '">'
ret = ret .. '<spotify uri="spotify:'
local t = frame.args.t or 'album:'
if (t == '' or t == 'a') then t = 'album:'
elseif t == 't' then t = 'track:'
elseif t == 'p' then t = 'playlist:'
end
ret = ret .. t .. uri .. '" ' .. 'height="' .. height .. '" width="' .. width .. '" />'
ret = ret .. '</div>'
if float == 'right' then
ret = '{| class="infobox" style="width: 20em; border: 0; background-color: inherit;"\n |-\n|' .. ret .. '\n |}'
end
ret = frame:preprocess(ret)
return ret
end
function p.renderCuetools(frame) -- Add cuetools link
local ret = ''
local CTID = frame.args[1] or '0'
ret = '更多信息请参考<b>[http://db.cuetools.net/cd/' .. CTID .. ' CueTools]</b>数据库。'
ret = frame:preprocess(ret)
return ret
end
function p.renderLyricsinfo(frame) -- Add cuetools link
local ret = ''
--local CTID = frame.args[1] or '0'
ret = '更多信息请参考<b>[https://www.musico.wiki/lyricsinfo/ Lyricsinfo 歌词维基]</b>的相关曲目歌词页。'
ret = frame:preprocess(ret)
return ret
end
function p.renderLog(frame)
local ret = ''
local logtext = frame.args[1] or ''
if (logtext == '') then
ret = '<br>详细对比的Log结果请参阅:[[/音质差异]]'
else
ret = '<pre class="log">' .. logtext .. '</pre>'
end
return ret
end
function p.renderFoobarBC(frame)
local ret = ''
--local logtext = frame.args[1] or ''
ret = '关于[[Foobar2000]]的Binary Comparison对比插件及对比结果Log的更详细解读,请参阅:[[音频对比]]'
return ret
end
function p.addGallery(frame) -- 专辑封套Artworks
local ret = ''
local images = frame.args[1] or 'Example.png#Discogs 示例封面'
images = string.gsub(images, '#', "|")
ret = '<gallery type="slideshow" position=center widths=250>' .. images .. '</gallery>'
ret = frame:preprocess(ret)
return ret
end
------------------- UNUSED Func -----------------------------
function p.addCatAlbum(frame) -- Add another function
local album = frame.args[1]
local ret = album
if string.match(album, "`UNIQ--") then
album = string.match(album, "(.-)\127") -- remove string after DEL
end
if string.match(album, '《') or string.match(album, ':w:c:zh.discogs:') then
local pattern = "%[%[.-|(.-)%]%]" -- This pattern looks for [[ followed by any number of characters until |, then captures everything until ]]
if (string.match(album, pattern)) then
ret = '[[Category:' .. string.match(album, pattern) .. ' (专辑)]]'
else
ret = '[[Category:' .. album .. ' (专辑)]]'
end
else
ret = '[[Category:' .. album .. ' (专辑)]]'
end
return ret
end
function p.addCreatePageDefault(frame) -- Add another function
local pagename = frame.args[1]
local ret = ''
if string.match(pagename, '%(') then
local pattern = "%([^()]+%)$" -- This pattern captures the last set of parentheses and its content
ret = '曲名 ' .. string.match(pagename, pattern)
else
ret = '曲名 (艺术家)'
end
return ret
end
------------------- UNUSED Func END -----------------------------
function p.getArtistfromPage()
local _ret = ''
local pname = tostring(mw.title.getCurrentTitle())
if string.match(pname, '%(') then
_ret = string.match(pname, "%(([^()]+)专辑%)$") or string.match(pname, "%(([^()]+)專輯%)$") or string.match(pname, "%(([^()]+)精選集%)$") or string.match(pname, "%(([^()]+)精选集%)$") or string.match(pname, "%(([^()]+)EP%)$") or '群星' --'Various'
else
_ret = '群星'
end
return _ret
end
function p.getArtist(frame) -- 从参数或歌词页面标题获取艺术家名
local ret = frame.args[1] or ''
if ret == '' then
ret = p.getArtistfromPage()
end
return ret
end
function p.addDiscogs(frame) -- Add discogs album link
local album = frame.args[1]
local artist = frame.args[2] or ''
if artist == '' then
artist = p.getArtistfromPage()
end
local albumtype = frame.args[3] or '专辑'
if albumtype == '' then albumtype = '专辑' end
local ret = album
local extra = ''
if string.match(album, "\127") then
--extra = string.match(album, "\127(.-)$") -- '"`UNIQUNIQ--ref-00000003-QINU`"'
--album = string.match(album, "(.-)\127")
return ret
end
if string.match(album, '《') then
elseif string.match(album, '%{') then
elseif string.match(album, ':w:c:zh.discogs:') then
elseif artist == '-' or artist == '群星' or artist == '合辑' or artist == 'Various Artists' then
ret = '[[' .. album .. ']]'
else
ret = '[[' .. album .. ' (' .. artist .. albumtype .. ')|' .. album .. ']]'
end
return ret
end
function p.getDiscogsRedirect(frame) -- 自动生成Discogs专辑重定向
local pagename = frame.args[1] -- 格式:歌手名周杰伦/专辑类型录音室专辑/年份2000 - 专辑名Jay
local ret = ''
if string.match(pagename, '%/') then
--ret = '#REDIRECT [['
local artist = pagename:match("^(.-)/") or ""
local albumtype = pagename:match("/(.-)/") or "专辑"
local title = pagename:match("-(.+)$"):match("^%s*(.-)%s*$") or ""
local year = pagename:match("/(%d%d%d%d) -") or "0000"
ret = ret .. title .. ' (' .. artist .. albumtype .. ')'
--ret = frame:preprocess(ret)
else
ret = ret .. pagename .. ''
end
return ret
end
function p.getDiscogsRedirectArtist(frame) -- 自动生成Discogs专辑重定向
local pagename = frame.args[1] -- 格式:歌手名周杰伦/专辑类型录音室专辑/年份2000 - 专辑名Jay
local ret = ''
if string.match(pagename, '%/') then
--ret = '#REDIRECT [['
ret = pagename:match("^(.-)/") or ""
--ret = frame:preprocess(ret)
else
ret = ret .. pagename .. ''
end
return ret
end
function p.getDiscogsRedirectTitle(frame) -- 自动生成Discogs专辑重定向
local pagename = frame.args[1] -- 格式:歌手名周杰伦/专辑类型录音室专辑/年份2000 - 专辑名Jay
local ret = ''
if string.match(pagename, '%/') then
--ret = '#REDIRECT [['
ret = pagename:match("^(.-)/") or ""
--ret = frame:preprocess(ret)
else
ret = ret .. pagename .. ''
end
return ret
end
function p.getDiscogsRedirectType(frame) -- 自动生成Discogs专辑重定向
local pagename = frame.args[1] -- 格式:歌手名周杰伦/专辑类型录音室专辑/年份2000 - 专辑名Jay
local ret = ''
if string.match(pagename, '%/') then
--ret = '#REDIRECT [['
ret = pagename:match("^(.-)/") or ""
--ret = frame:preprocess(ret)
else
ret = ret .. pagename .. ''
end
return ret
end
-------------------------------------------------------------------
function p.count_fruit(frame)
local num_bananas = tonumber(frame.args.bananas) or 0 -- Named arguments ({{#invoke:Example|count_fruit|foo=bar}})
local num_apples = tonumber(frame.args.apples) or 0 -- are likewise accessed by indexing `frame.args` by name (`frame.args["bananas"]`,
-- or equivalently `frame.args.bananas`.
local conj_bananas = num_bananas == 1 and 'banana' or 'bananas'
local conj_apples = num_apples == 1 and 'apple' or 'apples'
-- Ternary operators assign values based on a condition in a compact way.
-- Here, `conj_bananas` gets `'banana'` if `num_bananas` is 1, else `'bananas'`.
-- Similarly, `conj_apples` gets `'apple'` if `num_apples` is 1, else `'apples'`.
return 'I have ' .. num_bananas .. ' ' .. conj_bananas .. ' and ' .. num_apples .. ' ' .. conj_apples
-- Like above, concatenate a bunch of strings together to produce
-- a sentence based on the arguments given.
end
local function lucky(a, b) -- One can define custom functions for use. Here we define a function 'lucky' that has two inputs a and b. The names are of your choice.
if b == 'yeah' then -- Condition: if b is the string 'yeah'. Strings require quotes. Remember to include 'then'.
return a .. ' is my lucky number.' -- Outputs 'a is my lucky number.' if the above condition is met. The string concatenation operator is denoted by 2 dots.
else -- If no conditions are met, i.e. if b is anything else, output specified on the next line. 'else' should not have 'then'.
return a -- Simply output a.
end -- The 'if' section should end with 'end'.
end -- As should 'function'.
function p.Name2(frame)
-- The next five lines are mostly for convenience only and can be used as is for your module. The output conditions start on line 50.
local pf = frame:getParent().args -- This line allows template parameters to be used in this code easily. The equal sign is used to define variables. 'pf' can be replaced with a word of your choice.
local f = frame.args -- This line allows parameters from {{#invoke:}} to be used easily. 'f' can be replaced with a word of your choice.
local M = f[1] or pf[1] -- f[1] and pf[1], which we just defined, refer to the first parameter. This line shortens them as 'M' for convenience. You could use the original variable names.
local m = f[2] or pf[2] -- Second shortened as 'm'.
local l = f.lucky or pf.lucky -- A named parameter 'lucky' is shortend as l. Note that the syntax is different from unnamed parameters.
if m == nil then -- If the second parameter is not used.
return 'Lonely' -- Outputs the string 'Lonely' if the first condition is met.
elseif M > m then -- If the first condition is not met, this line tests a second condition: if M is greater than m.
return lucky(M - m, l) -- If the condition is met, the difference is calculated and passed to the self defined function along with l. The output depends on whether l is set to 'yeah'.
else
return 'Be positive!'
end
end
return p --All modules end by returning the variable containing their functions to Wikipedia.
-- Now we can use this module by calling {{#invoke: Example | hello }},
-- {{#invoke: Example | hello_to | foo }}, or {{#invoke:Example|count_fruit|bananas=5|apples=6}}
-- Note that the first part of the invoke is the name of the Module's wikipage,
-- and the second part is the name of one of the functions attached to the
-- variable that you returned.
-- The "print" function is not allowed in Wikipedia. All output is accomplished
-- via strings "returned" to Wikipedia.