Module:Documentation

From Vintage Story Wiki
Revision as of 13:43, 6 December 2023 by Nyuhnyash (talk | contribs)

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

local p = {}
local defaultDocPage = 'doc'

local getType = function( 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
	
function p.page(frame)
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local function trim( s )
		return (s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' ))
	end
	local docText = trim( args.content or '' )
	local page = mw.title.getCurrentTitle()
	local namespace = page.nsText
	local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
	if docText == '' then docText = nil end
	local docPage
	local noDoc
	local badDoc = args.baddoc
	if docText then
		docPage = page
	else
		local docPageEnd
		local isTranslatedDoc = false
		-- Translated version of documentation on /doc/{{lang}} subpage
		for langCode in page.text:gmatch('/%w+$') do
			langCode = langCode:sub(2)
			if langCode ~= 'en' and mw.language.isKnownLanguageTag(langCode) then
				isTranslatedDoc = true
				
			end
		end
		
		if isTranslatedDoc then
			translatedDocPageEnd = page.text:gsub('/%w+$', '/' .. defaultDocPage .. '%1')
			if mw.title.new( args.page or namespace .. ':' .. translatedDocPageEnd ).exists then
				docPageEnd = translatedDocPageEnd
			else
				docPageEnd = page.text:gsub('/%w+$', '/' .. defaultDocPage)
			end
		else
			docPageEnd = page.text .. '/' .. defaultDocPage
		end
		
		docPage = mw.title.new( args.page or namespace .. ':' .. docPageEnd )
		noDoc = args.nodoc or not docPage.exists
	end
	
	if not docText and not noDoc then
		docText = trim( frame:expandTemplate{ title = ':' .. docPage.fullText }  )
		
		if docText == '' then
			docText = nil
			noDoc = 1
		end
	end
	
	if docText then
		docText = '\n' .. docText .. '\n'
	end
	
	local action = frame:preprocess('{{lc:{{int:edit}}}}')
	local preload = ''
	local docCss = ''
	local colour = 'EAF4F9'
	local message
	local category
	if noDoc then
		action = frame:preprocess('{{lc:{{int:create}}}}')
		preload = '&preload=Template:Documentation/preload'
		colour = 'F9EAEA'
		message = "'''This " .. pageType .. " has no documentation. " ..
			"If you know how to use this " .. pageType .. ", please create it.'''"
		if not ( args.nocat or namespace == 'User' ) then
			category = pageType .. 's with no documentation'
			if not mw.title.new( 'Category:' .. category ).exists then
				category = 'Pages with no documentation'
			end
		end
	elseif badDoc then
		colour = 'F9F2EA'
		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

	local links = {
		'[' .. docPage:fullUrl( 'action=edit' .. preload ) .. ' ' .. action .. ']',
		'[' .. docPage:fullUrl( 'action=history' ) .. ' ' .. frame:preprocess('{{lc:{{int:history short}}}}') .. ']',
		'[' .. page:fullUrl( 'action=purge' ) .. ' ' .. frame:preprocess('{{lc:{{int:action-purge}}}}') .. ']'
	}
	if not noDoc and page ~= docPage then
		table.insert( links, 1, '[[' .. docPage.fullText .. '|' .. frame:preprocess('{{lc:{{int:view}}}}') .. ']]' )
	end

	local body = mw.html.create('div'):css{
		border = '1px solid black',
		margin = '1em',
		['margin-top'] = '3em',
		['background-color'] = '#' .. colour
	}
	
	local textBody = mw.html.create('div'):css{
		padding = '0.8em 1em 0.7em'
	}:wikitext(docText)

	links = mw.html.create('span')
		:css('float', 'right')
		:wikitext( mw.text.nowiki( '[' ), table.concat( links, ' | ' ), mw.text.nowiki( ']' ) )
	local header = mw.html.create('div'):css{
			background = '#ddd',
			padding = '1em',
			margin = '0em'
		}
	header
		:node(links)
		:tag('span')
			:css{
				['font-weight'] = 'bold',
				['font-size'] = '130%',
				color = '#111'
			}
			: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' )
				:css( 'white-space', 'nowrap' )
				:wikitext( '[[#the-code|Jump to code ↴]]' )
	end
	
	body
		:node( header ):done()
		:wikitext( message )
		:node(textBody):done()
		
	if not noDoc and page ~= docPage then
		body
			:tag( 'div' )
				:css{
					['border-top'] = 'inherit',
					padding = '0.8em 1em 0.7em',
					clear = 'both'
				}
				:node( links )
				:wikitext( 'The above documentation is transcluded from [[', docPage.fullText, ']].' )
	end
	return tostring( body )
end
return p