Modul:Header structure

Dokuméntasi antuk modul puniki prasida kakardi ring Modul:Header structure/doc

--[=[
This is a module to implement logic for header templates
]=]

require('strict')

local p = {} --p stands for package

local yesno = require('Module:Yesno')
local getArgs = require('Module:Arguments').getArgs

-- functions to process stylesheet

function p.get_stylesheet(template)
	return mw.getCurrentFrame():extensionTag('templatestyles', '', {src = template .. '/styles.css'})
end

function p.get_noexport_stylesheet(template)
	return tostring(mw.html.create('div'):addClass('ws-noexport'):wikitext(p.get_stylesheet(template)))
end

-- return true if any value in list is not nil or empty in args
-- (nil means not present; empty string is not nil)
local function has_any_arg(args, list)
	for k,v in pairs(list) do
		if args[v] ~= nil and args[v] ~= "" then
			return true
		end
	end
	return false
end

-- get sisters
local function get_plain_sister(args, sister_args)
	local ps_args = {}
	for k,v in pairs(sister_args) do
		if args[v] then
			ps_args[v] = args[v]
		end
	end
	
	return mw.getCurrentFrame():expandTemplate{title = "plain sister", args = ps_args}
end

--[=[
Construct the main block
]=]
local function construct_main_block(args)
	-- header and tracking categories
	local main_div = mw.html.create('div'):addClass('wst-header-mainblock header-mainblock ' .. (args.main_class or ''))
	if args.main_id then
		main_div:attr('id', args.main_id)
	end
	local cats = {}
	
	-- previous and next fields
	local prev_div = ''
	local next_div = ''
	if args.previous or args['next'] then
		prev_div = mw.html.create('div'):addClass('wst-header-back gen_header_backlink searchaux')
		if args.previous then
			prev_div:node(mw.html.create('div'):addClass('wst-header-back-arrow'):wikitext('←'))
			prev_div:node(mw.html.create('div')
				:attr('id', 'headerprevious')
				:addClass('wst-header-backlink wst-header-previous')
				:wikitext(args.previous)
			)
		else
			prev_div:wikitext('')
		end
		prev_div = tostring(prev_div)
		
		next_div = mw.html.create('div'):addClass('wst-header-forward gen_header_forelink searchaux')
		if args['next'] then
			next_div:node(mw.html.create('div')
				:attr('id', 'headernext')
				:addClass('wst-header-forwardlink wst-header-forward')
				:wikitext(args['next'])
			)
			next_div:node(mw.html.create('div'):addClass('wst-header-forward-arrow'):wikitext('→'))
		else
			next_div:wikitext('')
		end
		next_div = tostring(next_div)
	end
	
	-- central cell
	local center_div = mw.html.create('div'):addClass('wst-header-central-cell gen_header_central_cell')
	
	-- special case to manage exceptional formatting in disambiguation-like pages; not otherwise documented
	local pretitle = ''
	if args.pretitle then
		pretitle = tostring(mw.html.create('span'):addClass('wst-header-pretitle gen_header_pretitle'):wikitext(args.pretitle)) .. '<br/>'
	end
	
	local title = tostring(mw.html.create('span'):addClass('wst-header-title gen_header_title'):wikitext(args.main_title or ''))
	
	center_div:wikitext(pretitle ..title )
	center_div = tostring(center_div)
	
	-- assemble
	main_div:wikitext(prev_div .. center_div .. next_div)
	main_div = tostring(main_div)
	
	return main_div .. table.concat(cats)
end

--[=[
Construct the notes block
]=]
local function construct_notes_block(args)
	local main_div = mw.html.create('div'):addClass('wst-header-notes header_notes searchaux ' .. (args.notes_class or ''))
	if args.notes_id then
		main_div:attr('id', args.notes_id)
	end
	
	-- Left-floated content has to come first, or it may float below the sisters.
	local notes_left_content = ''
	if args.notes_left_content then
		notes_left_content = tostring(mw.html.create('div'):addClass('wst-header-left header_notes_left_content'):wikitext(args.notes_left_content))
	end
	
	local shortcut = ''
	if args.shortcut then
		shortcut = mw.getCurrentFrame():expandTemplate {
			title = 'shortcut',
			args = {args.shortcut}
		}
	end
	
	local notes_content = ''
	if args.notes then
		args.notes = mw.getCurrentFrame():preprocess(args.notes)
		-- linebreaks are so notes get paragraph formatting
		notes_content = tostring(mw.html.create('div'):addClass('wst-header-content header_notes_content'):wikitext('\n' .. args.notes .. '\n'))
	end
	
	local sister
	local wdid = mw.wikibase.getEntityIdForCurrentPage()
	local sister_args = {
		"disambiguation",
		"edition",
		"portal",
		"related_author",
		"wikipedia",
		"commons",
		"commonscat",
		"wikiquote",
		"wikinews",
		"wiktionary",
		"wikibooks",
		"wikidata",
		"wikivoyage",
		"wikiversity",
		"wikispecies",
		"meta",
		"wikidataswitch"
	}
	if wdid or has_any_arg(args, sister_args) then
		sister = get_plain_sister(args, sister_args)
	end
	
	if not sister and not args.notes and not args.notes_left_content then
		return ''
	end
	
	return tostring(main_div:wikitext(notes_left_content .. (sister or '') .. shortcut .. notes_content))
end

--[=[
Construct header
]=]

function p.construct_header(args)
	local headerContainer = mw.html.create('div'):attr('id', 'headerContainer')
	
	-- header_class from client modules, class from individual uses
	if args.header_class then
		headerContainer:addClass(args.header_class)
	end
	if args.class then
		headerContainer:addClass(args.class)
	end
	
	headerContainer:wikitext(table.concat({
		(args.pre_main or ''),
		construct_main_block(args),
		construct_notes_block(args),
		(args.post_notes or '')
	}))
	
	return p.get_stylesheet('Header structure') .. (args.pre_container or '') .. tostring(headerContainer)
end

function p.header_structure(frame)
	return p.construct_header(getArgs(frame))
end

return p