Модуль:Песочница/Pok: различия между версиями

Нет описания правки
Нет описания правки
Строка 3: Строка 3:
local CURRENT_PROJECT
local CURRENT_PROJECT


local function get_page_var()
local function get_page_var(frame)
local frame = mw.getCurrentFrame()
if mw.ext and mw.ext.VariablesLua and type(mw.ext.VariablesLua.var) == "function" then
if mw.ext and mw.ext.VariablesLua and type(mw.ext.VariablesLua.var) == "function" then
local v = mw.ext.VariablesLua.var("GetFieldPath")
local v = mw.ext.VariablesLua.var("GetFieldPath")
if v and v ~= "" then return v end
if v and v ~= "" then return v end
end
end
local ok, v = pcall(function() return frame:callParserFunction{ name = "var", args = { "GetFieldPath" } } end)
if frame then
if ok and v and v ~= "" then return v end
local ok, v = pcall(function() return frame:callParserFunction{ name = "var", args = { "GetFieldPath" } } end)
if ok and v and v ~= "" then return v end
end
return nil
return nil
end
end


local function find_ancestor_arg(name)
local function read_ancestor_arg()
local f = mw.getCurrentFrame()
local f = mw.getCurrentFrame()
while f do
while f do
local v = f:getArgument(name)
local v = f:getArgument("Название") or f:getArgument("project") or f:getArgument("path")
if v and v ~= "" then return v end
if v and v ~= "" then return v end
f = f:getParent()
f = f:getParent()
Строка 24: Строка 25:
end
end


local function resolve_project(pagePath)
local function resolve_project(frame, pagePath)
if pagePath and pagePath ~= "" then return pagePath end
if pagePath and pagePath ~= "" then return pagePath end
if CURRENT_PROJECT and CURRENT_PROJECT ~= "" then return CURRENT_PROJECT end
if CURRENT_PROJECT and CURRENT_PROJECT ~= "" then return CURRENT_PROJECT end
local v = get_page_var()
local v = get_page_var(frame)
if v and v ~= "" then CURRENT_PROJECT = v; return v end
if v and v ~= "" then CURRENT_PROJECT = v; return v end
local candidate = find_ancestor_arg("Название") or find_ancestor_arg("project") or find_ancestor_arg("path")
local ancestor = read_ancestor_arg()
if candidate and candidate ~= "" then CURRENT_PROJECT = candidate; return candidate end
if ancestor and ancestor ~= "" then CURRENT_PROJECT = ancestor; return ancestor end
return nil
return nil
end
end


local function get_module_name(pagePath)
function p.set_path(frame)
local project = resolve_project(pagePath) or "default"
local arg
if frame and frame.args and frame.args[1] then
arg = frame.args[1]
end
if (not arg or arg == "") and frame then
arg = frame:getArgument("1") or frame:getArgument("path") or frame:getArgument("GetFieldPath")
end
if arg and arg ~= "" then
CURRENT_PROJECT = arg
return ""
end
return ""
end
 
function p._debug_get_cached(frame)
local v = CURRENT_PROJECT or get_page_var(frame) or read_ancestor_arg() or "(nil)"
return tostring(v)
end
 
function p.get_module_name(frame, pagePath)
local fp, pp = frame, pagePath
if type(frame) == "string" and (not pagePath) then
pp = frame
fp = nil
end
local project = resolve_project(fp, pp) or "default"
return "Module:" .. BASE_USER .. project .. "/data"
return "Module:" .. BASE_USER .. project .. "/data"
end
end
p.get_module_name = get_module_name
p._debug_get_cached = function() return CURRENT_PROJECT, get_page_var() end


local function load_cached_data(moduleName)
local function load_cached_data(moduleName)