Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Navbar: Difference between revisions

From Poets.Wiki
No edit summary
Tag: Reverted
m Protected "Module:Navbar": Initial wiki setup ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
-- This module implements {{italic title}}.
local p = {}
local cfg = mw.loadData('Module:Navbar/configuration')


require('strict')
local function get_title_arg(is_collapsible, template)
local libraryUtil = require('libraryUtil')
local title_arg = 1
local checkType = libraryUtil.checkType
if is_collapsible then title_arg = 2 end
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
if template then title_arg = 'template' end
local yesno = require('Module:Yesno')
return title_arg
end


--------------------------------------------------------------------------------
local function choose_links(template, args)
-- ItalicTitle class
-- The show table indicates the default displayed items.
--------------------------------------------------------------------------------
-- view, talk, edit, hist, move, watch
-- TODO: Move to configuration.
local show = {true, true, true, false, false, false}
if template then
show[2] = false
show[3] = false
local index = {t = 2, d = 2, e = 3, h = 4, m = 5, w = 6,
talk = 2, edit = 3, hist = 4, move = 5, watch = 6}
-- TODO: Consider removing TableTools dependency.
for _, v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
local num = index[v]
if num then show[num] = true end
end
end


local ItalicTitle = {}
local remove_edit_link = args.noedit
if remove_edit_link then show[3] = false end
return show
end


do
local function add_link(link_description, ul, is_mini, font_style)
----------------------------------------------------------------------------
local l
-- Class attributes and functions
if link_description.url then
-- Things that belong to the class are here. Things that belong to each
l = {'[', '', ']'}
-- object are in the constructor.
else
----------------------------------------------------------------------------
l = {'[[', '|', ']]'}
end
ul:tag('li')
:addClass('nv-' .. link_description.full)
:wikitext(l[1] .. link_description.link .. l[2])
:tag(is_mini and 'abbr' or 'span')
:attr('title', link_description.html_title)
:cssText(font_style)
:wikitext(is_mini and link_description.mini or link_description.full)
:done()
:wikitext(l[3])
:done()
end


-- Keys of title parts that can be italicized.
local function make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
local italicizableKeys = {
namespace = true,
local title = mw.title.new(mw.text.trim(title_text), cfg.title_namespace)
title = true,
if not title then
dab = true,
error(cfg.invalid_title .. title_text)
end
local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
-- TODO: Get link_descriptions and show into the configuration module.
-- link_descriptions should be easier...
local link_descriptions = {
{ ['mini'] = 'v', ['full'] = 'view', ['html_title'] = 'View this template',
['link'] = title.fullText, ['url'] = false },
{ ['mini'] = 't', ['full'] = 'talk', ['html_title'] = 'Discuss this template',
['link'] = talkpage, ['url'] = false },
{ ['mini'] = 'e', ['full'] = 'edit', ['html_title'] = 'Edit this template',
['link'] = 'Special:EditPage/' .. title.fullText, ['url'] = false },
{ ['mini'] = 'h', ['full'] = 'hist', ['html_title'] = 'History of this template',
['link'] = 'Special:PageHistory/' .. title.fullText, ['url'] = false },
{ ['mini'] = 'm', ['full'] = 'move', ['html_title'] = 'Move this template',
['link'] = mw.title.new('Special:Movepage'):fullUrl('target='..title.fullText), ['url'] = true },
{ ['mini'] = 'w', ['full'] = 'watch', ['html_title'] = 'Watch this template',
['link'] = title:fullUrl('action=watch'), ['url'] = true }
}
}


----------------------------------------------------------------------------
local ul = mw.html.create('ul')
-- ItalicTitle constructor
if has_brackets then
-- This contains all the dynamic attributes and methods.
ul:addClass(cfg.classes.brackets)
----------------------------------------------------------------------------
:cssText(font_style)
end
for i, _ in ipairs(displayed_links) do
if displayed_links[i] then add_link(link_descriptions[i], ul, is_mini, font_style) end
end
return ul:done()
end


function ItalicTitle.new()
function p._navbar(args)
local obj = {}
 
-- TODO: We probably don't need both fontstyle and fontcolor...
-- Function for checking self variable in methods.
local font_style = args.fontstyle
local checkSelf = libraryUtil.makeCheckSelfFunction(
local font_color = args.fontcolor
'ItalicTitle',
local is_collapsible = args.collapsible
'obj',
local is_mini = args.mini
obj,
local is_plain = args.plain
'ItalicTitle object'
)
local collapsible_class = nil
 
if is_collapsible then
-- Checks a key is present in a lookup table.
collapsible_class = cfg.classes.collapsible
-- Param: name - the function name.
if not is_plain then is_mini = 1 end
-- Param: argId - integer position of the key in the argument list.
if font_color then
-- Param: key - the key.
font_style = (font_style or '') .. '; color: ' .. font_color .. ';'
-- Param: lookupTable - the table to look the key up in.
local function checkKey(name, argId, key, lookupTable)
if not lookupTable[key] then
error(string.format(
"bad argument #%d to '%s' ('%s' is not a valid key)",
argId,
name,
key
), 3)
end
end
end
end
local navbar_style = args.style
local div = mw.html.create():tag('div')
div
:addClass(cfg.classes.navbar)
:addClass(cfg.classes.plainlinks)
:addClass(cfg.classes.horizontal_list)
:addClass(collapsible_class) -- we made the determination earlier
:cssText(navbar_style)


-- Set up object structure.
if is_mini then div:addClass(cfg.classes.mini) end
local parsed = false
local categories = {}
local italicizedKeys = {}
local italicizedSubstrings = {}


-- Parses a title object into its namespace text, title, and
local box_text = (args.text or cfg.box_text) .. ' '
-- disambiguation text.
-- the concatenated space guarantees the box text is separated
-- Param: options - a table of options with the following keys:
if not (is_mini or is_plain) then
--    title - the title object to parse
div
--    ignoreDab - ignore any disambiguation parentheses
:tag('span')
-- Returns the current object.
:addClass(cfg.classes.box_text)
function obj:parseTitle(options)
:cssText(font_style)
checkSelf(self, 'parseTitle')
:wikitext(box_text)
checkType('parseTitle', 1, options, 'table')
end
checkTypeForNamedArg('parseTitle', 'title', options.title, 'table')
local title = options.title
local template = args.template
local displayed_links = choose_links(template, args)
-- Title and dab text
local has_brackets = args.brackets
local prefix, parentheses
local title_arg = get_title_arg(is_collapsible, template)
if not options.ignoreDab then
local title_text = args[title_arg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
prefix, parentheses = mw.ustring.match(
local list = make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
title.text,
div:node(list)
'^(.+) %(([^%(%)]+)%)$'
)
end
if prefix and parentheses then
self.title = prefix
self.dab = parentheses
else
self.title = title.text
end
-- Namespace
local namespace = mw.site.namespaces[title.namespace].name
if namespace and #namespace >= 1 then
self.namespace = namespace
end
 
-- Register the object as having parsed a title.
parsed = true
return self
end
 
-- Italicizes part of the title.
-- Param: key - the key of the title part to be italicized. Possible
-- keys are contained in the italicizableKeys table.
-- Returns the current object.
function obj:italicize(key)
checkSelf(self, 'italicize')
checkType('italicize', 1, key, 'string')
checkKey('italicize', 1, key, italicizableKeys)
italicizedKeys[key] = true
return self
end
 
-- Un-italicizes part of the title.
-- Param: key - the key of the title part to be un-italicized. Possible
-- keys are contained in the italicizableKeys table.
-- Returns the current object.
function obj:unitalicize(key)
checkSelf(self, 'unitalicize')
checkType('unitalicize', 1, key, 'string')
checkKey('unitalicize', 1, key, italicizableKeys)
italicizedKeys[key] = nil
return self
end
 
-- Italicizes a substring in the title. This only affects the main part
-- of the title, not the namespace or the disambiguation text.
-- Param: s - the substring to be italicized.
-- Returns the current object.
function obj:italicizeSubstring(s)
checkSelf(self, 'italicizeSubstring')
checkType('italicizeSubstring', 1, s, 'string')
italicizedSubstrings[s] = true
return self
end
 
-- Un-italicizes a substring in the title. This only affects the main
-- part of the title, not the namespace or the disambiguation text.
-- Param: s - the substring to be un-italicized.
-- Returns the current object.
function obj:unitalicizeSubstring(s)
checkSelf(self, 'unitalicizeSubstring')
checkType('unitalicizeSubstring', 1, s, 'string')
italicizedSubstrings[s] = nil
return self
end
 
-- Renders the object into a page name. If no title has yet been parsed,
-- the current title is used.
-- Returns string
function obj:renderTitle()
checkSelf(self, 'renderTitle')
 
-- Italicizes a string
-- Param: s - the string to italicize
-- Returns string.
local function italicize(s)
assert(type(s) == 'string', 's was not a string')
assert(s ~= '', 's was the empty string')
return string.format('<i>%s</i>', s)
end
-- Escape characters in a string that are magic in Lua patterns.
-- Param: pattern - the pattern to escape
-- Returns string.
local function escapeMagicCharacters(s)
assert(type(s) == 'string', 's was not a string')
return s:gsub('%p', '%%%0')
end
 
-- If a title hasn't been parsed yet, parse the current title.
if not parsed then
self:parseTitle{title = mw.title.getCurrentTitle()}
end
 
-- Italicize the different parts of the title and store them in a
-- titleParts table to be joined together later.
local titleParts = {}
 
-- Italicize the italicizable keys.
for key in pairs(italicizableKeys) do
if self[key] then
if italicizedKeys[key] then
titleParts[key] = italicize(self[key])
else
titleParts[key] = self[key]
end
end
end
 
-- Italicize substrings. If there are any substrings to be
-- italicized then start from the raw title, as this overrides any
-- italicization of the main part of the title.
if next(italicizedSubstrings) then
titleParts.title = self.title
for s in pairs(italicizedSubstrings) do
local pattern = escapeMagicCharacters(s)
local italicizedTitle, nReplacements = titleParts.title:gsub(
pattern,
italicize
)
titleParts.title = italicizedTitle
 
-- If we didn't make any replacements then it means that we
-- have been passed a bad substring or that the page has
-- been moved to a bad title, so add a tracking category.
if nReplacements < 1 then
categories['Pages using italic title with no matching string'] = true
end
end
end
 
-- Assemble the title together from the parts.
local ret = ''
if titleParts.namespace then
ret = ret .. titleParts.namespace .. ':'
end
ret = ret .. titleParts.title
if titleParts.dab then
ret = ret .. ' (' .. titleParts.dab .. ')'
end
 
return ret
end
 
-- Returns an expanded DISPLAYTITLE parser function called with the
-- result of obj:renderTitle, plus any other optional arguments.
-- Returns string
function obj:renderDisplayTitle(...)
checkSelf(self, 'renderDisplayTitle')
return mw.getCurrentFrame():callParserFunction(
'DISPLAYTITLE',
self:renderTitle(),
...
)
end


-- Returns an expanded DISPLAYTITLE parser function called with the
if is_collapsible then
-- result of obj:renderTitle, plus any other optional arguments, plus
local title_text_class
-- any tracking categories.
if is_mini then
-- Returns string
title_text_class = cfg.classes.collapsible_title_mini
function obj:render(...)
else
checkSelf(self, 'render')
title_text_class = cfg.classes.collapsible_title_full
local ret = self:renderDisplayTitle(...)
for cat in pairs(categories) do
ret = ret .. string.format(
'[[Category:%s]]',
cat
)
end
return ret
end
end
 
div:done()
return obj
:tag('div')
:addClass(title_text_class)
:cssText(font_style)
:wikitext(args[1])
end
end
local frame = mw.getCurrentFrame()
-- hlist -> navbar is best-effort to preserve old Common.css ordering.
return frame:extensionTag{
name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
} .. frame:extensionTag{
name = 'templatestyles', args = { src = cfg.templatestyles }
} .. tostring(div:done())
end
end


--------------------------------------------------------------------------------
function p.navbar(frame)
-- Exports
return p._navbar(require('Module:Arguments').getArgs(frame))
--------------------------------------------------------------------------------
 
local p = {}
 
local function getArgs(frame, wrapper)
assert(type(wrapper) == 'string', 'wrapper was not a string')
return require('Module:Arguments').getArgs(frame, {
wrappers = wrapper
})
end
end
-- Main function for {{italic title}}
function p._main(args)
checkType('_main', 1, args, 'table')
local italicTitle = ItalicTitle.new()
italicTitle:parseTitle{
title = mw.title.getCurrentTitle(),
ignoreDab = yesno(args.all, false)
}
if args.string then
italicTitle:italicizeSubstring(args.string)
else
italicTitle:italicize('title')
end
return italicTitle:render(args[1])
end
function p.main(frame)
return p._main(getArgs(frame, 'Template:Italic title'))
end
function p._dabonly(args)
return ItalicTitle.new()
:italicize('dab')
:render(args[1])
end
function p.dabonly(frame)
return p._dabonly(getArgs(frame, 'Template:Italic dab'))
end


return p
return p

Latest revision as of 18:41, 3 March 2025

This page uses Creative Commons Licensed content from Wikipedia (view authors).

This is a Lua implementation of {{Navbar}}. It is used in Module:Navbox.


local p = {}
local cfg = mw.loadData('Module:Navbar/configuration')

local function get_title_arg(is_collapsible, template)
	local title_arg = 1
	if is_collapsible then title_arg = 2 end
	if template then title_arg = 'template' end
	return title_arg
end

local function choose_links(template, args)
	-- The show table indicates the default displayed items.
	-- view, talk, edit, hist, move, watch
	-- TODO: Move to configuration.
	local show = {true, true, true, false, false, false}
	if template then
		show[2] = false
		show[3] = false
		local index = {t = 2, d = 2, e = 3, h = 4, m = 5, w = 6,
			talk = 2, edit = 3, hist = 4, move = 5, watch = 6}
		-- TODO: Consider removing TableTools dependency.
		for _, v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
			local num = index[v]
			if num then show[num] = true end
		end
	end

	local remove_edit_link = args.noedit
	if remove_edit_link then show[3] = false end
	
	return show
	
end

local function add_link(link_description, ul, is_mini, font_style)
	local l
	if link_description.url then
		l = {'[', '', ']'}
	else
		l = {'[[', '|', ']]'}
	end
	ul:tag('li')
		:addClass('nv-' .. link_description.full)
		:wikitext(l[1] .. link_description.link .. l[2])
		:tag(is_mini and 'abbr' or 'span')
			:attr('title', link_description.html_title)
			:cssText(font_style)
			:wikitext(is_mini and link_description.mini or link_description.full)
			:done()
		:wikitext(l[3])
		:done()
end

local function make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
	
	local title = mw.title.new(mw.text.trim(title_text), cfg.title_namespace)
	if not title then
		error(cfg.invalid_title .. title_text)
	end
	local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
	
	-- TODO: Get link_descriptions and show into the configuration module.
	-- link_descriptions should be easier...
	local link_descriptions = {
		{ ['mini'] = 'v', ['full'] = 'view', ['html_title'] = 'View this template',
			['link'] = title.fullText, ['url'] = false },
		{ ['mini'] = 't', ['full'] = 'talk', ['html_title'] = 'Discuss this template',
			['link'] = talkpage, ['url'] = false },
		{ ['mini'] = 'e', ['full'] = 'edit', ['html_title'] = 'Edit this template',
			['link'] = 'Special:EditPage/' .. title.fullText, ['url'] = false },
		{ ['mini'] = 'h', ['full'] = 'hist', ['html_title'] = 'History of this template',
			['link'] = 'Special:PageHistory/' .. title.fullText, ['url'] = false },
		{ ['mini'] = 'm', ['full'] = 'move', ['html_title'] = 'Move this template',
			['link'] = mw.title.new('Special:Movepage'):fullUrl('target='..title.fullText), ['url'] = true },
		{ ['mini'] = 'w', ['full'] = 'watch', ['html_title'] = 'Watch this template', 
			['link'] = title:fullUrl('action=watch'), ['url'] = true }
	}

	local ul = mw.html.create('ul')
	if has_brackets then
		ul:addClass(cfg.classes.brackets)
			:cssText(font_style)
	end
	
	for i, _ in ipairs(displayed_links) do
		if displayed_links[i] then add_link(link_descriptions[i], ul, is_mini, font_style) end
	end
	return ul:done()
	
end

function p._navbar(args)
	
	-- TODO: We probably don't need both fontstyle and fontcolor...
	local font_style = args.fontstyle
	local font_color = args.fontcolor
	local is_collapsible = args.collapsible
	local is_mini = args.mini
	local is_plain = args.plain
	
	local collapsible_class = nil
	if is_collapsible then
		collapsible_class = cfg.classes.collapsible
		if not is_plain then is_mini = 1 end
		if font_color then
			font_style = (font_style or '') .. '; color: ' .. font_color .. ';'
		end
	end
	
	local navbar_style = args.style
	local div = mw.html.create():tag('div')
	div
		:addClass(cfg.classes.navbar)
		:addClass(cfg.classes.plainlinks)
		:addClass(cfg.classes.horizontal_list)
		:addClass(collapsible_class) -- we made the determination earlier
		:cssText(navbar_style)

	if is_mini then div:addClass(cfg.classes.mini) end

	local box_text = (args.text or cfg.box_text) .. ' '
	 -- the concatenated space guarantees the box text is separated
	if not (is_mini or is_plain) then
		div
			:tag('span')
				:addClass(cfg.classes.box_text)
				:cssText(font_style)
				:wikitext(box_text)
	end
	
	local template = args.template
	local displayed_links = choose_links(template, args)
	local has_brackets = args.brackets
	local title_arg = get_title_arg(is_collapsible, template)
	local title_text = args[title_arg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
	local list = make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
	div:node(list)

	if is_collapsible then
		local title_text_class
		if is_mini then
			title_text_class = cfg.classes.collapsible_title_mini
		else
			title_text_class = cfg.classes.collapsible_title_full
		end
		div:done()
			:tag('div')
			:addClass(title_text_class)
			:cssText(font_style)
			:wikitext(args[1])
	end
	
	local frame = mw.getCurrentFrame()
	-- hlist -> navbar is best-effort to preserve old Common.css ordering.
	return frame:extensionTag{
		name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
	} .. frame:extensionTag{
		name = 'templatestyles', args = { src = cfg.templatestyles }
	} .. tostring(div:done())
end

function p.navbar(frame)
	return p._navbar(require('Module:Arguments').getArgs(frame))
end

return p