Difference between revisions of "Module:Documentation"

Jump to navigation Jump to search
finish splitting the _startBox function into smaller functions
en>Mr. Stradivarius
(add functions for rendering start box links and for making the start box link data)
en>Mr. Stradivarius
(finish splitting the _startBox function into smaller functions)
Line 302: Line 302:


function p._startBox(args, env)
function p._startBox(args, env)
local title = env.title
-- Generate [view][edit][history][purge] or [create] links.
local subjectSpace = env.subjectSpace
local links
 
-- Arg processing from {{documentation}}.
local preload = args[message('preloadArg', 'string')] -- Allow custom preloads.
local heading = args[message('headingArg', 'string')] -- Blank values are not removed.
local headingStyle = args[message('headingStyleArg', 'string')]
local content = args[message('contentArg', 'string')]
local content = args[message('contentArg', 'string')]
local docspace = env.docspace
if not content then
local docname = args[1] -- Other docname, if fed.
-- No need to include the links if the documentation is on the template page itself.
local templatePage = env.templatePage
local linksData = p.makeStartBoxLinksData(args, env)
 
links = p.renderStartBoxLinks(linksData)
-- Arg processing from {{documentation/start box2}}.
end
local docpage
-- Generate the start box html.
if docname then
local data = p.makeStartBoxData(args, env, links)
docpage = docname
if type(data) == 'table' then
return p.renderStartBox(data)
elseif type(data) == 'string' then
-- data is an error message.
return data
else
else
local namespace = docspace or title.nsText
-- User specified no heading.
local pagename = templatePage or title.text
docpage = namespace .. ':' .. pagename .. '/' .. message('docSubpage', 'string')
end
local docTitle = mw.title.new(docpage)
local docExist = docTitle.exists
-- Output from {{documentation/start box}}.
 
-- First, check the heading parameter.
if heading == '' then
-- Heading is defined but blank, so do nothing.
return nil
return nil
end
end
-- Build the start box div.
local sbox = htmlBuilder.create('div')
sbox
.css('padding-bottom', '3px')
.css('border-bottom', '1px solid #aaa')
.css('margin-bottom', '1ex')
.newline()
-- Make the heading.
local hspan = sbox.tag('span')
if headingStyle then
hspan.cssText(headingStyle)
elseif subjectSpace == 10 then
-- We are in the template or template talk namespaces.
hspan
.css('font-weight', 'bold')
.css('font-size', '125%')
else
hspan.css('font-size', '150%')
end
if heading then
-- "heading" has data.
hspan.wikitext(heading)
elseif subjectSpace == 10 then -- Template namespace
hspan.wikitext(message('documentationIconWikitext', 'string') .. ' ' .. message('templateNamespaceHeading', 'string'))
elseif subjectSpace == 828 then -- Module namespace
hspan.wikitext(message('documentationIconWikitext', 'string') .. ' ' .. message('moduleNamespaceHeading', 'string'))
elseif subjectSpace == 6 then -- File namespace
hspan.wikitext(message('fileNamespaceHeading', 'string'))
else
hspan.wikitext(message('otherNamespacesHeading', 'string'))
end
-- Add the [view][edit][history][purge] or [create] links.
-- Check for the content parameter first, as we don't need the links if the documentation
-- content is being entered directly onto the template page.
if not content then
local lspan = sbox.tag('span') -- lspan is short for "link span".
lspan
.addClass(message('startBoxLinkclasses', 'string'))
.attr('id', message('startBoxLinkId', 'string'))
if docExist then
local viewLink = makeWikilink(docpage, message('viewLinkDisplay', 'string'))
local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, message('editLinkDisplay', 'string'))
local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, message('historyLinkDisplay', 'string'))
local purgeLink = makeUrlLink(title:fullUrl{action = 'purge'}, message('purgeLinkDisplay', 'string'))
local text = '[%s] [%s] [%s] [%s]'
text = text:gsub('%[', '[') -- Replace square brackets with HTML entities.
text = text:gsub('%]', ']')
lspan.wikitext(mw.ustring.format(text, viewLink, editLink, historyLink, purgeLink))
else
if not preload then
if subjectSpace == 6 then -- File namespace
preload = message('fileDocpagePreload', 'string')
else
preload = message('docpagePreload', 'string')
end
end
lspan.wikitext(makeUrlLink(docTitle:fullUrl{action = 'edit', preload = preload}, message('createLinkDisplay', 'string')))
end
end
return tostring(sbox)
end
end


Line 448: Line 372:
ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink)
ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink)
else
else
lspan.wikitext(makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay))
ret = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay)
end
end
 
function p.makeStartBoxData(args, env, links)
local subjectSpace = env.subjectSpace
local data = {}
-- Heading
local heading = args[message('headingArg', 'string')] -- Blank values are not removed.
if heading == '' then
-- Don't display the start box if the heading arg is defined but blank.
return nil
end
end
if heading then
data.heading = heading
elseif subjectSpace == 10 then -- Template namespace
data.heading = message('documentationIconWikitext', 'string') .. ' ' .. message('templateNamespaceHeading', 'string')
elseif subjectSpace == 828 then -- Module namespace
data.heading = message('documentationIconWikitext', 'string') .. ' ' .. message('moduleNamespaceHeading', 'string')
elseif subjectSpace == 6 then -- File namespace
data.heading = message('fileNamespaceHeading', 'string')
else
data.heading = message('otherNamespacesHeading', 'string')
end
-- Heading CSS
local headingStyle = args[message('headingStyleArg', 'string')]
if headingStyle then
data.headingStyleText = headingStyle
elseif subjectSpace == 10 then
-- We are in the template or template talk namespaces.
data.headingFontWeight = 'bold'
data.headingFontSize = '125%'
else
data.headingFontSize = '150%'
end
-- [view][edit][history][purge] or [create] links.
if links then
data.linksClass = message('startBoxLinkclasses', 'string')
data.linksId = message('startBoxLinkId', 'string')
data.links = links
end
return data
end
end


Line 465: Line 433:
.css('font-size', data.headingFontSize)
.css('font-size', data.headingFontSize)
.wikitext(data.heading)
.wikitext(data.heading)
if data.showLinks then
local links = data.links
if links then
sbox.tag('span')
sbox.tag('span')
.addClass(data.linksClass)
.addClass(data.linksClass)
.attr('id', data.linksId)
.attr('id', data.linksId)
.wikitext(data.links)
.wikitext(links)
end
end
return tostring(sbox)
return tostring(sbox)
Anonymous user

Navigation menu