~ubuntu-branches/ubuntu/saucy/tdiary/saucy-proposed

« back to all changes in this revision

Viewing changes to tdiary/defaultio.rb

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2011-04-11 21:53:16 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110411215316-ih4gt4q8p29d2wf8
Tags: 3.0.1-1
* New upstream release (Closes: #542801, #594947)
* debian/control:
 - Bumped up Standards-Version to 3.9.1.
 - Updated version dependency.
* debian/tdiary-setup.rb: Followed the upstream changes, incorporating js and
  index.fcgi

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# defaultio.rb: tDiary IO class for tDiary 2.x format. $Revision: 1.43 $
3
 
#
4
 
# Copyright (C) 2001-2005, TADA Tadashi <sho@spc.gr.jp>
 
1
# -*- coding: utf-8; -*-
 
2
#
 
3
# defaultio.rb: tDiary IO class for tDiary 2.x format. $Revision: 1.44 $
 
4
#
 
5
# Copyright (C) 2001-2005, TADA Tadashi <t@tdtds.jp>
5
6
# You can redistribute it and/or modify it under GPL2.
6
7
#
7
8
module TDiary
8
9
        TDIARY_MAGIC_MAJOR = 'TDIARY2'
9
 
        TDIARY_MAGIC_MINOR = '00.00'
 
10
        TDIARY_MAGIC_MINOR = '01.00'
10
11
        TDIARY_MAGIC = "#{TDIARY_MAGIC_MAJOR}.#{TDIARY_MAGIC_MINOR}"
11
12
 
12
13
        def TDiary::parse_tdiary( data )
33
34
                end
34
35
 
35
36
                def restore_comment( file, diaries )
 
37
                        minor = ''
36
38
                        begin
37
 
                                File::open( file, 'r' ) do |fh|
 
39
                                File::open( file ) do |fh|
38
40
                                        fh.flock( File::LOCK_SH )
39
 
                                        fh.read.split( /\r?\n\.\r?\n/ ).each do |l|
 
41
 
 
42
                                        major, minor = fh.gets.chomp.split( /\./, 2 )
 
43
                                        unless TDIARY_MAGIC_MAJOR == major then
 
44
                                                raise StandardError, 'bad file format.'
 
45
                                        end
 
46
 
 
47
                                        s = fh.read
 
48
                                        s = migrate_to_01( s ) if minor == '00.00' and !@tdiary.conf['stop_migrate_01']
 
49
                                        s.split( /\r?\n\.\r?\n/ ).each do |l|
40
50
                                                headers, body = TDiary::parse_tdiary( l )
41
51
                                                next unless body
42
52
                                                comment = Comment::new(
50
60
                                end
51
61
                        rescue Errno::ENOENT
52
62
                        end
 
63
                        return minor == '00.00' ? TDiaryBase::DIRTY_COMMENT : TDiaryBase::DIRTY_NONE
53
64
                end
54
65
 
55
66
                def store_comment( file, diaries )
81
92
 
82
93
                def restore_referer( file, diaries )
83
94
                        begin
84
 
                                File::open( file, 'r' ) do |fh|
 
95
                                File::open( file ) do |fh|
85
96
                                        fh.flock( File::LOCK_SH )
86
97
                                        fh.read.split( /\r?\n\.\r?\n/ ).each do |l|
87
98
                                                headers, body = TDiary::parse_tdiary( l )
113
124
                                File::rename( file, file.sub( /\.tdr$/, '.tdr~' ) )
114
125
                        rescue Errno::ENOENT
115
126
                        end
 
127
                        return TDiaryBase::DIRTY_NONE
116
128
                end
117
129
 
118
130
                def store_referer( file, diaries )
149
161
                                fh.flock( File::LOCK_EX )
150
162
 
151
163
                                cache = @tdiary.restore_parser_cache( date, 'defaultio' )
 
164
                                force_save = TDiaryBase::DIRTY_NONE
152
165
                                unless cache then
153
 
                                        restore( fh, diaries )
154
 
                                        restore_comment( cfile, diaries )
155
 
                                        restore_referer( rfile, diaries )
 
166
                                        force_save |= restore( fh, diaries )
 
167
                                        force_save |= restore_comment( cfile, diaries )
 
168
                                        force_save |= restore_referer( rfile, diaries )
156
169
                                else
157
170
                                        diaries.update( cache )
158
171
                                end
159
172
                                dirty = yield( diaries ) if iterator?
160
 
                                store( fh, diaries ) if dirty & TDiaryBase::DIRTY_DIARY != 0
161
 
                                store_comment( cfile, diaries ) if dirty & TDiaryBase::DIRTY_COMMENT != 0
162
 
                                store_referer( rfile, diaries ) if dirty & TDiaryBase::DIRTY_REFERER != 0
 
173
                                store( fh, diaries ) if ((dirty | force_save) & TDiaryBase::DIRTY_DIARY) != 0
 
174
                                store_comment( cfile, diaries ) if ((dirty | force_save) & TDiaryBase::DIRTY_COMMENT) != 0
 
175
                                store_referer( rfile, diaries ) if ((dirty | force_save) & TDiaryBase::DIRTY_REFERER) != 0
163
176
                                if dirty != TDiaryBase::DIRTY_NONE or not cache then
164
177
                                        @tdiary.store_parser_cache( date, 'defaultio', diaries )
165
178
                                end
208
221
 
209
222
        private
210
223
                def restore( fh, diaries )
 
224
                        
211
225
                        begin
212
226
                                fh.seek( 0 )
213
227
                                begin
214
 
                                        major, minor = fh.gets.split( /\./, 2 )
 
228
                                        major, minor = fh.gets.chomp.split( /\./, 2 )
215
229
                                        unless TDIARY_MAGIC_MAJOR == major then
216
230
                                                raise StandardError, 'bad file format.'
217
231
                                        end
218
232
 
219
233
                                        # read and parse diary
220
 
                                        fh.read.split( /\r?\n\.\r?\n/ ).each do |l|
 
234
                                        style_name = ''
 
235
                                        s = fh.read
 
236
                                        s = migrate_to_01( s ) if minor == '00.00' and !@tdiary.conf['stop_migrate_01']
 
237
                                        s.split( /\r?\n\.\r?\n/ ).each do |l|
221
238
                                                headers, body = TDiary::parse_tdiary( l )
222
239
                                                style_name = headers['Format'] || 'tDiary'
223
 
                                                diary = eval( "#{style( style_name )}::new( headers['Date'], headers['Title'], body, Time::at( headers['Last-Modified'].to_i ) )" )
 
240
                                                diary = style( style_name )::new( headers['Date'], headers['Title'], body, Time::at( headers['Last-Modified'].to_i ) )
224
241
                                                diary.show( headers['Visible'] == 'true' ? true : false )
225
242
                                                diaries[headers['Date']] = diary
226
243
                                        end
229
246
                                        # no magic number when it is new file.
230
247
                                end
231
248
                        end
 
249
                        return minor == '00.00' ? TDiaryBase::DIRTY_DIARY : TDiaryBase::DIRTY_NONE
232
250
                end
233
251
 
234
252
                def store( fh, diaries )
235
253
                        begin
236
254
                                fh.seek( 0 )
237
255
                                fh.puts( TDIARY_MAGIC )
238
 
                                diaries.each do |date,diary|
 
256
                                diaries.sort_by {|date, diary| date}.each do |date,diary|
239
257
                                        # save diaries
240
258
                                        fh.puts( "Date: #{date}" )
241
259
                                        fh.puts( "Title: #{diary.title}" )
249
267
                                fh.truncate( fh.tell )
250
268
                        end
251
269
                end
 
270
 
 
271
                def migrate_to_01( day )
 
272
                        @tdiary.conf.migrate_to_utf8( day )
 
273
                end
252
274
        end
253
275
end
 
276
 
 
277
# Local Variables:
 
278
# mode: ruby
 
279
# indent-tabs-mode: t
 
280
# tab-width: 3
 
281
# ruby-indent-level: 3
 
282
# End: