Modul:ja-link
Jump to navigation
Jump to search
Pendokumenan untuk modul ini boleh diciptakan di Modul:ja-link/doc
local export = {}
local m_ja = require("Module:ja")
local lang = require("Module:languages").getByCode("ja")
local sc = require("Module:scripts").getByCode("Jpan")
local m_links = require("Module:links")
-- Similar to test_script function in [[Module:ja-usex]] and [[Module:headword]].
local function is_script(text, script_codes)
if type(script_codes) == "string" then
script_codes = { script_codes }
end
if type(text) == "string" and type(script_codes) == "table" then
local characters = {}
for i, script_code in pairs(script_codes) do
local sc = require("Module:scripts").getByCode(script_code)
if sc and sc:getCharacters() then
table.insert(characters, sc:getCharacters())
else
error("The script code " .. script_code .. " is invalid, or has no characters listed.")
end
end
local out
if #characters > 0 then
text = mw.ustring.gsub(text, "%W", "")
out = mw.ustring.find(text, "^[" .. table.concat(characters) .. "]+$")
else
return nil
end
if out then
return true
else
return false
end
else
mw.log("Parameters to test_script were incorrect.")
return nil
end
end
function export.link(data, options)
if not options then
options = {}
end
if not data.transliteration then
data.transliteration = m_ja.kana_to_romaji(data.kana, { hist = options.hist })
elseif options.hist then
error('If the transliteration has already been provided, the "' .. hist .. '" option does nothing.')
end
if not data.kana then
data.kana = data.lemma
end
if require("Module:scripts").getByCode('Hani'):countCharacters(data.transliteration) > 0 then
error('The transliteration "' .. data.transliteration .. '" seems to have kanji in it. Was the kana reading properly provided?')
end
if options.caps then
data.transliteration = mw.ustring.gsub(data.transliteration, "^%l", mw.ustring.upper)
data.transliteration = mw.ustring.gsub(data.transliteration, " %l", mw.ustring.upper)
end
if data.transliteration ~= '-' then
data.transliteration = m_links.remove_links(data.transliteration) -- needed if $lemma has manual wikilinks
data.transliteration = require("Module:script utilities").tag_translit(data.transliteration, "ja", "kata")
end
if data.lemma and data.kana and data.lemma ~= data.kana then
data.ruby = m_ja.add_ruby_backend(data.lemma, data.kana)
end
data.lemma = m_ja.remove_ruby_markup(data.lemma)
if data.gloss == "" then
data.gloss = nil
end
if data.pos == "" then
data.pos = nil
end
if data.ruby and mw.ustring.match(data.ruby, '%[%[') then -- for if $lemma has manual wikilinks
return m_links.full_link{
lang = lang,
sc = sc,
term = nil,
alt = data.ruby,
tr = data.transliteration,
gloss = data.gloss,
pos = data.pos
}
else
return m_links.full_link{
lang = lang,
sc = sc,
term = data.linkto or data.lemma,
alt = data.ruby,
tr = data.transliteration,
gloss = data.gloss,
pos = data.pos
}
end
end
function export.show(frame)
local params = {
[1] = { required = true },
[2] = {},
[3] = {},
['gloss'] = { alias_of = 3 },
['pos'] = {},
['linkto'] = {},
['caps'] = { type = "boolean" },
['rom'] = {},
['hist'] = { type = "boolean" },
}
local args = require("Module:parameters").process(frame:getParent().args, params)
local kana = args[2]
if not kana then
if args[1] and is_script(args[1], { "Hira", "Kana" }) then
kana = args[1]
else
error("Either the first parameter or the second parameter should be Japanese text written in kana only.")
end
end
local data = {
lemma = args[1],
kana = kana,
gloss = args[3],
pos = args["pos"],
linkto = args["linkto"],
transliteration = args["rom"],
}
local options = {
caps = args["caps"],
hist = args["hist"],
}
return export.link(data, options)
end
return export