Module:Documentation

From NeuroWiki
Revision as of 21:47, 9 November 2024 by Selfice (talk | contribs) (Translated)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This module is used to implement {{Documentation}}.

Dependencies



local p = {}

-- Load modules
local lan = require( 'Module:Lan' ).call

-- Customizable strings
local i18n = {
	-- Default settings, modify if necessary
	defaultDocPage = 'doc',	-- Suffix for documentation pages
	defaultSandboxPage = 'sandbox',	-- Suffix for sandbox pages
	defaultTestCasePage = 'testcases',	-- Suffix for test case pages
	defaultPreload = 'Template:Documentation/preload',	-- Page storing standard content of documentation pages
	defaultStyles = 'Module:Documentation/styles.css',	-- Stylesheet used when this module uses TemplateStyles; remove or set to nil if not used on your wiki

	-- Format strings, should not be translated
	commonInternalLink = '[[%s]]',
	commonInternalLinkPipe = '[[%s|%s]]',
	commonExternalLink = '[%s]',
	commonExternalLinkWithName = '[%s %s]',
	commonNamespacedPage = '%s:%s',
	commonNamespacedPageWithSub = '%s:%s/%s',

	-- Namespace names, translate if your language prefers localized names, though not translating will generally not affect functionality
	namespaceCategory = 'Category',
	namespaceSpecial = 'Special',
	namespaceUser = 'User',

	-- Special page names, translate if your language prefers localized names, though keeping the original will still link to the correct page
	specialPurge = 'Purge',
	specialEdit = 'EditPage',
	specialHistory = 'PageHistory',

	-- If in your language, type names have different displays, please translate
	pageType_page = 'page',
	pageType_template = 'template',
	pageType_module = 'module',
	pageType_stylesheet = 'stylesheet',
	pageType_script = 'script',
	pageType_json = 'JSON',
	pageType_message = 'message',

	-- Modify below if your wiki uses a different link display method
	linkBar = mw.text.nowiki( '[ ' ) .. '%s' .. mw.text.nowiki( ' ]' ),	-- Overall style used for the entire link bar
	linkFormat = '%s',	-- Style used for a single link
	linkSeparator = mw.text.nowiki( mw.getCurrentFrame():callParserFunction( 'int:pipe-separator' ) ),	-- Separator between different links

	-- Display names for different types of links, modify if necessary
	linkTextPurge = mw.getCurrentFrame():callParserFunction( 'int:purge' ),
	linkTextView = mw.getCurrentFrame():callParserFunction( 'int:view' ),
	linkTextEdit = mw.getCurrentFrame():callParserFunction( 'int:edit' ),
	linkTextHistory = mw.getCurrentFrame():callParserFunction( 'int:history_short' ),
	linkTextCreate = mw.getCurrentFrame():callParserFunction( 'int:create' ),

	-- Strings used in p.create(): content displayed when using {{doc}} or {{subst:doc}}
	createOutputFormat = '<noinclude>%s%s</noinclude>',	-- Overall format
	createSplitDocPagePrompt = '\n<!-- Please place categories/language links on the documentation page -->',	-- String displayed when creating the documentation page as a separate page
	createNoSubstCategory = 'Pages needing substitution template',	-- Tracking category added when using {{doc}} without substitution

	-- Strings used in p.docPage(): content displayed on the documentation page
	docPagePrompt = 'This is the documentation page, it should be placed in %s. See [[Template:Documentation]] for more information.',
	docPagePromptWill = 'will',
	docPagePromptShould = 'should',
	docPageBadDocPrompt = "<br>'''The documentation page for this %s needs improvements or additional information.'''",
	docPageCategory = 'Documentation pages',	-- Tracking category for documentation pages

	-- Strings used in p.page(): content displayed on the code page
	pageNoDocPrompt = "'''This %s does not have a documentation page. If you know how to use this %s, please help by creating a documentation page.'''",
	pageNoDocCategory = 'Pages without documentation for %s',	-- Tracking category for code pages without documentation, parameter is page type
	pageNoDocCategoryDefault = 'Pages without documentation',	-- Tracking category for code pages without documentation (fallback)
	pageBadDocPrompt = "'''The documentation page for this %s needs improvements or additional information.'''\n",
	pageBadDocCategory = 'Low-quality documentation for %s',	-- Tracking category for code pages with low-quality documentation, parameter is page type
	pageBadDocCategoryDefault = 'Low-quality documentation pages',	-- Tracking category for low-quality documentation pages (fallback)
	pageDocHeaderTitle = 'Documentation page',	-- Title at the top of the documentation page
	pageDocJumpToCode = 'Jump to code ↴',
	pageDocHeaderBottom = 'The above documentation is transcluded from %s.',
}

-- Customizable functions
local function pageCategoryHandler( category )
	return i18n.commonInternalLink:format( i18n.commonNamespacedPage:format( i18n.namespaceCategory, category ) )
end

-- Load modules
local loadStyles = require( 'Module:TSLoader' ).call

local static = require( 'Module:Static' )
if not static.Documentation then
	static.Documentation = {}
end

-- Internal functions
local function getType( namespace, page )
	local pageType = 'page'
	if namespace == 'Module' then
		pageType = 'module'
	elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.css$' ) then
		pageType = 'stylesheet'
	elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.js$' ) then
		pageType = 'script'
	elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.json$' ) then
		pageType = 'json'
	elseif namespace == 'MediaWiki' then
		pageType = 'message'
	end
	return pageType
end

local function getDisplayType( pageType )
	return i18n[ 'pageType_' .. pageType ] or i18n.pageType_page
end

-- Exported functions
function p.create( f )	-- Creating a documentation page or transclusion through {{subst:doc}}
	local args = require( 'Module:ProcessArgs' ).norm()
	local page = mw.title.getCurrentTitle()
	local docPage = args.page or i18n.commonNamespacedPageWithSub:format( page.nsText, page.baseText, i18n.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 = getDisplayType( pageType )
		out = f:preprocess( mw.title.new( i18n.defaultPreload ):getContent():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.commonNamespacedPage:format( i18n.namespaceCategory, i18n.createNoSubstCategory ) )
		end
	end

	return out
end

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

	if badDoc then
		static.Documentation.badDoc = '1'
	end

	local page = mw.title.getCurrentTitle()

	local subpage = page.subpageText
	if subpage == i18n.defaultSandboxPage or subpage == i18n.defaultTestCasePage then
		page = page.basePageTitle
	end

	local docPage = mw.title.new( args.page or i18n.commonNamespacedPageWithSub:format( page.nsText, page.baseText, i18n.defaultDocPage ) )
	if docPage ~= page then return end

	local namespace = page.nsText
	local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
	local pageTypeDisplay = getDisplayType( pageType )

	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( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialPurge, page.fullText ), i18n.linkTextPurge ) ) ) )
		:done()
		:wikitext( i18n.docPagePrompt:format( pageType == 'module' and i18n.docPagePromptWill or i18n.docPagePromptShould, i18n.commonInternalLink:format( i18n.commonNamespacedPage:format( namespace, page.baseText ) ) ) )
	if badDoc then
		body:wikitext( i18n.docPageBadDocPrompt:format( pageTypeDisplay ) )
	end
	if not ( args.nocat or namespace == i18n.namespaceUser ) then
		body:wikitext( i18n.commonInternalLink:format( i18n.commonNamespacedPage:format( i18n.namespaceCategory, i18n.docPageCategory ) ) )
	end

	return loadStyles( i18n.defaultStyles ) .. tostring( body )
end

function p.page( f )	-- Wrapper around the documentation on the main page
	-- 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 ]+$', '' ) )
	end
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local page = mw.title.getCurrentTitle()
	local subpage = page.subpageText
	if subpage == i18n.defaultSandboxPage or subpage == i18n.defaultTestCasePage 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 i18n.commonNamespacedPageWithSub:format( namespace, page.text, i18n.defaultDocPage ) )
		noDoc = args.nodoc or not docPage.exists
	end
	local badDoc = args.baddoc
	local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
	local pageTypeDisplay = getDisplayType( pageType )

	if not docText and not noDoc then
		docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText } )
		if static.Documentation.badDoc and static.Documentation.badDoc == '1' then
			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( pageTypeDisplay, pageTypeDisplay )
		if not ( args.nocat or namespace == i18n.namespaceUser ) then
			category = i18n.pageNoDocCategory:format( pageTypeDisplay )
			if not mw.title.new( i18n.commonNamespacedPage:format( i18n.namespaceCategory, category ) ).exists then
				category = i18n.pageNoDocCategoryDefault
			end
		end
	elseif badDoc then
		docClass = 'documentation-badDoc'
		message = i18n.pageBadDocPrompt:format( pageTypeDisplay )
		if not ( args.nocat or namespace == i18n.namespaceUser ) then
			category = i18n.pageBadDocCategory:format( pageTypeDisplay )
			if not mw.title.new( i18n.commonNamespacedPage:format( i18n.namespaceCategory, category ) ).exists then
				category = i18n.pageBadDocCategoryDefault
			end
		end
	end

	-- Generates the link bar
	local links = mw.html.create( 'span' )
		:attr( 'id', 'documentation-header-tools' )

	local linkList = {}
	if not noDoc then
		if page ~= docPage then
			table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( docPage.fullText, i18n.linkTextView ) ) )
		end
		table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialEdit, docPage.fullText ), i18n.linkTextEdit ) ) )
		table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialHistory, docPage.fullText ), i18n.linkTextHistory ) ) )
	else
		table.insert( linkList, i18n.linkFormat:format( i18n.commonExternalLinkWithName:format( docPage:canonicalUrl{ action = 'edit', preload = i18n.defaultPreload, preloadparams = pageTypeDisplay }, i18n.linkTextCreate ) ) )
	end
	table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialPurge, 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( i18n.pageDocHeaderTitle )

	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( i18n.commonInternalLinkPipe:format( '#the-code', i18n.pageDocJumpToCode ) )
	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( i18n.pageDocHeaderBottom:format( i18n.commonInternalLink:format( docPage.fullText ) ) )
	end

	if category then
		body:wikitext( pageCategoryHandler( category ) )
	end

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

	return loadStyles( i18n.defaultStyles ) .. tostring( body ) .. tostring( anchor )
end

return p