Module:ja-link

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

This does the work of {{ja-r}}.


local export = {}
local m_ja = require("Module:ja")
local lang = require("Module:languages").getByCode("ja")
local sc = require("Module:scripts").getByCode("Jpan")
local m_links = require("Module:links")

function export.link(data, options)
	if not options then
		options = {}
	end
	
	-- if only one arg passed, it may be kana with hyphens
	if not data.kana then
		data.kana = data.lemma
	end
	data.lemma = mw.ustring.gsub(data.lemma, '[%-^.]', '')
	if not data.transliteration then
		data.transliteration = m_ja.kana_to_romaji(data.kana)
	end

	if require("Module:scripts").getByCode('Hani'):countCharacters(data.transliteration) > 0 then
		error('The transliteration "' .. data.transliteration .. '" seems to have kanji in it. Was the kana reading properly provided?')
	end
	if options.caps then
		data.transliteration = mw.ustring.gsub(data.transliteration, "^%l", mw.ustring.upper)
		data.transliteration = mw.ustring.gsub(data.transliteration, " %l", mw.ustring.upper)
	end
	if data.transliteration ~= '-' then
		data.transliteration = m_links.remove_links(data.transliteration) -- needed if $lemma has manual wikilinks
		data.transliteration = require("Module:script utilities").tag_translit(data.transliteration, "ja", "term")
	end

	data.ruby = m_ja.add_ruby_backend(data.lemma, data.kana)
	data.lemma = mw.ustring.gsub(data.lemma, "[%% ]", "")
	
	if data.gloss == "" then
		data.gloss = nil
	end
	
	if data.pos == "" then
		data.pos = nil
	end

	if mw.ustring.match(data.ruby, '%[%[') then -- for if $lemma has manual wikilinks
		return m_links.full_link{
			lang = lang,
			sc = sc,
			term = nil,
			alt = data.ruby,
			tr = data.transliteration,
			gloss = data.gloss,
			pos = data.pos
		}
	else
		return m_links.full_link{
			lang = lang,
			sc = sc,
			term = data.linkto or data.lemma,
			alt = data.ruby,
			tr = data.transliteration,
			gloss = data.gloss,
			pos = data.pos
		}
	end
	
end

function export.show(frame)
	local params = {
		[1] = { required = true },
		[2] = {},
		[3] = {},
		['gloss'] = { alias_of = 3 },
		['pos'] = {},
		['linkto'] = {},
		['caps'] = { type = "boolean" },
		['rom'] = {},
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)

	local data = {
		lemma = args[1],
		kana = args[2],
		gloss = args[3],
		pos = args["pos"],
		linkto = args["linkto"],
		transliteration = args["rom"],
	}
	
	local options = {
		caps = args["caps"],
	}
	
	return export.link(data, options)
end

return export