Module:Documentation: Difference between revisions

mNo edit summary
m 1 revision imported
Tags: Mobile edit Mobile web edit
 
(19 intermediate revisions by 6 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


local static = require( 'Module:Static' )
-- Load modules (language wikis exclusive)
-- ...


-- Customizable strings
local i18n = {
local i18n = {
-- default settings, change when necessary
-- default settings, change when necessary
Line 16: Line 18:
commonExternalLink = '[%s]',
commonExternalLink = '[%s]',
commonExternalLinkWithName = '[%s %s]',
commonExternalLinkWithName = '[%s %s]',
commonNamespacedPage = '%s:%s',
commonNamespacedPageWithSub = '%s:%s/%s',


-- format strings, translate if your language prefers localized namespace name, although remain it untouched most likely not affect anything
-- namespace names, translate if your language prefers localized namespace name, although remain it untouched most likely not affect anything
commonCategoryPage = 'Category:%s',
namespaceCategory = 'Category',
commonSpecialPageSub = 'Special:%s/%s',
namespaceSpecial = 'Special',
commonUser = 'User',
namespaceUser = 'User',


-- names of special pages, translate if your language prefers localized namespace name, although remain it untouched will still correctly linked to target page
-- names of special pages, translate if your language prefers localized namespace name, although remain it untouched will still correctly linked to target page
Line 28: Line 32:


-- translate following types if your language displays differ
-- translate following types if your language displays differ
pageType_page = 'page',
pageType_template = 'template',
pageType_template = 'template',
pageType_module = 'module',
pageType_module = 'module',
pageType_widget = 'widget',
pageType_stylesheet = 'stylesheet',
pageType_stylesheet = 'stylesheet',
pageType_script = 'script',
pageType_script = 'script',
pageType_json = 'JSON',
pageType_message = 'message',
pageType_message = 'message',


Line 41: Line 46:


-- name of different type of links, change them if necessary
-- name of different type of links, change them if necessary
linkTextPurge = mw.getCurrentFrame():callParserFunction( 'int:smw_purge' ):lower(),
linkTextPurge = mw.message.new( 'smw_purge' ):plain():lower(),
linkTextView = mw.getCurrentFrame():callParserFunction( 'int:view' ):lower(),
linkTextView = mw.message.new( 'view' ):plain():lower(),
linkTextEdit = mw.getCurrentFrame():callParserFunction( 'int:edit' ):lower(),
linkTextEdit = mw.message.new( 'edit' ):plain():lower(),
linkTextHistory = mw.getCurrentFrame():callParserFunction( 'int:history_short' ):lower(),
linkTextHistory = mw.message.new( 'history_short' ):plain():lower(),
linkTextCreate = mw.getCurrentFrame():callParserFunction( 'int:create' ):lower(),
linkTextCreate = mw.message.new( 'create' ):plain():lower(),


-- strings used in p.create(): contents shown when using {{docc}} or {{subst:docc}}
-- strings used in p.create(): contents shown when using {{docc}} or {{subst:docc}}
createOutputFormat = '%s%s', -- overall format
createOutputFormat = '%s%s', -- overall format
createSplitDocPagePrompt = '\n<!-- Put categories/interwikis on the documentation page -->', -- this string will shown when a separate documentation page will be created
createSplitDocPagePrompt = '\n<!-- Put categories/interwikis on the documentation page -->', -- this string is shown when a separate documentation page is created
createNoSubstCategory = 'Pages with templates requiring substitution', -- tracking category for using {{docc}} without substitution
createNoSubstCategory = 'Pages with templates requiring substitution', -- tracking category for using {{docc}} without substitution


-- strings used in p.docPage(): contents shown in documentation page
-- strings used in p.docPage(): contents shown in documentation page
docPagePrompt = 'This is the documentation page. It %s be transcluded into %s. See [[Template:Documentation]] for more information.', -- message shown as documentation header in documentation pages. Params: word used when page is a module or not; code page's type
docPagePrompt = 'This is the documentation page. It %s transcluded into %s. See [[Template:Documentation]] for more information.', -- message shown as documentation header in documentation pages. Params: word used when page is a module or not; code page's type
docPagePromptWill = 'will', -- word used when code page is a module page
docPagePromptWill = 'is', -- word used when code page is a module page
docPagePromptShould = 'should', -- word used when code is not a module page
docPagePromptShould = 'should be', -- word used when code is not a module page
docPageBadDocPrompt = "<br>'''This %s's documentation needs improving or additional information.'''", -- additional message if a documentation page marked as baddoc
docPageBadDocPrompt = "<br>'''This %s's documentation needs improving or additional information.'''", -- additional message if a documentation page marked as baddoc
docPageCategory = 'Documentation pages', -- tracking category for documentation pages
docPageCategory = 'Documentation pages', -- tracking category for documentation pages
Line 71: Line 76:
}
}


local function loadStyles( stylesheet )
-- Customizable functions
if not stylesheet or mw.text.trim( stylesheet ) == '' then
local function pageCategoryHandler( category )
return ''
return mw.getCurrentFrame():expandTemplate{ title = 'translation category', args = { category, project = '0' } }
end
end
return mw.getCurrentFrame():extensionTag{ name = "templatestyles", args = { src = stylesheet } }
 
-- Load modules
local loadStyles = require( 'Module:TSLoader' ).call
 
local static = require( 'Module:Static' )
if not static.Documentation then
static.Documentation = {}
end
end


-- Internal functions
local function getType( namespace, page )
local function getType( namespace, page )
local pageType = 'template'
local pageType = 'page'
 
if namespace == 'Template' then
if namespace == 'Module' then
pageType = 'template'
elseif namespace == 'Module' then
pageType = 'module'
pageType = 'module'
elseif namespace == 'Widget' then
pageType = 'widget'
elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.css$' ) then
elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.css$' ) then
pageType = 'stylesheet'
pageType = 'stylesheet'
elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.js$' ) then
elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.js$' ) then
pageType = 'script'
pageType = 'script'
elseif page.fullText:gsub( '/' .. i18n.defaultDocPage .. '$', '' ):find( '%.json$' ) then
pageType = 'json'
elseif namespace == 'MediaWiki' then
elseif namespace == 'MediaWiki' then
pageType = 'message'
pageType = 'message'
end
end
return pageType
end


return pageType
local function getDisplayType( pageType )
return i18n[ 'pageType_' .. pageType ] or i18n.pageType_page
end
end


-- Creating a documentation page or transclusion through {{subst:docc}}
-- Exported functions
function p.create( f )
function p.create( f ) -- Creating a documentation page or transclusion through {{subst:docc}}
local args = require( 'Module:ProcessArgs' ).norm()
local args = require( 'Module:ProcessArgs' ).norm()
local page = mw.title.getCurrentTitle()
local page = mw.title.getCurrentTitle()
local docPage = args.page or page.nsText .. ':' .. page.baseText .. '/' .. i18n.defaultDocPage
local docPage = args.page or i18n.commonNamespacedPageWithSub:format( page.nsText, page.baseText, i18n.defaultDocPage )


local out
local out
if not args.content and tostring( page ) == docPage then
if not args.content and tostring( page ) == docPage then
local pageType = mw.ustring.lower( args.type or getType( page.nsText, page ) )
local pageType = mw.ustring.lower( args.type or getType( page.nsText, page ) )
local pageTypeDisplay = i18n[ 'pageType_' .. pageType ] or i18n.pageTypeTemplate
local pageTypeDisplay = getDisplayType( pageType )
out = f:preprocess( mw.title.new( i18n.defaultPreload ):getContent():gsub( '$1' , pageTypeDisplay ) )
out = f:preprocess( mw.title.new( i18n.defaultPreload ):getContent():gsub( '$1' , pageTypeDisplay ) )
else
else
Line 124: Line 140:
out = f:preprocess( out )
out = f:preprocess( out )
if not args.nocat then
if not args.nocat then
out = out .. i18n.commonInternalLink:format( i18n.commonCategoryPage:format( i18n.createNoSubstCategory ) )
out = out .. i18n.commonInternalLink:format( i18n.commonNamespacedPage:format( i18n.namespaceCategory, i18n.createNoSubstCategory ) )
end
end
end
end
Line 131: Line 147:
end
end


-- Header on the documentation page
function p.docPage( f ) -- Header on the documentation page
function p.docPage( f )
local args = require( 'Module:ProcessArgs' ).merge( true )
local args = require( 'Module:ProcessArgs' ).merge( true )
local badDoc = args.baddoc
local badDoc = args.baddoc


mw.logObject( badDoc, 'Module:Documentation: badDoc' )
if badDoc then
if badDoc then
static.badDoc = '1'
static.Documentation.badDoc = '1'
end
end


Line 144: Line 158:


local subpage = page.subpageText
local subpage = page.subpageText
if subpage == i18n.defaultSandboxPage or i18n.defaultTestCasePage then
if subpage == i18n.defaultSandboxPage or subpage == i18n.defaultTestCasePage then
page = page.basePageTitle
page = page.basePageTitle
end
end


local docPage = mw.title.new( args.page or page.nsText .. ':' .. page.baseText .. '/' .. i18n.defaultDocPage )
local docPage = mw.title.new( args.page or i18n.commonNamespacedPageWithSub:format( page.nsText, page.baseText, i18n.defaultDocPage ) )
mw.logObject( docPage, 'Module:Documentation: docPage' )
mw.logObject( page, 'Module:Documentation: page' )
if docPage ~= page then return end
if docPage ~= page then return end


local namespace = page.nsText
local namespace = page.nsText
local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
local pageTypeDisplay = i18n[ 'pageType_' .. pageType ] or i18n.pageTypeTemplate
local pageTypeDisplay = getDisplayType( pageType )


local body = mw.html.create( 'div' ):addClass( 'documentation' )
local body = mw.html.create( 'div' ):addClass( 'documentation' )
body
body
:addClass( badDoc and 'documentation-badDoc' or '' )
:addClass( badDoc and 'documentation-badDoc' or '' )
:tag( 'div' )
:tag( 'div' ):addClass( 'documentation-header-tools' )
:attr( 'id', 'documentation-header-tools' )
:wikitext( i18n.linkBar:format( i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialPurge, page.fullText ), i18n.linkTextPurge ) ) ) )
:wikitext( i18n.linkBar:format( i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonSpecialPageSub:format( i18n.specialPurge, page.fullText ), i18n.linkTextPurge ) ) ) )
:done()
:done()
:wikitext( i18n.docPagePrompt:format( pageType == 'module' and i18n.docPagePromptWill or i18n.docPagePromptShould, i18n.commonInternalLink:format( namespace .. ':' .. page.baseText ) ) )
:wikitext( i18n.docPagePrompt:format( pageType == 'module' and i18n.docPagePromptWill or i18n.docPagePromptShould, i18n.commonInternalLink:format( i18n.commonNamespacedPage:format( namespace, page.baseText ) ) ) )
if badDoc then
if badDoc then
body:wikitext( i18n.docPageBadDocPrompt:format( pageTypeDisplay ) )
body:wikitext( i18n.docPageBadDocPrompt:format( pageTypeDisplay ) )
end
end
if not ( args.nocat or namespace == i18n.commonUser ) then
if not ( args.nocat or namespace == i18n.namespaceUser ) then
body:wikitext( i18n.commonInternalLink:format( i18n.commonCategoryPage:format( i18n.docPageCategory ) ) )
body:wikitext( i18n.commonInternalLink:format( i18n.commonNamespacedPage:format( i18n.namespaceCategory, i18n.docPageCategory ) ) )
end
end
mw.logObject( body, 'Module:Documentation: body' )


return loadStyles( i18n.defaultStyles ) .. tostring( body )
return loadStyles( i18n.defaultStyles ) .. tostring( body )
end
end


-- Wrapper around the documentation on the main page
function p.page( f ) -- 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
-- mw.text.trim uses mw.ustring.gsub, which silently fails on large strings
local function trim( s )
local function trim( s )
Line 187: Line 195:
local page = mw.title.getCurrentTitle()
local page = mw.title.getCurrentTitle()
local subpage = page.subpageText
local subpage = page.subpageText
if subpage == i18n.defaultSandboxPage or i18n.defaultTestCasePage then
if subpage == i18n.defaultSandboxPage or subpage == i18n.defaultTestCasePage then
page = page.basePageTitle
page = page.basePageTitle
end
end
Line 199: Line 207:
docPage = page
docPage = page
else
else
docPage = mw.title.new( args.page or namespace .. ':' .. page.text .. '/' .. i18n.defaultDocPage )
docPage = mw.title.new( args.page or i18n.commonNamespacedPageWithSub:format( namespace, page.text, i18n.defaultDocPage ) )
noDoc = args.nodoc or not docPage.exists
noDoc = args.nodoc or not docPage.exists
end
end
local badDoc = args.baddoc
local badDoc = args.baddoc
local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
local pageType = mw.ustring.lower( args.type or getType( namespace, page ) )
local pageTypeDisplay = i18n[ 'pageType_' .. pageType ] or i18n.pageTypeTemplate
local pageTypeDisplay = getDisplayType( pageType )


if not docText and not noDoc then
if not docText and not noDoc then
docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText } )
docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText } )
if static.badDoc and static.badDoc == '1' then
if static.Documentation.badDoc and static.Documentation.badDoc == '1' then
mw.logObject( static, 'static' )
badDoc = 1
badDoc = 1
end
end
Line 228: Line 235:
docClass = 'documentation-noDoc'
docClass = 'documentation-noDoc'
message = i18n.pageNoDocPrompt:format( pageTypeDisplay, pageTypeDisplay )
message = i18n.pageNoDocPrompt:format( pageTypeDisplay, pageTypeDisplay )
if not ( args.nocat or namespace == i18n.commonUser ) then
if not ( args.nocat or namespace == i18n.namespaceUser ) then
category = i18n.pageNoDocCategory:format( pageTypeDisplay )
category = i18n.pageNoDocCategory:format( pageTypeDisplay )
if not mw.title.new( i18n.commonCategoryPage:format( category ) ).exists then
if not mw.title.new( i18n.commonNamespacedPage:format( i18n.namespaceCategory, category ) ).exists then
category = i18n.pageNoDocCategoryDefault
category = i18n.pageNoDocCategoryDefault
end
end
Line 237: Line 244:
docClass = 'documentation-badDoc'
docClass = 'documentation-badDoc'
message = i18n.pageBadDocPrompt:format( pageTypeDisplay )
message = i18n.pageBadDocPrompt:format( pageTypeDisplay )
if not ( args.nocat or namespace == i18n.commonUser ) then
if not ( args.nocat or namespace == i18n.namespaceUser ) then
category = i18n.pageBadDocCategory:format( pageTypeDisplay )
category = i18n.pageBadDocCategory:format( pageTypeDisplay )
if not mw.title.new( i18n.commonCategoryPage:format( category ) ).exists then
if not mw.title.new( i18n.commonNamespacedPage:format( i18n.namespaceCategory, category ) ).exists then
category = i18n.pageBadDocCategoryDefault
category = i18n.pageBadDocCategoryDefault
end
end
Line 246: Line 253:


-- Generates the link bar
-- Generates the link bar
local links = mw.html.create( 'span' )
local links = mw.html.create( 'span' ):addClass( 'documentation-header-tools' )
:attr( 'id', 'documentation-header-tools' )


local linkList = {}
local linkList = {}
if not noDoc and page ~= docPage then
if not noDoc then
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( docPage.fullText, i18n.linkTextView ) ) )
if page ~= docPage then
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonSpecialPageSub:format( i18n.specialEdit, docPage.fullText ), i18n.linkTextEdit ) ) )
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( docPage.fullText, i18n.linkTextView ) ) )
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonSpecialPageSub:format( i18n.specialHistory, docPage.fullText ), i18n.linkTextHistory ) ) )
end
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialEdit, docPage.fullText ), i18n.linkTextEdit ) ) )
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialHistory, docPage.fullText ), i18n.linkTextHistory ) ) )
else
else
table.insert( linkList, i18n.linkFormat:format( i18n.commonExternalLinkWithName:format( docPage:canonicalUrl{ action = 'edit', preload = i18n.defaultPreload, preloadparams = pageTypeDisplay }, i18n.linkTextCreate ) ) )
table.insert( linkList, i18n.linkFormat:format( i18n.commonExternalLinkWithName:format( docPage:canonicalUrl{ action = 'edit', preload = i18n.defaultPreload, preloadparams = pageTypeDisplay }, i18n.linkTextCreate ) ) )
end
end
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonSpecialPageSub:format( i18n.specialPurge, docPage.fullText ), i18n.linkTextPurge ) ) )
table.insert( linkList, i18n.linkFormat:format( i18n.commonInternalLinkPipe:format( i18n.commonNamespacedPageWithSub:format( i18n.namespaceSpecial, i18n.specialPurge, docPage.fullText ), i18n.linkTextPurge ) ) )
links:wikitext( i18n.linkBar:format( table.concat( linkList, i18n.linkSeparator ) ) )
links:wikitext( i18n.linkBar:format( table.concat( linkList, i18n.linkSeparator ) ) )


Line 268: Line 276:


header
header
:node( links )
:tag( 'span' )
:tag( 'span' )
:addClass( 'documentation-header-title' )
:addClass( 'documentation-header-title' )
:wikitext( i18n.pageDocHeaderTitle )
:wikitext( i18n.pageDocHeaderTitle )
header
        :node( links )


local codePages = {
local codePages = {
Line 299: Line 308:


if category then
if category then
body:wikitext( f:expandTemplate{ title = 'Translation category', args = { category, project = '0' } } )
body:wikitext( pageCategoryHandler( category ) )
end
end