root/HTTP/WikiDate.lua

Revision 1476 (checked in by rsz, 5 days ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               WikiDate.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       January 30, 2007
6 -- Legal:               Copyright (C) 2007 Raphaël Szwarc
7 --                      Under the terms of the MIT License
8 --                      http://www.opensource.org/licenses/mit-license.html
9 --------------------------------------------------------------------------------
10
11 -- import dependencies
12 local Wiki = require( 'Wiki' )
13
14 local os = require( 'os' )
15
16 local error = error
17 local getmetatable = getmetatable
18 local ipairs = ipairs
19 local require = require
20 local setmetatable = setmetatable
21 local tonumber = tonumber
22 local type = type
23
24 --------------------------------------------------------------------------------
25 -- WikiDate
26 --------------------------------------------------------------------------------
27
28 module( 'WikiDate' )
29 _VERSION = '1.0'
30
31 local self = setmetatable( _M, {} )
32 local meta = getmetatable( self )
33
34 local location = Wiki.Location()
35
36 --------------------------------------------------------------------------------
37 -- Utilities
38 --------------------------------------------------------------------------------
39
40 local function Directory( aDate, shouldCreate )
41     local File = require( 'File' )
42     local aDirectory = File( location, 'wiki', 'date' )
43     local someFormats = { '%04d', '%02d', '%02d' }
44     local aDate = aDate or {}
45     local someValues = { aDate.year, aDate.month, aDate.day }
46    
47     if shouldCreate then
48         aDirectory.mkdir = true
49     end
50    
51     for anIndex, aValue in ipairs( someValues ) do
52         if aValue then
53             local aFormat = someFormats[ anIndex ]
54        
55             aDirectory = File( aDirectory.path, ( aFormat ):format( aValue ) )
56            
57             if shouldCreate then
58                 aDirectory.mkdir = true
59             end
60         end
61     end
62    
63     return aDirectory
64 end
65
66 local function DateIterator( aDate )
67     local aDirectory = Directory( aDate, false )
68    
69     if aDirectory.exists then
70         local anIterator = aDirectory()
71
72         return function()           
73             local aFile = anIterator()
74
75             while aFile and not aFile.name:find( '^%d+$' ) do
76                 aFile = anIterator()
77             end
78            
79             if aFile and aFile.name:find( '^%d+$' ) then               
80                 return tonumber( aFile.name ), aFile
81             end
82
83         end
84     end
85    
86     return function() end
87 end
88
89 local function Iterator( aDate )
90     local aDirectory = Directory( aDate, false )
91    
92     return Wiki.NameIterator( aDirectory, true )
93 end
94
95 local function Index( aName, aTime )
96     local File = require( 'File' )
97     local aDate = os.date( '!*t', aTime )
98     local aDirectory = Directory( aDate, true )
99     local aFile = File( aDirectory.path, ( '%s.id' ):format( aName )  )
100    
101     aFile.mkdir = true
102 end
103
104 local function Delete( aName )
105     local WikiContent = require( 'WikiContent' )
106     local aContent = WikiContent( aName )
107    
108     if aContent.exists then
109         local File = require( 'File' )
110         local aTime = aContent.creation
111         local aDate = os.date( '!*t', aTime )
112         local aDirectory = Directory( aDate )
113         local aFile = File( aDirectory.path, ( '%s.id' ):format( aName )  )
114
115         if aFile.exists then
116             aFile.delete = true
117         end
118        
119         return
120     end
121    
122     error( ( '%q doesn\'t exist' ):format( aName ) )
123 end
124
125 --------------------------------------------------------------------------------
126 -- Metamethods
127 --------------------------------------------------------------------------------
128
129 function meta:__call( aDate )
130     return DateIterator( aDate )
131 end
132
133 function meta:__index( aKey )
134     if aKey == 'modification' then
135         return Directory().modification
136     end
137      
138     return Iterator( aKey )
139 end
140
141 function meta:__newindex( aKey, aValue )
142     if type( aKey ) =='string' and type( aValue ) == 'boolean' and aValue == false then
143         return Delete( aKey )
144     elseif type( aKey ) == 'string' and type( aValue ) == 'number' then
145         return Index( aKey, aValue )
146     end
147    
148     error( ( 'Invalid parameters: %q = %q' ):format( tostring( aKey ), tostring( aValue ) ) )
149 end
150
151 function meta:__concat( aValue )
152     return tostring( self ) .. tostring( aValue )
153 end
154
155 function meta:__tostring()
156     return ( '%s/%s' ):format( self._NAME, self._VERSION )
157 end
Note: See TracBrowser for help on using the browser.