Minecraft Wiki
Register
Advertisement

Цей модуль реалізує шаблон {{Документація}}.

Залежності

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:док}}
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' ), ' оновити]]' )
		: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 string.gsub( s, '^[\t\r\n\f ]*(.-)[\t\r\n\f ]*$', '%1' )
	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 = 'редагувати'
	local preload = ''
	local docclass = "documentation-norm"
	local message
	local category
	if noDoc then
		action = 'створити'
		preload = '&preload=Шаблон:Документація/передзавантаження'
		docclass = "documentation-no"
		message = "'''У " .. typeForms.gen .. " немає документації. " ..
			"Якщо ви знаєте, як використовувати " .. typeForms.acc .. ", будь ласка, додайте відповідну інформацію.'''"
		if not ( args["некат"] 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 == 'Користувач' ) 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 == 'Користувач' ) then
			category = typeForms.nomP .. ' з поганою документацією'
			if not mw.title.new( 'Категория:' .. category ).exists then
				category = 'Сторінки з поганою документацією'
			end
		end
	end
	
	local links = {
		'[' .. docPage:fullUrl( 'action=edit' .. preload ) .. ' ' .. action .. ']',
		'[' .. docPage:fullUrl( 'action=history' ) .. ' історія]',
		'[' .. page:fullUrl( 'action=purge' ) .. ' оновити]'
	}
	if not noDoc and page ~= docPage then
		table.insert( links, 1, '[[' .. docPage.fullText .. '|перегляд]]' )
	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