Module:ja-usex: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
the betterest
removing ruby markup before generating ruby markup caused problems; in any case, it seems not to be needed now
Line 82: Line 82:
elseif test_script(args[1], "Jpan") then
elseif test_script(args[1], "Jpan") then
example = args[1]
example = args[1]
example = m_ja.remove_ruby_markup(example)


if args[2] and test_script(args[2], "Jpan") then
if args[2] and test_script(args[2], "Jpan") then

Revision as of 22:25, 6 September 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 export = {}

local m_ja = require("Module:ja")
--[=[
	also [[Module:scripts]], [[Module:parameters]], [[Module:utilities]]
]=]

local pagename = mw.title.getCurrentTitle().text

local function test_script(text, scriptCode, entirely)
	if type(text) == "string" and type(scriptCode) == "string" then
		local characters = require("Module:scripts").getByCode(scriptCode):getCharacters()
		
		local out
		if entirely then
			out = mw.ustring.match(text, "^[" .. characters .. "]+$")
		else
			text = mw.ustring.gsub(text, "%W", "")
			out = mw.ustring.find(text, "[" .. characters .. "]")
		end
		
		return out
	else
		mw.log("Parameters to test_script were incorrect.")
	end
end

-- main entry point
function export.show(frame)
	local params = {
		[1] = {},
		[2] = {},
		[3] = {},
		["lit"] = {},
		["rom"] = {},
		["manyou"] = {},
		["m"] = { alias_of = "manyou" },
		["manyou_kana"] = {},
		["m_kana"] = { alias_of = "manyou_kana" },
		["ref"] = {},
		["sort"] = {},
		["inline"] = {},	-- not currently used
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local literally = args.lit
	
	local sortkey
	-- Use sort parameter, or title if it consists only of kana.
	if args.sort then
		sortkey = args.sort
	elseif test_script(pagename, "Hira", true) or test_script(pagename, "Kana", true) then
		sortkey = pagename
	else
		-- [[Special:WhatLinksHere/Template:tracking/ja-usex/no sortkey]]
		require("Module:debug").track("ja-usex/no sortkey")
	end
	
	-- Process sortkey.
	if sortkey then
		sortkey = m_ja.jsort(sortkey)
	end
	
	local text = {}
	local categories = { "Japanese terms with usage examples" }

	local example, kana, translation

	-- 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.
	if not args[1] then
		error("Usage example text has not been given.")
	elseif test_script(args[1], "Hani") then
		example = args[1]

		if args[2] and test_script(args[2], "Jpan") then
			kana = args[2]
			translation = args[3]
		else
			error("Kana spelling of the usage example text has not been given.")
		end
	elseif test_script(args[1], "Jpan") then
		example = args[1]

		if args[2] and test_script(args[2], "Jpan") then
			kana = args[2]
			translation = args[3]
		else
			kana = args[1]
			translation = args[2]
		end
	end
	
	if not test_script(translation, "Latn") then
		translation = '<small>(please add an English translation of this example)</small>'
		table.insert(categories, "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

	local ruby_text = m_ja.add_ruby_backend(example, kana)
	table.insert(text, ('<span lang="ja" class="Jpan">%s</span>'):format(ruby_text))
		
	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

	if kana or romaji or translation then
		table.insert(text, "<dl>")
	end
	if kana or romaji then
		table.insert(text, "<dd><i>")
		if romaji then
			table.insert(text, romaji)
		elseif kana then
			-- 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>")
	end
	if translation then
		table.insert(text, "<dd>" .. translation .. "</dd>")
	end
	if literally then
		table.insert(text, "<dd>(literally, “" ..  literally .. "”)</dd>")
	end
	table.insert(text, "</dl>")

	return table.concat(text) .. require("Module:utilities").format_categories(categories, lang, args.sort)
end

return export