Difference between revisions of "Module:List"

From Confrontation.wiki
Jump to navigation Jump to search
en>Raylton P. Sousa
m (Undo revision 618730 by Raylton P. Sousa (talk))
en>Raylton P. Sousa
m
Line 1: Line 1:
local p = {}
-- Based on parseCollectionLine from https://gerrit.wikimedia.org/r/gitweb?p=mediawiki/extensions/Collection.git;a=blob;f=Collection.body.php;hb=f2bd4ee2be12ab3df5f2dcb3bd56ff17247fff66#l793)
-- 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:
-- Get "Book/Title" from a line in any of these formats:
Line 34: Line 32:
end
end


function p.getListOfChapters( frame )
function getListOfChapters( frame )
     local colPageText = frame:preprocess( '{{:User:Raylton P. Sousa/Livro/Xadrez}}' )
     local colPageText = frame:preprocess( '{{:User:Raylton P. Sousa/Livro/Xadrez}}' )
     return getListOfChaptersFromText( colPageText )
     return getListOfChaptersFromText( colPageText )
end
end
return p

Revision as of 17:13, 20 December 2012

Documentation for this module may be created at Module:List/doc

-- 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]
function getChapterFromLine( line )
    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
function getListOfChaptersFromText(str)
	local delim = "\r?\n+"
	if string.find(str, delim) == nil then
		-- This book doesn't have a collection page
		return ""
	end
	local result = {}
	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

function getListOfChapters( frame )
    local colPageText = frame:preprocess( '{{:User:Raylton P. Sousa/Livro/Xadrez}}' )
    return getListOfChaptersFromText( colPageText )
end