Module:ja-see/sandbox

local export = {}

local m_ja = require("Module:ja") local m_ja_parse = require("Module:ja-parse/sandbox")

local function link(lemma, key, bracket) local anchor = key and ('ja-' .. key) or 'Japanese' return ' ' .. (bracket and '【' or '') ..  .. lemma ..  .. (bracket and '】' or '') .. ' ' end

local function ja(text) return ' ' .. text .. ' ' end

local function spelling_type(source, target) local source_spelling_type = m_ja.script(source) local target_spelling_type = m_ja.script(target) if source_spelling_type:find('Hani') then return target_spelling_type:find('Hani') and 'an alternative kanji' or 'a kanji' elseif source_spelling_type == 'Hira' and target_spelling_type ~= 'Hira' then return 'a hiragana' elseif source_spelling_type == 'Kana' and target_spelling_type ~= 'Kana' then return 'a katakana' else return 'an alternative' end end

local function format_definitions(defs, frame) -- format inspired by User:Wyang/zh-def local def_lines = {} local def_number = #defs for i = 1, #defs do		local def = defs[i] local def_line = def[1] if def.pos ~= '' then def_line = def_line:gsub('^# ', '# [' .. def.pos:lower .. '] ') end def_line = def_line:gsub('^#', ':') table.insert(def_lines, def_line) end if def_number > 5 then local shorter = { unpack(def_lines, 1, 5) } def_lines = { unpack(def_lines, 6) } local result = { ' ' } for i, v in ipairs(shorter) do			if i > 1 then table.insert(result, '\n') end table.insert(result, v)		end table.insert(result, ' ') for i, v in ipairs(def_lines) do			if i > 1 then table.insert(result, '\n') end table.insert(result, v)		end table.insert(result, ' ') def_lines = table.concat(result, '\n') else def_lines = table.concat(def_lines, '\n') end def_lines = frame:preprocess(def_lines) local log def_lines, log = def_lines:gsub(' %( ', ' [ ')	def_lines = def_lines:gsub(' %) %s*', ' ] ') return def_lines, def_number end

local function get_definitions_and_categories(lemma, key, frame) local wikitext = m_ja_parse.get_etym_section(lemma, key, frame) local def, cat = m_ja_parse.extract_definitions_and_categories(wikitext, lemma, key, frame) local def_number def, def_number = format_definitions(def, frame) if mw.title.getCurrentTitle.nsText ~=  then cat =  end return def, cat, def_number end

function export.show(frame) local args = frame:getParent.args local source = args.pagename or mw.title.getCurrentTitle.text local target = args[1] local source_link = ja(source) local source_spelling_type = spelling_type(source, target) local target_link = (source_spelling_type == 'a kanji' or source_spelling_type == 'an alternative kanji') and link(target, source) or link(target) local def, cat, def_number = get_definitions_and_categories(target, source, frame) local result = { [=[{| class="wikitable" style="min-width:70%" if def ~= '' then table.insert(result, 'the entry at ') end table.insert(result, " " .. target_link .. ' ') if args.gloss then table.insert(result, " (“" .. args.gloss .. "”).' ")	elseif def ==  then table.insert(result, ".''' ") else table.insert(result, " with the following definition" .. (def_number == 1 and  or 's') .. ".'\n" .. def .. '\n') end table.insert(result, '(This term, ' .. source_link .. ", is ''" .. source_spelling_type .. "'' spelling of " .. target_link .. [=[.)	table.insert(result, cat)	return table.concat(result) end
 * '''For pronunciation and definitions of ]=] .. source_link .. " – see " }
 * '''For pronunciation and definitions of ]=] .. source_link .. " – see " }
 * }]=])

local kanji_grade_labels = { ' 1 ',	' 2 ',	' 3 ',	' 4 ',	' 5 ',	' 6 ',	'S ', 'J ', 'H ' }

function export.show_kango(frame) local args = frame:getParent.args local source = args.pagename or mw.title.getCurrentTitle.text local kanji_table = { '{| style="width: 100%"' } local cats = {} local args_len = 0 -- too bad #args doesn't work for _, v in ipairs(args) do		local def, cat = get_definitions_and_categories(v, source, frame) table.insert(kanji_table, '|-') local kanji_grade_label = '' if mw.ustring.len(v) == 1 then local kanji_grade = m_ja.kanji_grade(v) if kanji_grade then kanji_grade_label = kanji_grade_labels[kanji_grade] end end table.insert(kanji_table, '| style="vertical-align:top; ' .. (args_len == 0 and '' or 'border-top:1px solid lightgray') .. '" | ' .. link(v, nil, true) .. ' ' .. kanji_grade_label) table.insert(kanji_table, args_len == 0 and '|' or '| style="border-top:1px solid lightgray" |') table.insert(kanji_table, def) table.insert(cats, cat) args_len = args_len + 1 end table.insert(kanji_table, '|}') kanji_table = table.concat(kanji_table, '\n') local source_link = ja(source) local result = { '{| class="wikitable" style="min-width:70%"' } table.insert(result, '|-') table.insert(result, "| For pronunciation and definitions of " .. source_link .. " – see the following " .. (args_len == 1 and "entry." or "entries.'''")) table.insert(result, '|-') table.insert(result, '| style="background-color: white" |') table.insert(result, kanji_table) table.insert(result, '|-') table.insert(result, "| (This term, " .. source_link .. ", is the hiragana spelling of " .. (args_len == 1 and "the Sino-Japanese term.)" or "several Sino-Japanese terms.)"))	table.insert(result, '|}')	cats = table.concat(cats)	return table.concat(result, '\n') .. cats end

return export