「モジュール:jawikt」の版間の差分

削除された内容 追加された内容
編集の要約なし
モジュール:Utilityに一部を分離
 
4行目:
--
 
local function importrequireAny(name...)
for i, fullName in ipairs({...}) do
local succeeded, instance = pcall(require, "Module:" .. name)
if local succeeded, theninstance = pcall(require, fullName)
if succeeded then
return instance
return instance
else -- Ignores the exception message.
end
mw.log("Could not load the module " .. name .. ".")
return nil
end
error(table.concat({...}, ", ") .. " not found.")
end
 
local MODULE_CONSOLE = mw.getCurrentFrame():preprocess('{{BASEPAGENAME}}') .. "/Console"
 
local export = {}
local util = requireAny("Module:Utility", "Module:サンドボックス/MysteryPedia/Utility")
local console = import(MODULE_CONSOLE)
 
do
if console then
console.attach(export)
end
end
 
local function size(data)
if data[1] == nil then -- 何だよnillって...
return 0
end
local low = 1
local high = 0xFFFFFFFF
while low <= high do
n = low + (high - low) / 2
if data[n] ~= nil and data[n + 1] == nil then
return n
elseif data[n] ~= nil then
low = n + 1
else
high = n - 1
end
end
end
 
-- An ad hoc function. I'm too lazy to find out whether there's a standard way or not.
local function getCurrentModuleName()
return mw.ustring.match(debug.traceback(), "%c+([^%c]+):%d+: in function")
end
 
-- PENDING:
local function serialize(object)
local s = mw.dumpObject(object)
local root = mw.html.create()
root:node("<!--" .. mw.text.nowiki(s) .. "-->") -- TODO: This will totally be erased. Does not work at all.
return tostring(root)
end
 
function export.order(frame)
63 ⟶ 24行目:
local restricted = (timeslot % 2 == 0)
local moduleName = getCurrentModuleNameutil.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)
debugInfo = serializemw.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.
92 ⟶ 53行目:
local succeeded, t = pcall(mw.title.new, word)
if succeeded and not t.exists then
return "[[:" .. word .. "]]" .. "<!-- #" .. n .. " -->" .. debugInfo
end
end
end
return '<span style="color: gray;">(nothing)</span>' .. debugInfo
end
 
function export.codepointshow_codepoint(frame)
local ad = mw.ustring.codepoint(frame.args[1])
return mw.ustring.codepoint(a[1] or error("Arguments need at least one."), a[2] or 1, a[3] or 1)
return frame:expandTemplate({
end
title = "unicode code",
 
args = {
local function requireAny(...)
["16進"] = mw.ustring.format("%X", d),
for i, name in ipairs({...}) do
["10進"] = d,
local instance = import(name)
}})
if instance then
return instance
end
end
error("Module " .. table.concat({...}, ", ") .. " not found.")
end
 
129 ⟶ 86行目:
end
local validator = requireAny("Module:User:CodeCat/isValidPageName", "Module:IsValidPageName")
local buffer = {"{{lang|" .. lang .. "|" .. "'''"}
for i, c in ipairs(sequence) do
177 ⟶ 134行目:
local sortKey = frame.args[1]
local title = mw.title.getCurrentTitle().fullText
local data = mw.loadData(getCurrentModuleNameutil.getModuleName() .. "/data/categories")
return util.categorizeFromData(data, title, sortKey)
local cats = {}
end
for catname, pages in pairs(data) do
 
for i, t in ipairs(pages) do
function export.kanji_variants(frame)
local id = table.concat({catname, title}, "|")
local a = frame.args
if title == t and not cats[id] then
local buffer = {"* [[異体字]] : "}
cats[id] = true
for k, v in pairs(frame:getParent().args) do
table.insert(cats, "[[Category:" .. catname .. (sortKey and "|" .. sortKey or "") .. "]]")
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))
if #cats > 0 then
return frame:preprocess(table.concat(cats))
else -- Disables the line break just in case.
return frame:preprocess('<nowiki />')
end
end
 
199 ⟶ 163行目:
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(getCurrentModuleNameutil.getModuleName() .. "/data/wanted pages/" .. (index or "basic"))
end,
})
['getCurrentModuleName'] = getCurrentModuleName,
['size'] = size })
end
end
--]]
 
return export