Module:ja-usex: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Line 32: Line 32:
translation = second_param
translation = second_param
kana = example
kana = example
end

if translation == '' then
translation = '<small>(please add an English translation of this example)</small> [[Category:Japanese usage examples with the translation missing]]'
end
end

Revision as of 07:47, 21 August 2017

This does all of the work of {{ja-usex}}. It displays an example or quote like {{usex}}, but unlike usex it adds furigana to the example and generates the romanization to be used as the transliteration comprising the second line of the quote or example.

Requiring Module:ja, Module:scripts, Module:parameters, Module:utilities, Module:links, Module:languages, Module:ja-ruby.

Informal testcases: [1]


local m_ja = require("Module:ja")

local export = {}

-- main entry point
function export.show(frame)
	local params = {
		[1] = {},
		[2] = {},
		[3] = {},
		["rom"] = {},
		["manyou"] = {},
		["m"] = { alias_of = "manyou" },
		["manyou_kana"] = {},
		["m_kana"] = { alias_of = "manyou_kana" },
		["ref"] = {},
	}
	local args = frame:getParent().args
	local text = {}
	-- Custom errors are preferable to the generic "These parameters are required.",
	-- which would be generated if "required = true" were added to the tables for parameters 1 and 2.
	local example = args[1] or error("Example has not been specified. Please pass parameter 1 to the module invocation.")
	local second_param = args[2] or error("A kana form or a translation must be passed in parameter 2.")
	
	-- if user only specified two params, assume second param is translation, first param has no kanji
	local translation = args[3]
	
	local kana
	if translation then 
		kana = second_param
	else
		translation = second_param
		kana = example
	end

	if translation == '' then
		translation = '<small>(please add an English translation of this example)</small> [[Category:Japanese usage examples with the translation missing]]'
	end
	
	local romaji = args["rom"]
	local manyou = args["manyou"]
	local old_kana = args["manyou_kana"]
	local ref = args["ref"]
	
	local tag_start = " <span style=\"color:darkgreen; font-size:x-small;\">&#91;" -- see also [[module:zh-usex]]
	local tag_end = "&#93;</span>"
	
	if manyou then
		table.insert(text, ('<span lang="ja" class="Jpan">%s</span>'):format((old_kana ~= "") and m_ja.add_ruby_backend(manyou, old_kana) or manyou))
		table.insert(text, tag_start)
		table.insert(text, "[[w:Man'yōgana|Man'yōgana]]")
		table.insert(text, tag_end)
		table.insert(text, "<dd>")
	end
	table.insert(text, ('<span lang="ja" class="Jpan">%s</span>'):format(m_ja.add_ruby_backend(example, kana)))
	if ref then
		table.insert(text, ref)
	end
	if manyou then
		table.insert(text, tag_start)
		table.insert(text, "Modern spelling")
		table.insert(text, tag_end)
		table.insert(text, "</dd>")
	end
	table.insert(text, "<dl><dd><i>")
	if romaji then
		table.insert(text, romaji)
	else
		-- add capitalization markup to the kana if manual capitalization is not already present
		if mw.ustring.match(kana, "[。?!]") and not mw.ustring.match(kana, "%^") then
			-- "「テスト」です。"→"「^テスト」^です。"
			kana = mw.ustring.gsub(kana, "([^「」『』。?!]+)", "^%1")
			-- "「^此れ (^これ) ^は テスト です。」"→"「^此れ (^これ) は テスト です。」"
			-- (remove a "^" that appears between an ending bracket and a sentence-ending punctuation)
			kana = mw.ustring.gsub(kana, "([)」』]+)%^(.+)([。?!])", "%1%2%3")
		end

		-- add romaji
		table.insert(text, m_ja.kana_to_romaji(kana))
	end
	table.insert(text, "</i></dd><dd>")
	table.insert(text, translation)
	table.insert(text, "</dd></dl>")
	return table.concat(text)
end

return export