Module:Documentation

Revision as of 06:54, 3 August 2025 by HyperNervie (talk | contribs)

Documentation for this module may be created at Module:Documentation/doc

local p = {}

function p.main(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		args[k] = v
	end
	return p.createDoc(args)
end

function p.createDoc(args)
	local currentPage = mw.title.getCurrentTitle()
	local docPage = mw.title.new(args[1] or "") or currentPage:subPageTitle("doc")
	local docContainer = mw.html.create("div"):addClass("documentation")
	
	if args.type == nil then
		if currentPage.namespace == 10 then args.type = "Template"
		elseif currentPage.namespace == 828 then args.type = "Module"
		else args.type = "Page" end
	end
	local header = "[[File:Template-info.svg|x24px]] <b>" .. args.type .. " Documentation</b>"
	if args.content == nil and docPage.exists then 
		header = header .. tostring(mw.html.create("span"):addClass("documentation-actions")
			:tag("span"):addClass("documentation-action-label"):wikitext("[Options]"):done()
			:tag("ul"):addClass("documentation-action-menu")
				:tag("li"):wikitext("[[" .. docPage.fullText .. "|View documentation page]]"):done()
				:tag("li"):wikitext("[[" .. docPage.talkPageTitle.fullText .. "|Documentation talk page]]"):done()
				:tag("li"):wikitext("[[Special:EditPage/" .. docPage.fullText .. "|Edit documentation]]"):done()
				:tag("li"):wikitext("[[Special:PageHistory/" .. docPage.fullText .. "|Documentation revision history]]"):done()
				:tag("li"):wikitext("[[Special:Purge/" .. currentPage.fullText .. "|Purge this page]]"):done()
			:done()
		:done())
	end
	docContainer:tag("div"):addClass("documentation-header"):wikitext(header):done()
	
	local content = args.content
	if content == nil then
		if docPage.exists then
			local docTitle = docPage.fullText
			if docPage.namespace == 0 then docTitle = ":" .. docTitle end
			content = mw.getCurrentFrame():expandTemplate{ title = docTitle, args = {} }
		else content = "Documentation page [[" .. docPage.fullText .. "]] doesn't exist. Click the link to create one!"
		end
	end
	docContainer:tag("div"):addClass("documentation-content"):wikitext("\n" .. content .. "\n"):done()
	
	local footer = " ([[Template:Documentation|How does this work?]])"
	if currentPage.fullText == "Template:Documentation" then footer = "" end
	if args.content then
		footer = "This is an inline documentation." .. footer
	elseif docPage.exists then
		footer = "This documentation is transcluded from [[" .. docPage.fullText .. "]]." .. footer
	end
	docContainer:tag("div"):addClass("documentation-footer"):wikitext(footer):done()
	
	local templateStyles = mw.getCurrentFrame():extensionTag("templatestyles", nil, {src = "Module:Documentation/styles.css"})
	return tostring(docContainer) .. templateStyles
end

return p