Difference between revisions of "Module:About"

From blackwiki
Jump to navigation Jump to search
test>Nihiltres
(Added tests for a set of unusual parameterizations)
test>Nihiltres
m (Protected "Module:About": High-risk Lua module: Soon to be implemented in high-risk Template:About ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)))
Line 1: Line 1:
{{Hatnote|<!--
+
local mArguments --initialize lazily
-->{{#if: {{{1|}}}|<!--
+
local mHatnote = require('Module:Hatnote')
  -->This {{#if:{{{section|}}}|section|{{#switch:{{NAMESPACENUMBER}}|0=article|14=category|page}}}} is about {{{1}}}.&#32;&#32;}}<!--
+
local mHatList = require('Module:Hatnote list')
-->For {{#if:{{{2|}}}|{{{2}}}|other uses}}, see {{#if:{{{3|}}}|[[{{{3}}}]]{{#ifeq:{{{4|}}}|and|&#32;and {{#if:{{{5|}}}|[[{{{5}}}]]|[[{{PAGENAME}} (disambiguation)]]}}}}|[[{{PAGENAME}} (disambiguation)]]}}.<!--
+
local libraryUtil = require('libraryUtil')
-->{{#if:{{{2|}}}|{{#if:{{{4|}}}|<!--
+
local checkType = libraryUtil.checkType
  -->{{#ifeq:{{{4|}}}|and||<!-- "and" is a special word, don't output "For and, ..."
+
local p = {}
    -->&#32;&#32;For {{#ifeq:{{{4}}}|1|other uses|{{{4}}}}}, see {{#if:{{{5|}}}|[[{{{5}}}]]{{#ifeq:{{{6|}}}|and|&#32;and {{#if:{{{7|}}}|[[{{{7}}}]]|[[{{PAGENAME}} (disambiguation)]]}}}}|[[{{PAGENAME}} (disambiguation)]]}}.}}<!--
+
 
  -->{{#if:{{{6|}}}|<!--
+
function p.about (frame)
    -->{{#ifeq:{{{6|}}}|and||<!--
+
-- A passthrough that gets args from the frame and all
      -->&#32;&#32;For {{#ifeq:{{{6}}}|1|other uses|{{{6}}}}}, see {{#if:{{{7|}}}|[[{{{7}}}]]{{#ifeq:{{{8|}}}|and|&#32;and {{#if:{{{9|}}}|[[{{{9}}}]]|[[{{PAGENAME}} (disambiguation)]]}}}}|[[{{PAGENAME}} (disambiguation)]]}}.}}<!--
+
 
    -->{{#if:{{{8|}}}|<!--
+
mArguments = require('Module:Arguments')
      -->{{#ifeq:{{{8|}}}|and||<!--
+
args = mArguments.getArgs(frame)
        -->&#32;&#32;For {{#ifeq:{{{8}}}|1|other uses|{{{8}}}}}, see {{#if:{{{9|}}}|[[{{{9}}}]]|[[{{PAGENAME}} (disambiguation)]]}}.}}<!--
+
return p._about(args)
    -->}}<!--
+
end
  -->}}<!--
+
 
-->}}}}}}<!--
+
 
-->{{#if:{{{_nocat|}}}||{{#if:{{{1|}}}{{{2|}}}||{{#if:{{{3|}}}|[[Category:Hatnote templates using unusual parameters|A{{PAGENAME}}]]}}}}{{#ifeq:{{str left|{{{1}}}|3}}|is |[[Category:Hatnote templates using unusual parameters|B{{PAGENAME}}]]}}}}<!--
+
function p._about (args, options)
-->{{#ifeq:{{#if:{{{5|}}}{{{6|}}}{{{7|}}}{{{8|}}}{{{9|}}}|a}}{{#if:{{{4|}}}||b}}|ab|[[Category:Hatnote templates using unusual parameters]]}}<!--If 5 to 9 and not 4
+
-- Produces "about" hatnote.
-->{{#ifeq:{{#if:{{{7|}}}{{{8|}}}{{{9|}}}|a}}{{#if:{{{6|}}}||b}}|ab|[[Category:Hatnote templates using unusual parameters]]}}<!--If 7 to 9 and not 6
+
 
-->{{#ifeq:{{#if:{{{9|}}}|a}}{{#if:{{{8|}}}||b}}|ab|[[Category:Hatnote templates using unusual parameters]]}}<!--If 9 and not 8
+
-- Type checks and defaults
--><noinclude>
+
checkType('_about', 1, args, 'table', true)
{{Documentation}}
+
args = args or {}
</noinclude>
+
checkType('_about', 2, options, 'table', true)
 +
options = options or {}
 +
local defaultOptions = {
 +
aboutForm = 'This %s is about %s. ',
 +
defaultPageType = 'page',
 +
namespace = mw.title.getCurrentTitle().namespace,
 +
pageTypesByNamespace = {
 +
[0] = 'article',
 +
[14] = 'category'
 +
},
 +
sectionString = 'section'
 +
}
 +
for k, v in pairs(defaultOptions) do
 +
if options[k] == nil then options[k] = v end
 +
end
 +
 
 +
-- Set initial "about" string
 +
local pageType = (args.section and options.sectionString) or
 +
options.pageTypesByNamespace[options.namespace] or
 +
options.defaultPageType
 +
local about = ''
 +
if args[1] then
 +
about = string.format(options.aboutForm, pageType, args[1])
 +
end
 +
 
 +
-- Set for-see list
 +
local forSee = mHatList._forSee(args, 2)
 +
 
 +
-- Concatenate and return
 +
return mHatnote._hatnote(about .. forSee)
 +
end
 +
 
 +
return p

Revision as of 23:44, 6 May 2016

This module implements the {{about}} hatnote template.

Usage

  • The about function implements the {{about}} template directly, using the frame and applying no options. This should probably only be used in {{about}}.
  • The _about function can be used from Lua to supply a custom arguments list (its first argument) and/or an options table (its second argument). Options include:
    • aboutForm A string that can be used to substitute the form of the initial "about" message. It should include two substitution points: the first for the page type (e.g. "article"), and the second for the description of what the page is about. It should also include a trailing space for easy concatenation or omission. Defaults to 'This %s is about %s. '.
    • sectionString Gives the page type string for when the named argument section is specified. Defaults to 'section'.

local mArguments --initialize lazily
local mHatnote = require('Module:Hatnote')
local mHatList = require('Module:Hatnote list')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local p = {}

function p.about (frame)
	-- A passthrough that gets args from the frame and all

	mArguments = require('Module:Arguments')
	args = mArguments.getArgs(frame)
	return p._about(args)
end


function p._about (args, options)
	-- Produces "about" hatnote.

	-- Type checks and defaults
	checkType('_about', 1, args, 'table', true)
	args = args or {}
	checkType('_about', 2, options, 'table', true)
	options = options or {}
	local defaultOptions = {
		aboutForm = 'This %s is about %s. ',
		defaultPageType = 'page',
		namespace = mw.title.getCurrentTitle().namespace,
		pageTypesByNamespace = {
			[0] = 'article',
			[14] = 'category'
		},
		sectionString = 'section'
	}
	for k, v in pairs(defaultOptions) do
		if options[k] == nil then options[k] = v end
	end

	-- Set initial "about" string
	local pageType = (args.section and options.sectionString) or
		options.pageTypesByNamespace[options.namespace] or
		options.defaultPageType
	local about = ''
	if args[1] then
		about = string.format(options.aboutForm, pageType, args[1])
	end

	-- Set for-see list
	local forSee = mHatList._forSee(args, 2)

	-- Concatenate and return
	return mHatnote._hatnote(about .. forSee)
end

return p