Module:Notice: Difference between revisions
No edit summary |
m removed unnecesary if-clause |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local | -- Define constants -- | ||
local TEMPLATE_STYLES = 'Module:Notice/styles.css' | |||
-- Define tables of valid values -- | |||
local borderColors ={ | |||
["blue"] = true, | |||
["gray"] = true, | |||
["green"] = true, | |||
["maroon"] = true, | |||
["orange"] = true, | |||
["pink"] = true, | |||
["purple"] = true, | |||
["red"] = true, | |||
["teal"] = true, | |||
["yellow"] = true | |||
} | |||
local backgroundColors ={ | |||
["blue"] = true, | |||
["gray"] = true, | |||
["green"] = true, | |||
["maroon"] = true, | |||
["orange"] = true, | |||
["pink"] = true, | |||
["purple"] = true, | |||
["red"] = true, | |||
["teal"] = true, | |||
["yellow"] = true | |||
} | |||
local alignValues = { | |||
["center"] = true, | |||
["left"] = true, | |||
["right"] = true | |||
} | |||
local textAlignValues = { | |||
["center"] = true, | |||
["left"] = true, | |||
["right"] = true | |||
} | |||
local p = {} | |||
-- Entry point for the module | -- Entry point for the module -- | ||
function p.main(frame) | function p.main(frame) | ||
local | local args = require('Module:ProcessArgs').merge(true) | ||
local alignBox = args.align or 'left' | |||
local alignContent = args.alignContent or '' | |||
local alignHeader = args.alignHeader or '' | |||
local alignText = args.alignText or 'left' | |||
local class = args.class or '' | |||
local color = args.color or 'green' | |||
local colorBack = args.colorBack or '' | |||
local content = args.content or '' | |||
local file = args.file or '' | |||
local fileRight = args.fileRight or '' | |||
local fileSize = args.filesize or '48px' | |||
local header = args.header or '' | |||
local small = args.small or '' | |||
local width = args.width or '' | |||
if alignHeader == '' then alignHeader = alignText end | |||
if alignContent == '' then alignContent = alignText end | |||
-- use template styles -- | |||
local root = mw.html.create() | |||
root:wikitext(frame:extensionTag{ | |||
name = 'templatestyles', | |||
args = { src = TEMPLATE_STYLES}, | |||
}) | |||
-- base div-section of the notice box -- | |||
local box = root:tag('div') | |||
if small == 'yes' then | |||
box:addClass('notice-small') | |||
else | |||
box:addClass('notice') | |||
if alignValues[alignBox] then | |||
box:addClass(string.format('notice-%s', alignBox)) | |||
else | |||
box:addClass('notice-left') | |||
end | |||
end | |||
if borderColors[color] then | |||
box:addClass(string.format('notice-border-%s', color)) | |||
else | |||
box:css('border-color', color) | |||
end | |||
if colorBack ~= '' then | |||
if backgroundColors[colorBack] then | |||
box:addClass(string.format('notice-background-%s', colorBack)) | |||
else | |||
box:css('background-color', colorBack) | |||
end | |||
end | |||
if width ~= '' then | |||
box:css('width', width) | |||
end | |||
-- add additional classes to the box (optional) -- | |||
if class ~= '' then box:addClass(class) end | |||
-- adding elements to the box -- | |||
-- place file left -- | |||
if file ~= '' then | if file ~= '' then | ||
if fileRight == '' then | |||
box:tag('div') | |||
:addClass('notice-image') | |||
:wikitext(string.format('[[File:%s|%s|link=|class=notpageimage]]', file, fileSize)) | |||
end | |||
end | |||
-- place text content -- | |||
local contentDiv = '' | |||
if header ~= '' then | |||
contentDiv = box:tag('div') | |||
:addClass('notice-content') | |||
local headerDiv = contentDiv:tag('div') | |||
:addClass('notice-header') | |||
:addClass(string.format('notice-text-%s', alignHeader)) | |||
:wikitext(header) | |||
end | end | ||
if content ~= '' then | |||
if contentDiv == '' then | |||
contentDiv = box:tag('div') | |||
:addClass('notice-content') | |||
end | |||
local descriptionDiv = contentDiv:tag('div') | |||
:addClass('notice-description') | |||
:addClass(string.format('notice-text-%s', alignContent)) | |||
:wikitext(content) | |||
end | |||
-- place file right -- | |||
if fileRight ~= '' then | |||
if file ~= '' then | |||
box:tag('div') | |||
:addClass('notice-image') | |||
return tostring(root) | :wikitext(string.format('[[File:%s|%s|link=|class=notpageimage]]', file, fileSize)) | ||
end | |||
end | |||
return tostring(root) | |||
end | end | ||
return p | return p |
Latest revision as of 21:45, 16 June 2025

An Module to create a Notice box with image (optional).
Usage
Use this template to create other Notice Templates.
Syntax: {{notice|color=|colorBack=|text-align=|width=|file=|filesize=|header=|content=}}
color
|
Named, optional | The color preset for the border. Defined options are green , blue , teal , orange , yellow , red , maroon , pink , and gray . However, if a color is a known CSS color, it can be used. Defaults to green .
| |||
colorBack
|
Named, optional | The color preset for the background color. Defined options are green , blue , teal , orange , yellow , red , maroon , pink , and gray with 0.2 for alpha channel. However, if a color is a known CSS color, it can be used. Defaults to none .
| |||
text-align
|
Named, optional | The text-align attribute of the header and content text, e.g. center . Defaults to left .
| |||
file
|
Named, optional | The file to show on the left-hand section as a filename. e.g. Example.jpg . Defaults to nothing shown.
| |||
filesize
|
Named, optional | The file's size on the left-hand section. e.g. 30px . Defaults to 48px if a file was specified, otherwise none.
| |||
header
|
Named, optional | The header text. e.g. Notice . Defaults to nothing shown.
| |||
content
|
Named, optional | The content text. e.g. Test message . Defaults to nothing shown.
| |||
width
|
Named, optional | The width attribute of the notice, e.g. 30% . Defaults to unwritten (inherit).
|
Examples
{{notice|file=Beans.png|header=Under Construction|content=This article is under construction.}}

{{notice|file=Beans.png|header=Notice with header text only.}}

{{notice|file=Beans.png|content=Small notice without a header.}}

{{notice|file=Beans.png|header=Green notice|color=green}}

{{notice|file=Beans.png|header=Green notice with green background|color=green|colorBack=green}}

{{notice|file=Beans.png|header=Blue notice with custom width|width=30%|color=blue}}

{{notice|file=Beans.png|header=Teal notice|color=teal}}

{{notice|file=Beans.png|header=Orange notice|color=orange}}

{{notice|file=Beans.png|header=Yellow notice|color=yellow}}

{{notice|file=Beans.png|header=Red notice|color=red}}

{{notice|file=Beans.png|header=Maroon notice|color=maroon}}

{{notice|file=Beans.png|header=Pink notice|color=pink}}

{{notice|file=Beans.png|header=Gray notice|color=gray}}

{{notice|file=Beans.png|header=Custom color notice|color=slateblue|text-align=center}}

-- Define constants --
local TEMPLATE_STYLES = 'Module:Notice/styles.css'
-- Define tables of valid values --
local borderColors ={
["blue"] = true,
["gray"] = true,
["green"] = true,
["maroon"] = true,
["orange"] = true,
["pink"] = true,
["purple"] = true,
["red"] = true,
["teal"] = true,
["yellow"] = true
}
local backgroundColors ={
["blue"] = true,
["gray"] = true,
["green"] = true,
["maroon"] = true,
["orange"] = true,
["pink"] = true,
["purple"] = true,
["red"] = true,
["teal"] = true,
["yellow"] = true
}
local alignValues = {
["center"] = true,
["left"] = true,
["right"] = true
}
local textAlignValues = {
["center"] = true,
["left"] = true,
["right"] = true
}
local p = {}
-- Entry point for the module --
function p.main(frame)
local args = require('Module:ProcessArgs').merge(true)
local alignBox = args.align or 'left'
local alignContent = args.alignContent or ''
local alignHeader = args.alignHeader or ''
local alignText = args.alignText or 'left'
local class = args.class or ''
local color = args.color or 'green'
local colorBack = args.colorBack or ''
local content = args.content or ''
local file = args.file or ''
local fileRight = args.fileRight or ''
local fileSize = args.filesize or '48px'
local header = args.header or ''
local small = args.small or ''
local width = args.width or ''
if alignHeader == '' then alignHeader = alignText end
if alignContent == '' then alignContent = alignText end
-- use template styles --
local root = mw.html.create()
root:wikitext(frame:extensionTag{
name = 'templatestyles',
args = { src = TEMPLATE_STYLES},
})
-- base div-section of the notice box --
local box = root:tag('div')
if small == 'yes' then
box:addClass('notice-small')
else
box:addClass('notice')
if alignValues[alignBox] then
box:addClass(string.format('notice-%s', alignBox))
else
box:addClass('notice-left')
end
end
if borderColors[color] then
box:addClass(string.format('notice-border-%s', color))
else
box:css('border-color', color)
end
if colorBack ~= '' then
if backgroundColors[colorBack] then
box:addClass(string.format('notice-background-%s', colorBack))
else
box:css('background-color', colorBack)
end
end
if width ~= '' then
box:css('width', width)
end
-- add additional classes to the box (optional) --
if class ~= '' then box:addClass(class) end
-- adding elements to the box --
-- place file left --
if file ~= '' then
if fileRight == '' then
box:tag('div')
:addClass('notice-image')
:wikitext(string.format('[[File:%s|%s|link=|class=notpageimage]]', file, fileSize))
end
end
-- place text content --
local contentDiv = ''
if header ~= '' then
contentDiv = box:tag('div')
:addClass('notice-content')
local headerDiv = contentDiv:tag('div')
:addClass('notice-header')
:addClass(string.format('notice-text-%s', alignHeader))
:wikitext(header)
end
if content ~= '' then
if contentDiv == '' then
contentDiv = box:tag('div')
:addClass('notice-content')
end
local descriptionDiv = contentDiv:tag('div')
:addClass('notice-description')
:addClass(string.format('notice-text-%s', alignContent))
:wikitext(content)
end
-- place file right --
if fileRight ~= '' then
if file ~= '' then
box:tag('div')
:addClass('notice-image')
:wikitext(string.format('[[File:%s|%s|link=|class=notpageimage]]', file, fileSize))
end
end
return tostring(root)
end
return p