Bước tới nội dung

Mô đun:ryu-link

Từ điển mở Wiktionary
local export = {}
local m_ryu = require("Module:ryu")
local lang = require("Module:languages").getByCode("ryu")
local sc = require("Module:scripts").getByCode("Jpan")
local m_links = require("Module:links")

local function if_not_empty(var)
	if var == "" then
		return nil
	else
		return var
	end
end

-- Similar to test_script function in [[Module:ja-usex]] and [[Module:headword]].
local function is_script(text, script_codes)
	if type(script_codes) == "string" then
		script_codes = { script_codes }
	end
	
	if type(text) == "string" and type(script_codes) == "table" then
		local characters = {}
		
		for i, script_code in pairs(script_codes) do
			local sc = require("Module:scripts").getByCode(script_code)
			if sc and sc:getCharacters() then
				table.insert(characters, sc:getCharacters())
			else
				error("The script code " .. script_code .. " is invalid, or has no characters listed.")
			end
		end
		
		local out
		if #characters > 0 then
			text = mw.ustring.gsub(text, "%W", "")
			out = mw.ustring.find(text, "^[" .. table.concat(characters) .. "]+$")
		else
			return nil
		end
		
		if out then
			return true
		else
			return false
		end
	else
		mw.log("Parameters to test_script were incorrect.")
		return nil
	end
end

local function track(code)
	require("Module:debug").track("ryu-link/" .. code)
end

function export.link(data, options)
	if not options then
		options = {}
	end
	
	if not data.transliteration then
		data.transliteration = m_ryu.kana_to_romaji(data.kana, { hist = options.hist })
	elseif options.hist then
		error('If the transliteration has already been provided, the "' .. hist .. '" option does nothing.')
	end
	
	if not data.kana then
		data.kana = data.lemma
	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
		--[[Special:WhatLinksHere/Template:tracking/ja-link/caps]]
		track("caps")
		
		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, "ryu", "term")
	end
	
	if data.lemma and data.kana and data.lemma ~= data.kana then
		data.ruby = m_ryu.add_ruby_backend(data.lemma, data.kana)
	else
		--[[Special:WhatLinksHere/Template:tracking/ja-link/no ruby]]
		track("no ruby")
	end
	data.lemma = m_ryu.remove_ruby_markup(data.lemma)
	
	data.gloss = if_not_empty(data.gloss)
	
	data.pos = if_not_empty(data.pos)
	
	if data.linkto == "" or data.linkto == "-" then
		-- if $data.linkto is explicitly empty, make both $data.linkto and $data.lemma nil
		-- $data.linkto can be "" because of "allow_empty=true"; without it, "|linkto=<nothing>" would be ignored
		-- $data.lemma is the fallback if $data.linkto is falsy, so blank $data.lemma too
		if if_not_empty(data.linkto) then
			--[[Special:WhatLinksHere/Template:tracking/ja-link/ignored link page]]
			track("ignored link page")
		end
		if if_not_empty(data.lemma) then
			--[[Special:WhatLinksHere/Template:tracking/ja-link/ignored lemma]]
			track("ignored lemma")
		end
		data.linkto = nil
		data.lemma = nil
		options.noLink = true
		
		--[[Special:WhatLinksHere/Template:tracking/ja-link/disabled link]]
		track("disabled link")
	end

	if data.ruby and data.ruby:find('[[', 1, true) then -- for if $lemma has manual wikilinks
		--[[Special:WhatLinksHere/Template:tracking/ja-link/manual wikilink]]
		track("manual wikilink")
		
		return m_links.full_link(
			{
				lang = lang,
				sc = sc,
				term = nil,
				alt = data.ruby,
				tr = data.transliteration,
				gloss = data.gloss,
				lit = data.lit,
				pos = data.pos,
			},
			nil, -- face
			true -- allowSelfLink
		)
	else
		return m_links.full_link(
			{
				lang = lang,
				sc = sc,
				term = not options.noLink and (data.linkto or data.lemma),
				alt = data.ruby or data.lemma or data.kana,
				tr = data.transliteration,
				gloss = data.gloss,
				lit = data.lit,
				pos = data.pos,
			},
			nil, -- face
			true -- allowSelfLink
		)
	end
	
end

function export.show(frame)
	local params = {
		[1] = { required = true },
		[2] = {},
		[3] = {},
		['gloss'] = { alias_of = 3 },
		['lit'] = {},
		['pos'] = {},
		['linkto'] = { allow_empty = true },
		['caps'] = { type = "boolean" },
		['rom'] = {},
		['hist'] = { type = "boolean" },
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local kana = args[2]
	if not kana then
		if args[1] and is_script(args[1], { "Hira", "Kana" }) then
			kana = args[1]
		else
			error("Either the first parameter or the second parameter should be Okinawan text written in kana only.")
		end
	end

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

return export