Module:Documentation

From Modded Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Documentation/doc

local p = {}
local defaultDocPage = 'doc'
local static = require( 'Module:Static' )

local i18n = {
	commonInternalLink = '[[%s]]',
	commonInternalLinkPipe = '[[%s|%s]]',
	commonExternalLink = '[%s]',
	commonExternalLinkWithName = '[%s %s]',
	commonCategory = 'Category:%s',
	pageType_template = 'template',
	pageType_module = 'module',
	pageType_widget = 'widget',
	pageType_stylesheet = 'stylesheet',
	pageType_script = 'script',
	pageType_message = 'message',
	linkBar = '%s',
	linkFormat = mw.text.nowiki( '[' ) .. '%s' .. mw.text.nowiki( ']' ),
	linkSeparator = ' ',
	linkTextPurge = mw.getCurrentFrame():callParserFunction( 'int:smw_purge' ):lower(),
	linkTextView = mw.getCurrentFrame():callParserFunction( 'int:view' ):lower(),
	linkTextEdit = mw.getCurrentFrame():callParserFunction( 'int:edit' ):lower(),
	linkTextHistory = mw.getCurrentFrame():callParserFunction( 'int:history_short' ):lower(),
	linkTextCreate = mw.getCurrentFrame():callParserFunction( 'int:create' ):lower(),
	createOutputFormat = '%s%s',
	createSplitDocPagePrompt = '\n<!-- Put categories/interwikis on the documentation page -->',
	createNoSubstCategory = 'Pages with templates requiring substitution',
	docPagePrompt = 'This is the documentation page. It %s be transcluded into the main %s page. See [[Template:Documentation]] for more information.',
	docPagePromptWill = 'will',
	docPagePromptShould = 'should',
	docPageBadDocPrompt = "<br>'''This %s's documentation needs improving or additional information.'''",
	docPageCategory = 'Documentation pages',
	pageNoDocPrompt = "'''This %s has no documentation. If you know how to use this %s, please create it.'''",
	pageNoDocCategory = '%ss with no documentation',
	pageNoDocCategoryDefault = 'Pages with no documentation',
}

local function getType( namespace, page )
	local pageType = 'template'

	if namespace == 'Module' then
		pageType = 'module'
	elseif namespace == 'Widget' then
		pageType = 'widget'
	elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.css$' ) then
		pageType = 'stylesheet'
	elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.js$' ) then
		pageType = 'script'
	elseif namespace == 'MediaWiki' then
		pageType = 'message'
	end

	return pageType
end

-- Creating a documentation page or transclusion through {{subst:docc}}
function p.create( f )
	local args = require( 'Module:ProcessArgs' ).norm()
	local page = mw.title.getCurrentTitle()
	local docPage = args.page or page.nsText .. ':' .. page.baseText .. '/' .. defaultDocPage
	
	local out
	if not args.content and tostring( page ) == docPage then
		-- local pageType = mw.ustring.lower( args.type or getType( page.nsText, page ) )
		-- local pageTypeDisplay = i18n[ 'pageType_' .. pageType ] or i18n.pageTypeTemplate
		out = f:preprocess( '{{subst:Template:Documentation/preload}}' )
		-- out = out:gsub( '$1' , pageTypeDisplay )
	else
		local templateArgs = {}
		for _, key in ipairs{ 'type', 'page', 'content', 'nodoc', 'baddoc' } do
			local val = args[ key ]
			if val then
				if key == 'content' then val = '\n' .. val .. '\n' end
				table.insert( templateArgs, key .. '=' .. val )
			end
		end
		out = '{{documentation|' .. table.concat( templateArgs, '|' ) .. '}}'
		out = out:gsub( '|}}', '}}' )
		out = i18n.createOutputFormat:format( out, args.content and '' or i18n.createSplitDocPagePrompt )
	end

	if not mw.isSubsting() then
		out = f:preprocess( out )
		if not args.nocat then
			out = out .. i18n.commonInternalLink:format( i18n.commonCategory:format( i18n.createNoSubstCategory ) )
		end
	end

	return out
end

-- Header on the documentation page
function p.docPage( f )
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local badDoc = args.baddoc

	mw.logObject( badDoc, 'Module:Documentation: badDoc' )
	if badDoc then
		static.badDoc = '1'
	end

	local page = mw.title.getCurrentTitle()

	local subpage = page.subpageText
	if subpage == 'sandbox' or subpage == 'testcases' then
		page = page.basePageTitle
	end

	local docPage = mw.title.new( args.page or page.nsText .. ':' .. page.baseText .. '/' .. defaultDocPage )
	mw.logObject( docPage, 'Module:Documentation: docPage' )
	mw.logObject( page, 'Module:Documentation: page' )
	if docPage ~= page then return end

	local namespace = page.nsText
	local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
	local pageTypeDisplay = i18n[ 'pageType_' .. pageType ] or i18n.pageTypeTemplate

	local body = mw.html.create( 'div' ):addClass( 'documentation' )
	body
		:addClass( badDoc and 'documentation-badDoc' or '' )
		:tag( 'div' )
			:attr( 'id', 'documentation-header-tools' )
			:wikitext( i18n.linkBar:format( i18n.linkFormat:format( '[[Special:Purge/' .. page.fullText .. '|' .. i18n.linkTextPurge .. ']]' ) ) )
		:done()
		:wikitext( i18n.docPagePrompt:format( pageType == 'module' and i18n.docPagePromptWill or i18n.docPagePromptShould, pageTypeDisplay ) )
	if badDoc then
		body:wikitext( i18n.docPageBadDocPrompt:format( pageTypeDisplay ) )
	end
	if not ( args.nocat or namespace == 'User' ) then
		body:wikitext( i18n.commonInternalLink:format( i18n.commonCategory:format( i18n.docPageCategory ) ) )
	end

	mw.logObject( body, 'Module:Documentation: body')

	return mw.getCurrentFrame():extensionTag{ name = "templatestyles", args = { src = 'Module:Documentation/styles.css' } } .. tostring( body )
end

-- Wrapper around the documentation on the main page
function p.page( f )
	-- mw.text.trim uses mw.ustring.gsub, which silently fails on large strings
	local function trim( s )
		return (s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' ))
		--return string.gsub( s, '^[\t\r\n\f ]*(.-)[\t\r\n\f ]*$', '%1' )
	end
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local page = mw.title.getCurrentTitle()
	local subpage = page.subpageText
	if subpage == 'sandbox' or subpage == 'testcases' then
		page = page.basePageTitle
	end
	local namespace = page.nsText
	local docText = trim( args.content or '' )
	if docText == '' then docText = nil end

	local docPage
	local noDoc
	if docText then
		docPage = page
	else
		docPage = mw.title.new( args.page or namespace .. ':' .. page.text .. '/' .. defaultDocPage )
		noDoc = args.nodoc or not docPage.exists
	end
	local badDoc = args.baddoc
	local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )

	if not docText and not noDoc then
		docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText }  )
		if static.badDoc and static.badDoc == '1' then
			mw.logObject(static, "static")
			badDoc = 1
		end
		
		if docText == '' then
			docText = nil
			noDoc = 1
		end
	end
	if docText then
		docText = '\n' .. docText .. '\n'
	end

	local docClass = ''
	local message
	local category
	if noDoc then
		docClass = 'documentation-noDoc'
		message = i18n.pageNoDocPrompt:format( pageType, pageType )
		if not ( args.nocat or namespace == 'User' ) then
			category = i18n.pageNoDocCategory:format( pageType )
			if not mw.title.new( 'Category:' .. category ).exists then
				category = i18n.pageNoDocCategoryDefault
			end
		end
	elseif badDoc then
		docClass = 'documentation-badDoc'
		message = "'''This " .. pageType .. "'s documentation needs improving or additional information.'''\n"
		if not ( args.nocat or namespace == 'User' ) then
			category = pageType .. 's with bad documentation'
			if not mw.title.new( 'Category:' .. category ).exists then
				category = 'Pages with bad documentation'
			end
		end
	end

	-- Generates the [view][edit][history][refresh] or [create][refresh] links
	local links = mw.html.create( 'span' )
		:attr( 'id', 'documentation-header-tools' )

	local linkList = {}
	if not noDoc and page ~= docPage then
		table.insert( linkList, i18n.linkFormat:format( '[[' .. docPage.fullText .. '|' .. i18n.linkTextView .. ']]' ) )
		table.insert( linkList, i18n.linkFormat:format( '[[Special:EditPage/' .. docPage.fullText .. '|' .. i18n.linkTextEdit .. ']]' ) )
		table.insert( linkList, i18n.linkFormat:format( '[[Special:PageHistory/' .. docPage.fullText .. '|' .. i18n.linkTextHistory .. ']]' ) )
	else
		table.insert( linkList, i18n.linkFormat:format( '[' .. docPage:canonicalUrl{ action = 'edit', preload = 'Template:Documentation/preload' } .. ' ' .. i18n.linkTextCreate .. ']' ) )
	end
	table.insert( linkList, i18n.linkFormat:format( '[[Special:Purge/' .. docPage.fullText .. '|' .. i18n.linkTextPurge .. ']]' ) )
	links:wikitext( i18n.linkBar:format( table.concat( linkList, i18n.linkSeparator ) ) )

	local body = mw.html.create( 'div' ):addClass( 'documentation' )
	body
		:addClass( docClass )

	local header = mw.html.create( 'div' )
		:addClass( 'documentation-header-top' )

	header
		:node( links )
		:tag( 'span' )
			:addClass('documentation-header-title' )
			:wikitext( 'Documentation' )

	local codePages = {
		module = true,
		stylesheet = true,
		script = true,
	}
	if not noDoc and codePages[pageType] then
		header
			:tag( 'span' )
				:attr( 'id', 'documentation-jump-to-code' )
				:wikitext( '[[#the-code|Jump to code ↴]]' )
	end

	body
		:node( header ):done()
		:wikitext( message )
		:wikitext( docText )

	if not noDoc and page ~= docPage then
		body
			:tag( 'div' )
				:addClass( 'documentation-header-bottom' )
				:node( links )
				:wikitext( 'The above documentation is transcluded from [[', docPage.fullText, ']].' )
	end

	if category then
		body:wikitext( f:expandTemplate{ title = 'Translation category', args = { category, project = '0' } } )
	end

	local anchor = ''
	if not noDoc and pageType ~= 'template' and pageType ~= 'message' then
		anchor = mw.html.create( 'div' ):attr( 'id', 'the-code' )
	end

	return mw.getCurrentFrame():extensionTag('templatestyles', '', {src='Module:Documentation/styles.css'}) .. tostring( body ) .. tostring( anchor )
end

return p