Minecraft Wiki
Регистрация
Advertisement

Реализует шаблоны {{Документация}} и {{Документация/Шапка}}

Использует стили со страницы MediaWiki:Common-all.css

Зависимости

local p = {}

local defaultDocPage = 'док'

local getType = function( namespace, page )
	local pageType = 'template'
	if namespace == 'Модуль' or 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

local getTypeForms = function( pageType )
	if pageType == 'module' then
		return {gen = 'этого модуля', acc = 'этот модуль', pronoun_gen = "его", nomP = 'Модули'}
	elseif pageType == 'widget' then
		return {gen = 'этого виджета', acc = 'этот виджет', pronoun_gen = "его", nomP = 'Виджеты'}
	elseif pageType == 'stylesheet' then
		return {gen = 'этой таблицы стилей', acc = 'эту таблицу стилей', pronoun_gen = "её", nomP = 'Таблицы стилей'}
	elseif pageType == 'script' then
		return {gen = 'этого скрипта', acc = 'этот скрипт', pronoun_gen = "его", nomP = 'Скрипты'}
	elseif pageType == 'message' then
		return {gen = 'этого сообщения', acc = 'это сообщение', pronoun_gen = "его", nomP = 'Сообщения'}
	else -- type == 'template'
		return {gen = 'этого шаблона', acc = 'этот шаблон', pronoun_gen = "его", nomP = 'Шаблоны'}
	end
end

-- Creating a documentation page or transclution through {{subst:doc}}
function p.create( f )
	local args = require( 'Модуль:ProcessArgs' ).norm()
	local page = mw.title.getCurrentTitle()
	local docPage = args['страница'] or page.nsText .. ':' .. page.baseText .. '/' .. defaultDocPage
	
	local out
	if not args["содержимое"] and tostring( page ) == docPage then
		out = f:preprocess( '{{subst:Шаблон:Документация/Предзагрузка}}' )
	else
		local templateArgs = {}
		for _, key in ipairs{ 'тип', 'страница', 'содержимое' } do
			local val = args[key]
			if val then
				if key == 'содержимое' then val = '\n' .. val .. '\n' end
				table.insert( templateArgs, key .. '=' .. val )
			end
		end
	
		out = '{{Документация|' .. table.concat( templateArgs, '|' ) .. '}}'
		out = out:gsub( '|}}', '}}' )
		
		if not args["содержимое"] then
			out = out .. '\n<!-- Размещайте категории/интервики на странице документации -->'
		end
	end
	
	if not mw.isSubsting() then
		out = f:preprocess( out )
		if not args['некат'] then
			out = out .. '[[Категория:Страницы с требующими подстановки шаблонами]]'
		end
	end
	
	return out
end

-- Header2 on the documentation page
function p.docPage( f )
	local args = require( 'Модуль:ProcessArgs' ).merge( true )
	local badDoc = args['плохдок']
	local forceNoDoc = args['нетдок']
	
	local docclass = "documentation-norm"
	if badDoc then
		docclass = "documentation-bad"
	elseif forceNoDoc then
		docclass = "documentation-no"
	end
	if forceNoDoc then
		f:callParserFunction( '#dplvar:set', '$doc forced_none', '1' )
	end
	
	if f:callParserFunction( '#dplvar', '$doc noheader' ) == '1' then
		if badDoc then
			f:callParserFunction( '#dplvar:set', '$doc bad', '1' )
		end
		return
	end
	
	local page = mw.title.getCurrentTitle()
	local namespace = page.nsText
	local pageType = mw.ustring.lower( args['тип'] or getType( namespace, page ) )
	local typeForms = getTypeForms( pageType )

	local body = mw.html.create( 'div' ):addClass( 'documentation-header ' .. docclass )
	body
		:css{
			['margin-bottom'] = '0.8em',
			padding = '0.8em 1em 0.7em',
		}
		:tag( 'div' )
			:css( 'float', 'right' )
			:wikitext( '[[', page:fullUrl( 'action=purge' ), ' ' .. f:preprocess('{{lc:{{int:purge}}}}') .. ']]' )
		:done()
		:wikitext(
			'Эта страница документации, она ',
			pageType == 'module' and 'будет' or 'должна быть',
			' включена в основную страницу. ',
			'Больше информации на странице [[Шаблон:Документация]].'
		)
	if badDoc then
		body:wikitext( "<br>'''Документацию " .. typeForms.gen .. " нужно улучшить или дополнить.'''" )
	elseif forceNoDoc then
		body:wikitext( "<br>'''У " .. typeForms.gen .. " отсутствует документация. Если вы знаете, как использовать " .. typeForms.acc .. ", просьба задокументировать " .. typeForms.pronoun_gen .. ".'''" )
	end

	if not ( args["некат"] or namespace == 'Участник' ) then
		body:wikitext( '[[Категория:Страницы документации]]' )
	end
	
	return 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 ]+$', '' ))
	end
	local args = require( 'Модуль:ProcessArgs' ).merge( true )
	local page = mw.title.getCurrentTitle()
	local namespace = page.nsText
	local docText = trim( args["содержимое"] or '' )
	if docText == '' then docText = nil end
	
	local docPage
	local noDoc
	local forceNoDoc
	if docText then
		docPage = page
	else
		docPage = mw.title.new( args["страница"] or namespace .. ':' .. page.text .. '/' .. defaultDocPage)
		noDoc = args["нетдок"] or not docPage.exists
	end
	local badDoc = args["плохдок"]
	local pageType = mw.ustring.lower( args['тип'] or getType( namespace, page ) )
	local typeForms = getTypeForms( pageType )
	
	if not docText and not noDoc then
		f:callParserFunction( '#dplvar:set', '$doc noheader', '1' )
		docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText }  )
		if f:callParserFunction( '#dplvar', '$doc bad' ) == '1' then
			badDoc = 1
		end
		forceNoDoc = f:callParserFunction( '#dplvar', '$doc forced_none' ) == '1'
		
		if docText == '' and not forceNoDoc then
			docText = nil
			noDoc = 1
		end
	end
	if docText then
		docText = '\n' .. docText .. '\n'
	end
	
	local action = f:preprocess('{{lc:{{int:edit}}}}')
	local preload = ''
	local docclass = "documentation-norm"
	local message
	local category
	if noDoc then
		action = f:preprocess('{{lc:{{int:create}}}}')
		preload = '&preload=Шаблон:Документация/Предзагрузка'
		docclass = "documentation-no"
		message = "'''У " .. typeForms.gen .. " нет документации. " ..
			"Если вы знаете, как использовать " .. typeForms.acc .. ", пожалуйста, добавьте соответствующую информацию.'''"
		if not ( args["некат"] or namespace == 'Участник' or namespace == 'Участница' ) then
			category = typeForms.nomP .. ' без документации'
			if not mw.title.new( 'Категория:' .. category ).exists then
				category = 'Страницы без документации'
			end
		end
	elseif forceNoDoc then
		docclass = "documentation-no"
		message = "'''У " .. typeForms.gen .. " нет документации. " ..
			"Если вы знаете, как использовать " .. typeForms.acc .. ", пожалуйста, добавьте соответствующую информацию.'''"
		if not ( args["некат"] or namespace == 'Участник' or namespace == 'Участница' ) then
			category = typeForms.nomP .. ' без документации'
			if not mw.title.new( 'Категория:' .. category ).exists then
				category = 'Страницы без документации'
			end
		end
	elseif badDoc then
		docclass = "documentation-bad"
		message = "'''Документацию " .. typeForms.gen .. " нужно улучшить или дополнить.'''\n"
		if not ( args["некат"] or namespace == 'Участник' or namespace == 'Участница' ) then
			category = typeForms.nomP .. ' с плохой документацией'
			if not mw.title.new( 'Категория:' .. category ).exists then
				category = 'Страницы с плохой документацией'
			end
		end
	end
	
	local historyLink =	f:preprocess('{{lc:{{int:history short}}}}')
	local purgeLink =	f:preprocess('{{lc:{{int:purge}}}}')
	local viewLink =	f:preprocess('{{lc:{{int:view}}}}')

	local links = {
		'[' .. docPage:fullUrl( 'action=edit' .. preload ) .. ' ' .. action .. ']',
		'[' .. docPage:fullUrl( 'action=history' ) .. ' ' .. historyLink .. ']',
		'[' .. page:fullUrl( 'action=purge' ) .. ' ' .. purgeLink .. ']'
	}
	if not noDoc and page ~= docPage then
		table.insert( links, 1, '[[' .. docPage.fullText .. '|' .. viewLink .. ']]' )
	end
	links = mw.html.create( 'span' )
		:css( 'float', 'right' )
		:wikitext( mw.text.nowiki( '[' ), table.concat( links, ' | ' ), mw.text.nowiki( ']' ) )
	
	local body = mw.html.create( 'div' ):addClass( 'documentation ' .. docclass )
	body:css{
		padding = '0.8em 1em 0.7em',
		['margin-top'] = '1em',
		clear = 'both'
	}
	
	local header = mw.html.create( 'div' )
		:addClass( 'documentation-header' )
		:css{
			margin = '-0.8em -1em 0.8em',
			padding = '0.8em 1em 0.7em',
			['border-bottom'] = 'inherit'
		}
	
	header
		:node( links )
		:tag( 'span' )
			:css{
				['font-weight'] = 'bold',
				['font-size'] = '130%',
				['margin-right'] = '1em',
				['line-height'] = '1'
			}
			:wikitext( 'Документация' )
	
	if not noDoc and pageType ~= 'template' and pageType ~= 'message' then
		header
			:tag( 'span' )
				:css( 'white-space', 'nowrap' )
				:wikitext( '[[#the-code|Перейти к коду ↴]]' )
	end
	
	body
		:node( header ):done()
		:wikitext( message )
		:wikitext( docText )
	
	if not noDoc and page ~= docPage then
		body
			:tag( 'div' )
				:addClass( 'documentation-footer' )
				:css{
					margin = '0.7em -1em -0.7em',
					['border-top'] = 'inherit',
					padding = '0.8em 1em 0.7em',
					clear = 'both'
				}
				:node( links )
				:wikitext( 'Расположенная выше документация включена из [[' .. docPage.fullText .. ']].' )
	end
	
	if category then
		body:wikitext( "[[Категория:" .. 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 tostring( body ) .. tostring( anchor )
end

return p
Advertisement