Module:Information/slim

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Examples[edit]

Code Render
Test simple case:

{{#invoke:Information/slim|information
|description    = description
|date           = 2024-05-30
|source         = {{own}}
|author         = author
|permission     = permission     
|other versions = <gallery>File:Image-x-generic - black.svg</gallery>
}}
Description description
Date
Source Own work
Author author
Permission
(Reusing this file)
permission
Other versions


Test case with missing source, author and description:

{{#invoke:Information/slim|information
|description    = 
|date           = 2024-05-30   
|source         = 
|author         = 
}}
Description
Date
Source
Author


More examples[edit]

{{#invoke:Information/slim|information | description = {{Waarneming.nl|42121}} | date = {{Waarneming.nl|42121}} | source = {{Waarneming.nl|42121}} | author = {{Waarneming.nlUser|4190|Tello Neckheim}} |permission = {{PD-old-100-expired}} }}

gives

Description {| style="width:100%; border:1px solid #aaa; background:#efd; text-align:center"

| | This image is uploaded as image number 42121 at Waarneming.nl, a source of nature observations in the Netherlands.
This tag does not indicate the copyright status of the attached work. A normal copyright tag is still required. See Commons:Licensing for more information.

|}
Date {| style="width:100%; border:1px solid #aaa; background:#efd; text-align:center"

| | This image is uploaded as image number 42121 at Waarneming.nl, a source of nature observations in the Netherlands.
This tag does not indicate the copyright status of the attached work. A normal copyright tag is still required. See Commons:Licensing for more information.

|}
Source {| style="width:100%; border:1px solid #aaa; background:#efd; text-align:center"

| | This image is uploaded as image number 42121 at Waarneming.nl, a source of nature observations in the Netherlands.
This tag does not indicate the copyright status of the attached work. A normal copyright tag is still required. See Commons:Licensing for more information.

|}
Author {| style="width:100%; border:1px solid #aaa; background:#efd; text-align:center"

| | This image is created by user Tello Neckheim at Waarneming.nl, a source of nature observations in the Netherlands.

|}
Permission
(Reusing this file)
{| class="licensetpl" style="display:none"

|Public domainPublic domainfalsefalse |}

Public domain

This work is in the public domain in its country of origin and other countries and areas where the copyright term is the author's life plus 100 years or fewer.


This work is in the public domain in the United States because it was published (or registered with the U.S. Copyright Office) before January 1, 1929.

Code

--[[  
  __  __           _       _        ___        __                            _   _             
 |  \/  | ___   __| |_   _| | ___ _|_ _|_ __  / _| ___  _ __ _ __ ___   __ _| |_(_) ___  _ __  
 | |\/| |/ _ \ / _` | | | | |/ _ (_)| || '_ \| |_ / _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \ 
 | |  | | (_) | (_| | |_| | |  __/_ | || | | |  _| (_) | |  | | | | | | (_| | |_| | (_) | | | |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)___|_| |_|_|  \___/|_|  |_| |_| |_|\__,_|\__|_|\___/|_| |_|
                                                                                               
This module is intended to be the engine behind "Template:Information".

Please do not modify this code without applying the changes first at
"Module:Information/sandbox" and testing at "Module:Information/testcases".

Authors and maintainers:
* User:Jarekt - original version 
]]
local ISOdate = require('Module:ISOdate')

-- ====================================================================
-- === This function is just responsible for producing HTML of the  ===
-- === template. At this stage all the fields are already filed     ===
-- ====================================================================
local function Build_html(args)
	
	-- get text direction
	local dir = 'ltr'
	if mw.language.new( args.lang ):isRTL() then
		dir = 'rtl'
	end 

	-- Permissions tag
	local tag1 = mw.message.new( "wm-license-information-permission" ):inLanguage(args.lang):plain()
	local tag2 = mw.message.new( "wm-license-information-permission-reusing-link" ):inLanguage(args.lang):plain()
	local tag3 = mw.message.new( "wm-license-information-permission-reusing-text" ):inLanguage(args.lang):plain()
	local permission_tag  = string.format("%s<br /><small>([[%s|%s]])</small>", tag1, tag2, tag3)
	tag1 = mw.message.new( "wm-license-information-description" ):inLanguage(args.lang):plain()
	tag2 = mw.title.getCurrentTitle().text -- pagename
	local description_tag = string.format('%s<span class="summary fn" style="display:none">%s</span>', tag1, tag2)
	tag3 = string.format(' lang="%s"', args.lang)

	-- add other fields
	local params = {
		{field='description'    , id='fileinfotpl_desc'  , tag=description_tag,                  alt='Description missing', wrapper='%s', td=' class="description"'},
		{field='other_fields_1'},
		{field='date'           , id='fileinfotpl_date'  , tag='wm-license-information-date',                               wrapper='%s', td=tag3},
		{field='source'         , id='fileinfotpl_src'   , tag='wm-license-information-source',  alt='Source missing',      wrapper='%s'}, 
		{field='author'         , id='fileinfotpl_aut'   , tag='wm-license-information-author',  alt='Author missing',      wrapper='%s'},
		{field='permission'     , id='fileinfotpl_perm'  , tag=permission_tag,                                              wrapper='%s'},
		{field='other_versions' , id='fileinfotpl_ver'   , tag='wm-license-information-other-versions',                     wrapper='%s'}, 
		{field='other_fields'}
	}
	local results = {}
	for _, param in ipairs(params) do
		local field, tag, cell1, cell2, id
		field = args[param.field]
		if field and mw.ustring.match(field,"^[%s%p]+$") then field=nul; end -- ignore panctuatin only fields

		if param.id then -- skip "other fields" parameter
			if field then  -- add "id" to first <td> cell only if the field is present
				id = string.format('id="%s" ', param.id)
			elseif param.alt then -- Some field have "Missing" message if field is not provided
				field = mw.getCurrentFrame():expandTemplate{ title = param.alt }
			end
			if field or args.demo then  -- skip the row if still no field
				tag = param.tag
				if string.sub(tag,1,10) == 'wm-license' then
					tag = mw.message.new( tag ):inLanguage(args.lang):plain() -- label message in args.lang language
				end
				cell1 = string.format('<td %sclass="fileinfo-paramfield" lang="%s">%s</td>\n', id or '', args.lang, tag)
				cell2 = string.format('<td%s>'.. param.wrapper ..'</td>', param.td or '', field or '')
				field = string.format('<tr style="vertical-align: top">\n%s%s\n</tr>\n\n', cell1, cell2)
			end
		end
		table.insert(results, field)
	end

	-- add table and outer layers
	local style = string.format('class="fileinfotpl-type-information toccolours vevent mw-content-%s" style="width: 100%%; direction: %s;" cellpadding="4"', dir, dir)
	results = string.format('<table %s>\n\n%s\n</table>\n', style, table.concat(results))
	results = string.format('<div class="hproduct commons-file-information-table">\n%s\n</div>\n', results)
	return results
end

-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}

-- ===========================================================================
-- === Version of the function to be called from other LUA codes
-- ===========================================================================
function p._information(args)
	
	-- ====================================================
	-- === add tag templates used for tracking          === 
	-- ====================================================
	local frame = mw.getCurrentFrame()
	if args.date then
		args.date = ISOdate._ISOdate(args.date, args.lang, '', 'dtstart', '100-999')      -- apply ISODate to function to date string to convert date in ISO format to translated date string
	    -- add an empty template which can be used as a tag in PetScan
		local d    = os.date('!*t')                   -- current date table
		local current_year  = tonumber(d.year)        -- current year
		local creation_year = tonumber(ISOdate._ISOyear(args.date))
		if creation_year and current_year and (current_year-creation_year)>200 then
			frame:expandTemplate{ title ='Works created more than 200 years ago' }
		end
	end 
	frame:expandTemplate{ title = 'Infobox template tag' } -- add the template tag
	return frame:preprocess(Build_html(args))
end

-- ===========================================================================
-- === Version of the function to be called from template namespace
-- ===========================================================================
function p.information(frame)
	-- switch to lowercase parameters to make them case independent
	local args = {}
	for name, value in pairs( frame:getParent().args ) do 
		if value ~= '' then -- nuke empty strings
			local name1 = string.gsub( string.lower(name), ' ', '_')
			args[name1] = value
		end
	end
	for name, value in pairs( frame.args ) do 
		if value ~= '' then -- nuke empty strings
			local name1 = string.gsub( string.lower(name), ' ', '_')
			args[name1] = value
		end
	end
	
	if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then 
		args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
	end
	
	-- call the inner "core" function
	return p._information(args)	
end

return p