local M = {}
local len = mw.ustring.len
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local find = mw.ustring.find
local function format_Chinese_text(text) return '<span class="Hani" lang="zh">' .. text .. '</span>' end
local function format_Chinese_text_trad(text) return '<span class="Hant" lang="zh-Hant">' .. text .. '</span>' end
local function format_Chinese_text_simp(text) return '<span class="Hans" lang="zh-Hans">' .. text .. '</span>' end
local function format_rom(text) return text and '<i><span class="tr Latn" lang="zh-Latn">' .. text .. '</span></i>' or nil end
local function format_gloss(text) return text and '“' .. text .. '”' or nil end
function M.link(frame, mention, args, pagename, no_transcript)
if (args and args == '') or (frame and frame:getParent().args and frame:getParent().args == '') then
return ''
end
local params = {
= {},
= {},
= {},
= {},
= {},
= {},
= {},
= { alias_of = 'gloss' },
}
if mention then
params = {}
end
local moduleCalled
if args then
moduleCalled = true
end
args = args or frame:getParent().args
if not moduleCalled then
params.required = true
end
args = require("Module:parameters").process(args, params)
if moduleCalled then
if not args then
return ""
end
end
if not pagename then
pagename = mw.loadData("Module:headword/data").pagename
end
local text, tr, gloss, cat
if args and match(args, '') then
gloss = args
tr = args
text = args .. '/' .. args
else
text = args
if args then
tr = args
gloss = args
else
if args or (args and (match(args, '') or match(args, 'h?y???g?'))) then
tr = args
gloss = args
else
gloss = args
end
end
end
if args then
tr = args
gloss = gloss or args
end
if text then
if not text:match'%%]' then
local m_zh = require("Module:zh")
local words = mw.text.split(text, "/", true)
if #words == 1 and m_zh.ts_determ(words) == 'trad' and not match(words, '%*') then
table.insert(words, m_zh.ts(words))
end
if not tr and not no_transcript and words then
cap = find(words, "^%^")
words = gsub(words, "^%^", "")
if words then
words = gsub(words, "^%^", "")
end
tr, cat = require("Module:zh/extract").extract_pron(words, "m", cap)
end
for i, word in ipairs(words) do
word = gsub(word, '%*', '')
if mention then
if m_zh.ts_determ(words) == 'both' then
words = '<i class="Hani mention" lang="zh">]</i>'
elseif m_zh.ts_determ(words) == 'trad' then
words = '<i class="Hant mention" lang="zh-Hant">]</i>'
else
words = '<i class="Hans mention" lang="zh-Hans">]</i>'
end
--[[ (disabled to allow links to, for example, a link to 冥王星#Chinese from 冥王星#Japanese. 18 May, 2016)
elseif word == pagename then
word = format_Chinese_text('<b>' .. word .. '</b>')
]]
else
if m_zh.ts_determ(words) == 'both' then
words = format_Chinese_text(']')
elseif m_zh.ts_determ(words) == 'trad' then
words = format_Chinese_text_trad(']')
else
words = format_Chinese_text_simp(']')
end
end
end
text = table.concat(words, format_Chinese_text('/'))
else
text = require("Module:links").language_link{
term = text,
lang = require("Module:languages").getByCode("zh"),
}
if mention then
text = '<i class="Hani mention" lang="zh">' .. gsub(text, "%*", "") .. '</i>'
else
text = format_Chinese_text(gsub(text, "%*", ""))
end
end
end
if tr == '-' or no_transcript then
tr = nil -- allow translit to be disabled: remove translit if it is "-", just like normal {{l}}
end
local notes = args
local lit = args
if tr or gloss or notes or lit then
local annotations = {}
if tr then
tr = format_rom(tr)
tr = gsub(tr, "'", "'")
tr = gsub(tr, "#", "")
table.insert(annotations, tr)
end
if gloss then
table.insert(annotations, format_gloss(gloss))
end
table.insert(annotations, notes)
if lit then
table.insert(annotations, "literally " .. format_gloss(lit))
end
annotations = table.concat(annotations, ", ")
text = text .. " (" .. annotations .. ")"
end
return text .. (cat or "")
end
-- we cannot just return the function here because this is also invoked by a template.
return M