Jump to content

Module:Script doc auto

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 21:14, 21 January 2024 (core2 -> makeMessage). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local messageBox = require('Module:Message box')
local getArgs = require('Module:Arguments').getArgs
local p = {}

p.main = function(frame)
	local args = getArgs(frame)
	return p.core(args.page or mw.title.getCurrentTitle().fullText)
end

p.core = function(page)
	local len = page:len()
	if len < 4 then
		-- Too short page name, do nothing
		return ''
	end
	
	if page:sub(-4, -1) == '.css' then
		local basename = page:sub(0, -5)
		local docpage = basename
		local sisterpage = basename..'.js'
		return p.makeMessage('css', mw.title.new(docpage), mw.title.new(sisterpage), 'js')
	end
	
	if page:sub(-3, -1) == '.js' then
		local basename = page:sub(0, -4)
		local docpage = basename
		local sisterpage = basename..'.css'
		return p.makeMessage('js', mw.title.new(docpage), mw.title.new(sisterpage), 'css')
	end
end

local skins = {
	['vector-2022'] = true,
	['vector'] = true,
	['timeless'] = true,
	['minerva'] = true,
	['monobook'] = true,
	['modern'] = true,
	['cologneblue'] = true
}

p.makeMessage = function(pagetype, docpage, sisterpage, sistertype)
	local text = ''
	if docpage.namespace == 2 then
		if skins[docpage.subpageText] ~= nil then
			-- We are on a user skin file
			text = 'The accompanying .'..sistertype..' page for this skin '..
				(sisterpage.exists and 'is' or 'can be added')..' at [['..sisterpage.fullText..']].'
		else
			-- We are on some script page, not a user skin file
			local docpageExists = docpage.exists
			local sisterpageExists = sisterpage.exists
			
			if docpageExists and sisterpageExists then
				text = 'This [[Wikipedia:User scripts|user script]] seems to have a documentation page at [['..docpage.fullText..']] and an accompanying .'..sistertype..' page at [['..sisterpage.fullText..']].'
			elseif docpageExists and not sisterpageExists then
				text = 'This [[Wikipedia:User scripts|user script]] seems to have a documentation page at [['..docpage.fullText..']].'
			elseif sisterpageExists then
				text = 'Documentation for this [[Wikipedia:User scripts|user script]] can be added at [['..docpage.fullText..']]. This user script seems to have an accompanying .'..sistertype..' page at [['..sisterpage.fullText..']]. '
			else 
				text = 'Documentation for this [[Wikipedia:User scripts|user script]] can be added at [['..docpage.fullText..']].'
			end
		end
	end
	
	if text ~= '' then
		return messageBox.main('fmbox', {
			id = 'mw-script-doc',
			type = 'system',
			image = '[[File:Template-info.svg|43x40px]]',
			style = 'background: #ecfcf4;',
			text = text
		})
	end
end

return p