local export = {}
local m_ja = require('Module:ja')
local m_links = require('Module:links')
local m_languages = require('Module:languages')
local m_scripts = require('Module:scripts')
local match = mw.ustring.match
local gsub = mw.ustring.gsub
local delimiter_conversion_table = {
= ' < ',
= ' > ',
= ', ',
}
function export.link(lang, term, face)
return m_links.full_link(
{
lang = lang,
-- sc = m_scripts.getByCode('Jpan'),
term = term,
},
face,
true -- allowSelfLink
)
end
function export.process_terms(lang, text, face, text_type)
if text == '' then
return export.link(lang, false, face) -- '' term request
end
local ret = ''
if match(text, '') then
local term, delimiter, remainder = match(text, '^(+)()(.*)$')
ret = ret .. export.process_terms(lang, term, face, text_type)
ret = ret .. delimiter_conversion_table
ret = ret .. export.process_terms(lang, remainder, face, text_type)
else
local term = text
term = mw.text.trim(term)
-- term = m_ja.remove_ruby_markup(term)
if text_type == 'term' then
ret = ret .. export.link(lang, term, face)
elseif text_type == 'tr' then
-- XXX: different types of romaji for different situations
ret = ret .. m_ja.kana_to_romaji(term)
end
end
return ret
end
--[==[
* $args.1: { 'ja' , 'あける', '開ける', 'to open' }
* $data.face: {{jpx-m}}, $face: 'term'
]==]
function export.show(frame)
local params = {
= { },
= { },
= { alias_of = 'gloss' },
= { },
= { },
= { },
}
local args = require('Module:parameters').process(frame:getParent().args, params)
local data = {}
data.lang = nil
data.terms = nil
data.tr = nil
data.gloss = args.gloss
data.pos = args.pos
data.lit = args.lit
data.face = frame.args.face -- ] or ]
-- determine what $args is composed of
-- $data.lang
local m_ja_script_result
if match(args, '^?$') then
data.lang = table.remove(args, 1)
else
data.lang = 'ja'
end
data.lang = m_languages.getByCode(data.lang)
-- $data.gloss
m_ja_script_result = m_ja.script(args])
if match(m_ja_script_result, 'Romaji') then
data.gloss = (data.gloss and error('Duplicate glosses.') or table.remove(args))
end
-- determine what $args is composed of
-- generate $data.kana from entry
if not data.kana then
-- XXX: fairly hacky
local readings = require('Module:User:Suzukaze-c/02').extract_ja_readings(data.kanji)
if readings then
data.kana, data.tr = readings, data.lang.tr(readings)
end
end
-- process $data.kanji, process $data.kana, generate $data.tr
if data.kanji then
data.kanji = export.process_terms(data.lang, data.kanji, data.face, 'term')
end
if data.kana == '-' then
data.kana = nil
end
if data.kana then
if not data.tr and data.kana ~= '' then
data.tr = export.process_terms(data.lang, data.kana, data.face, 'tr')
end
data.kana = export.process_terms(data.lang, data.kana, data.face, 'term')
end
-- process $data.gloss, process $data.tr, process $data.pos, process $data.lit
data.gloss = (data.gloss and m_links.mark(data.gloss, 'gloss', data.face, data.lang))
data.tr = (data.tr and m_links.mark(data.tr, 'tr', data.face, data.lang))
data.pos = (data.pos and m_links.mark(data.pos, 'pos', data.face, data.lang))
data.lit = (data.lit and 'literally' .. m_links.mark(data.lit, 'gloss', data.face, data.lang))
-- generate link annotations
local annotations = {}
if data.kana and data.kanji then
table.insert(annotations, (data.is_kana_first and data.kanji or data.kana))
end
table.insert(annotations, data.tr)
table.insert(annotations, data.gloss)
table.insert(annotations, data.pos)
table.insert(annotations, data.lit)
if #annotations > 0 then
annotations = ' ' .. m_links.mark(table.concat(annotations, ', '), 'annotations', data.face, data.lang)
else
annotations = false
end
return table.concat(
{
(data.is_kana_first and data.kana or data.kanji),
(annotations and annotations or ''),
' <code>[lang: ',
data.lang:getCode(),
']</code>',
}
)
end
return export