Class: TDiary::Style::EtdiaryDiary
- Inherits:
-
Object
- Object
- TDiary::Style::EtdiaryDiary
- Defined in:
- lib/tdiary/style/etdiary.rb
Constant Summary collapse
- TAG_BEG_REGEXP =
/\A<([A-Za-z][0-9A-Za-z]*)([^>]*)>([^\r]*)\z/
- TAG_END_REGEXP =
/\A([^\r]*)<\/([A-Za-z][0-9A-Za-z]*)>\n*\z/
- PRE_REGEXP =
/\A<[Pp][Rr][Ee][^>]*>([^\r]*)<\/[Pp][Rr][Ee]>\n*\z/
- TITLE_REGEXP =
/\A<<([^\r]*?)>>[^>]/
Instance Method Summary collapse
- #add_section(subtitle, body) ⇒ Object
- #append(body, author = nil) ⇒ Object
- #delete_section(index) ⇒ Object
- #each_paragraph ⇒ Object
- #each_section {|section| ... } ⇒ Object
-
#initialize(date, title, body, modified = Time::now) ⇒ EtdiaryDiary
constructor
A new instance of EtdiaryDiary.
- #replace(date, title, body) ⇒ Object
- #style ⇒ Object
- #to_html(opt = {'anchor' => true, 'index' => true}, mode = :HTML) ⇒ Object
- #to_html_section(section, factory) ⇒ Object
- #to_s ⇒ Object
- #to_src ⇒ Object
Constructor Details
#initialize(date, title, body, modified = Time::now) ⇒ EtdiaryDiary
Returns a new instance of EtdiaryDiary.
280 281 282 283 284 285 286 287 288 289 |
# File 'lib/tdiary/style/etdiary.rb', line 280 def initialize( date, title, body, modified = Time::now ) init_diary set_date( date ) set_title( title ) @sections = [] if body != '' then append( body ) end @last_modified = modified end |
Instance Method Details
#add_section(subtitle, body) ⇒ Object
363 364 365 366 367 368 369 |
# File 'lib/tdiary/style/etdiary.rb', line 363 def add_section(subtitle, body) sec = EtdiarySection::new( '' ) sec.subtitle = subtitle sec.body = body @sections << sec @sections.size end |
#append(body, author = nil) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/tdiary/style/etdiary.rb', line 302 def append( body, = nil ) section = nil buffer = nil tag_kind = nil body.gsub(/\r/,'').sub(/\A\n*/,'').sub(/\n*\z/,"\n\n").each_line('') do |fragment| if buffer and TAG_END_REGEXP =~ fragment and $2.downcase == tag_kind then section << buffer + fragment.sub(/\n*\z/,"\n\n") tag_kind = nil buffer = nil elsif buffer then buffer << fragment else if section @sections << section end title = TITLE_REGEXP.match(fragment+"\n").to_a[1] section = EtdiarySection::new( title, ) fragment = fragment[ title.length + 4 .. -1 ].sub(/\A\n/,'') if title if TAG_BEG_REGEXP =~ fragment then tag_kind = $1.downcase if TAG_END_REGEXP =~ fragment and $2.downcase == tag_kind then section << fragment.sub(/\n*\z/,"\n\n") tag_kind = nil else buffer = fragment end else section << fragment end end end if buffer section << buffer << "</#{tag_kind}>(tDiary warning: tag <#{tag_kind}> is not terminated.)" end if section @sections << section end @last_modified = Time::now self end |
#delete_section(index) ⇒ Object
371 372 373 |
# File 'lib/tdiary/style/etdiary.rb', line 371 def delete_section(index) @sections.delete_at(index - 1) end |
#each_paragraph ⇒ Object
343 344 345 346 347 |
# File 'lib/tdiary/style/etdiary.rb', line 343 def each_paragraph @sections.each do |fragment| yield fragment end end |
#each_section {|section| ... } ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/tdiary/style/etdiary.rb', line 349 def each_section section = nil each_paragraph do |fragment| if section and nil == fragment.anchor_type then section << fragment.body else yield section if section and section.anchor_type section = fragment.dup section.set_body( [ fragment.body ] ) end end yield section if section end |
#replace(date, title, body) ⇒ Object
295 296 297 298 299 300 |
# File 'lib/tdiary/style/etdiary.rb', line 295 def replace( date, title, body ) set_date( date ) set_title( title ) @sections = [] append( body ) end |
#style ⇒ Object
291 292 293 |
# File 'lib/tdiary/style/etdiary.rb', line 291 def style 'etDiary' end |
#to_html(opt = {'anchor' => true, 'index' => true}, mode = :HTML) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/tdiary/style/etdiary.rb', line 408 def to_html( opt = {'anchor' => true, 'index' => true}, mode = :HTML ) case mode when :CHTML f = EtCHtmlFactory::new(opt) else f = EtHtml4Factory::new(opt) end r = f.section_start( date ) each_paragraph do |fragment| if :H3 == fragment.anchor_type and r != f.section_start( date ) then r << f.section_end( date ) << "\n" << f.section_start( date ) end r << to_html_section(fragment,f) end r + f.section_end( date ) end |
#to_html_section(section, factory) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/tdiary/style/etdiary.rb', line 383 def to_html_section(section, factory) r = '' s = if section.bodies then section.body else nil end t = factory.title( date, section ) if factory.block_title?(section) then r << t if t t = nil end if s && PRE_REGEXP =~ s then r << factory.p_start << t << factory.p_end << "\n" if t r << factory.pre_start r << $1.gsub(/&/,"&").gsub(/</,"<").gsub(/>/,">") r << factory.pre_end << "\n" elsif s && /\A</ =~ s then r << factory.p_start << t << factory.p_end << "\n" if t r << s.sub( /\n*\z/, "\n" ) else r << factory.p_start if t || s r << t if t r << s.sub(/\A\n*/,"\n").sub(/\n*\z/, "\n") if s r << factory.p_end << "\n" if t || s end r end |
#to_s ⇒ Object
425 426 427 428 |
# File 'lib/tdiary/style/etdiary.rb', line 425 def to_s "date=#{date.strftime('%Y%m%d')}, title=#{title}, " \ + "body=[#{@sections.join('][')}]" end |
#to_src ⇒ Object
375 376 377 378 379 380 381 |
# File 'lib/tdiary/style/etdiary.rb', line 375 def to_src src = '' each_paragraph do |fragment| src << fragment.to_src end src.sub(/\n*\z/,"\n") end |