सामग्री पर जाएँ

मॉड्यूल:ja-see/old

विक्षनरी से

इस मॉड्यूल हेतु प्रलेख मॉड्यूल:ja-see/old/doc पर बनाया जा सकता है

local export = {}

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

local function link(lemma, bracket)
	local anchor = 'Japanese'
	return '<span lang="ja" class="Jpan">' .. (bracket and '【' or '') .. '[[' .. lemma .. '#' .. anchor .. '|' .. lemma .. ']]' .. (bracket and '】' or '') .. '</span>'
end

local function ja(text)
	return '<span lang="ja" class="Jpan">' .. text .. '</span>'
end

local function contains(list, item)
	for i = 1, #list do
		if list[i] == item then return true end
	end
	return false
end

local function format_headword(word)
	local result = table.concat(word.kana_spellings, ',')
	--[[
	local historical_spelling = word.historical_spellings[1]
	if not contains(word.kana_spellings, historical_spelling) then
		result = result .. '〔' .. m_ja.hira_to_kata(historical_spelling) .. '〕'
	end
	]]
	if #word.kanji_spellings > 0 then
		result = result .. '【' .. table.concat(word.kanji_spellings, '・') .. '】'
	end
	return result
end

local function spelling_type(nonlemma, lemma, hist)
	local nonlemma_spelling_type = m_ja.script(nonlemma)
	local lemma_spelling_type = m_ja.script(lemma)
	if hist then
		if nonlemma_spelling_type == 'Hira' then
			return 'a historical hiragana[[Category:Japanese historical hiragana|' .. m_ja.jsort(nonlemma_spelling_type) .. ']]'
		elseif nonlemma_spelling_type == 'Kana' then
			return 'a historical katakana[[Category:Japanese historical katakana|' .. m_ja.jsort(nonlemma_spelling_type) .. ']]'
		elseif nonlemma_spelling_type == 'Hira+Kana' then
			return 'a historical mixed kana[[Category:Japanese terms spelled with mixed historical kana|' .. m_ja.jsort(nonlemma_spelling_type) .. ']]'
		else
			return 'a historical'
		end
	else
		if nonlemma_spelling_type:find('Hani') then
			return lemma_spelling_type:find('Hani') and 'an alternative kanji' or 'a kanji'
		elseif nonlemma_spelling_type == 'Hira' and lemma_spelling_type ~= 'Hira' then
			return 'a hiragana[[Category:Japanese hiragana|' .. m_ja.jsort(nonlemma_spelling_type) .. ']]'
		elseif nonlemma_spelling_type == 'Kana' and lemma_spelling_type ~= 'Kana' then
			return 'a katakana[[Category:Japanese katakana|' .. m_ja.jsort(nonlemma_spelling_type) .. ']]'
		elseif nonlemma_spelling_type == 'Hira+Kana' and lemma_spelling_type ~= 'Hira+Kana' then
			return 'a mixed kana[[Category:Japanese terms spelled with mixed kana|' .. m_ja.jsort(nonlemma_spelling_type) .. ']]'
		else
			return 'an alternative'
		end
	end
end

local function format_definitions(def, frame, reading) -- format inspired by [[User:Wyang/zh-def]]
	local def_lines = {}
	local def_count = #def
	for i = 1, #def do
		local def_line = def[i][1]
		if def[i].pos ~= '' then
			def_line = def_line:gsub('^# ', '# <span style="padding-right:.6em;color:#5A5C5A;font-size:80%%">[' .. def[i].pos:lower() .. ']</span>')
		end
		def_line = def_line:gsub('^#', ':')
		table.insert(def_lines, def_line)
	end
	def_lines = table.concat(def_lines, '\n')
	def_lines = frame:preprocess(def_lines)
	def_lines = def_lines:gsub('%[%[Category:([^%]|]*)%]%]', '[[Category:%1|' .. m_ja.jsort(reading) .. ']]')
	if def_count <= 5 then
		if def_lines == "" then require('Module:debug').track('ja-see/no definitions found') end
		return def_lines, def_count
	else
		local shorter = {}
		for i, v in ipairs(mw.text.split(def_lines, '\n')) do
			if i <= 5 then table.insert(shorter, v) end
		end
		shorter = table.concat(shorter, '\n')
		local result = { '<div class="vsSwitcher" data-toggle-category="glosses"><span class="vsToggleElement"></span><div class="vsShow" style="display: none;">' }
		table.insert(result, shorter)
		table.insert(result, '</div><div class="vsHide">')
		table.insert(result, def_lines)
		table.insert(result, '</div></div>')
		return table.concat(result, '\n'), def_count
	end
end

function export.show(frame)
	local args = frame:getParent().args
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	local nonlemma = args.pagename or mw.title.getCurrentTitle().subpageText
	local lemma = args[1]
	
	if lemma == nonlemma then error('ja-see: the soft-directed item is equivalent to the page title') end
	if args.gloss then error('ja-see: the parameter "gloss" is obsolete') end
	if args[2] then error('ja-see: parameter #2 is temporarily disabled.') end
	
	local defs = {}
	local cats = {}
	
	local is_nonlemma_modern = false
	local is_nonlemma_historical = false
	
	local word_count = 0
	local words = m_ja_parse.words(lemma)
	for _, word in ipairs(words) do
		if word.type == 'lemma' then
			local is_modern = contains(word.kana_spellings, nonlemma) or contains(word.kanji_spellings, nonlemma)
			local is_historical = contains(word.historical_spellings, nonlemma)
			if is_modern or is_historical then
				word_count = word_count + 1
				if is_modern then is_nonlemma_modern = true end
				if is_historical then is_nonlemma_historical = true end
				local wikitext = word[1]
				local def, cat = m_ja_parse.parse_word(wikitext, lemma, nonlemma, frame, word.kana_spellings[1])
				def = format_definitions(def, frame, word.kana_spellings[1])
				if NAMESPACE ~= '' then cat = '' end
				table.insert(defs, '; ' .. ja(format_headword(word)) .. '\n' .. def .. '\n')
				table.insert(cats, cat)
			end
		end
	end
	if word_count > 1 then require('Module:debug').track('ja-see/multiple readings') end
	
	local nonlemma_spelling_type = spelling_type(nonlemma, lemma, is_nonlemma_historical and not is_nonlemma_modern)
	
	local result = { [=[{| class="wikitable" style="min-width:70%"
|-
| '''For pronunciation and definitions of ]=] .. ja(nonlemma) .. " – see " }
	if #defs > 0 then table.insert(result, 'the following entr' .. ((#defs == 1) and 'y' or 'ies') .. ' at ') end
	table.insert(result, "<span style='font-size:120%'>" .. link(lemma) .. '</span>')
	
	if #defs == 0 then
		table.insert(result, ".'''<br>")
	else
		table.insert(result, ".'''\n|-\n| style=\"background-color: white\" |\n" .. table.concat(defs, '\n') .. '\n|-\n| ')
	end
	table.insert(result, '(This term, ' .. ja(nonlemma) .. ", is ''" .. nonlemma_spelling_type .. "'' spelling of " .. link(lemma) .. [=[.)
|}]=])
	table.insert(result, table.concat(cats))
	return table.concat(result)
end

local kanji_grade_labels = {
	'<span class="explain" title="Grade 1 kanji" style="vertical-align: top;">1</span>',
	'<span class="explain" title="Grade 2 kanji" style="vertical-align: top;">2</span>',
	'<span class="explain" title="Grade 3 kanji" style="vertical-align: top;">3</span>',
	'<span class="explain" title="Grade 4 kanji" style="vertical-align: top;">4</span>',
	'<span class="explain" title="Grade 5 kanji" style="vertical-align: top;">5</span>',
	'<span class="explain" title="Grade 6 kanji" style="vertical-align: top;">6</span>',
	'<span class="explain" title="Jōyō kanji" style="vertical-align: top;">S</span>',
	'<span class="explain" title="Jinmeiyō kanji" style="vertical-align: top;">J</span>',
	'<span class="explain" title="Hyōgaiji kanji" style="vertical-align: top;">H</span>'
}

local sort

function export.show_kango(frame)
	local args = frame:getParent().args
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	local nonlemma = args.pagename or mw.title.getCurrentTitle().text
	
	if args.sort or args[1]:find('【') then return sort(args) end
	
	local kanji_table = { '{| style="width: 100%"' }
	local cats = {}
	local single_kanji = false
	
	local args_len = 0
	local historical_only = {}
	local is_modern = {}
	
	for _, lemma in ipairs(args) do
		local words = m_ja_parse.words(lemma)
		local wikitext = {}
		for _, word in ipairs(words) do
			if word.type == 'lemma' and (contains(word.kana_spellings, nonlemma) or contains(word.historical_spellings, nonlemma)) then
				table.insert(wikitext, word[1])
				if not contains(word.kana_spellings, nonlemma) then
					table.insert(historical_only, lemma)
				else
					table.insert(is_modern, lemma)
				end
			end
		end
		
		wikitext = table.concat(wikitext, '\n')
		
		local def, cat = m_ja_parse.parse_word(wikitext, lemma, nonlemma, frame, nonlemma)
		def = format_definitions(def, frame, nonlemma)
		if NAMESPACE ~= '' then cat = '' end
		
		table.insert(kanji_table, '|-')
		
		local kanji_grade_label = ''
		if mw.ustring.len(lemma) == 1 then
			local kanji_grade = m_ja.kanji_grade(lemma)
			if kanji_grade then kanji_grade_label = kanji_grade_labels[kanji_grade] end
			single_kanji = true
		end
		table.insert(kanji_table, '| style="white-space:nowrap;vertical-align:top; ' .. (args_len == 0 and '' or 'border-top:1px solid lightgray') .. '" | <span style="font-size:x-large">' .. link(lemma, true) .. '</span>' .. 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 function set_difference(a, b)
		local result = {}
		for _, item in ipairs(a) do
			if not contains(b, item) then
				table.insert(result, item)
			end
		end
		return result
	end
	historical_only = set_difference(historical_only, is_modern)
	
	if #historical_only > 0 and #is_modern > 0 then error('It seems that the current page title is the modern kana spelling of some words (' .. table.concat(is_modern, ', ') .. ') and the historical kana spelling of some other (' .. table.concat(historical_only, ', ') .. '), please put them in different etymology sections.') end
	local is_historical = #historical_only > 0
	
	local nonlemma_link = ja(nonlemma)
	
	local result = { '{| class="wikitable" style="min-width:70%"' }
	table.insert(result, '|-')
	table.insert(result, "| '''For pronunciation and definitions of " .. nonlemma_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, " .. nonlemma_link .. ", is ''the " .. spelling_type(nonlemma, args[1], is_historical):gsub('^an? ', '') .. "'' spelling of " .. (args_len == 1 and "the Sino-Japanese term.)" or "several Sino-Japanese terms.)")
		.. (single_kanji and "<br><small>For a list of all kanji with " .. (is_historical and "historical on'yomi" or "on'yomi") .. " " .. nonlemma_link .. ", not just those used in Sino-Japanese terms, see " .. (is_historical and ("[[:Category:Japanese kanji with historical on reading " .. nonlemma .. "]]") or ("[[:Category:Japanese kanji with on reading " .. nonlemma .. "]]")) .. ".</small>" or ""))
	table.insert(result, '|}')
	
	cats = table.concat(cats)
	
	return table.concat(result, '\n') .. cats
end

function sort(list)
	if list[1]:find('【') then -- when copying 漢字項目 from daijirin@welio
		local list2 = {}
		for i in mw.ustring.gmatch(list[1], '【 ?(.)') do table.insert(list2, i) end
		list = list2
	end
	
	local buckets = {}
	for i = 1, 10 do buckets[i] = {} end
	
	for _, kanji in ipairs(list) do
		local grade = mw.ustring.len(kanji) == 1 and m_ja.kanji_grade(kanji) or 10
		table.insert(buckets[grade], kanji)
	end
	
	local result = {}
	for _, bucket in ipairs(buckets) do
		for _, kanji in ipairs(bucket) do
			table.insert(result, kanji)
		end
	end
	
	return '{{ja-see-kango|' .. table.concat(result, '|') .. '}}'
end

function export.show_gv(frame)
	local args = frame:getParent().args
	local NAMESPACE = mw.title.getCurrentTitle().nsText
	local variant_glyph = args.pagename or mw.title.getCurrentTitle().subpageText
	local normalized_glyph = args[1]
	if normalized_glyph == variant_glyph then error('ja-see: the soft-directed item is equivalent to the page title') end

	local words = {}
	local kanjitabs = {}
	for _, word in ipairs(m_ja_parse.words(normalized_glyph)) do
		for kanjitab in word[1]:gmatch('{{ja%-kanjitab.-}}') do
			table.insert(kanjitabs, (kanjitab:gsub('|alt=[^|}]*', '')))
		end
		if word.type == 'lemma' then
			word.lemma = normalized_glyph
			table.insert(words, word)
		elseif word.type == 'redirect' then
			local function insert_lemma(lemmas, lemma)
				if lemma ~= normalized_glyph and not contains(lemmas, lemma) then table.insert(lemmas, lemma) end
			end
			local lemmas = {}
			for _, lemma in ipairs(word.kana_spellings) do insert_lemma(lemmas, lemma) end
			for _, lemma in ipairs(word.kanji_spellings) do insert_lemma(lemmas, lemma) end
			for _, lemma in ipairs(lemmas) do
				for _, word2 in ipairs(m_ja_parse.words(lemma)) do
					if word2.type == 'lemma' and (contains(word2.kana_spellings, normalized_glyph) or contains(word2.kanji_spellings, normalized_glyph) or contains(word2.historical_spellings, normalized_glyph)) then
						word2.lemma = lemma
						table.insert(words, word2)
					end
				end
			end
		end
	end
	
	local lemma = {}
	for _, word in ipairs(words) do
		if not contains(lemma, word.lemma) then table.insert(lemma, word.lemma) end
	end
	lemma = #lemma == 1 and lemma[1] or nil
	
	local defs = {}
	local cats = {}
	
	for _, word in ipairs(words) do
		local def, cat = m_ja_parse.parse_word(word[1], word.lemma, normalized_glyph, frame, word.kana_spellings[1])
		if #def > 0 then
			def = format_definitions(def, frame, word.kana_spellings[1])
			if NAMESPACE ~= '' then cat = '' end
			table.insert(defs, '; ' .. ja(format_headword(word)) .. (lemma and '' or ('<span lang="ja" class="Jpan><span style="font-size:120%;background:#f8f9fa;padding: 0 0.2em;">[[' .. word.lemma .. '#Japanese|→' .. word.lemma .. ']]</span></span>')) .. '\n' .. def .. '\n')
			table.insert(cats, cat)
		end
	end
	
	kanjitabs = table.concat(kanjitabs, '\n') .. (#kanjitabs > 0 and '\n' or '')
	if kanjitabs ~= '' then kanjitabs = frame:preprocess(kanjitabs) end
	local result = { kanjitabs, [=[{| class="wikitable" style="min-width:70%"
|-
| '''For pronunciation and definitions of ]=] .. ja(variant_glyph) .. " – see " }
	if #defs > 0 then
		table.insert(result, 'the following entr' .. ((#defs == 1) and 'y' or 'ies'))
		if lemma then table.insert(result, " at <span style='font-size:120%'>" .. link(lemma) .. '</span>') end
	else
		table.insert(result, "<span style='font-size:120%'>" .. link(normalized_glyph) .. '</span>')
	end
	
	if #defs == 0 then
		table.insert(result, ".'''<br>")
	else
		table.insert(result, ".'''\n|-\n| style=\"background-color: white\" |\n" .. table.concat(defs, '\n') .. '\n|-\n| ')
	end
	table.insert(result, '(This term, ' .. ja(variant_glyph) .. ", is ''" .. 'a glyph variant' .. "'' of " .. link(normalized_glyph) .. (#defs > 0 and (lemma and (lemma == normalized_glyph and '' or (", which in turn is ''" .. spelling_type(normalized_glyph, lemma, false) .. "'' spelling of " .. link(lemma))) or ", which in turn is ''a kanji'' spelling of several words") or '') .. [=[.)
|}]=])
	table.insert(result, table.concat(cats))
	return table.concat(result)
	
end

return export