Módulo:Book: diferenças entre revisões
Aspeto
[edição verificada] | [edição verificada] |
Conteúdo apagado Conteúdo adicionado
Desfeita a edição de Helder.wiki: não resolveu; Nova tentativa: considerar apenas os 20000 primeiros caracteres da coleção e ignorar o resto (só 5 coleções são maiores que isso atualmente) |
Considerar apenas 18000 caracteres da coleção (poucas são maiores que isso: Guia do Linux, Livro de receitas, Logística, Tornando-se profissional em Blender 3D, Curso de Geografia, FreeBSD Handbook, Panda3D) |
||
Linha 69: | Linha 69: | ||
end |
end |
||
-- FIXME: Optimize the code so it works for large books |
-- FIXME: Optimize the code so it works for large books |
||
local chapters = getListOfChaptersFromText( mw.ustring.sub( colContent, 1, |
local chapters = getListOfChaptersFromText( mw.ustring.sub( colContent, 1, 18000 ) ) |
||
return chapters |
return chapters |
||
end |
end |
Revisão das 16h36min de 28 de janeiro de 2014
Uso
- ...
Tarefas pendentes
- Documentar este módulo
- Conferir os arquivos dos diálogos comunitários para ver o que ainda precisa ser feito...
- Atualizar Ajuda:Navegação automática
- ...
Ver também
local collectionPrefix = 'Wikilivros:Livros/'
local arraySearch = function ( needle, haystack )
for k,v in pairs( haystack ) do
if v == needle then
result = k;
end
end
return result;
end
local getBookName = function ( page )
page = page or mw.title.getCurrentTitle().text
local pos = mw.ustring.find( page, '/' )
if pos == nil then
return page
else
return mw.ustring.sub( page, 1, pos - 1 )
end
end
local getChapterName = function ( page )
page = page or mw.title.getCurrentTitle().text
local pos = mw.ustring.find( page, '/' )
if pos == nil then
return ''
else
return mw.ustring.sub( page, pos + 1 )
end
end
-- Based on parseCollectionLine from https://gerrit.wikimedia.org/r/gitweb?p=mediawiki/extensions/Collection.git;a=blob;f=Collection.body.php;hb=f2bd4ee2be12ab3df5f2dcb3bd56ff17247fff66#l793)
-- Get "Book/Title" from a line in any of these formats:
-- :[[Book/Title]]
-- :[[Book/Title|Text]]
-- :[{{fullurl:Book/Title|oldid=12345}} Text]
local getChapterFromLine = function ( line )
if mw.ustring.sub( line, 1, 1 ) ~= ':' then
-- There is no chapter on this line
return nil
end
line = mw.text.trim( mw.ustring.sub( line, 2, -1 ) )
local title = mw.ustring.match( line, '^%[%[:?(.-)|.-%]%]$' ) or
mw.ustring.match( line, '^%[%[:?(.-)%]%]$' ) or
mw.ustring.match( line, '^%[{{fullurl:(.-)|oldid=.-}}%s+.-%]$' ) or
''
return title:gsub( '_',' ' )
end
local getListOfChaptersFromText = function ( str )
local lines = mw.text.split( str, '[\r\n]+' )
local i, line, chapter
local result = {}
for i, line in ipairs( lines ) do
chapter = getChapterFromLine( line )
if chapter and chapter ~= '' then
table.insert( result, chapter )
end
end
return result
end
local getChapters = function ( collectionPage )
collectionPage = collectionPage or ( collectionPrefix .. getBookName() )
local colContent = mw.title.new( collectionPage ):getContent()
if colContent == nil then
-- This book doesn't have a collection page
return {}
end
-- FIXME: Optimize the code so it works for large books
local chapters = getListOfChaptersFromText( mw.ustring.sub( colContent, 1, 18000 ) )
return chapters
end
local getPrintableVersion = function ( chapters )
chapters = chapters or getChapters()
local frame = mw.getCurrentFrame()
local result = ''
local i, chapter
for i, chapter in ipairs( chapters ) do
if mw.title.new( chapter ).exists then
result = result ..
'<h1>[[' .. chapter .. '|' .. getChapterName( chapter ) .. ']]</h1>\n' ..
frame:expandTemplate{ title = ':' .. chapter } .. '\n\n'
end
end
return result
end
local core = function ( chapters, page, pPosition, relPosition )
if pPosition == 'first' then
return chapters[1];
end
if pPosition == 'last' then
return chapters[#chapters];
end
if pPosition ~= nil and chapters[pPosition] then
return chapters[pPosition];
end
local cPosition = arraySearch( page, chapters );
if cPosition == nil then
return '';
end
if pPosition == 'prev' then
return chapters[cPosition - 1];
end
if pPosition == 'next' then
return chapters[cPosition + 1];
end
if relPosition == nil then
return '';
end
return chapters[cPosition + relPosition];
end
return {
_arraySearch = arraySearch,
_getBookName = getBookName,
_getChapterName = getChapterName,
_getChapterFromLine = getChapterFromLine,
_getListOfChaptersFromText = getListOfChaptersFromText,
_getChapters = getChapters,
_getPrintableVersion = getPrintableVersion,
_core = core,
getBookName = function( frame )
return getBookName( frame.args[1] )
end,
getChapterName = function( frame )
return getChapterName( frame.args[1] )
end,
getPrintableVersion = function( frame )
local bookName = frame.args[1] or getBookName()
local chapters = getChapters( collectionPrefix .. bookName )
return getPrintableVersion( chapters )
end,
nav = function ( frame )
local chapters = getChapters( frame.args['list'] )
-- On page [[*A'B&C"D]], {{PAGENAME}} returns "*A'B&C&#34;D", so decode it first!
local page = mw.text.decode( frame.args['page'] or mw.title.getCurrentTitle().text );
local pPosition = frame.args['position'];
local relPosition = frame.args['relative-position'];
return core( chapters, page, pPosition, relPosition );
end
};