Модуль:Common
Для документации этого модуля может быть создана страница Модуль: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_number_type=roman
-- }}
-- By default volume has 1 digit, page has 3 digits and page number in arabic
-- style, but this could be changed.
function common.page_id( frame )
local vNum = frame.args['volume']
local pNum = frame.args['page']
local nFormat = '%.3d'
-- If we deal with regular number then for v.1, p.17 return 1:017
if not isempty(frame.args['page_number_type']) then
pNum = string.format(nFormat, frame.args['page'])
-- Check if we should alter default values
if not isempty(frame.args['volume_digits']) then
nFormat = '%.'.. frame.args['volume_digits'] ..'d'
vNum = string.format(nFormat, frame.args['volume'])
end
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 frame.args['page_number_type'] == 'roman' 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(value)
-- 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