Module:Hatnote list and Module:Navbar: Difference between pages

From Polyphasic Sleep Wiki
(Difference between pages)
switch back to Module:Hatnote instead of Module:Hatnote/sandbox
 
m 1 revision imported
 
Line 1: Line 1:
--------------------------------------------------------------------------------
--                          Module:Hatnote list                              --
--                                                                            --
-- This module produces and formats lists for use in hatnotes. In particular, --
-- it implements the for-see list, i.e. lists of "For X, see Y" statements,  --
-- as used in {{about}}, {{redirect}}, and their variants. Also introduced    --
-- are andList & orList helpers for formatting lists with those conjunctions. --
--------------------------------------------------------------------------------
local mArguments --initialize lazily
local mHatnote = require('Module:Hatnote')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local p = {}
local p = {}
local cfg = mw.loadData('Module:Navbar/configuration')


--------------------------------------------------------------------------------
local function get_title_arg(is_collapsible, template)
-- List stringification helper functions
local title_arg = 1
--
if is_collapsible then title_arg = 2 end
-- These functions are used for stringifying lists, usually page lists inside
if template then title_arg = 'template' end
-- the "Y" portion of "For X, see Y" for-see items.
return title_arg
--------------------------------------------------------------------------------
end
 
--default options table used across the list stringification functions
local stringifyListDefaultOptions = {
conjunction = "and",
separator = ",",
altSeparator = ";",
space = " ",
formatted = false
}


-- Stringifies a list generically; probably shouldn't be used directly
local function choose_links(template, args)
function stringifyList(list, options)
-- The show table indicates the default displayed items.
-- Type-checks, defaults, and a shortcut
-- view, talk, edit, hist, move, watch
checkType("stringifyList", 1, list, "table")
-- TODO: Move to configuration.
if #list == 0 then return nil end
local show = {true, true, true, false, false, false}
checkType("stringifyList", 2, options, "table", true)
if template then
options = options or {}
show[2] = false
for k, v in pairs(stringifyListDefaultOptions) do
show[3] = false
if options[k] == nil then options[k] = v end
local index = {t = 2, d = 2, e = 3, h = 4, m = 5, w = 6,
end
talk = 2, edit = 3, hist = 4, move = 5, watch = 6}
local s = options.space
-- TODO: Consider removing TableTools dependency.
-- Format the list if requested
for _, v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
if options.formatted then list = mHatnote.formatPages(unpack(list)) end
local num = index[v]
-- Set the separator; if any item contains it, use the alternate separator
if num then show[num] = true end
local separator = options.separator
--searches display text only
local function searchDisp(t, f)
return string.find(string.sub(t, (string.find(t, '|') or 0) + 1), f)
end
for k, v in pairs(list) do
if searchDisp(v, separator) then
separator = options.altSeparator
break
end
end
end
end
-- Set the conjunction, apply Oxford comma, and force a comma if #1 has "§"
 
local conjunction = s .. options.conjunction .. s
local remove_edit_link = args.noedit
if #list == 2 and searchDisp(list[1], "§") or #list > 2 then
if remove_edit_link then show[3] = false end
conjunction = separator .. conjunction
end
return show
-- Return the formatted string
return mw.text.listToText(list, separator .. s, conjunction)
end
end


--DRY function
local function add_link(link_description, ul, is_mini, font_style)
function conjList (conj, list, fmt)
local l
return stringifyList(list, {conjunction = conj, formatted = fmt})
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
end


-- Stringifies lists with "and" or "or"
local function make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
function p.andList (...) return conjList("and", ...) end
function p.orList (...) return conjList("or", ...) end
local title = mw.title.new(mw.text.trim(title_text), cfg.title_namespace)
 
if not title then
--------------------------------------------------------------------------------
error(cfg.invalid_title .. title_text)
-- For see
end
--
local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
-- Makes a "For X, see [[Y]]." list from raw parameters. Intended for the
-- {{about}} and {{redirect}} templates and their variants.
-- TODO: Get link_descriptions and show into the configuration module.
--------------------------------------------------------------------------------
-- link_descriptions should be easier...
 
local link_descriptions = {
--default options table used across the forSee family of functions
{ ['mini'] = 'v', ['full'] = 'view', ['html_title'] = 'View this template',
local forSeeDefaultOptions = {
['link'] = title.fullText, ['url'] = false },
andKeyword = 'and',
{ ['mini'] = 't', ['full'] = 'talk', ['html_title'] = 'Discuss this template',
title = mw.title.getCurrentTitle().text,
['link'] = talkpage, ['url'] = false },
otherText = 'other uses',
{ ['mini'] = 'e', ['full'] = 'edit', ['html_title'] = 'Edit this template',
forSeeForm = 'For %s, see %s.',
['link'] = title:fullUrl('action=edit'), ['url'] = true },
}
{ ['mini'] = 'h', ['full'] = 'hist', ['html_title'] = 'History of this template',
 
['link'] = title:fullUrl('action=history'), ['url'] = true },
--Collapses duplicate punctuation
{ ['mini'] = 'm', ['full'] = 'move', ['html_title'] = 'Move this template',
function punctuationCollapse (text)
['link'] = mw.title.new('Special:Movepage'):fullUrl('target='..title.fullText), ['url'] = true },
local replacements = {
{ ['mini'] = 'w', ['full'] = 'watch', ['html_title'] = 'Watch this template',
["%.%.$"] = ".",
['link'] = title:fullUrl('action=watch'), ['url'] = true }
["%?%.$"] = "?",
["%!%.$"] = "!",
["%.%]%]%.$"] = ".]]",
["%?%]%]%.$"] = "?]]",
["%!%]%]%.$"] = "!]]"
}
}
for k, v in pairs(replacements) do text = string.gsub(text, k, v) end
return text
end


-- Structures arguments into a table for stringification, & options
local ul = mw.html.create('ul')
function p.forSeeArgsToTable (args, from, options)
if has_brackets then
-- Type-checks and defaults
ul:addClass(cfg.classes.brackets)
checkType("forSeeArgsToTable", 1, args, 'table')
:cssText(font_style)
checkType("forSeeArgsToTable", 2, from, 'number', true)
from = from or 1
checkType("forSeeArgsToTable", 3, options, 'table', true)
options = options or {}
for k, v in pairs(forSeeDefaultOptions) do
if options[k] == nil then options[k] = v end
end
end
-- maxArg's gotten manually because getArgs() and table.maxn aren't friends
local maxArg = 0
for i, _ in ipairs(displayed_links) do
for k, v in pairs(args) do
if displayed_links[i] then add_link(link_descriptions[i], ul, is_mini, font_style) end
if type(k) == 'number' and k > maxArg then maxArg = k end
end
end
-- Structure the data out from the parameter list:
return ul:done()
-- * forTable is the wrapper table, with forRow rows
-- * Rows are tables of a "use" string & a "pages" table of pagename strings
end
-- * Blanks are left empty for defaulting elsewhere, but can terminate list
 
local forTable = {}
function p._navbar(args)
local i = from
local terminated = false
-- TODO: We probably don't need both fontstyle and fontcolor...
-- If there is extra text, and no arguments are given, give nil value
local font_style = args.fontstyle
-- to not produce default of "For other uses, see foo (disambiguation)"
local font_color = args.fontcolor
if options.extratext and i > maxArg then return nil end
local is_collapsible = args.collapsible
-- Loop to generate rows
local is_mini = args.mini
repeat
local is_plain = args.plain
-- New empty row
local forRow = {}
local collapsible_class = nil
-- On blank use, assume list's ended & break at end of this loop
if is_collapsible then
forRow.use = args[i]
collapsible_class = cfg.classes.collapsible
if not args[i] then terminated = true end
if not is_plain then is_mini = 1 end
-- New empty list of pages
if font_color then
forRow.pages = {}
font_style = (font_style or '') .. '; color: ' .. font_color .. ';'
-- Insert first pages item if present
table.insert(forRow.pages, args[i + 1])
-- If the param after next is "and", do inner loop to collect params
-- until the "and"'s stop. Blanks are ignored: "1|and||and|3" → {1, 3}
while args[i + 2] == options.andKeyword do
if args[i + 3] then
table.insert(forRow.pages, args[i + 3])
end
-- Increment to next "and"
i = i + 2
end
end
-- Increment to next use
end
i = i + 2
-- Append the row
table.insert(forTable, forRow)
until terminated or i > maxArg
return forTable
local navbar_style = args.style
end
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


-- Stringifies a table as formatted by forSeeArgsToTable
local box_text = (args.text or cfg.box_text) .. ' '
function p.forSeeTableToString (forSeeTable, options)
-- the concatenated space guarantees the box text is separated
-- Type-checks and defaults
if not (is_mini or is_plain) then
checkType("forSeeTableToString", 1, forSeeTable, "table", true)
div
checkType("forSeeTableToString", 2, options, "table", true)
:tag('span')
options = options or {}
:addClass(cfg.classes.box_text)
for k, v in pairs(forSeeDefaultOptions) do
:cssText(font_style)
if options[k] == nil then options[k] = v end
:wikitext(box_text)
end
end
-- Stringify each for-see item into a list
local strList = {}
local template = args.template
if forSeeTable then
local displayed_links = choose_links(template, args)
for k, v in pairs(forSeeTable) do
local has_brackets = args.brackets
local useStr = v.use or options.otherText
local title_arg = get_title_arg(is_collapsible, template)
local pagesStr = p.andList(v.pages, true) or mHatnote._formatLink{link = mHatnote.disambiguate(options.title)}
local title_text = args[title_arg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
local forSeeStr = string.format(options.forSeeForm, useStr, pagesStr)
local list = make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
forSeeStr = punctuationCollapse(forSeeStr)
div:node(list)
table.insert(strList, forSeeStr)
 
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
end
div:done()
:tag('div')
:addClass(title_text_class)
:cssText(font_style)
:wikitext(args[1])
end
end
if options.extratext then table.insert(strList, punctuationCollapse(options.extratext..'.')) end
-- Return the concatenated list
return mw.getCurrentFrame():extensionTag{
return table.concat(strList, ' ')
name = 'templatestyles', args = { src = cfg.templatestyles }
end
} .. tostring(div:done())
 
-- Produces a "For X, see [[Y]]" string from arguments. Expects index gaps
-- but not blank/whitespace values. Ignores named args and args < "from".
function p._forSee (args, from, options)
local forSeeTable = p.forSeeArgsToTable(args, from, options)
return p.forSeeTableToString(forSeeTable, options)
end
end


-- As _forSee, but uses the frame.
function p.navbar(frame)
function p.forSee (frame, from, options)
return p._navbar(require('Module:Arguments').getArgs(frame))
mArguments = require('Module:Arguments')
return p._forSee(mArguments.getArgs(frame), from, options)
end
end


return p
return p

Latest revision as of 19:06, 13 December 2020

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

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'] = title:fullUrl('action=edit'), ['url'] = true },
		{ ['mini'] = 'h', ['full'] = 'hist', ['html_title'] = 'History of this template',
			['link'] = title:fullUrl('action=history'), ['url'] = true },
		{ ['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
	
	return mw.getCurrentFrame():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