モジュール:jawikt

出典: フリー多機能辞典『ウィクショナリー日本語版(Wiktionary)』
ナビゲーションに移動 検索に移動

このモジュールについての説明文ページを モジュール:jawikt/doc に作成できます

-- Settings
local HOUR_INTERVALS = 3
local NUMBER_OF_WANTEDPAGES_SEGMENTS = 2	-- NOTE: excepting the 'basic' page
-- 

local function requireAny(...)
	for i, fullName in ipairs({...}) do
		local succeeded, instance = pcall(require, fullName)
		if succeeded then
			return instance
		end
	end
	error(table.concat({...}, ", ") .. " not found.")
end

local export = {}
local util = requireAny("Module:Utility", "Module:サンドボックス/MysteryPedia/Utility")

function export.order(frame)
	local orderNumber = frame.args[1] or error("Arguments need at least one.")
	local hours = math.floor(os.time() / 3600)
	local timeslot = math.floor(hours / HOUR_INTERVALS)
	local index = timeslot % NUMBER_OF_WANTEDPAGES_SEGMENTS + 1
	local restricted = (timeslot % 2 == 0)
	
	local moduleName = util.getModuleName()
	local basic = mw.loadData(moduleName .. "/data/wanted pages/basic")
	local array = mw.loadData(moduleName .. "/data/wanted pages/" .. index)
	-- NOTE: objects generated by mw.loadData do not support the # operator.
	local max1 = util.size(basic)
	local max2 = util.size(array)
	
	mw.logObject({ hours = hours, timeslot = timeslot, index = index, ["max basic"] = max1, ["max page" .. index] = max2 })
	
	-- This function must return a random word, which is the same value until the time-slot has changed.
	for i = 1, math.huge, 1 do
		local n = math.floor(timeslot * i * math.exp(orderNumber) % (max1 * 2 + max2 + 1))
		local word = basic[n] or basic[n - max1] or array[n - (max1 * 2)]
		if not word then
			break
		end
		-- PENDING:
		-- In this time-slot, hiragana and kanji are highly recommended.
--[[
		if restricted then
			-- I know this is a rude way. I don't want exactness this time.
			if not mw.ustring.match(word, "^[一-龠ぁ-ゞ]+$") then
				word = nil
			end
		end
--]]
		if word then
			local succeeded, t = pcall(mw.title.new, word)
			if succeeded and not t.exists then
				return "[[:" .. word .. "]]"
			end
		end
	end
	return '<span style="color: gray;">(nothing)</span>'
end

function export.show_codepoint(frame)
	local d = mw.ustring.codepoint(frame.args[1])
	
	return frame:expandTemplate({
		title = "unicode code",
		args = {
			["16進"] = mw.ustring.format("%X", d),
			["10進"] = d,
		}})
end

local function xxchar(lang, frame)
	local parameters = frame.args
	local sequence
	-- NOTE: frame.args does not support the # operator.
	if parameters[1] then
		if not parameters[2] and mw.ustring.len(parameters[1]) > 1 then -- maybe {{PAGENAME}} passed
			sequence = mw.text.split(parameters[1], '')
		else -- conventional use
			sequence = parameters
		end
	else
		error("Arguments need at least one.")
		return ''
	end
	
	local validator = requireAny("Module:User:CodeCat/isValidPageName", "Module:IsValidPageName")
	local buffer = {"{{lang|" .. lang .. "|" .. "'''"}
	for i, c in ipairs(sequence) do
		if i > 1 then
			table.insert(buffer, ' ')
		end
		if validator.isValidPageName(frame:newChild({ args = {c} })) ~= '' then
			table.insert(buffer, "[[:" .. c .. "#{{" .. lang .. "}}|")
			local label = parameters['l' .. i]
			if label then
				table.insert(buffer, label)
			else
				table.insert(buffer, c)
			end
			table.insert(buffer, "]]")
		else
			table.insert(buffer, c)
		end
	end
	table.insert(buffer, "'''" .. "}}")
	return frame:preprocess(table.concat(buffer))
end

-- If you called {{#invoke:}} - the Frame object has arguments passed.
-- You can pass arguments via a template. In this case the parent Frame object has arguments passed. See [[wikt:en:Template:categorize]].
local function getInvoker(frame)
	if frame.args[1] and not frame.args['auto'] then
		return frame
	else
		return frame:getParent()
	end
end

function export.xxchars(frame)
	local lang = frame.args[1] or error("Arguments need at least one.")
	local title = frame.args[2] or mw.title.getCurrentTitle().text
	
	return xxchar(lang, frame:newChild({ args = {title} }))
end

-- @deprecated
function export.jachar(frame)
	return xxchar("ja", getInvoker(frame))
end

function export.auto_categorize(frame)
	local sortKey = frame.args[1]
	local title = mw.title.getCurrentTitle().fullText
	local data = mw.loadData(util.getModuleName() .. "/data/categories")
	
	return util.categorizeFromData(data, title, sortKey)
end

function export.kanji_variants(frame)
	local a = frame.args
	local buffer = {"* [[異体字]] : "}
	for k, v in pairs(frame:getParent().args) do
		if #buffer > 1 then
			table.insert(buffer, a["delimiter"])
		end
		if type(k) == "number" then
			table.insert(buffer, mw.ustring.format(a["glyph"], "[[" .. v .. "]]"))
		else
			table.insert(buffer, mw.ustring.format(a["glyph"], "[[" .. k .. "]]"))
			local s = v
			if v ~= "異体字" then
				s = "[[" .. v .. "]]"
			end
			table.insert(buffer, mw.ustring.format(a["classification"], s))
		end
	end
	return frame:preprocess(table.concat(buffer))
end

function export.defaultsort(frame)	-- {{defsort}} or something
	error("Not Implemented")
end

--[[
local console = util.import(util.getModuleName("Console"))

do
	if console then
		console.attach(export)
		console.define({
			['data'] =
				function(index)
					return mw.loadData(util.getModuleName() .. "/data/wanted pages/" .. (index or "basic"))
				end,
			})
	end
end
--]]

return export