Module:Asbox stubtree and Module:Asbox stubtree/sandbox: Difference between pages
(Difference between pages)
imported>WOSlinker m Changed protection level of Module:Asbox stubtree: allow template editors to modify ([Edit=Protected template] (indefinite) [Move=Protected template] (indefinite)) |
imported>Izno save this while I'm thinking about it |
||
| Line 1: | Line 1: | ||
local | local p = {} | ||
function | function p.exists(pagename) | ||
return mw.title.new(pagename, "Template").exists | |||
end | end | ||
function | function p.pcase(word) | ||
return mw.ustring.upper(mw.ustring.sub(word,1,1)) .. mw.ustring.sub(word,2) | return mw.ustring.upper(mw.ustring.sub(word,1,1)) .. mw.ustring.sub(word,2) | ||
end | end | ||
function | function p._subtree(pagename) | ||
-- split items on dash into table | |||
local r = 0 | local r = 0 | ||
local t = {} | local t = {} | ||
for token in mw.ustring.gmatch(pagename, "[^-]+") do | for token in mw.ustring.gmatch(pagename, "[^-]+") do | ||
-- don't add numbered items to list | -- don't add numbered items to list | ||
| Line 32: | Line 23: | ||
table.remove(t, #t) | table.remove(t, #t) | ||
local out = {"",pagename} | |||
local temppage | |||
local temppageexists | |||
local removeditem1 = "" | |||
local removeditem2 = "" | |||
while (#t > 1) do | while (#t > 1) do | ||
if r == 1 then | if r == 1 then | ||
| Line 42: | Line 38: | ||
temppage = table.concat(t, "-") .. "-stub" | temppage = table.concat(t, "-") .. "-stub" | ||
temppageexists = | temppageexists = p.exists(temppage) | ||
if temppageexists == true then | if temppageexists == true then | ||
table.insert(out,"[[Template:" .. temppage .. "|" .. | table.insert(out,"[[Template:" .. temppage .. "|" .. p.pcase(temppage) .. "]]") | ||
else | else | ||
-- If template with first item does not exist, try removing last item | -- If template with first item does not exist, try removing last item | ||
| Line 53: | Line 49: | ||
temppage = removeditem1 .. "-stub" | temppage = removeditem1 .. "-stub" | ||
end | end | ||
temppageexists = | temppageexists = p.exists(temppage) | ||
if temppageexists == true then | if temppageexists == true then | ||
-- if exists then add first item back to list | -- if exists then add first item back to list | ||
table.insert(t,1,removeditem1) | table.insert(t,1,removeditem1) | ||
table.insert(out,"[[Template:" .. temppage .. "|" .. | table.insert(out,"[[Template:" .. temppage .. "|" .. p.pcase(temppage) .. "]]") | ||
else | else | ||
-- if exists then add last item back to list | -- if exists then add last item back to list | ||
| Line 64: | Line 60: | ||
end | end | ||
end | end | ||
finalresult = ' | local list = mw.html.create('ul') | ||
for _, item in ipairs(out) do | |||
list:tag('li'):wikitext(item):done() | |||
end | |||
list:tag('li'):wikitext('[[Template:Stub|Stub]]'):allDone() | |||
return finalresult | |||
local finalresult = mw.html.create('div') | |||
finalresult:cssText('float:right; border-style:dotted; border-width:2px; padding:5px; margin:5px;') | |||
:tag('span') | |||
:attr('title', 'This shows the hierarchy of the stub template in relation to other templates.') | |||
:cssText('font-size:125%; font-weight:bold;') | |||
:wikitext('Stub hierarchy') | |||
:done() | |||
:addNode(list) | |||
:done() | |||
return tostring(finalresult) | |||
end | end | ||
function | function p.subtree(frame) | ||
return | return p._subtree(frame.args["pagename"]) | ||
end | end | ||
return | return p | ||