Modulus:Path
Appearance
De usu
[fontem recensere]{{#invoke:Path|nomen functionis}}
De functionibus
[fontem recensere]Moduli functiones hic numerantur.
Note: In all functions the |1= argument can indifferently be a relative (i.e. beginning with ./ or ../ or /) or an absolute path.
abs
[fontem recensere]The abs function parses the |1= argument and returns it as an absolute path.
Examples
[fontem recensere]{{#invoke:path|abs|./example}}- ↳ Modulus:Path/example
{{#invoke:path|abs|.}}- ↳ Modulus:Path
{{#invoke:path|abs|Wikipedia:Lua}}- ↳ Vicipaedia:Lua
{{#invoke:path|abs}}- ↳ Modulus:Path
rel
[fontem recensere]The rel function parses the |1= argument and returns it as a relative path.
Examples
[fontem recensere]{{#invoke:path|rel|Modulus:Path/example}}- ↳ ./example
{{#invoke:path|rel|Modulus:Path}}- ↳ .
{{#invoke:path|rel|Wikipedia:Lua}}- ↳ Vicipaedia:Lua
{{#invoke:path|rel}}- ↳ .
[[Modulus:Path/example|{{#invoke:path|rel|Modulus:Path/example}}]]- ↳ ./example
- See also {{Nexus Vicipaedianus relativus}}
- ↳ ./example
sub
[fontem recensere]The sub function parses the |1= argument and returns it as a partial path with only the subpages shown.
Examples
[fontem recensere]{{#invoke:path|sub|Modulus:Path/example}}- ↳ example
{{#invoke:path|sub|Modulus:Path}}- ↳
{{#invoke:path|sub|Wikipedia:Lua}}- ↳ Vicipaedia:Lua
{{#invoke:path|sub}}- ↳
[[Modulus:Path/example|{{#invoke:path|sub|Modulus:Path/example}}]]- ↳ example
- See also: {{Nexus Vicipaedianus tacite relativus}}
Ulteriora si cupis
[fontem recensere]Documentatio pristina transclusa est ex Modulus:Path/doc. (recense | historiam inspice)
Quaesumus auctores experturos esse in harenarii (crea | effinge) ac periclitationum (crea) subpaginis huius moduli.
Vide etiam subpaginas huius moduli.
require[[strict]]
local function split_path (path)
local len, parts = 0, {}
for token in path:gmatch("[^/]+") do
len = len + 1
parts[len] = token
end
return parts, len
end
local function get_abs_path (current, page)
local len_o, tbl_o
local tbl_i = split_path(page)
local tmp = tbl_i[1]
if tmp == '.' or tmp == '..' then
tbl_o, len_o = split_path(current)
elseif tmp ~= '' then
tmp = mw.title.new(tmp)
if tmp == nil then error('Module:Path: invalid title', 0) end
tbl_i[1], tbl_o, len_o = tmp.fullText, {}, 0
end
for key, val in ipairs(tbl_i) do
if val == '..' then
if len_o < 1 then
error('Module:Path: invalid path', 0) end
tbl_o[len_o], len_o = nil, len_o - 1
elseif val ~= '.' then
len_o = len_o + 1
tbl_o[len_o] = val
end
end
if len_o < 1 then len_o, tbl_o[1] = 1, '' end
return tbl_o, len_o
end
local function get_rel_path (current, page, add_prefixes)
local minlen
local tbl_i, len_i = get_abs_path(current, page)
local tbl_c, len_c = split_path(current)
local new_at, len_o, tbl_o = 0, 0, {}
if len_c < len_i then minlen = len_c else minlen = len_i end
for idx = 1, minlen do
if tbl_c[idx] ~= tbl_i[idx] then
new_at = idx
break
end
end
if new_at == 1 then return table.concat(tbl_i, '/') end
if add_prefixes then
if new_at == 0 then new_at, tbl_o[1] = minlen + 1, '.' end
for idx = new_at, len_c do
len_o = len_o + 1
tbl_o[len_o] = '..'
end
if len_o < 1 then len_o = 1 end
elseif new_at == 0 then new_at = minlen + 1 end
for idx = new_at, len_i do
len_o = len_o + 1
tbl_o[len_o] = tbl_i[idx]
end
return table.concat(tbl_o, '/')
end
local iface = {}
iface.abs = function (frame)
local page = frame.args[1]
if page ~= nil then page = page:match'^%s*(.*%S)' end
if page == nil then return mw.title.getCurrentTitle().prefixedText end
local retval = table.concat(get_abs_path(
mw.title.getCurrentTitle().prefixedText,
page:gsub('^%s*/%s*', './', 1)
), '/')
return retval
end
iface.rel = function (frame)
local page = frame.args[1]
if page ~= nil then page = page:match'^%s*(.*%S)' end
if page == nil then return '.' end
return get_rel_path(mw.title.getCurrentTitle().prefixedText,
page:gsub('^%s*/%s*', './', 1), true)
end
iface.sub = function (frame)
local page = frame.args[1]
if page ~= nil then page = page:match'^%s*(.*%S)' end
if page == nil then return '' end
return get_rel_path(mw.title.getCurrentTitle().prefixedText,
page:gsub('^%s*/%s*', './', 1), false)
end
return iface