-------------------------------------------------------------------------------- -- Title: WikiIndexService.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: January 30, 2007 -- Legal: Copyright (C) 2007 Raphaël Szwarc -- Under the terms of the MIT License -- http://www.opensource.org/licenses/mit-license.html -------------------------------------------------------------------------------- -- import dependencies local HTTP = require( 'HTTP' ) local HTTPExtra = require( 'HTTPExtra' ) local Template = require( 'Template' ) local URL = require( 'URL' ) local URLPath = require( 'URLPath' ) local WikiIndex = require( 'WikiIndex' ) local WikiService = require( 'WikiService' ) local BaseLink = WikiService.BaseLink local DateLink = WikiService.DateLink local FeedLink = WikiService.FeedLink local IndexLink = WikiService.IndexLink local ContentIterator = WikiService.ContentIterator local Description = WikiService.Description local Encode = WikiService.Encode local GetType = WikiService.GetType local HTML = WikiService.HTML local Path = WikiService.Path local Tag = WikiService.Tag local getmetatable = getmetatable local require = require local rawset = rawset local setmetatable = setmetatable local tostring = tostring -------------------------------------------------------------------------------- -- WikiIndexService -------------------------------------------------------------------------------- module( 'WikiIndexService' ) _VERSION = '1.0' local self = setmetatable( _M, {} ) local meta = getmetatable( self ) -------------------------------------------------------------------------------- -- Utilities -------------------------------------------------------------------------------- local function PathArgument( self ) if self.second then local WikiIndexService = require( 'WikiIndexService' ) local aService = WikiIndexService( self.first ) aService.path = self.path() return self, aService end return self end -------------------------------------------------------------------------------- -- DAV Utitities -------------------------------------------------------------------------------- local function DAVPrefixIterator( aPrefix ) local anIterator = WikiIndex( aPrefix ) return function() local aName, aFile = anIterator() if aName then local aModification = aFile.modification local aResource = { name = aName, mode = 'directory', modification = aModification, size = 0 } return aResource end end end local function DAVContentIterator( aPrefix ) local WikiContent = require( 'WikiContent' ) local anIterator = WikiIndex[ aPrefix ] return function() local aName = anIterator() if aName then local aContent = WikiContent( aName ) local aModification = aContent.modification local aResource = { name = aName, mode = 'directory', modification = aModification, size = 0 } return aResource end end end local function DAVIterator( aPrefix ) if aPrefix and aPrefix:len() == 2 then return DAVContentIterator( aPrefix ) end return DAVPrefixIterator( aPrefix ) end local function DAVResource( aPrefix ) local anIterator = DAVIterator( aPrefix ) local aModification = WikiIndex[ 'modification' ] local aResource = { iterator = anIterator, mode = 'directory', modification = aModification, size = 0 } return aResource end -------------------------------------------------------------------------------- -- Service methods -------------------------------------------------------------------------------- self.toURL =function( aService, anObject ) local aPath = URLPath() aPath.absolute = true if anObject.first then aPath[ #aPath + 1 ] = anObject.first end if anObject.second then aPath[ #aPath + 1 ] = anObject.second end return URL( aService.prefix .. aPath ) end self.toObject = function( aService, aURL ) local aPath = aURL.path local aFirst = aPath[ 2 ] local aSecond = aPath[ 3 ] local WikiIndexService = require( 'WikiIndexService' ) local aService = WikiIndexService( aFirst, aSecond ) return aService end function self:getHtml() local WikiIndexNavigation = require( 'WikiIndexNavigation' ) local aLayoutTemplate = Template[ 'WikiLayout.txt' ] local aTemplate = Template[ 'WikiIndexService.txt' ] local aNameTemplate = aTemplate[ 'names' ] local anIterator, aCount, hasMore = ContentIterator( WikiIndex[ self.prefix ] ) local aTitle = self.first:upper() .. ( self.second or '' ):lower() aTemplate[ 'names' ] = nil aTemplate[ 'description' ] = Encode( Description( aCount, hasMore ) ) aTemplate[ 'navigation' ] = WikiIndexNavigation( self.prefix ) if aCount == 0 then HTTP.response.status.code = 404 HTTP.response.status.description = 'Not Found' end for aContent, aURL in anIterator do local aNameTemplate = aTemplate[ 'names' ] aNameTemplate[ 'href' ] = Encode( aURL.path ) aNameTemplate[ 'name' ] = Encode( aContent.title ) aNameTemplate[ 'tag' ] = Tag( aContent.modification, true ) aTemplate[ 'names' ] = aNameTemplate end aTemplate[ 'title' ] = aTitle aLayoutTemplate[ 'baseLink' ] = Encode( BaseLink() ) aLayoutTemplate[ 'indexLink' ] = Encode( IndexLink( self.prefix ) ) aLayoutTemplate[ 'dateLink' ] = Encode( DateLink() ) aLayoutTemplate[ 'feedLink' ] = FeedLink( self, aCount ) aLayoutTemplate[ 'path' ] = Path( PathArgument( self ) ) aLayoutTemplate[ 'query' ] = nil aLayoutTemplate[ 'robot' ] = 'noindex' aLayoutTemplate[ 'title' ] = Encode( aTitle .. ' — Index' ) aLayoutTemplate[ 'content' ] = aTemplate return tostring( aLayoutTemplate ) end function self:getXml() local WikiFeed = require( 'WikiFeed' ) local anIterator = ContentIterator( WikiIndex[ self.prefix ] ) local aGenerator = HTML local aTitle = self.first:upper() .. ( self.second or '' ):lower() .. ' — Index' local aContext = { title = aTitle, link = HTTP.request.url, creation = WikiIndex[ 'creation' ] } HTTP.response.header[ 'content-type' ] = 'application/atom+xml; charset=utf-8' return tostring( WikiFeed( anIterator, aGenerator, aContext ) ) end function self:get( aType ) if self.prefix then return GetType( self, aType ) end return nil, HTTP.request.url.path( 'a' ) end -------------------------------------------------------------------------------- -- DAV service methods -------------------------------------------------------------------------------- function self:options() HTTP.response.header[ 'allow' ] = 'GET, HEAD, OPTIONS, PROPFIND' HTTP.response.header[ 'content-type' ] = 'text/plain' return HTTP.response.header[ 'allow' ] end function self:propfind() local WikiDAV = require( 'WikiDAV' ) local aResource = DAVResource( self.prefix ) return WikiDAV( aResource ):propfind() end -------------------------------------------------------------------------------- -- Metamethods -------------------------------------------------------------------------------- function meta:__call( aFirst, aSecond ) local aPrefix = nil local aService = {} if aFirst and aFirst:len() > 0 then local aFirst = aFirst:match( '^(%w)' ) or '' aService.first = aFirst:lower() aPrefix = aService.first end if aSecond and aSecond:len() > 0 then local aSecond = aSecond:match( '^(%w)' ) or '' aService.second = aSecond:lower() aPrefix = aService.first .. aService.second end aService.prefix = aPrefix setmetatable( aService, self ) return aService end function meta:__concat( aValue ) return tostring( self ) .. tostring( aValue ) end function meta:__tostring() return ( '%s/%s' ):format( self._NAME, self._VERSION ) end function self:__index( aKey ) local aValue = getmetatable( self )[ aKey ] if not aValue and aKey:find( '^get.+' ) then local WikiContent = require( 'WikiContent' ) local aName = aKey:match( '^get(.+)$' ) local aContent = WikiContent( aName ) if aContent and aContent.exists then local WikiContentService = require( 'WikiContentService' ) local aService = WikiContentService( aContent ) return function() return aService end end end self[ aKey ] = aValue return aValue end function self:__concat( aValue ) return tostring( self ) .. tostring( aValue ) end function self:__tostring() return self.second or self.first:upper() end