Minecraft Wiki
Нет описания правки
(версия без capture group занимает меньше памяти и поэтому работает на больших документациях)
(не показано 10 промежуточных версий этого же участника)
Строка 22: Строка 22:
 
local getTypeForms = function( pageType )
 
local getTypeForms = function( pageType )
 
if pageType == 'module' then
 
if pageType == 'module' then
return {gen = 'этого модуля', acc = 'этот модуль', nomP = 'Модули'}
+
return {gen = 'этого модуля', acc = 'этот модуль', pronoun_gen = "его", nomP = 'Модули'}
 
elseif pageType == 'widget' then
 
elseif pageType == 'widget' then
return {gen = 'этого виджета', acc = 'этот виджет', nomP = 'Виджеты'}
+
return {gen = 'этого виджета', acc = 'этот виджет', pronoun_gen = "его", nomP = 'Виджеты'}
 
elseif pageType == 'stylesheet' then
 
elseif pageType == 'stylesheet' then
return {gen = 'этой таблицы стилей', acc = 'эту таблицу стилей', nomP = 'Таблицы стилей'}
+
return {gen = 'этой таблицы стилей', acc = 'эту таблицу стилей', pronoun_gen = "её", nomP = 'Таблицы стилей'}
 
elseif pageType == 'script' then
 
elseif pageType == 'script' then
return {gen = 'этого скрипта', acc = 'этот скрипт', nomP = 'Скрипты'}
+
return {gen = 'этого скрипта', acc = 'этот скрипт', pronoun_gen = "его", nomP = 'Скрипты'}
 
elseif pageType == 'message' then
 
elseif pageType == 'message' then
return {gen = 'этого сообщения', acc = 'это сообщение', nomP = 'Сообщения'}
+
return {gen = 'этого сообщения', acc = 'это сообщение', pronoun_gen = "его", nomP = 'Сообщения'}
 
else -- type == 'template'
 
else -- type == 'template'
return {gen = 'этого шаблона', acc = 'этот шаблон', nomP = 'Шаблоны'}
+
return {gen = 'этого шаблона', acc = 'этот шаблон', pronoun_gen = "его", nomP = 'Шаблоны'}
 
end
 
end
 
end
 
end
Строка 77: Строка 77:
 
local args = require( 'Модуль:ProcessArgs' ).merge( true )
 
local args = require( 'Модуль:ProcessArgs' ).merge( true )
 
local badDoc = args['плохдок']
 
local badDoc = args['плохдок']
  +
local forceNoDoc = args['нетдок']
  +
  +
local background_color = "#EAF4F9"
  +
if badDoc then
  +
background_color = "#F9F2EA"
  +
elseif forceNoDoc then
  +
background_color = "#F9EAEA"
  +
end
  +
if forceNoDoc then
  +
f:callParserFunction( '#dplvar:set', '$doc forced_none', '1' )
  +
end
  +
 
if f:callParserFunction( '#dplvar', '$doc noheader' ) == '1' then
 
if f:callParserFunction( '#dplvar', '$doc noheader' ) == '1' then
 
if badDoc then
 
if badDoc then
Строка 93: Строка 105:
 
['margin-bottom'] = '0.8em',
 
['margin-bottom'] = '0.8em',
 
padding = '0.8em 1em 0.7em',
 
padding = '0.8em 1em 0.7em',
['background-color'] = '#' .. ( badDoc and 'F9F2EA' or 'EAF4F9' ),
+
['background-color'] = background_color,
 
border = '1px solid #AAA'
 
border = '1px solid #AAA'
 
}
 
}
Строка 108: Строка 120:
 
if badDoc then
 
if badDoc then
 
body:wikitext( "<br>'''Документацию " .. typeForms.gen .. " нужно улучшить или дополнить.'''" )
 
body:wikitext( "<br>'''Документацию " .. typeForms.gen .. " нужно улучшить или дополнить.'''" )
  +
elseif forceNoDoc then
  +
body:wikitext( "<br>'''У " .. typeForms.gen .. " отсутствует документация. Если вы знаете, как использовать " .. typeForms.acc .. ", просьба задокументировать " .. typeForms.pronoun_gen .. ".'''" )
 
end
 
end
  +
 
if not ( args["некат"] or namespace == 'Участник' ) then
 
if not ( args["некат"] or namespace == 'Участник' ) then
 
body:wikitext( '[[Категория:Страницы документации]]' )
 
body:wikitext( '[[Категория:Страницы документации]]' )
Строка 120: Строка 135:
 
-- mw.text.trim uses mw.ustring.gsub, which silently fails on large strings
 
-- mw.text.trim uses mw.ustring.gsub, which silently fails on large strings
 
local function trim( s )
 
local function trim( s )
return string.gsub( s, '^[\t\r\n\f ]*(.-)[\t\r\n\f ]*$', '%1' )
+
return (s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' ))
 
end
 
end
 
local args = require( 'Модуль:ProcessArgs' ).merge( true )
 
local args = require( 'Модуль:ProcessArgs' ).merge( true )
Строка 130: Строка 145:
 
local docPage
 
local docPage
 
local noDoc
 
local noDoc
  +
local forceNoDoc
 
if docText then
 
if docText then
 
docPage = page
 
docPage = page
Строка 146: Строка 162:
 
badDoc = 1
 
badDoc = 1
 
end
 
end
  +
forceNoDoc = f:callParserFunction( '#dplvar', '$doc forced_none' ) == '1'
 
 
if docText == '' then
+
if docText == '' and not forceNoDoc then
 
docText = nil
 
docText = nil
 
noDoc = 1
 
noDoc = 1
Строка 164: Строка 181:
 
action = 'создать'
 
action = 'создать'
 
preload = '&preload=Шаблон:Документация/Предзагрузка'
 
preload = '&preload=Шаблон:Документация/Предзагрузка'
  +
colour = 'F9EAEA'
  +
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
 
colour = 'F9EAEA'
 
colour = 'F9EAEA'
 
message = "'''У " .. typeForms.gen .. " нет документации. " ..
 
message = "'''У " .. typeForms.gen .. " нет документации. " ..

Версия от 01:43, 7 июня 2020

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

Использует стили со страницы 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 background_color = "#EAF4F9"
	if badDoc then
		background_color = "#F9F2EA"
	elseif forceNoDoc then
		background_color = "#F9EAEA"
	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' )
	body
		:css{
			['margin-bottom'] = '0.8em',
			padding = '0.8em 1em 0.7em',
			['background-color'] = background_color,
			border = '1px solid #AAA'
		}
		: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 (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 = 'править'
	local preload = ''
	local colour = 'EAF4F9'
	local message
	local category
	if noDoc then
		action = 'создать'
		preload = '&preload=Шаблон:Документация/Предзагрузка'
		colour = 'F9EAEA'
		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
		colour = 'F9EAEA'
		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
		colour = 'F9F2EA'
		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 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' )
	body:css{
		['background-color'] = '#' .. colour,
		border = '1px solid #AAA',
		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',
			['background-color'] = '#EAF4F9',
			['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',
					['background-color'] = '#EAF4F9',
					['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