Module:Navbar
来自滚动的天空Wiki
local p = {}
local hcreate = mw.html.create
local pageName = require('module:page name').pageName
function p.noExist(span,fstyle,tText,bra,tag,mini)
local tText = tText
if tText:sub(1,1) == ":" then tText = tText:match(":(.+)") end
local found = tostring(mw.html.create()
:wikitext('[[' .. tText .. '|')
:tag(tag)
:attr('title', '页面不存在,你可以创建它。')
:cssText(fstyle)
:wikitext(mini and '建' or '创建')
:done()
:wikitext(']]')
)
return tostring(
span:wikitext((bra and "[" or "")..found..(bra and "]" or ""))
);
end
function p._navbar(args,frame)
local frame = frame or mw.getCurrentFrame()
if not args.plain then
args.mini = 1
end
if args.fontcolor then
args.fontstyle = 'color:' .. args.fontcolor .. ';'
end
-- args.style = 'float:left; text-align:left; width:6em;'
local titleText = args[1] or args.title or (':' .. frame:getTitle())
local title = mw.title.new(mw.text.trim(titleText), 'Template') or {void=true}
local pageName = not title.void and pageName(title) or '不存在页面'
local talkpage = title.talkPageTitle and title.talkPageTitle.fullText
local span = hcreate():tag('span')
span
:addClass('hlist')
:addClass('navbar')
:cssText(args.style)
if args.mini then span:addClass('navbar-mini') end
local ul = not args.brackets and span:tag('ul')
local tag = args.mini and 'abbr' or 'span'
if pageName == '不存在页面' then return p.noExist(span,fontstyle,titleText,args.brackets,tag,args.mini) end
local view = tostring(hcreate()
:wikitext('[[' .. title.fullText .. '|')
:tag(tag)
:attr('title', '-{zh-hans:查看;zh-hant:檢視;}-该'..pageName)
:cssText(args.fontstyle)
:wikitext(args.mini and '-{zh-hans:查;zh-hant:閱;}-' or '-{zh-hans:查看;zh-hant:檢視;}-')
:done()
:wikitext(']]')
)
local talk = tostring(hcreate()
:wikitext('[[' .. talkpage .. '|')
:tag(tag)
:attr('title', '讨论该'..pageName)
:cssText(args.fontstyle)
:wikitext(args.mini and '论' or '讨论')
:done()
:wikitext(']]')
)
local edit = tostring(hcreate()
:wikitext('[[Special:EditPage/')
:wikitext(title.fullText)
:wikitext '|'
:tag(tag)
:attr('title', '编辑此' .. pageName)
:cssText(args.fontstyle)
:wikitext(args.mini and '编' or '编辑')
:done()
:wikitext ']]')
local hist = tostring(hcreate()
:wikitext('[[Special:PageHistory/')
:wikitext(title.fullText)
:wikitext '|'
:tag(tag)
:attr('title', '查看此' .. pageName .. '的历史')
:cssText(args.fontstyle)
:wikitext(args.mini and '史' or '历史')
:done()
:wikitext ']]'
)
local function brac(t,c)
return (
tostring(
hcreate("span")
:addClass(c)
:wikitext("["..t.."]")
)
)
end
local function li(t,c)
return (
tostring(
hcreate("li")
:addClass(c)
:wikitext(t)
)
)
end
if ul then
ul:wikitext(
li(view,"nv-view")..
li(talk,"nv-talk")..
(args.noedit and "" or li(edit,"nv-edit"))..
(args.hist and li(hist) or '')
);
else
span:wikitext(
brac(view,"nv-view")..
brac(talk,"nv-talk")..
(args.noedit and "" or brac(edit,"nv-edit"))..
(args.hist and brac(hist) or '')
);
end
return tostring(span:done())
end
function p.navbar(frame)
local rawargs = frame.args
local args = {}
local booleanArgs = {
noedit=true,
brackets=true,
mini=true
}
for k,v in pairs(rawargs) do
if booleanArgs[k] then
args[k]=v=="1"
else
args[k]=v
end
end
return p._navbar(args,frame)
end
return p