Anonymous
×
Create a new article
Write your page title here:
We currently have 15 articles on TwistedFates Database. Type your article name above or click on one of the titles below and start writing!



TwistedFates Database
15Articles

Module:Navbar and Module:File link: Difference between pages

(Difference between pages)
wikip>Izno
(some refactoring + templatestyles)
 
m (1 revision imported)
 
Line 1: Line 1:
-- This module provides a library for formatting file wikilinks.
local yesno = require('Module:Yesno')
local checkType = require('libraryUtil').checkType
local p = {}
local p = {}
local cfg = mw.loadData('Module:Navbar/configuration')


local function get_title_arg(is_collapsible, template)
function p._main(args)
local title_arg = 1
checkType('_main', 1, args, 'table')
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)
-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our
-- The show table indicates the default displayed items.
-- own function to get the right error level.
-- view, talk, edit, hist, move, watch
local function checkArg(key, val, level)
-- TODO: Move to configuration.
if type(val) ~= 'string' then
local show = {true, true, true, false, false, false}
error(string.format(
if template then
"type error in '%s' parameter of '_main' (expected string, got %s)",
show[2] = false
key, type(val)
show[3] = false
), level)
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
end
end


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


local function add_link(link_description, ul, is_mini, font_style)
-- Adds a positional parameter to the buffer.
local l
local function addPositional(key)
if link_description.url then
local val = args[key]
l = {'[', '', ']'}
if not val then
else
return nil
l = {'[[', '|', ']]'}
end
checkArg(key, val, 4)
ret[#ret + 1] = val
end
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)
-- Adds a named parameter to the buffer. We assume that the parameter name
-- is the same as the argument key.
local title = mw.title.new(mw.text.trim(title_text), cfg.title_namespace)
local function addNamed(key)
if not title then
local val = args[key]
error(cfg.invalid_title .. title_text)
if not val then
return nil
end
checkArg(key, val, 4)
ret[#ret + 1] = key .. '=' .. val
end
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')
-- Filename
if has_brackets then
checkArg('file', args.file, 3)
ul:addClass(cfg.classes.brackets)
ret[#ret + 1] = 'File:' .. args.file
:cssText(font_style)
 
-- Format
if args.format then
checkArg('format', args.format)
if args.formatfile then
checkArg('formatfile', args.formatfile)
ret[#ret + 1] = args.format .. '=' .. args.formatfile
else
ret[#ret + 1] = args.format
end
end
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)
-- Border
if yesno(args.border) then
-- TODO: We probably don't need both fontstyle and fontcolor...
ret[#ret + 1] = 'border'
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
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
addPositional('location')
addPositional('alignment')
addPositional('size')
addNamed('upright')
addNamed('link')
addNamed('alt')
addNamed('page')
addNamed('class')
addNamed('lang')
addNamed('start')
addNamed('end')
addNamed('thumbtime')
addPositional('caption')


local box_text = (args.text or cfg.box_text) .. ' '
return string.format('[[%s]]', table.concat(ret, '|'))
-- the concatenated space guarantees the box text is separated
end
if not (is_mini or is_plain) then
 
div
function p.main(frame)
:tag('span')
local origArgs = require('Module:Arguments').getArgs(frame, {
:addClass(cfg.classes.box_text)
wrappers = 'Template:File link'
:cssText(font_style)
})
:wikitext(box_text)
if not origArgs.file then
error("'file' parameter missing from [[Template:File link]]", 0)
end
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
-- Copy the arguments that were passed to a new table to avoid looking up
local title_text_class
-- every possible parameter in the frame object.
if is_mini then
local args = {}
title_text_class = cfg.classes.collapsible_title_mini
for k, v in pairs(origArgs) do
else
-- Make _BLANK a special argument to add a blank parameter. For use in
title_text_class = cfg.classes.collapsible_title_full
-- conditional templates etc. it is useful for blank arguments to be
-- ignored, but we still need a way to specify them so that we can do
-- things like [[File:Example.png|link=]].
if v == '_BLANK' then
v = ''
end
end
div:done()
args[k] = v
:tag('div')
:addClass(title_text_class)
:cssText(font_style)
:wikitext(args[1])
end
end
return p._main(args)
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
end


return p
return p

Latest revision as of 21:26, 21 May 2021

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

-- This module provides a library for formatting file wikilinks.

local yesno = require('Module:Yesno')
local checkType = require('libraryUtil').checkType

local p = {}

function p._main(args)
	checkType('_main', 1, args, 'table')

	-- This is basically libraryUtil.checkTypeForNamedArg, but we are rolling our
	-- own function to get the right error level.
	local function checkArg(key, val, level)
		if type(val) ~= 'string' then
			error(string.format(
				"type error in '%s' parameter of '_main' (expected string, got %s)",
				key, type(val)
			), level)
		end
	end

	local ret = {}

	-- Adds a positional parameter to the buffer.
	local function addPositional(key)
		local val = args[key]
		if not val then
			return nil
		end
		checkArg(key, val, 4)
		ret[#ret + 1] = val
	end

	-- Adds a named parameter to the buffer. We assume that the parameter name
	-- is the same as the argument key.
	local function addNamed(key)
		local val = args[key]
		if not val then
			return nil
		end
		checkArg(key, val, 4)
		ret[#ret + 1] = key .. '=' .. val
	end

	-- Filename
	checkArg('file', args.file, 3)
	ret[#ret + 1] = 'File:' .. args.file

	-- Format
	if args.format then
		checkArg('format', args.format)
		if args.formatfile then
			checkArg('formatfile', args.formatfile)
			ret[#ret + 1] = args.format .. '=' .. args.formatfile
		else
			ret[#ret + 1] = args.format
		end
	end

	-- Border
	if yesno(args.border) then
		ret[#ret + 1] = 'border'
	end

	addPositional('location')
	addPositional('alignment')
	addPositional('size')
	addNamed('upright')
	addNamed('link')
	addNamed('alt')
	addNamed('page')
	addNamed('class')
	addNamed('lang')
	addNamed('start')
	addNamed('end')
	addNamed('thumbtime')
	addPositional('caption')

	return string.format('[[%s]]', table.concat(ret, '|'))
end

function p.main(frame)
	local origArgs = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:File link'
	})
	if not origArgs.file then
		error("'file' parameter missing from [[Template:File link]]", 0)
	end

	-- Copy the arguments that were passed to a new table to avoid looking up
	-- every possible parameter in the frame object.
	local args = {}
	for k, v in pairs(origArgs) do
		-- Make _BLANK a special argument to add a blank parameter. For use in
		-- conditional templates etc. it is useful for blank arguments to be
		-- ignored, but we still need a way to specify them so that we can do
		-- things like [[File:Example.png|link=]].
		if v == '_BLANK' then
			v = ''
		end
		args[k] = v
	end
	return p._main(args)
end

return p