Модуль:Common: различия между версиями
мНет описания правки |
мНет описания правки |
||
Строка 1: | Строка 1: | ||
local common = {} -- common functions package for common usage through Teopedia Library pages | 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). | -- Return TRUE if string is empty or not defined (null). | ||
Строка 18: | Строка 19: | ||
function common.page_id( frame ) | function common.page_id( frame ) | ||
local vNum = frame.args['volume'] | local vNum = frame.args['volume'] | ||
local pNum = string.format( | local nFormat = '%.3d' | ||
local pNum = string.format(nFormat, frame.args['page']) | |||
-- Check if we should alter default values | -- Check if we should alter default values | ||
if not isempty(frame.args['volume_digits']) then | if not isempty(frame.args['volume_digits']) then | ||
nFormat = '%.'.. frame.args['volume_digits'] ..'d' | |||
vNum = string.format( | vNum = string.format(nFormat, frame.args['volume']) | ||
end | end | ||
if not isempty(frame.args['page_digits']) then | if not isempty(frame.args['page_digits']) then | ||
nFormat = '%.'.. frame.args['page_digits'] ..'d' | |||
pNum = string.format( | pNum = string.format(nFormat, frame.args['page']) | ||
end | |||
-- Check if we deal with Roman number | |||
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_digits'] ..')' | |||
end | end | ||
Версия от 04:18, 1 февраля 2024
Для документации этого модуля может быть создана страница Модуль: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 nFormat = '%.3d'
local 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
-- Check if we deal with Roman number
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_digits'] ..')'
end
return vNum .. ':' .. pNum
end
return common