Module:Path
Appearance
From Wikipedia, the free encyclopedia
This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected.
This module offers utilities to convert paths into different formats.
Functions
[edit ]information Note: In all functions the |1= argument can indifferently be a relative (i.e. beginning with ./ or ../ or /) or an absolute path.
abs
[edit ]The abs function parses the |1= argument and returns it as an absolute path.
Examples
[edit ]{{#invoke:path|abs|./example}}- ↳ Module:Path/example
{{#invoke:path|abs|.}}- ↳ Module:Path
{{#invoke:path|abs|Wikipedia:Lua}}- ↳ Wikipedia:Lua
{{#invoke:path|abs}}- ↳ Module:Path
rel
[edit ]The rel function parses the |1= argument and returns it as a relative path.
Examples
[edit ]{{#invoke:path|rel|Module:Path/example}}- ↳ ./example
{{#invoke:path|rel|Module:Path}}- ↳ .
{{#invoke:path|rel|Wikipedia:Lua}}- ↳ Wikipedia:Lua
{{#invoke:path|rel}}- ↳ .
[[Module:Path/example|{{#invoke:path|rel|Module:Path/example}}]]- ↳ ./example
- See also {{Relative link }}
- ↳ ./example
sub
[edit ]The sub function parses the |1= argument and returns it as a partial path with only the subpages shown.
Examples
[edit ]{{#invoke:path|sub|Module:Path/example}}- ↳ example
{{#invoke:path|sub|Module:Path}}- ↳
{{#invoke:path|sub|Wikipedia:Lua}}- ↳ Wikipedia:Lua
{{#invoke:path|sub}}- ↳
[[Module:Path/example|{{#invoke:path|sub|Module:Path/example}}]]- ↳ example
- See also {{Relative link implicit }}
- ↳ example
See also
[edit ]The above documentation is transcluded from Module:Path/doc. (edit | history)
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.
require[[strict]] localfunctionsplit_path(path) localparts={} locallen=0 fortokeninpath:gmatch("[^/]+")do len=len+1 parts[len]=token end returnparts,len end localfunctionget_abs_path(current,page) localtbl_i,len_i=split_path(page) localtbl_o locallen_o iftbl_i[1]=='.'ortbl_i[1]=='..'then tbl_o,len_o=split_path(current) else tbl_i[1]=tbl_i[1]:gsub('^([^:]?)([^:]*)(:?)(.?)', function(s1,s2,s3,s4) ifs3==''thenreturns1:upper()..s2end returns1:upper()..s2..s3..s4:upper() end, 1 ) tbl_o={} len_o=0 end forkey,valinipairs(tbl_i)do ifval=='..'then iflen_o<1then error('Module:Path: invalid path',0)end tbl_o[len_o]=nil len_o=len_o-1 elseifval~='.'then len_o=len_o+1 tbl_o[len_o]=val end end iflen_o<1then tbl_o[1]='' len_o=1 end returntbl_o,len_o end localfunctionget_rel_path(current,page,add_prefixes) localtbl_i,len_i=get_abs_path(current,page) localtbl_c,len_c=split_path(current) localtbl_o={} locallen_o=0 localminlen localnew_at=0 iflen_c<len_ithenminlen=len_celseminlen=len_iend foridx=1,minlendo iftbl_c[idx]~=tbl_i[idx]then new_at=idx break end end ifnew_at==1thenreturntable.concat(tbl_i,'/')end ifadd_prefixesthen ifnew_at==0then tbl_o[1]='.' new_at=minlen+1 end foridx=new_at,len_cdo len_o=len_o+1 tbl_o[len_o]='..' end iflen_o<1thenlen_o=1end elseifnew_at==0thennew_at=minlen+1end foridx=new_at,len_ido len_o=len_o+1 tbl_o[len_o]=tbl_i[idx] end returntable.concat(tbl_o,'/') end localiface={} iface.abs=function(frame) localpage=frame.args[1] ifpage~=nilthenpage=page:match'^%s*(.*%S)'end ifpage==nilthenreturnmw.title.getCurrentTitle().prefixedTextend localretval,_=table.concat(get_abs_path( mw.title.getCurrentTitle().prefixedText, page:gsub('^%s*/%s*','./',1) ),'/') returnretval end iface.rel=function(frame) localpage=frame.args[1] ifpage~=nilthenpage=page:match'^%s*(.*%S)'end ifpage==nilthenreturn'.'end returnget_rel_path(mw.title.getCurrentTitle().prefixedText, page:gsub('^%s*/%s*','./',1),true) end iface.sub=function(frame) localpage=frame.args[1] ifpage~=nilthenpage=page:match'^%s*(.*%S)'end ifpage==nilthenreturn''end returnget_rel_path(mw.title.getCurrentTitle().prefixedText, page:gsub('^%s*/%s*','./',1),false) end returniface