Module:Navbar

来自滚动的天空Wiki
文档图示 模块文档[查看] [编辑] [查看历史] [清除缓存]

本模块是{{navbar}}的Lua语言版本,并用于模块:Navbox模块:Infobox

上述文档内容嵌入自Module:Navbar/doc编辑 | 历史
编者可以在本模块的沙盒创建 | 镜像和测试样例创建页面进行实验。
请将模块自身所属的分类添加在文档中。本模块的子页面
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 "&#91" or "")..found..(bra and "&#93" 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