Module:Documentation: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
finish splitting the _startBox function into smaller functions
(add functions for rendering start box links and for making the start box link data)
(finish splitting the _startBox function into smaller functions)
Line 302:
 
function p._startBox(args, env)
-- Generate [view][edit][history][purge] or [create] links.
local title = env.title
local links
local subjectSpace = env.subjectSpace
 
-- 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')]
if not content then
local docspace = env.docspace
-- No need to include the links if the documentation is on the template page itself.
local docname = args[1] -- Other docname, if fed.
local linksData = p.makeStartBoxLinksData(args, env)
local templatePage = env.templatePage
links = p.renderStartBoxLinks(linksData)
 
end
-- Arg processing from {{documentation/start box2}}.
-- Generate the start box html.
local docpage
local data = p.makeStartBoxData(args, env, links)
if docname then
if type(data) == 'table' then
docpage = docname
return p.renderStartBox(data)
elseif type(data) == 'string' then
-- data is an error message.
return data
else
-- User specified no heading.
local namespace = docspace or title.nsText
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
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
 
Line 448 ⟶ 372:
ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink)
else
lspan.wikitext(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
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
 
Line 465 ⟶ 433:
.css('font-size', data.headingFontSize)
.wikitext(data.heading)
iflocal links = data.showLinks thenlinks
if links then
sbox.tag('span')
.addClass(data.linksClass)
.attr('id', data.linksId)
.wikitext(data.links)
end
return tostring(sbox)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu