Открыть меню
Открыть персональное меню
Вы не представились системе
Your IP address will be publicly visible if you make any edits.

Модуль:Documentation: различия между версиями

Материал из Create Wiki
creng>T3sT3ro
мНет описания правки
м (1 версия импортирована)
 
(не показано 8 промежуточных версий 4 участников)
Строка 1: Строка 1:
-- <nowiki>
local dependencyList = require( 'Module:DependencyList' )
local hatnote = require( 'Module:Hatnote' )._hatnote
local mbox = require( 'Module:Mbox' )._mbox
local TNT = require( 'Module:Translate' ):new()
local lang = mw.getContentLanguage()
local p = {}
local p = {}
local defaultDocPage = 'doc'


local getType = function( namespace, page )
 
local pageType = 'template'
--- FIXME: This should go to somewhere else, like Module:Common
if namespace == 'Module' then
--- Calls TNT with the given key
pageType = 'module'
---
elseif namespace == 'Widget' then
--- @param key string The translation key
pageType = 'widget'
--- @return string If the key was not found in the .tab page, the key is returned
elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.css$' ) then
local function translate( key, ... )
pageType = 'stylesheet'
local success, translation = pcall( TNT.format, 'Module:Documentation/i18n.json', key or '', ... )
elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.js$' ) then
 
pageType = 'script'
if not success or translation == nil then
elseif namespace == 'MediaWiki' then
return key
pageType = 'message'
end
end
 
return pageType
return translation
end
end


-- Creating a documentation page or transclution through {{subst:doc}}
 
function p.create( f )
function p.doc( frame )
local args = require( 'Module:ProcessArgs' ).norm()
    local title = mw.title.getCurrentTitle()
local page = mw.title.getCurrentTitle()
    local args = frame:getParent().args
local docPage = args.page or page.nsText .. ':' .. page.baseText .. '/' .. defaultDocPage
    local page = args[1] or mw.ustring.gsub( title.fullText, '/[Dd]o[ck]u?$', '' )
    local ret, cats, ret1, ret2, ret3
local out
    local pageType = title.namespace == 828 and translate( 'module' ) or translate( 'template' )
if not args.content and tostring( page ) == docPage then
 
out = f:preprocess( '{{subst:Template:Documentation/preload}}' )
    -- subpage header
else
    if title.subpageText == 'doc' then
local templateArgs = {}
ret = mbox(
for _, key in ipairs{ 'type', 'page', 'content' } do
translate( 'message_subpage_title', page ),
local val = args[key]
translate( 'message_subpage_desc', page, pageType ),
if val then
{ icon = 'WikimediaUI-Notice.svg' }
if key == 'content' then val = '\n' .. val .. '\n' end
    )
table.insert( templateArgs, key .. '=' .. val )
 
        if title.namespace == 10 then -- Template namespace
            cats = '[[Category:' .. translate( 'category_template_documentation' ) .. '|' .. title.baseText .. ']]'
            ret2 = dependencyList._main()
        elseif title.namespace == 828 then -- Module namespace
            cats = '[[Category:' .. translate( 'category_module_documentation' ) .. '|' .. title.baseText .. ']]'
            ret2 = dependencyList._main()
            ret2 = ret2 .. require('Module:Module toc').main()
        else
            cats = ''
            ret2 = ''
        end
 
        return tostring( ret ) .. ret2 .. cats
    end
 
    -- template header
    -- don't use mw.html as we aren't closing the main div tag
    ret1 = '<div class="documentation">'
 
    ret2 = mw.html.create( nil )
        :tag( 'div' )
            :addClass( 'documentation-header' )
            :tag( 'span' )
                :addClass( 'documentation-title' )
                :wikitext( lang:ucfirst( translate('message_documentation_title', pageType ) ) )
                :done()
            :tag( 'span' )
                :addClass( 'documentation-links plainlinks' )
                :wikitext(
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='view'} ) ) .. ' view]]' ..
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='edit'} ) ) .. ' edit]]' ..
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='history'} ) ) .. ' history]]' ..
                    '[<span class="jsPurgeLink">[' .. tostring( mw.uri.fullUrl( title.fullText, { action = 'purge' } ) ) .. ' purge]</span>]'
                )
                :done()
            :done()
        :tag( 'div' )
            :addClass( 'documentation-subheader' )
            :tag( 'span' )
                :addClass( 'documentation-documentation' )
                :wikitext( translate( 'message_transclude_desc', page ) )
                :done()
            :wikitext( frame:extensionTag{ name = 'templatestyles', args = { src = 'Module:Documentation/styles.css'} } )
            :done()
 
    ret3 = {}
 
    if args.scwShared then
    --- Message box
    table.insert( ret3,
    mbox(
    translate(
'message_shared_across',
title.fullText,
mw.uri.encode( title.rootText, 'PATH' )
),
translate(
'message_shared_across_subtext',
pageType
),
{ icon = 'WikimediaUI-ArticleDisambiguation-ltr.svg' }
)
  )
  --- Set category
  table.insert( ret3, '[[Category:' .. translate( 'category_shared_across', lang:ucfirst( pageType ) ) .. ']]' )
--- Interlanguage link
--- TODO: Make this into a for loop when there are more wikis
for _, code in pairs{ 'de', 'en' } do
if lang:getCode() ~= code then
    table.insert( ret3, string.format( '[[%s:%s]]', code, title.fullText ) )
end
end
end
end
    end
out = '{{documentation|' .. table.concat( templateArgs, '|' ) .. '}}'
 
out = out:gsub( '|}}', '}}' )
    if args.fromWikipedia then
    table.insert( ret3,
if not args.content then
    mbox(
out = out .. '\n<!-- Put categories/interwiki on the documentation page -->'
    translate(
'message_from_wikipedia',
title.fullText,
mw.uri.encode( page, 'WIKI' ),
page
),
translate(
'message_from_wikipedia_subtext',
pageType
),
{ icon = 'WikimediaUI-Logo-Wikipedia.svg' }
)
  )
  --- Set category
  table.insert( ret3, '[[Category:' .. translate( 'category_from_wikipedia', lang:ucfirst( pageType ) ) .. ']]' )
    end
 
    if title.namespace == 828 then
    -- Has config
    if mw.title.new( title.fullText .. '/config.json', 'Module' ).exists then
table.insert( ret3,
mbox(
    translate(
    'message_module_configuration',
    title.fullText,
    title.fullText
    ),
    translate( 'message_module_configuration_subtext' ),
    { icon = 'WikimediaUI-Settings.svg' }
    )
)
    end
 
    -- Has localization
    if mw.title.new( title.fullText .. '/i18n.json', 'Module' ).exists then
table.insert( ret3,
mbox(
    translate(
    'message_module_i18n',
    title.fullText,
    title.fullText
    ),
    translate( 'message_module_i18n_subtext' ),
    { icon = 'WikimediaUI-Language.svg' }
    )
)
    end
 
    -- Testcase page
    if title.subpageText == 'testcases' then
    table.insert( ret3,
    hatnote(
    translate( 'message_module_tests', title.baseText ),
    { icon = 'WikimediaUI-LabFlask.svg' }
    )
    )
end
end
end
if not mw.isSubsting() then
out = f:preprocess( out )
if not args.nocat then
out = out .. '[[Category:Pages with templates requiring substitution]]'
end
end
return out
end


-- Header on the documentation page
table.insert( ret3, string.format( '[[Category:%s]]', translate( 'category_module' ) ) )
function p.docPage( f )
    end
local args = require( 'Module:ProcessArgs' ).merge( true )
 
local badDoc = args.baddoc
    --- Dependency list
if f:callParserFunction( '#dplvar', '$doc noheader' ) == '1' then
    table.insert( ret3, dependencyList._main( nil, args.category, args.isUsed ) )
if badDoc then
 
f:callParserFunction( '#dplvar:set', '$doc bad', '1' )
    -- Has templatestyles
end
if mw.title.new( title.fullText .. '/styles.css' ).exists then
return
table.insert( ret3,
end
hatnote(
    translate( 'message_styles', title.fullText, title.fullText ),
local page = mw.title.getCurrentTitle()
    { icon = 'WikimediaUI-Palette.svg' }
local namespace = page.nsText
    )
local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
local body = mw.html.create( 'div' ):addClass( 'documentation-header' )
body
:css{
['margin-bottom'] = '0.8em',
padding = '0.8em 1em 0.7em',
['background-color'] = '#' .. ( badDoc and 'F9F2EA' or 'f9f5ea' ),
border = '1px solid #AAA'
}
:tag( 'div' )
:css( 'float', 'right' )
:wikitext( '[[', page:fullUrl( 'action=purge' ), ' purge]]' )
:done()
:wikitext(
'This is the documentation page. It ',
pageType == 'module' and 'will' or 'should',
' be transcluded into the main ', pageType, ' page. ',
'See [[Template:Documentation]] for more information'
)
)
if badDoc then
body:wikitext( "<br>'''This ", pageType, "'s documentation needs improving or additional information.'''" )
end
end
if not ( args.nocat or namespace == 'User' ) then
body:wikitext( '[[Category:Documentation pages]]' )
end
return body
end


-- Wrapper around the documentation on the main page
    --- Module stats bar
function p.page( f )
    if title.namespace == 828 then
-- mw.text.trim uses mw.ustring.gsub, which silently fails on large strings
table.insert( ret3, '<div class="documentation-modulestats">' )
local function trim( s )
 
return (s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' ))
-- Function list
--return string.gsub( s, '^[\t\r\n\f ]*(.-)[\t\r\n\f ]*$', '%1' )
table.insert( ret3, require( 'Module:Module toc' ).main() )
end
 
local args = require( 'Module:ProcessArgs' ).merge( true )
-- Unit tests
local page = mw.title.getCurrentTitle()
local testcaseTitle = title.baseText .. '/testcases'
local namespace = page.nsText
if mw.title.new( testcaseTitle, 'Module' ).exists then
local docText = trim( args.content or '' )
-- There is probably a better way :P
if docText == '' then docText = nil end
table.insert( ret3, frame:preprocess( '{{#invoke:' .. testcaseTitle .. '|run}}' ) )
    end
local docPage
 
local noDoc
    table.insert( ret3, '</div>' )
if docText then
    end
docPage = page
 
else
    return ret1 .. tostring( ret2 ) .. '<div class="documentation-content">' .. table.concat( ret3 )
docPage = mw.title.new( args.page or namespace .. ':' .. page.text .. '/' .. defaultDocPage )
noDoc = args.nodoc or not docPage.exists
end
local badDoc = args.baddoc
local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
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
if docText == '' then
docText = nil
noDoc = 1
end
end
if docText then
docText = '\n' .. docText .. '\n'
end
local action = 'edit'
local preload = ''
local message
local category
if noDoc then
action = 'create'
preload = '&preload=Template:Documentation/preload'
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
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' ) .. ' history]',
'[' .. page:fullUrl( 'action=purge' ) .. ' purge]'
}
if not noDoc and page ~= docPage then
table.insert( links, 1, '[[' .. docPage.fullText .. '|view]]' )
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{
border = '1px solid #AAA',
padding = '0.8em 1em 0.7em',
['margin-top'] = '1em',
clear = 'both'
}
local header = mw.html.create( 'div' )
: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( 'Documentation' )
local codePages = {
module = true,
stylesheet = true,
script = true,
}
if not noDoc and codePages[pageType] then
header
:tag( 'span' )
:css( 'white-space', 'nowrap' )
:wikitext( '[[#the-code|Jump to code ↴]]' )
end
body
:node( header ):done()
:wikitext( message )
:wikitext( docText )
if not noDoc and page ~= docPage then
body
:tag( 'div' )
:css{
margin = '0.7em -1em -0.7em',
['border-top'] = 'inherit',
padding = '0.8em 1em 0.7em',
clear = 'both'
}
:node( links )
:wikitext( 'The above documentation is transcluded from [[', docPage.fullText, ']].' )
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
end


return p
return p
-- </nowiki>

Текущая версия от 22:55, 29 февраля 2024

Для документации этого модуля может быть создана страница Модуль:Documentation/doc

-- <nowiki>
local dependencyList = require( 'Module:DependencyList' )
local hatnote = require( 'Module:Hatnote' )._hatnote
local mbox = require( 'Module:Mbox' )._mbox
local TNT = require( 'Module:Translate' ):new()
local lang = mw.getContentLanguage()
local p = {}


--- FIXME: This should go to somewhere else, like Module:Common
--- Calls TNT with the given key
---
--- @param key string The translation key
--- @return string If the key was not found in the .tab page, the key is returned
local function translate( key, ... )
	local success, translation = pcall( TNT.format, 'Module:Documentation/i18n.json', key or '', ... )

	if not success or translation == nil then
		return key
	end

	return translation
end


function p.doc( frame )
    local title = mw.title.getCurrentTitle()
    local args = frame:getParent().args
    local page = args[1] or mw.ustring.gsub( title.fullText, '/[Dd]o[ck]u?$', '' )
    local ret, cats, ret1, ret2, ret3
    local pageType = title.namespace == 828 and translate( 'module' ) or translate( 'template' )

    -- subpage header
    if title.subpageText == 'doc' then
		ret = mbox(
			translate( 'message_subpage_title', page ),
			translate( 'message_subpage_desc', page, pageType ),
			{ icon = 'WikimediaUI-Notice.svg' }
    	)

        if title.namespace == 10 then -- Template namespace
            cats = '[[Category:' .. translate( 'category_template_documentation' ) .. '|' .. title.baseText .. ']]'
            ret2 = dependencyList._main()
        elseif title.namespace == 828 then -- Module namespace
            cats = '[[Category:' .. translate( 'category_module_documentation' ) .. '|' .. title.baseText .. ']]'
            ret2 = dependencyList._main()
            ret2 = ret2 .. require('Module:Module toc').main()
        else
            cats = ''
            ret2 = ''
        end

        return tostring( ret ) .. ret2 .. cats
    end

    -- template header
    -- don't use mw.html as we aren't closing the main div tag
    ret1 = '<div class="documentation">'

    ret2 = mw.html.create( nil )
        :tag( 'div' )
            :addClass( 'documentation-header' )
            :tag( 'span' )
                :addClass( 'documentation-title' )
                :wikitext( lang:ucfirst( translate('message_documentation_title', pageType ) ) )
                :done()
            :tag( 'span' )
                :addClass( 'documentation-links plainlinks' )
                :wikitext(
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='view'} ) ) .. ' view]]' ..
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='edit'} ) ) .. ' edit]]' ..
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='history'} ) ) .. ' history]]' ..
                    '[<span class="jsPurgeLink">[' .. tostring( mw.uri.fullUrl( title.fullText, { action = 'purge' } ) ) .. ' purge]</span>]'
                )
                :done()
            :done()
        :tag( 'div' )
            :addClass( 'documentation-subheader' )
            :tag( 'span' )
                :addClass( 'documentation-documentation' )
                :wikitext( translate( 'message_transclude_desc', page ) )
                :done()
            :wikitext( frame:extensionTag{ name = 'templatestyles', args = { src = 'Module:Documentation/styles.css'} } )
            :done()

    ret3 = {}

    if args.scwShared then
    	--- Message box
    	table.insert( ret3,
    		mbox(
	    		translate(
					'message_shared_across',
					title.fullText,
					mw.uri.encode( title.rootText, 'PATH' )
				),
				translate(
					'message_shared_across_subtext',
					pageType
				),
				{ icon = 'WikimediaUI-ArticleDisambiguation-ltr.svg' }
			)
	   )
	   --- Set category
	   table.insert( ret3, '[[Category:' .. translate( 'category_shared_across', lang:ucfirst( pageType ) ) .. ']]' )
		--- Interlanguage link
		--- TODO: Make this into a for loop when there are more wikis
		for _, code in pairs{ 'de', 'en' } do
			if lang:getCode() ~= code then
	    		table.insert( ret3, string.format( '[[%s:%s]]', code, title.fullText ) )
			end
		end
    end

    if args.fromWikipedia then
    	table.insert( ret3,
    		mbox(
	    		translate(
					'message_from_wikipedia',
					title.fullText,
					mw.uri.encode( page, 'WIKI' ),
					page
				),
				translate(
					'message_from_wikipedia_subtext',
					pageType
				),
				{ icon = 'WikimediaUI-Logo-Wikipedia.svg' }
			)
	   )
	   --- Set category
	   table.insert( ret3, '[[Category:' .. translate( 'category_from_wikipedia', lang:ucfirst( pageType ) ) .. ']]' )
    end

    if title.namespace == 828 then
    	-- Has config
    	if mw.title.new( title.fullText .. '/config.json', 'Module' ).exists then
			table.insert( ret3,
				mbox(
		    		translate(
		    			'message_module_configuration',
		    			title.fullText,
		    			title.fullText
		    		),
		    		translate( 'message_module_configuration_subtext' ),
		    		{ icon = 'WikimediaUI-Settings.svg' }
		    	)
			)
    	end

    	-- Has localization
    	if mw.title.new( title.fullText .. '/i18n.json', 'Module' ).exists then
			table.insert( ret3,
				mbox(
		    		translate(
		    			'message_module_i18n',
		    			title.fullText,
		    			title.fullText
		    		),
		    		translate( 'message_module_i18n_subtext' ),
		    		{ icon = 'WikimediaUI-Language.svg' }
		    	)
			)
    	end

    	-- Testcase page
    	if title.subpageText == 'testcases' then
    		table.insert( ret3,
		    	hatnote(
		    		translate( 'message_module_tests', title.baseText ),
		    		{ icon = 'WikimediaUI-LabFlask.svg' }
		    	)
		    )
		end

		table.insert( ret3, string.format( '[[Category:%s]]', translate( 'category_module' ) ) )
    end

    --- Dependency list
    table.insert( ret3, dependencyList._main( nil, args.category, args.isUsed ) )

    -- Has templatestyles
	if mw.title.new( title.fullText .. '/styles.css' ).exists then
		table.insert( ret3,
			hatnote(
	    		translate( 'message_styles', title.fullText, title.fullText ),
	    		{ icon = 'WikimediaUI-Palette.svg' }
	    	)
		)
	end

    --- Module stats bar
    if title.namespace == 828 then
		table.insert( ret3, '<div class="documentation-modulestats">' )

		-- Function list
		table.insert( ret3, require( 'Module:Module toc' ).main() )

		-- Unit tests
		local testcaseTitle = title.baseText .. '/testcases'
		if mw.title.new( testcaseTitle, 'Module' ).exists then
			-- There is probably a better way :P
			table.insert( ret3, frame:preprocess( '{{#invoke:' .. testcaseTitle .. '|run}}' ) )
    	end

    	table.insert( ret3, '</div>' )
    end

    return ret1 .. tostring( ret2 ) .. '<div class="documentation-content">' .. table.concat( ret3 )
end

return p

-- </nowiki>
Сайт использует Cookie для нормальной работы