Модуль:Common

<div style="color: #555555; font-size: 80%; font-style: italic; font-family: serif; text-align: center;">Материал из '''Библиотеки Теопедии''', http://ru.teopedia.org/lib</div>
Перейти к навигации Перейти к поиску

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

local common = {} -- common functions package for common usage through Teopedia Library pages
local roman = require('Module:Roman') -- To deal with Roman numbers

-- Return TRUE if string is empty or not defined (null).
local function isempty(s)
  return s == nil or s == ''
end

-- Create unique sortable page ID in multy-volume book: V:PPP
-- Used in: Шаблон:Исходный текст ТД начинается
-- ID example: 1-017
-- Call examples: {{#invoke: Common | page_id | volume=1 | page=17 }}
--   {{#invoke: Common | page_id | volume=1 | page=17
--    | volume_digits=2 | page_digits=4
--   }}
-- page could be Roman (XVII, xvii) or Arabic (17)
-- By default volume has 1 digit, page has 3 digits.
function common.page_id( frame )
	local vNum = frame.args['volume']
	local pNum = frame.args['page']
	local nFormat = '%.3d'

	-- Check if we should alter volume number
	if not isempty(frame.args['volume_digits']) then
		nFormat = '%.'.. frame.args['volume_digits'] ..'d'
		vNum = string.format(nFormat, frame.args['volume'])
	end

	-- If we deal with regular page number then for v.1, p.17 return 1:017
	if not roman.isRoman(frame.args['page']) then
		pNum = string.format(nFormat, frame.args['page'])

		if not isempty(frame.args['page_digits']) then
			nFormat = '%.'.. frame.args['page_digits'] ..'d'
			pNum = string.format(nFormat, frame.args['page'])
		end
	end
	
	-- If we deal with Roman number then for v.1, p.xvii return 1:000017(xvii)
	if roman.isRoman(frame.args['page']) then
		-- 1. Fill with zeros all places for arabic digits for correct sorting.
		pNum = '000'
		if not isempty(frame.args['page_digits']) then
			nFormat = '%.'.. frame.args['page_digits'] ..'d'
			pNum = string.format(nFormat, 0)
		end
		
		-- 2. Convert number from Roman to Arabic
		local pArabic = roman.toArabic(frame.args['page'])
		
		-- 3. Add zeros to this number
		local nLenght = 3
		if not isempty(frame.args['page_digits']) then
			nLenght = frame.args['page_digits']
		end
		nFormat = '%.'.. nLenght ..'d'
		pArabic = string.format(nFormat, pArabic)
			
		-- 4. Add original Roman number after Arabic
		pNum = pNum .. pArabic ..'('.. frame.args['page'] ..')'
	end

	return vNum .. ':' .. pNum 
end

return common