Module:Documentation

--Load loader function local loadLib = require('Module:LoadLib')

--Load __TYPE__s loadLib(_G)

--Begin Exports local Doc = {} local nowiki = mw.text.nowiki local lb, rb = nowiki('['), nowiki(']') local sep = ' &bull; ' local titleObj = mw.title.getCurrentTitle local pagename = titleObj.fullText

function Doc:linksToList(t, ignoreBrackets) local ret = {} local t2 = {}

for i, v in ipairs(t) do		table.push(ret, self:parseLinkEntry(v, ignoreBrackets or t.main)) end ret = table.concat(ret, sep) if t.main then ret = table.concat{ t.prepend and self:parseLinkEntry(t.prepend, true)..' ' or '', self:parseLinkEntry(t.main, true), ' ', 			#t ~= 0 and '(' or , 			ret, 			#t ~= 0 and ')' or , ' ',		}	end return ret end

function Doc:exists(title) return mw.title.new(title).exists end

function Doc:parseLinkEntry(t2, isMain) t2[1] = string.parseDualArg(t2[1]) local val local title = string.makeTitle(t2[2] or '', t2[3], { disableAbbr=true }) if t2.query then val = string.fullUrl(t2[1], t2.query or {}, title) else val = string.makeLink(t2[1], title) end return isMain and (t2.alt or val) or table.concat{ lb, val, rb } end

function Doc:renderVariables self.title = self._title or 'Sandbox' self.type = string.ucfirst(self._type or 'Module') self.title = table.concat{ self.type, ':', (self.title:gsub(table.concat{ '^', self.type, ':?' }, '')) } self.sandbox = table.concat{ self.title, '/sandbox' } self.testcases = table.concat{ self.title, '/testcases' } self.doc = table.concat{ self.title, '/doc' } self.titleObj = mw.title.new(self.title) self.titleExists = self.titleObj.exists self.talkObj = self.titleObj.talkPageTitle self.sandboxObj = mw.title.new(self.sandbox) self.testcasesObj = mw.title.new(self.testcases) self.docObj = mw.title.new(self.doc) self.talk = self.titleObj.talkPageTitle.fullText return self end

function Doc:generateData self.createDocQuery = { action='edit', redlink=1, minor=1, useeditor="source", preload='Template:__DIR__/preload/doc', editintro='Template:__DIR__/editintro/doc', summary=table.format{ "Creating __TYPE__ Documenation for \"%s\"", self.doc }; }	self.createSandbox = { self.sandbox, string.makeTitle('Create Sandbox', 'Create the sandbox subpage for this __TYPE__', { disableAbbr=true }), query = { action="edit", redlink=1, minor=1, useeditor="source", preload=self.title, summary=table.format{ "Creating Sandbox __TYPE__ for \"%s\"", self.doc }; }	}	self.links = {} self.links.pageTools = { {			{ self.doc, 'view', 'View the Documentation subpage for this __TYPE__', }, { self.doc, 'edit', 'Edit the Documentation subpage for this __TYPE__', query={ action='edit' }}, { self.doc, 'latest diff', 'View the diff on the latest Edit to the Documentation subpage for this __TYPE__', query={ diff='cur' }}, { self.doc, 'hist', 'View the history of this __TYPE__\'s documentation subpage', query={ action='history' }}, { self.title, 'purge', 'Purge this page', query={ action="purge" }}, },		{			{ self.doc, 'create', 'Create the Documentation for this __TYPE__', query=self.createDocQuery }, { self.title, 'purge', 'Purge this page', query={ action="purge" }}, }	}	self.links.sandbox = { main={ self.sandbox, 'Sandbox' }, { self.sandbox, 'edit', 'Edit the sandbox __TYPE__', query={ action="edit" }}, { self.sandbox, 'latest diff', 'View the latest edit\'s diff on the sandbox __TYPE__', query={ diff="cur" }}, { self.sandbox, 'hist', 'View the history on the sandbox __TYPE__', query={ action="history" }}, { 			alt=string.wrapHtml{ "reset", " ", attrs={ title="Reset the sandbox __TYPE__ to match the main __TYPE__", id="re-mirror-sandbox", style={ cursor="pointer"; } }			}, 		},	}	self.links.tools = { { { 'Special:PrefixIndex/', self.title, '/' }, 'View subpages', 'View the subpages of this __TYPE__' }, { { 'Special:WhatLinksHere/', self.title, }, 'View links', 'View what links here to this __TYPE__' }, }	self.links.talk = { {			main={ self.talk, 'Talk' }, prepend={ self.talk, '+', 'Add a new section to the __TYPE__\'s talk page', query={ action="edit", section="new" }}, { self.talk, 'edit', 'Edit the __TYPE__\'s talk page', query={ action="edit" }}, { self.talk, 'latest diff', 'View the latest edit\'s diff on the __TYPE__\'s talk page', query={ diff="cur" }}, { self.talk, 'hist', 'View the history on the __TYPE__\'s talk page', query={ action="history" }}, },		{			main={ self.talk, 'Create talk', 'Create the talk page for this __TYPE__', query={ action='edit', redlink=1, minor=1, useeditor="source", summary=table.format{ 'Creating talk page for __TYPE__ \"%s\"', self.title }, }			},		},	}	self.links.pageLinks = { { alt=self:linksToList(self.links.talk[self.talkObj.exists and 1 or 2]) }, { self.title, 'Latest Diff', 'View the latest edit\'s diff on the __TYPE__', query={ diff="cur" }}, { { 'Special:Log/', self.title }, 'Page Logs', 'View the logs for this page', }, }	self.links.testcases = { {			 main={ self.testcases, 'Testcases', 'View the testcases subpage for this __TYPE__' }, { self.testcases, 'edit', 'Edit the testcases subpage for this __TYPE__' }, { self.testcases, 'latest diff', 'Edit the testcases subpage for this __TYPE__' }, self.testcasesObj.talkPageTitle.exists and { self.testcasesObj.talkPageTitle.fullText, 'run', 'Run the testcases subpage for this __TYPE__' } or { self.testcasesObj.talkPageTitle.fullText, 'create', 'Create the testcases results subpage for this __TYPE__', query={ action='edit', redlink=1, minor=1, useeditor="source", preload='Template:__DIR__/preload/testcasesResults', editintro='Template:__DIR__/editintro/testcasesResults', summary=table.format{ 'Creating testcases results page for __TYPE__ \"%s\"', self.title }, },				},		},		{ 			self.testcases, 'Create testcases', 'Create the testcases subpage for this __TYPE__', query={ action='edit', redlink=1, minor=1, useeditor="source", preload='Template:__DIR__/preload/testcases', editintro='Template:__DIR__/editintro/testcases', summary=table.format{ 'Creating testcases subpage for __TYPE__ \"%s\"', self.title }, }		}	} end

function Doc:buildMainBox local inst = self.html :css{ ['font-family']='Arial !important'; }		:tag('div') :addClass('color1') :css{ ['padding'] = '2px'; ['border-top'] = '1.3px solid black'; ['border-left'] = '1.3px solid black'; ['border-right'] = '1.3px solid black'; ['border-bottom'] = 'none'; ['margin'] = '4px 4px 0 4px'; ['box-shadow'] = '0 0 2px black'; }			:tag('span') :css{ ['float'] = 'right'; ['font'] = 'italic small arial'; }				:wikitext('(', string.makeLink('#code', string.makeTitle('jump to code', "Jump to the code of this __TYPE__", { disableAbbr=true })), ')')			:done :tag('span') :css{ float="left"; }				:wikitext(string.makeImage('Template Info Icon.svg', { link='', size=60 })) :done :tag('span') :attr{ id="header" } :css{ ['font'] = 'bold x-large arial'; ['margin-left'] = '7px'; ['text-align'] = 'center'; }				:wikitext('__TYPE__ Documentation') :done :tag('span') :css{ ['margin-left']='7px'; }				:wikitext(self:linksToList(self.docObj.exists and self.links.pageTools[1] or self.links.pageTools[2])) :done if self.type == 'Template' then inst:tag('small') :css{ display='block'; }			:tag('i') :tag('b'):wikitext('Note: '):done :wikitext('Some or whole parts of the template may not be visible due to some parameters not being provided.') :done :done end inst = inst:tag('hr') :addClass('page-header__separator') :css{ ['background-color'] = '#daa9ad'; ['border'] = 0; ['height'] = '1px'; ['margin-bottom'] = '3px'; ['margin-top'] = 0; ['width'] = '100%'; }			:done :tag('div') :tag('p') :css{ margin=0; }					:wikitext(						string.wrapTag('Page Tools: ', 'b'), 						table.concat(table.merge({							self:exists(self.sandbox) 								and self:linksToList(self.links.sandbox) 								or self:parseLinkEntry(self.createSandbox, true),						}, self:linksToList(self.links.tools, true)), sep)					) :done :done :tag('hr') :addClass('page-header__separator') :css{ ['background-color'] = '#daa9ad'; ['border'] = 0; ['height'] = '1px'; ['margin-bottom'] = '3px'; ['margin-top'] = 0; ['width'] = '100%'; }			:done :tag('div') :tag('p') :css{ margin=0; }					:wikitext(						string.wrapTag('__TYPE__ Links: ', 'b'), 						self:linksToList(self.links.pageLinks, true)					) :done :done :done :tag('div') :css{ ['background-color'] = 'rgba(120,120,120,0.05)'; ['box-shadow'] = '0 0 2px black'; ['border-bottom'] = 'none'; ['border-top'] = 'none'; ['padding'] = '0.5em'; ['margin'] = '0 4px 0 4px'; ['border-right'] = '1.3px solid black'; ['border-left'] = '1.3px solid black'; }			:wikitext(				self.docObj.exists and					mw.getCurrentFrame:preprocess(self.docObj:getContent)				or					table.concat{ 						string.wrapHtml{							'The documentation for this __TYPE__ does not exist. Please create it if you are familiar with this __TYPE__. ',							' ', {								class="error"							},						},						string.wrapTag({ '(', string.fullUrl(self.doc, self.createDocQuery, 'create'), ')', }, 'strong'),					}			) :wikitext(not self.docObj.exists and  or ) :tag('span') :css{ display="none" } :wikitext(, self.doc, ) :done :done :tag('div') :addClass('color1') :css{ ['padding'] = '2px'; ['border-left'] = '1.3px solid black'; ['border-right'] = '1.3px solid black'; ['border-bottom'] = '1.3px solid black'; ['border-top'] = 'none'; ['margin'] = '0 4px 4px 4px'; ['box-shadow'] = '0 0 2px black'; }			:tag('span') :css{ ['font-size']='20px'; ['margin']='5px'; }				:wikitext('(', string.makeLink({ '__TYPE__:Documentation' }, 'How does this work?'), ')') :done :tag('span') :css{ ['margin-left']=self.titleObj.exists and '13%' or '23%'; }				:wikitext(self.type == "Module" and string.wrapTag('Other Tools: ', 'b') or '') :wikitext(					self.type == "Module" and ( self:exists(self.testcases) and self:linksToList(self.links.testcases[1]) or self:parseLinkEntry(self.links.testcases[2], true) ) or ''				) :done :tag('span') :css{ float='right'; ['font-style']='italic'; }				:wikitext('(', string.makeLink('#header', 'back to top'), ')') :done :done :allDone return inst end

function Doc:constructor(title, _type) checkType(true, 1, title, 'string', true) checkType(true, 2, _type, 'string', true) self._title, self._type = title, string.ucfirst(_type or 'Module') self:renderVariables self.html = mw.html.create('div') self:generateData return tostring(self:buildMainBox) :gsub('__TYPE__', self.type) :gsub('__DIR__', self.type == 'Module' and 'LuaDocumentation' or 'Documentation') end

--Create Class Doc = table.makeClass(Doc, nil, nil, { ignore=true })

function Doc.main(frame) local args = getArgs(frame) return Doc:constructor(table.unpack(table.deepCopy(args, false))) end

function Doc.module(title) checkType(1, title, 'string', true) return Doc(title or titleObj.text, 'Module') end

function Doc.template(title) checkType(1, title, 'string', true) return Doc(title or titleObj.text, 'Template') end

function Doc._module(frame) local args = getArgs(frame) return Doc:constructor(args[1] or titleObj.text, 'Module') end

function Doc._template(frame) local args = getArgs(frame) return Doc:constructor(args[1] or titleObj.text, 'Template') end

--Finish __TYPE__/Exports return Doc