Module:Portal header

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local construct_header = require('Module:Header structure').construct_header
local get_image = require('Module:Author')._get_image
local portal_class = require('Module:Portal class')._portal_class
local portal_parent = require('Module:Portal parent').portal_parent

local tracking_cats = {
	['parent'] = '[[Category:' .. 'Portals linking to a parent' .. ']]',
	['shortcuts'] = '[[Category:' .. 'Portals with shortcuts' .. ']]',
	['subpages'] = '[[Category:' .. 'Portal subpages' .. ']]',
	['review'] = '[[Category:' .. 'Portals needing review' .. ']]',
	['portals'] = function(sortkey)
		if sortkey then
			return '[[Category:' .. 'Portals|' .. sortkey .. ']]'
		else
			return '[[Category:' .. 'Portals' .. ']]'
		end
	end
}

local function parent_text(parent)
	return 'Parent portal: [[Portal:' .. parent .. '|' .. parent .. ']]'
end

function p._portal_header(args)
	local current_title = mw.title.getCurrentTitle()
	local namespace = args.namespace or current_title.nsText
	local cats = {}
	
	local categorize = not yesno(args.nocat) or args.forcecat == 'forced' or (yesno(args.forcecat) ~= false and namespace == 'Portal')
	
	args.template_ns = args.template_ns or 'Portal'
	args.header_class = 'wst-portal-header'
	args.main_class = 'wst-portal-header-mainblock ws-header ws-noexport noprint'
	args.main_title = args.title or current_title.subpageText
	
	args.prefer_logo = yesno(args.prefer_logo) ~= false and yesno(args['prefer-logo']) ~= false and yesno(args['prefer logo']) ~= false
	args['prefer-logo'] = nil
	args['prefer logo'] = nil
	
	if not yesno(args.noparent) then
		if args.parent then
			-- use the explicit parent
			args.previous = parent_text(args.parent)
			-- tracking cat
			if categorize then
				table.insert(cats, tracking_cats['parent'])
			end
		else
			args.previous = tostring(mw.html.create('div'):addClass('wst-portal-parentlink'):wikitext(portal_parent(args)))
		end
	end
	
	args.notes_class = 'wst-portal-header-notes'
	
	if not yesno(args.noclass) then
		args.notes_left_content = portal_class(args)
	end
	
	if categorize then
		if args.shortcut then
			table.insert(cats, tracking_cats['shortcuts'])
		end

		if args.forcecat ~= 'forced' and current_title.isSubpage then
			table.insert(cats, tracking_cats['subpages'])
		else
			table.insert(cats, tracking_cats['portals'](args.sortkey))
			if not yesno(args.reviewed) then
				table.insert(cats, tracking_cats['review'])
			end
		end
	end
	if yesno(args.noimage) then
		args.post_notes = table.concat(cats)
	else
		args.post_notes = get_image(args) .. table.concat(cats)
	end
	
	local stylesheet = tostring(mw.html.create('div'):addClass('ws-noexport'):wikitext(mw.getCurrentFrame():extensionTag('templatestyles', '', {src = 'Portal header/styles.css'})))
	
	return stylesheet .. construct_header(args)
end

function p.portal_header(frame)
	return p._portal_header(getArgs(frame))
end

return p
Category:Templates using TemplateStyles Category:Templates using TemplateStyles with a different protection level Category:Templates using TemplateStyles without padlocks Category:Wikisource semi-protected modules