Jump to content
Toggle menu
  • 12,3 E szócikkek
  • 21,5 E fájlok
  • 4 felhasználók
  • 63,9 E szerkesztések
SCWIKI
Toggle preferences menu
Toggle personal menu
Nem vagy bejelentkezve
Your IP address will be publicly visible if you make any edits.
Module documentation[view][edit][history][purge]
This documentation is transcluded from Modul:JumpPointMagazine/doc. Changes can be proposed in the talk page.
Function list
L 22 — t
L 30 — startsWith
L 38 — endsWith
L 43 — trim
L 50 — split
L 59 — convertCategories
L 74 — JPM.main

Implements {{JumpPointMagazine}}


require( 'strict' )

local JPM = {}

local Date = require('Module:Date')._Date
local Infobox = require( 'Module:InfoboxNeue' )
local TNT = require( 'Module:Translate' ):new()
local config = mw.loadJsonData( 'Module:JumpPointMagazine/config.json' )

local lang
if config[ 'module_lang' ] then
	lang = mw.getLanguage( config[ 'module_lang' ] )
else
	lang = mw.getContentLanguage()
end
local langCode = lang:getCode()

--- Wrapper function for Module:Translate.translate
-- @param key string The translation key
-- @param addSuffix boolean Adds a language suffix if config.smw_multilingual_text is true
-- @return string If the key was not found in the .tab page, the key is returned
local function t( key, addSuffix, ... )
	return TNT:translate( 'Module:JumpPointMagazine/i18n.json', config, key, addSuffix, {...} ) or key
end

--- Does string start with x
-- @param str string
-- @param prefix string
-- @return boolean
local function startsWith( str, prefix )
    return string.sub( str, 1, string.len( prefix ) ) == prefix
end

--- Does string end with x
-- @param str string
-- @param suffix string
-- @return boolean
local function endsWith( str, suffix )
    return string.sub( str, -string.len( suffix ) ) == suffix
end

-- @param str string
local function trim( str )
    return (str:gsub("^%s*(.-)%s*$", "%1"))
end

--- Split a string with seperator
-- @param str string Input
-- @param sep string Seperator
local function split( str, sep )
	local matches = {}
	for str in string.gmatch( str, '([^' .. sep .. ']+)' ) do
		table.insert( matches, string.gsub( str, '%b()', '' ) or '' )
	end
	return matches
end

-- @param categories table Plain text categories in array
local function convertCategories( categories )
	local mapped = {}
	for _, category in pairs( categories ) do
		if category ~= nil then
			if string.sub( category, 1, 2 ) ~= '[[' then
				category = string.format( '[[Category:%s]]', category )
			end

			table.insert( mapped, category )
		end
	end
	return table.concat( mapped )
end

-- @param frame table https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object
function JPM.main( frame )
	local args = frame:getParent().args
	local infobox = Infobox:new( { placeholderImage = config[ 'placeholder_image' ] } )

	--- The idea of this mega table is to get all data, then set it in the infobox, better organization.
	local mega = {
		[ 'image' ] = nil,
		[ 'title' ] = nil,
		[ 'total_number' ] = nil,
		[ 'number' ] = nil,
		[ '#number' ] = nil,
		[ 'year' ] = nil,
		[ 'published' ] = nil,
		[ '#published' ] = nil,
		[ 'pages' ] = {},
		[ 'editor_and_writer' ] = nil,
		[ '#editor_and_writer' ] = nil,
		[ 'copy_editor' ] = nil,
		[ '#copy_editor' ] = "",
		[ 'layout' ] = nil,
		[ '#layout' ] = "",
		[ 'correspondents' ] = nil,
		[ '#correspondents' ] = "",
		[ 'categories' ] = {},
	}

	table.insert( mega[ 'categories' ], 'Jump Point (magazine)' ) -- Default category for JP magazines

	mega[ 'image' ] = args[ 'image' ]
	mega[ 'title' ] = args[ 'title' ]
	mega[ 'total_number' ] = args[ 'total_number' ]
	mega[ 'number' ] = args[ 'number' ]
	if mega[ 'title' ] == nil and mega[ 'number' ] == nil then
		return infobox:renderInfobox( infobox:renderMessage( {
			title = t( 'error_title' ),
			desc = t( 'error_invalid_args_desc' )
		} ) )
	end

	mega[ '#number' ] = mega[ 'number' ]
	--- Add 'Issue ' to start of `#number`
	if startsWith( mega[ '#number' ], 'Issue ' ) == false then
		mega[ '#number' ] = 'Issue ' .. mega[ '#number' ]
	end

	mega[ 'year' ] = args[ 'year' ]
	if mega[ 'year' ] ~= nil then
		table.insert( mega[ 'categories' ], 'Jump Point Year ' .. mega[ 'year' ] )
	end

	mega[ 'published' ] = args[ 'published' ]
	if mega[ 'published' ] ~= nil then
		mega[ 'published' ] = Date( mega[ 'published' ] ):text('ymd')
		mega[ '#published' ] = Date( mega[ 'published' ] ):text('mdy')
	end

	mega[ 'pages' ] = args[ 'pages' ]
	if mega[ 'pages' ] ~= nil and tonumber( mega[ 'pages' ] ) then
		mega[ 'pages' ] = tonumber( mega[ 'pages' ] )
		mega[ '#pages' ] = tostring( mega[ 'pages' ] )
	else mega[ '#pages' ] = '?' end

	mega[ 'editor_and_writer' ] = args[ 'editor_and_writer' ]
	if mega[ 'editor_and_writer' ] ~= nil and startsWith( mega[ 'editor_and_writer' ], '[[' ) == false and endsWith( mega[ 'editor_and_writer' ], ']]' ) == false then
		mega[ '#editor_and_writer' ] = '[[' .. mega[ 'editor_and_writer' ] .. ']]'
	end

	mega[ 'copy_editor' ] = args[ 'copy_editor' ]
	if mega[ 'copy_editor' ] ~= nil and startsWith( mega[ 'copy_editor' ], '[[' ) == false and endsWith( mega[ 'copy_editor' ], ']]' ) == false then
		local list = split(mega[ 'copy_editor' ], ',')
		for i,v in ipairs(list) do
			mega[ '#copy_editor' ] = mega[ '#copy_editor' ] .. '[[' .. trim(v) .. ']]'
			if i < #list then
				mega[ '#copy_editor' ] = mega[ '#copy_editor' ] .. ', '
			end
		end
	end

	mega[ 'layout' ] = args[ 'layout' ]
	if mega[ 'layout' ] ~= nil and startsWith( mega[ 'layout' ], '[[' ) == false and endsWith( mega[ 'layout' ], ']]' ) == false then
		local list = split(mega[ 'layout' ], ',')
		for i,v in ipairs(list) do
			mega[ '#layout' ] = mega[ '#layout' ] .. '[[' .. trim(v) .. ']]'
			if i < #list then
				mega[ '#layout' ] = mega[ '#layout' ] .. ', '
			end
		end
	end

	mega[ 'correspondents' ] = args[ 'correspondents' ]
	if mega[ 'correspondents' ] ~= nil and startsWith( mega[ 'correspondents' ], '[[' ) == false and endsWith( mega[ 'correspondents' ], ']]' ) == false then
		local list = split(mega[ 'correspondents' ], ',')
		for i,v in ipairs(list) do
			mega[ '#correspondents' ] = mega[ '#correspondents' ] .. '[[' .. trim(v) .. ']]'
			if i < #list then
				mega[ '#correspondents' ] = mega[ '#correspondents' ] .. ', '
			end
		end
	end

	infobox:renderImage( mega[ 'image' ] )
	infobox:renderHeader( {
		title = mega[ 'title' ],
		subtitle = mega[ '#number' ]
	} )
	infobox:renderSection( {
		content = {
			infobox:renderItem( {
				label = t( 'lbl_published' ),
				data = mega[ '#published' ],
			} ),
			infobox:renderItem( {
				label = t( 'lbl_pages' ),
				data = mega[ '#pages' ],
			} )
		},
		col = 2
	} )
	infobox:renderSection( {
		title = t( 'lbl_editorial_team' ),
		content = {
			infobox:renderItem( {
				label = t( 'lbl_editor_and_writer' ),
				data = mega[ '#editor_and_writer' ]
			} ),
			infobox:renderItem( {
				label = t( 'lbl_copy_editor' ),
				data = mega[ '#copy_editor' ]
			} ),
			infobox:renderItem( {
				label = t( 'lbl_layout' ),
				data = mega[ '#layout' ]
			} ),
			infobox:renderItem( {
				label = t( 'lbl_correspondents' ),
				data = mega[ '#correspondents' ]
			} )
		},
		col = 2
	} )

	-- Set Semantic MediaWiki properties
	mw.smw.set( {
		[ t( 'lbl_title' ) ] = mega[ 'title' ],
		[ t( 'lbl_total_number' ) ] = mega[ 'total_number' ],
		[ t( 'lbl_number' ) ] = mega[ 'number' ],
		[ t( 'lbl_year' ) ] = mega[ 'year' ],
		[ t( 'lbl_published' ) ] = mega[ 'published' ],
		[ t( 'lbl_pages' ) ] = mega[ 'pages' ]
	} )

	return tostring( infobox:renderInfobox( nil, mega[ 'title' ] ) ) .. convertCategories( mega[ 'categories' ] )
end

return JPM