Class: Diary

Inherits:
Object show all
Includes:
TDiary::BaseDiary, TDiary::UncategorizableDiary
Defined in:
lib/tdiary/io/pstore.rb

Overview

Diary class

Management a day of diary

Instance Method Summary collapse

Constructor Details

#initialize(date, title, body) ⇒ Diary

Returns a new instance of Diary.



140
141
142
143
# File 'lib/tdiary/io/pstore.rb', line 140

def initialize( date, title, body )
  init_diary
  replace( date, title, body )
end

Instance Method Details

#append(body, author = nil) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/tdiary/io/pstore.rb', line 156

def append( body, author = nil )
  body.gsub( /\r/, '' ).split( /\n\n+/ ).each do |fragment|
    paragraph = Paragraph::new( fragment, author )
    @paragraphs << paragraph if paragraph
  end
  @last_modified = Time::now
  self
end

#each_sectionObject



165
166
167
168
169
# File 'lib/tdiary/io/pstore.rb', line 165

def each_section
  @paragraphs.each do |paragraph|
    yield paragraph
  end
end

#replace(date, title, body) ⇒ Object



149
150
151
152
153
154
# File 'lib/tdiary/io/pstore.rb', line 149

def replace( date, title, body )
  set_date( date )
  set_title( title )
  @paragraphs = []
  append( body )
end

#styleObject



145
146
147
# File 'lib/tdiary/io/pstore.rb', line 145

def style
  'tDiary'
end

#to_chtml(opt) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/tdiary/io/pstore.rb', line 221

def to_chtml( opt )
  idx = 0
  r = ''
  each_section do |paragraph|
    if paragraph.subtitle then
      r << %Q[<P><A NAME="p#{'%02d' % idx += 1}">*</A> #{paragraph.subtitle}</P>]
    end
    if /^</ =~ paragraph.body then
      idx += 1
      r << paragraph.body
    elsif paragraph.subtitle
      r << %Q[<P>#{paragraph.body.collect{|l|l.chomp.sub( /^[  ]/u, '' )}.join( "</P>\n<P>" )}</P>]
    else
      r << %Q[<P><A NAME="p#{'%02d' % idx += 1}">*</A> ]
      if opt['multi_user'] and paragraph.author then
        r << %Q|[#{paragraph.author}]|
      end
      r << %Q[#{paragraph.body.collect{|l|l.chomp.sub( /^[  ]/u, '' )}.join( "</P>\n<P>" )}</P>]
    end
  end
  r
end

#to_html(opt, mode = :HTML) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/tdiary/io/pstore.rb', line 179

def to_html( opt, mode = :HTML )
  case mode
  when :CHTML
    to_chtml( opt )
  else
    to_html4( opt )
  end
end

#to_html4(opt) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tdiary/io/pstore.rb', line 188

def to_html4( opt )
  idx = 1
  r = ''
  each_section do |paragraph|
    r << %Q[<div class="section">\n]
    if paragraph.subtitle then
      r << %Q[<h3><a ]
      if opt['anchor'] then
        r << %Q[name="p#{'%02d' % idx}" ]
      end
      r << %Q[href="#{opt['index']}<%=anchor "#{@date.strftime( '%Y%m%d' )}#p#{'%02d' % idx}" %>">#{opt['section_anchor']}</a> ]
      if opt['multi_user'] and paragraph.author then
        r << %Q|[#{paragraph.author}]|
      end
      r << %Q[#{paragraph.subtitle}</h3>]
    end
    if /^</ =~ paragraph.body then
      r << %Q[#{paragraph.body}]
    elsif paragraph.subtitle
      r << %Q[<p>#{paragraph.body.collect{|l|l.chomp.sub( /^[  ]/u, '' )}.join( "</p>\n<p>" )}</p>]
    else
      r << %Q[<p><a ]
      if opt['anchor'] then
        r << %Q[name="p#{'%02d' % idx}" ]
      end
      r << %Q[href="#{opt['index']}<%=anchor "#{@date.strftime( '%Y%m%d' )}#p#{'%02d' % idx}" %>">#{opt['section_anchor']}</a> #{paragraph.body.collect{|l|l.chomp.sub( /^[  ]/u, '' )}.join( "</p>\n<p>" )}</p>]
    end
    r << %Q[</div>]
    idx += 1
  end
  r
end

#to_sObject



244
245
246
# File 'lib/tdiary/io/pstore.rb', line 244

def to_s
  "date=#{@date.strftime('%Y%m%d')}, title=#{@title}, body=[#{@paragraphs.join('][')}]"
end

#to_srcObject



171
172
173
174
175
176
177
# File 'lib/tdiary/io/pstore.rb', line 171

def to_src
  src = ''
  each_section do |para|
    src << para.to_src
  end
  src
end