Diff: Module:Asbox stubtree
Comparing revision #1 (2013-10-18 12:29:46) with revision #2 (2023-02-06 04:51:00).
| Old | New |
|---|---|
local i = {} |
local i = {} |
function i.exists(pagename) |
function i.exists(pagename) |
local t = mw.title.new(pagename, "Template") |
local t = mw.title.new(pagename, "Template") |
return t.exists |
return t.exists |
end |
end |
function i.pcase(word) |
function i.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 i._subtree(pagename) |
function i._subtree(pagename) |
local finalresult |
local finalresult |
local out = {"",pagename} |
local out = {"",pagename} |
local tt |
local tt |
local temppage |
local temppage |
local temppageexists |
local temppageexists |
local r = 0 |
local r = 0 |
local t = {} |
local t = {} |
local removeditem1 = "" |
local removeditem1 = "" |
local removeditem2 = "" |
local removeditem2 = "" |
-- split items on dash into table |
-- split items on dash into table |
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 |
if tonumber(mw.ustring.sub(token,1,1)) == nil then |
if tonumber(mw.ustring.sub(token,1,1)) == nil then |
table.insert(t,token) |
table.insert(t,token) |
else |
else |
r = 1 |
r = 1 |
end |
end |
end |
end |
table.remove(t, #t) |
table.remove(t, #t) |
while (#t > 1) do |
while (#t > 1) do |
if r == 1 then |
if r == 1 then |
r = 0 |
r = 0 |
else |
else |
-- Remove 1st item from list |
-- Remove 1st item from list |
removeditem1 = t[1] |
removeditem1 = t[1] |
table.remove(t, 1) |
table.remove(t, 1) |
end |
end |
temppage = table.concat(t, "-") .. "-stub" |
temppage = table.concat(t, "-") .. "-stub" |
temppageexists = i.exists(temppage) |
temppageexists = i.exists(temppage) |
if temppageexists == true then |
if temppageexists == true then |
table.insert(out,"[[Template:" .. temppage .. "|" .. i.pcase(temppage) .. "]]") |
table.insert(out,"[[Template:" .. temppage .. "|" .. i.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 |
removeditem2 = t[#t] |
removeditem2 = t[#t] |
table.remove(t, #t) |
table.remove(t, #t) |
temppage = removeditem1 .. "-" .. table.concat(t, "-") .. "-stub" |
temppage = removeditem1 .. "-" .. table.concat(t, "-") .. "-stub" |
if #t == 0 then |
if #t == 0 then |
temppage = removeditem1 .. "-stub" |
temppage = removeditem1 .. "-stub" |
end |
end |
temppageexists = i.exists(temppage) |
temppageexists = i.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 .. "|" .. i.pcase(temppage) .. "]]") |
table.insert(out,"[[Template:" .. temppage .. "|" .. i.pcase(temppage) .. "]]") |
else |
else |
-- if exists then add last item back to list |
-- if exists then add last item back to list |
table.insert(t,removeditem2) |
table.insert(t,removeditem2) |
end |
end |
end |
end |
end |
end |
finalresult = '<div style="float:right; border-style:dotted; border-width:2px; padding:5px; margin:5px;">' |
finalresult = '<div style="float:right; border-style:dotted; border-width:2px; padding:5px; margin:5px;">' |
finalresult = finalresult .. '<span title="This shows the hierarchy of the stub template in relation to other templates." style="font-size:125%; font-weight:bold;">Stub hierarchy</span>' |
finalresult = finalresult .. '<span title="This shows the hierarchy of the stub template in relation to other templates." style="font-size:125%; font-weight:bold;">Stub hierarchy</span>' |
finalresult = finalresult .. table.concat(out, "\n* ") |
finalresult = finalresult .. table.concat(out, "\n* ") |
finalresult = finalresult .. '\n* [[Template:Stub|Stub]]' |
finalresult = finalresult .. '\n* [[Template:Stub|Stub]]' |
finalresult = finalresult .. '\n</div>' |
finalresult = finalresult .. '\n</div>' |
return finalresult |
return finalresult |
end |
end |
function i.subtree(frame) |
function i.subtree(frame) |
return i._subtree(frame.args["pagename"]) |
return i._subtree(frame.args["pagename"]) |
end |
end |
return i |
return i |