Module:Documentation subpage

From Wiktionary
Documentation icon Module documentation[view] [change] [history] [refresh]

This module is used by {{documentation subpage}} to generate a header box given the correct context. It should not be used directly except for testing purposes. For general usage, please use {{documentation subpage}} instead.

-- The documentation subpage module
local title = mw.title.getCurrentTitle()

local p = {}

-- Find out what the context it currently is
local function getContext( override )
	local subpage = string.lower( title.subpageText )
	if ( subpage == override ) then
		return 'docpage'
	else
		return 'mainpage'
	end
end

-- Add the relevant category for the page depending on whether the basepage exists
local function addCategory( defaultsort )
	local normalcat = mw.ustring.format( 'Category:%s documentation pages', title.subjectNsText )
	local specialcat = 'Category:Documentation subpages without corresponding pages'
	local basepage = title.basePageTitle
	local output = ''
	local sort = ''

	if ( basepage.exists == true ) then
		output = mw.ustring.format( '{{DEFAULTSORT:%s}}<includeonly>[[%s]]</includeonly>', defaultsort, normalcat )
	else
		output = mw.ustring.format( '{{DEFAULTSORT:%s}}<includeonly>[[%s]]</includeonly>', defaultsort, specialcat )
	end
	return output
end

-- The output when on a documentation subpage
local function onDocPage( one, defaultsort )
	local root = mw.html.create()
	local link = ''
	if ( one == '' ) then
		link = mw.ustring.format( '[[:%s:%s]]', title.subjectNsText, title.baseText )
	else
		link = one
	end

	root
		:tag( 'table' )
			:addClass( 'messagebox standard-talk' )
			:tag( 'tr' )
				:css( 'vertical-align', 'top' )
				:css( 'text-align', 'center' )
				:tag( 'td' )
					:css( 'padding', '1ex' )
					:tag( 'div' )
						:addClass( 'floatnone' )
						:wikitext( '[[Image:Edit-paste.svg|none|48px]]' )
						:done()
					:done()
				:tag( 'td' )
					:wikitext( mw.ustring.format( "'''This is a documentation subpage for %s'''.", link ) )
					:wikitext( '<br />' )
					:wikitext( mw.ustring.format( 'It contains usage information, categories and other content that are not part of the original %s page.', mw.ustring.lower( title.subjectNsText ) ) )
					:wikitext( addCategory( defaultsort ) )
					:done()
				:done()
			:done()

	local output = tostring( root )
	return output
end

-- Main function to run the module
function p.run( frame )
	local override = frame.args['override']
	local one = frame.args['1']
	local defaultsort = frame.args['defaultsort']
	local output = ''
	local context = getContext( override )

	if ( context == 'docpage' ) then
		output = onDocPage( one, defaultsort )
	else
		output = ''
	end

	return frame:preprocess( output )
end

return p