コンテンツにスキップ

モジュール:Asbox stubtree

提供:KANOTYPE WIKI

このモジュールについての説明文ページを モジュール:Asbox stubtree/doc に作成できます

 1 local i = {}
 2 
 3 function i.exists(pagename)
 4     local t = mw.title.new(pagename, "Template")
 5     return t.exists
 6 end
 7 
 8 function i.pcase(word)
 9    return mw.ustring.upper(mw.ustring.sub(word,1,1)) .. mw.ustring.sub(word,2)
10 end
11 
12 function i._subtree(pagename)
13     local finalresult
14     local out = {"",pagename}
15     local tt
16     local temppage
17     local temppageexists
18     local r = 0
19     local t = {}
20     local removeditem1 = ""
21     local removeditem2 = ""
22     
23     -- split items on dash into table
24     for token in mw.ustring.gmatch(pagename, "[^-]+") do
25         -- don't add numbered items to list
26         if tonumber(mw.ustring.sub(token,1,1)) == nil then
27             table.insert(t,token)
28         else
29             r = 1
30         end
31     end
32     table.remove(t, #t)
33 
34     while (#t > 1) do
35         if r == 1 then
36             r = 0
37         else
38             -- Remove 1st item from list
39             removeditem1 = t[1]
40             table.remove(t, 1)
41         end
42 
43         temppage = table.concat(t, "-") .. "-stub"
44         temppageexists = i.exists(temppage)
45         if temppageexists == true then
46             table.insert(out,"[[Template:" .. temppage .. "|" .. i.pcase(temppage) .. "]]")
47         else
48             -- If template with first item does not exist, try removing last item
49             removeditem2 = t[#t]
50             table.remove(t, #t)
51             temppage = removeditem1 .. "-" .. table.concat(t, "-") .. "-stub"
52             if #t == 0 then
53                 temppage = removeditem1 .. "-stub"
54             end
55             temppageexists = i.exists(temppage)
56             if temppageexists == true then
57                 -- if exists then add first item back to list
58                 table.insert(t,1,removeditem1)
59                 table.insert(out,"[[Template:" .. temppage .. "|" .. i.pcase(temppage) .. "]]")
60             else
61                 -- if exists then add last item back to list
62                 table.insert(t,removeditem2)
63             end
64         end
65     end
66 
67     finalresult = '<div style="float:right; border-style:dotted; border-width:2px; padding:5px; margin:5px;">'
68     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>'
69     finalresult = finalresult .. table.concat(out, "\n* ")
70     finalresult = finalresult .. '\n* [[Template:Stub|Stub]]'
71     finalresult = finalresult .. '\n</div>'
72     return finalresult
73 end
74 
75 function i.subtree(frame)
76     return i._subtree(frame.args["pagename"])
77 end
78 
79 return i