Модуль:LinksOut
Материал из Указатель частей и соединений РККА 1941-1945
Для документации этого модуля может быть создана страница Модуль:LinksOut/doc
local linksout = {}
local cargo = mw.ext.cargo
local ref = require( 'Module:Ref' )
local function pamyatParts( link )
return (link or ''):match( '^([1-9][0-9]*) (%S[^%c]*)$' )
end
local mediaproc = {
['1'] = function ( link )
return 'Статья «<i>[[вп:' .. link .. '|' .. link .. ']]</i>» в Википедии'
end,
['2'] = function ( link )
return 'Статья «<i>[http://allaces.ru' .. link .. ']</i>» на сайте <i>allaces.ru</i>'
end,
['3'] = function ( link )
return 'Статья «<i>[https://www.proza.ru' .. link .. ']</i>» Владимира Савончика'
end,
['4'] = function ( link )
local id, title = pamyatParts( link )
return 'Страница «[https://pamyat-naroda.ru/warunit/id' .. id .. '/ '
.. mw.text.nowiki( title ) .. ']» на «Памяти народа»'
end,
}
function linksout.collect( names )
local where = 'unit="' .. table.concat( names, '" OR unit="' ) .. '"'
local query = cargo.query(
'ext_links',
'unit, media, link, addendum, ref',
{
where = where,
orderBy = 'media, link',
} )
-- Ссылку ПН показываем только на карточке, к которой относится запись.
local current
for i = #query, 1, -1 do
local row = query[i]
if row.media == '4' then
current = current or mw.title.getCurrentTitle()
if current.namespace ~= 0 or row.unit ~= current.text or not pamyatParts( row.link ) then
table.remove( query, i )
else
row.addendum, row.ref = row.addendum or '', row.ref or ''
end
end
end
if #query ~= 0 then
local out = {}
local adds, refs = {}, {}
for i = 1, #query do
local dsc = query[i]
if dsc.addendum ~= '' then
table.insert( adds, dsc.addendum )
end
if dsc.ref ~= '' then
table.insert( refs, dsc.ref )
end
if i == #query or dsc.media ~= query[i+1].media or dsc.link ~= query[i+1].link then
table.insert( out, '* ' .. mediaproc[dsc.media]( dsc.link ) )
if #refs ~= 0 then
ref.refStart()
for _, refer in ipairs( refs ) do
table.insert( out, ref.use( refer ) )
end
ref.refReturn()
end
table.insert( out, '. ' .. table.concat( adds, ' ' ) .. '\n' )
adds, refs = {}, {}
end
end
return table.concat( out )
end
end
return linksout