Модуль:SummaryII/dependencies

Материал из свободной русской энциклопедии «Традиция»
Перейти к навигации Перейти к поиску

Для документации этого модуля может быть создана страница Модуль:SummaryII/dependencies/doc

--[[
	Dependencies:
--]]
local type	= type

local str	= mw.ustring or string
local array = require 'Module:Array' or {}
local formatter = require 'Module:FormatterII' or {}
--formatter.config.regex = 'pcre' -- pcre2 is not yet available at this server.
formatter.initialise ()

local function clone (tbl)	
	local cloned
  	if type (tbl) == 'table' then
  		cloned = {}
  		setmetatable (cloned, getmetatable (tbl))
  		for key, value in pairs (tbl) do
  			cloned [key] = clone (value)
  		end
  	else
  		cloned = tbl
  	end
	return cloned
end	-- local function clone (tbl)

local function unpack_args (args)
	local cloned = {}
	for key, value in pairs (args) do
		cloned [key] = value
	end
	return unpack (cloned)
end

local function wrapper (func)
	return function (first, ...)
		if type (first) == 'table' and first.args then
			return func (unpack_args (first.args))
		else
			return func (first, ...)
		end
	end
end

return {
	wrap	= coroutine.wrap
  , yield	= coroutine.yield
  
  , sort	= table.sort
  , join	= table.concat
  , clone	= mw.clone or clone
  , merge	= function (...)
		local type = type
		local merged = {}
		for _, tbl in ipairs {...} do
			for key, value in ipairs (type (tbl) == 'table' and tbl or {tbl}) do
				merged [#merged + 1] = value
			end
			for key, value in pairs (tbl) do
				if type (key) ~= 'number' then
					merged [key] = value
				end
			end
		end
		return merged
	end	-- merge	= array.table_merge or function (...)
  , merge_to_first	= array.table_merge_to_first or function (first, ...)
  		local type = type
		for _, tbl in ipairs {...} do
			for key, value in pairs (type (tbl) == 'table' and tbl or {tbl}) do
				first [key] = value
			end
		end
	return first
	end	-- table_merge_to_first	= array.table_merge or function (first, ...)
  , keys	= array.table_keys or function (tbl)
		local keys = {}
		for key, _ in pairs (tbl) do
			keys [#keys + 1] = key
		end
		return keys
	end	-- keys	= array.table_keys or function (tbl)
  , lc		= str.lower
  , sub		= str.sub
  , gsub	= str.gsub
  , find	= str.find
  , gmatch	= str.gmatch
  , len		= str.len
  , trim	= mw.text.trim	or function (s)
		return s:gsub ('^%s*(.-)%s*$', '%1')
	end
  , split	= mw.text.split	or function (s, sep)
		sep = sep or '%s'
		local t = {}
		for str in str.gmatch (s, '([^' .. sep .. ']+)') do
			table.insert (t, str)
		end
		return t
	end	-- function (s, sep)
  , gsplit = mw.text.gsplit
  , Set = require 'Module:Set' -- The Set library.
  , regex	= (rex_pcre or require 'lrexlib.so').new
  ,	lpeg	= lpeg or require 'lualpeg.so'
  , re		= (require 'Module:Re' or require 're.lua').compile
  
	-- MediaWiki:
  , title		= mw.title
  , uri			= mw.uri
  , namespaces	= mw.site.namespaces
  , namespace_ids = {
  		all = (function ()
  			local namespaces = {}
  			for id, _ in pairs (mw.site.namespaces) do
  				namespaces [#namespaces + 1] = id
	  		end
  			return namespaces
  		end) (),
		content = (function ()
  			local namespaces = {}
  			for id, config in pairs (mw.site.namespaces) do
	  			if config.isContent then
  					namespaces [#namespaces + 1] = id
  				end
  			end
  			return namespaces
		end) (),
		subject = (function ()
  			local namespaces = {}
  			for id, config in pairs (mw.site.namespaces) do
	  			if config.isSubject then
  					namespaces [#namespaces + 1] = id
  				end
  			end
  			return namespaces
  		end) ()
	}

	-- Semantic MediaWiki:
  , ask		= mw.smw.ask
  , set		= mw.smw.set
  
  , formatter	= formatter.formatter
  , tokens		= {
  		open	= formatter.config.open,
  		pipe	= formatter.config.pipe,
  		close	= formatter.config.close,
  		delim	= '~',
  		equals	= '='
  	}
  , templates	= {
  		card	= 'SummaryII/card',
  		section	= 'SummaryII/section',
  		item	= 'SummaryII/item'
	}
  , wrapper = wrapper
}	-- return {...}