Módulo:Book: diferenças entre revisões

Origem: Wikilivros, livros abertos por um mundo aberto.
[edição verificada][edição verificada]
Conteúdo apagado Conteúdo adicionado
Ajustes (com base em uma sugestão de mw:User:MarkTraceur); Uso de "camelCase" nos nomes de variáveis; +espaçamento
frame:preprocess( '{{:'..frame.args['list']..'}}' ); --> mw.title.new( frame.args['list'] ):getContent();
(Há uma edição intermédia do mesmo utilizador que não está a ser apresentada)
Linha 73: Linha 73:
_core = core,
_core = core,
nav = function ( frame )
nav = function ( frame )
local list = frame:preprocess( '{{:'..frame.args['list']..'}}' );
local list = mw.title.new( frame.args['list'] ):getContent();
local page = frame.args['page'];
local page = frame.args['page'];
if page == nil then
page = mw.title.getCurrentTitle().text
end
local pPosition = frame.args['position'];
local pPosition = frame.args['position'];
local relPosition = frame.args['relative-position'];
local relPosition = frame.args['relative-position'];

Revisão das 20h46min de 22 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 function arraySearch( needle, haystack )
	for k,v in pairs( haystack ) do
		if v == needle then 
			result = k;
		end
	end
	return result; 
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 function getChapterFromLine( line )
	local title = string.match( line, "^%s*:%s*%[%[:?(.-)|(.-)%]%]%s*$" ) or
		string.match( line, "^%s*:%s*%[%[:?(.-)%]%]%s*$" ) or
		string.match( line, "^%s*:%s*%[{{fullurl:(.-)|oldid=.-}}%s+.-]%s*$" ) or
		""
	return title:gsub( '_',' ' )
end

-- Adapted from http://lua-users.org/wiki/SplitJoin
local function getListOfChaptersFromText( str )
	local delim = "\r?\n+"
	local result = {}
	if string.find( str, delim ) == nil then
		-- This book doesn't have a collection page
		return result;
	end
	local pat = "(.-)" .. delim .. "()"
	local nb = 0
	local lastPos
	for line, pos in string.gfind( str, pat ) do
		nb = nb + 1
		result[nb] = getChapterFromLine( line )
		lastPos = pos
	end
	result[nb + 1] = getChapterFromLine( string.sub( str, lastPos ) )
	return result
end

local function core( list, page, pPosition, relPosition )
	local chapters = getListOfChaptersFromText( list );
	if pPosition == 'first' then
		return chapters[1];
	end
	if pPosition == 'last' then
		return chapters[#chapters];
	end
	if pPosition == not nil 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,
	_getChapterFromLine = getChapterFromLine,
	_getListOfChaptersFromText = getListOfChaptersFromText,
	_core = core,
	nav = function ( frame )
		local list = mw.title.new( frame.args['list'] ):getContent();
		local page = frame.args['page'];
		if page == nil then
			page = mw.title.getCurrentTitle().text
		end
		local pPosition = frame.args['position'];
		local relPosition = frame.args['relative-position'];
		return core( list, page, pPosition, relPosition );
	end
};