More actions
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local defaultDocPage = 'doc' | |||
local getType = function( namespace, page ) | |||
local pageType = 'template' | |||
if namespace == 'Module' then | |||
local | |||
local pageType = ' | |||
if | |||
pageType = 'module' | pageType = 'module' | ||
elseif namespace == 'Widget' then | elseif namespace == 'Widget' then | ||
pageType = 'widget' | pageType = 'widget' | ||
elseif page.fullText:gsub( '/' . | elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.css$' ) then | ||
pageType = 'stylesheet' | pageType = 'stylesheet' | ||
elseif page.fullText:gsub( '/' . | elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.js$' ) then | ||
pageType = 'script' | pageType = 'script' | ||
elseif namespace == 'MediaWiki' then | elseif namespace == 'MediaWiki' then | ||
pageType = 'message' | pageType = 'message' | ||
end | end | ||
return pageType | return pageType | ||
end | end | ||
-- Creating a documentation page or transclusion through {{subst:docc}} | |||
function p.create( f ) | |||
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 | local docPage = args.page or page.nsText .. ':' .. page.baseText .. '/' .. defaultDocPage | ||
local out | local out | ||
if not args.content and tostring( page ) == docPage then | if not args.content and tostring( page ) == docPage then | ||
out = f:preprocess( '{{subst:Template:Documentation/preload}}' ) | |||
out = f:preprocess( | |||
else | else | ||
local templateArgs = {} | local templateArgs = {} | ||
for _, key in ipairs{ 'type', 'page', 'content | for _, key in ipairs{ 'type', 'page', 'content' } do | ||
local val = args[ key ] | local val = args[key] | ||
if val then | if val then | ||
if key == 'content' then val = '\n' .. val .. '\n' end | if key == 'content' then val = '\n' .. val .. '\n' end | ||
Line 131: | Line 37: | ||
end | end | ||
end | end | ||
out = '{{documentation|' .. table.concat( templateArgs, '|' ) .. '}}' | out = '{{documentation|' .. table.concat( templateArgs, '|' ) .. '}}' | ||
out = out:gsub( '|}}', '}}' ) | out = out:gsub( '|}}', '}}' ) | ||
out = | |||
if not args.content then | |||
out = out .. '\n<!-- Put categories/interwikis on the documentation page -->' | |||
end | |||
end | end | ||
if not mw.isSubsting() then | if not mw.isSubsting() then | ||
out = f:preprocess( out ) | out = f:preprocess( out ) | ||
if not args.nocat then | if not args.nocat then | ||
out = out .. | out = out .. '[[Category:Pages with templates requiring substitution]]' | ||
end | end | ||
end | end | ||
return out | return out | ||
end | end | ||
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 | ||
if f:callParserFunction( '#dplvar', '$doc noheader' ) == '1' then | |||
if | if badDoc then | ||
f:callParserFunction( '#dplvar:set', '$doc bad', '1' ) | |||
end | |||
return | |||
end | end | ||
local page = mw.title.getCurrentTitle() | local page = mw.title.getCurrentTitle() | ||
local subpage = page.subpageText | local subpage = page.subpageText | ||
if subpage == | if subpage == 'sandbox' or subpage == 'testcases' then | ||
page = page.basePageTitle | page = page.basePageTitle | ||
end | 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 body = mw.html.create( 'div' ):addClass( 'documentation-header' ) | |||
local body = mw.html.create( 'div' ):addClass( 'documentation' ) | |||
body | body | ||
:addClass( badDoc and 'documentation-badDoc' or '' ) | :addClass( badDoc and 'documentation-badDoc' or '' ) | ||
:css{ | |||
['margin-bottom'] = '0.8em', | |||
padding = '0.8em 1em 0.7em', | |||
['background-color'] = '#' .. ( badDoc and '888888' or '33333333' ), | |||
border = '1px solid #AAA' | |||
} | |||
:tag( 'div' ) | :tag( 'div' ) | ||
:attr( 'id', 'documentation-header-tools' ) | :attr( 'id', 'documentation-header-tools' ) | ||
:wikitext( | :css( 'float', 'right' ) | ||
:wikitext( '[[', page:fullUrl( 'action=purge' ), ' ' .. f:preprocess('{{lc:{{int:page-header-action-button-purge}}}}') .. ']]' ) | |||
:done() | :done() | ||
:wikitext( | :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 | if badDoc then | ||
body:wikitext( | body:wikitext( "<br>'''This ", pageType, "'s documentation needs improving or additional information.'''" ) | ||
end | end | ||
if not ( args.nocat or namespace == | if not ( args.nocat or namespace == 'User' ) then | ||
body:wikitext( | body:wikitext( '[[Category:Documentation pages]]' ) | ||
end | end | ||
return | return body | ||
end | 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 | -- mw.text.trim uses mw.ustring.gsub, which silently fails on large strings | ||
local function trim( s ) | local function trim( s ) | ||
return ( s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' ) ) | return (s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' )) | ||
--return string.gsub( s, '^[\t\r\n\f ]*(.-)[\t\r\n\f ]*$', '%1' ) | --return string.gsub( s, '^[\t\r\n\f ]*(.-)[\t\r\n\f ]*$', '%1' ) | ||
end | end | ||
Line 195: | Line 115: | ||
local page = mw.title.getCurrentTitle() | local page = mw.title.getCurrentTitle() | ||
local subpage = page.subpageText | local subpage = page.subpageText | ||
if subpage == | if subpage == 'sandbox' or subpage == 'testcases' then | ||
page = page.basePageTitle | page = page.basePageTitle | ||
end | end | ||
Line 201: | Line 121: | ||
local docText = trim( args.content or '' ) | local docText = trim( args.content or '' ) | ||
if docText == '' then docText = nil end | if docText == '' then docText = nil end | ||
local docPage | local docPage | ||
local noDoc | local noDoc | ||
Line 207: | Line 127: | ||
docPage = page | docPage = page | ||
else | else | ||
docPage = mw.title.new( args.page or | docPage = mw.title.new( args.page or namespace .. ':' .. page.text .. '/' .. 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 ) ) | ||
if not docText and not noDoc then | if not docText and not noDoc then | ||
docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText } ) | f:callParserFunction( '#dplvar:set', '$doc noheader', '1' ) | ||
if | docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText } ) | ||
if f:callParserFunction( '#dplvar', '$doc bad' ) == '1' then | |||
badDoc = 1 | badDoc = 1 | ||
end | end | ||
if docText == '' then | if docText == '' then | ||
docText = nil | docText = nil | ||
Line 228: | Line 148: | ||
docText = '\n' .. docText .. '\n' | docText = '\n' .. docText .. '\n' | ||
end | end | ||
local action = f:preprocess('{{lc:{{int:edit}}}}') | |||
local preload = '' | |||
local docClass = '' | local docClass = '' | ||
local colour = '00000000' | |||
local message | local message | ||
local category | local category | ||
if noDoc then | if noDoc then | ||
action = f:preprocess('{{lc:{{int:create}}}}') | |||
preload = '&preload=Template:Documentation/preload' | |||
docClass = 'documentation-noDoc' | docClass = 'documentation-noDoc' | ||
message = | colour = '33333333' | ||
if not ( args.nocat or namespace == | message = "'''This " .. pageType .. " has no documentation. " .. | ||
category = | "If you know how to use this " .. pageType .. ", please create it.'''" | ||
if not mw.title.new( | if not ( args.nocat or namespace == 'User' ) then | ||
category = | category = pageType .. 's with no documentation' | ||
if not mw.title.new( 'Category:' .. category ).exists then | |||
category = 'Pages with no documentation' | |||
end | end | ||
end | end | ||
elseif badDoc then | elseif badDoc then | ||
docClass = 'documentation-badDoc' | docClass = 'documentation-badDoc' | ||
message = | colour = 'F9F2EA' | ||
if not ( args.nocat or namespace == | message = "'''This " .. pageType .. "'s documentation needs improving or additional information.'''\n" | ||
category = | if not ( args.nocat or namespace == 'User' ) then | ||
if not mw.title.new( | category = pageType .. 's with bad documentation' | ||
category = | if not mw.title.new( 'Category:' .. category ).exists then | ||
category = 'Pages with bad documentation' | |||
end | end | ||
end | end | ||
end | end | ||
-- | local links = { | ||
'[' .. docPage:fullUrl( 'action=edit' .. preload ) .. ' ' .. action .. ']', | |||
'[' .. docPage:fullUrl( 'action=history' ) .. ' ' .. f:preprocess('{{lc:{{int:history short}}}}') .. ']', | |||
'[' .. page:fullUrl( 'action=purge' ) .. ' ' .. f:preprocess('{{lc:{{int:page-header-action-button-purge}}}}') .. ']' | |||
} | |||
if not noDoc and page ~= docPage then | |||
table.insert( links, 1, '[[' .. docPage.fullText .. '|' .. f:preprocess('{{lc:{{int:view}}}}') .. ']]' ) | |||
end | |||
links = mw.html.create( 'span' ) | |||
:attr( 'id', 'documentation-header-tools' ) | :attr( 'id', 'documentation-header-tools' ) | ||
:css( 'float', 'right' ) | |||
:wikitext( mw.text.nowiki( '[' ), table.concat( links, ' | ' ), mw.text.nowiki( ']' ) ) | |||
local body = mw.html.create( 'div' ):addClass( 'documentation' ) | local body = mw.html.create( 'div' ):addClass( 'documentation' ) | ||
body | body | ||
:addClass( docClass ) | :addClass( docClass ) | ||
: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' ) | local header = mw.html.create( 'div' ) | ||
:addClass( 'documentation-header-top' ) | :addClass( 'documentation-header-top' ) | ||
:css{ | |||
margin = '-0.8em -1em 0.8em', | |||
padding = '0.8em 1em 0.7em', | |||
['background-color'] = '#33333333', | |||
['border-bottom'] = 'inherit' | |||
} | |||
header | header | ||
:node( links ) | :node( links ) | ||
:tag( 'span' ) | :tag( 'span' ) | ||
: | :attr( 'id', 'documentation-header-title' ) | ||
:wikitext( | :css{ | ||
['font-weight'] = 'bold', | |||
['font-size'] = '130%', | |||
['margin-right'] = '1em', | |||
['line-height'] = '1' | |||
} | |||
:wikitext( 'Documentation' ) | |||
local codePages = { | local codePages = { | ||
module = true, | module = true, | ||
Line 287: | Line 230: | ||
script = true, | script = true, | ||
} | } | ||
if not noDoc and codePages[ pageType ] then | if not noDoc and codePages[pageType] then | ||
header | header | ||
:tag( 'span' ) | :tag( 'span' ) | ||
:attr( 'id', 'documentation-jump-to-code' ) | :attr( 'id', 'documentation-jump-to-code' ) | ||
: | :css( 'white-space', 'nowrap' ) | ||
:wikitext( '[[#the-code|Jump to code ↴]]' ) | |||
end | end | ||
body | body | ||
:node( header ):done() | :node( header ):done() | ||
:wikitext( message ) | :wikitext( message ) | ||
:wikitext( docText ) | :wikitext( docText ) | ||
if not noDoc and page ~= docPage then | if not noDoc and page ~= docPage then | ||
body | body | ||
:tag( 'div' ) | :tag( 'div' ) | ||
:addClass( 'documentation-header-bottom' ) | :addClass( 'documentation-header-bottom' ) | ||
:css{ | |||
margin = '0.7em -1em -0.7em', | |||
['background-color'] = '#33333333', | |||
['border-top'] = 'inherit', | |||
padding = '0.8em 1em 0.7em', | |||
clear = 'both' | |||
} | |||
:node( links ) | :node( links ) | ||
:wikitext( | :wikitext( 'The above documentation is transcluded from [[', docPage.fullText, ']].' ) | ||
end | end | ||
local anchor = '' | local anchor = '' | ||
if not noDoc and pageType ~= 'template' and pageType ~= 'message' then | if not noDoc and pageType ~= 'template' and pageType ~= 'message' then | ||
anchor = mw.html.create( 'div' ):attr( 'id', 'the-code' ) | anchor = mw.html.create( 'div' ):attr( 'id', 'the-code' ) | ||
end | end | ||
return | return tostring( body ) .. tostring( anchor ) | ||
end | end | ||
return p | return p |