Модуль:Common: различия между версиями
мНет описания правки |
мНет описания правки |
||
(не показаны 3 промежуточные версии этого же участника) | |||
Строка 13: | Строка 13: | ||
-- {{#invoke: Common | page_id | volume=1 | page=17 | -- {{#invoke: Common | page_id | volume=1 | page=17 | ||
-- | volume_digits=2 | page_digits=4 | -- | volume_digits=2 | page_digits=4 | ||
-- }} | -- }} | ||
-- By default volume has 1 digit, page has 3 digits | -- page could be Roman (XVII, xvii) or Arabic (17) | ||
-- By default volume has 1 digit, page has 3 digits. | |||
function common.page_id( frame ) | function common.page_id( frame ) | ||
local vNum = frame.args['volume'] | local vNum = frame.args['volume'] | ||
Строка 22: | Строка 21: | ||
local nFormat = '%.3d' | local nFormat = '%.3d' | ||
-- If we deal with regular number then for v.1, p.17 return 1:017 | -- Check if we should alter volume number | ||
if not | 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']) | pNum = string.format(nFormat, frame.args['page']) | ||
if not isempty(frame.args['page_digits']) then | if not isempty(frame.args['page_digits']) then | ||
nFormat = '%.'.. frame.args['page_digits'] ..'d' | nFormat = '%.'.. frame.args['page_digits'] ..'d' | ||
Строка 38: | Строка 38: | ||
-- If we deal with Roman number then for v.1, p.xvii return 1:000017(xvii) | -- If we deal with Roman number then for v.1, p.xvii return 1:000017(xvii) | ||
if frame.args[' | if roman.isRoman(frame.args['page']) then | ||
-- 1. Fill with zeros all places for arabic digits for correct sorting. | -- 1. Fill with zeros all places for arabic digits for correct sorting. | ||
pNum = '000' | pNum = '000' | ||
Строка 47: | Строка 47: | ||
-- 2. Convert number from Roman to Arabic | -- 2. Convert number from Roman to Arabic | ||
local pArabic = roman.toArabic( | local pArabic = roman.toArabic(frame.args['page']) | ||
-- 3. Add zeros to this number | -- 3. Add zeros to this number |
Текущая версия от 05:49, 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 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