Module:PliMulti/Data
Apparence
La documentation pour ce module peut être créée à Module:PliMulti/Data/Documentation
local p = {}
local t = {} --nom de la sous-page:nopage du f-s
local ecarts = {} --table des changements d’écarts (nopage du livre:écart avec page du f-s)
local pageRoot
local ns
local pageNom
local pageObj
local pageIndex
function initSpace()
pageObj = mw.title.getCurrentTitle()
pageNom = pageObj.text
ns = pageObj.nsText
if ns == 'Page' then
pageIndex = 'Livre:' .. pageObj.baseText
elseif ns == '' then
content = pageObj:getContent(pageObj)
pageIndex = 'Livre:' .. mw.ustring.match(content, 'index="(.-)"')
end
pageRoot = mw.title.new(pageIndex)
content = pageRoot:getContent(pageRoot)
pageRoot = mw.ustring.match( content, '|Titre=%[%[(.-)[|%]]',1)
--pageRoot = mw.ustring.gsub(pageRoot, '([%^%$%(%)%%%.%*%+%-%?])', '%%' .. '%1')
end
--[[
Recursive deep copy function.
Preserves identities of subtables.
]]
function _deepCopy(orig, includeMetatable, already_seen)
-- Stores copies of tables indexed by the original table.
already_seen = already_seen or {}
local copy = already_seen[orig]
if copy ~= nil then
return copy
end
if type(orig) == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen)
end
already_seen[orig] = copy
if includeMetatable then
local mt = getmetatable(orig)
if mt ~= nil then
local mt_copy = deepcopy(mt, includeMetatable, already_seen)
setmetatable(copy, mt_copy)
already_seen[mt] = mt_copy
end
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function getEcarts()
titre = mw.title.new(pageIndex)
textContent = titre:getContent(titre)
pagelist = mw.ustring.match( textContent, '<pagelist(.-)/>',1)
pagelist = mw.ustring.gsub(pagelist,'"','')
table.insert(ecarts, { 1, 0 })
--ecarts[1] = { 1, 0 }
--i = 2
for paire in mw.ustring.gmatch(pagelist, '[0-9]+=[0-9]+') do
pages = mw.text.split(paire,'=', true)
table.insert(ecarts, { tonumber(pages[2]), (tonumber(pages[1]) - tonumber(pages[2])) })
--ecarts[i] = { tonumber(pages[2]), (tonumber(pages[1]) - tonumber(pages[2])) }
--i = i + 1
end
--return ecarts
end
function getEcart(noPage)
noPage = tonumber(noPage)
for i, p1 in ipairs(ecarts) do
if ecarts[i + 1] ~= nil then
p2 = ecarts[i + 1]
if p1[1] == nil then return 1000 end
if noPage >= p1[1] and noPage < p2[1] then
return p1[2] or ''
end
else
return p1[2] or ''
end
end
end
function getIndex()
local titre
local tdm
--local t = {}
titre = mw.title.new(pageIndex)
textContent = titre:getContent(titre)
sommaire = mw.ustring.match( textContent, '|Sommaire=(.-)|Epigraphe=',1)
tdm = mw.getCurrentFrame():preprocess(sommaire)
--ajouter % devant tout caractère magique dans le nom de la page
titre = mw.ustring.gsub(pageRoot, '([%^%$%(%)%%%.%*%+%-%?])', '%%' .. '%1')
i = 1
if ns == "Page" then
for chap, page, pageFS in mw.ustring.gmatch(tdm, '%[%[' .. titre .. '.-/(.-)|[^[]+%[%[.-/(.-)|(.-)%]%]') do --.-{{pli|(.-)|
--table.insert(t, { chap, tonumber(page), pageFS })
t[i] = { chap, tonumber(page), pageFS }
i = i + 1
end
elseif ns == '' then
--for chap, page in mw.ustring.gmatch(tdm, '%[%[' .. titre .. '.-/(.-)|.->(.-)</span></span>') do --.-{{pli|(.-)|
for chap, page in mw.ustring.gmatch(tdm, '%[%[' .. titre .. '.-/(.-)|.-%]%].-</(.-)</span></') do --.-{{pli|(.-)|
page = mw.ustring.match(page, '[0-9]*$')
if page and page ~= '' then --and p.getEcart(page)
table.insert(t, { chap, page + getEcart(page), tonumber(page) })
end
end
end
end
initSpace()
getEcarts()
getIndex()
local orig = {}
orig["t"] = t
orig["ecarts"] = ecarts
orig["pageRoot"] = pageRoot
orig["ns"] = ns
orig["pageNom"] = pageNom
--orig["pageObj"] = pageObj
orig["pageIndex"] = pageIndex
--local data = _deepCopy( orig, false)
return orig