Difference between revisions 268513 and 268514 on ptwikibooks

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, maxChaptersAfterThis )
	local lines = mw.text.split( str, '[\r\n]+' )
	local i, line, chapter
	local result = {}
	local curChapter = mw.title.getCurrentTitle().text
	local nextChapter
	for i, line in ipairs( lines ) do
		chapter = getChapterFromLine( line )
		if chapter and chapter ~= '' then
			table.insert( result, chapter )
			if maxChaptersAfterThis then
				if chapter == curChapter then
					nextChapter = 1
				elseif nextChapter then
					nextChapter = nextChapter + 1
				end
				if nextChapter > maxChaptersAfterThis then
					return result
				end
			end
		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
	local chapters = getListOfChaptersFromText( colContent )
	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

local navBar = 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
		-- FIXME: Return an empty navigation bar?
		return ''
	end
	local curChapter = mw.title.getCurrentTitle().text
	-- Find the current chapter in the collection
	local a,b = mw.ustring.find( colContent, '[\r\n]:%s*%[%[:?' + curChapter + '|.-%]%]' ) or
		mw.ustring.find( colContent, '[\r\n]:%s*%[%[:?' + curChapter + '%]%]' ) or
		mw.ustring.find( colContent, '[\r\n]:%s*%[{{fullurl:' + curChapter + '|oldid=.-}}%s+.-%]' )
	-- Find the previous chapter in the collection, starting before the current one
	-- TODO: implement this
	-- Find the next chapter in the collection, starting after the current one
	mw.log(a)
	mw.log(b)
	local linkAndText = '[\r\n]:%s*%[%[:?(.-)|.-%]%]'
	local link = '[\r\n]:%s*%[%[:?(.-)%]%]'
	local fullUrl = '[\r\n]:%s*%[{{fullurl:(.-)|oldid=.-}}%s+.-%]'
	local nextChapter = mw.ustring.match( line, linkAndText ) or
		mw.ustring.match( line, link ) or
		mw.ustring.match( line, fullUrl )

	return chapters
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 "&#42;A&#39;B&#38;C&amp;#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
};