class TDiary::Style::EtdiaryDiary

Constants

PRE_REGEXP
TAG_BEG_REGEXP
TAG_END_REGEXP
TITLE_REGEXP

Public Class Methods

new( date, title, body, modified = Time::now ) click to toggle source
# File lib/tdiary/style/etdiary.rb, line 279
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

Public Instance Methods

add_section(subtitle, body) click to toggle source
# File lib/tdiary/style/etdiary.rb, line 362
def add_section(subtitle, body)
        sec = EtdiarySection::new( '' )
        sec.subtitle = subtitle
        sec.body     = body
        @sections << sec
        @sections.size
end
append( body, author = nil ) click to toggle source
# File lib/tdiary/style/etdiary.rb, line 301
def append( body, author = 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, author )
                        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 &lt;#{tag_kind}&gt; is not terminated.)"
        end
        if section
                @sections << section
        end
        @last_modified = Time::now
        self
end
delete_section(index) click to toggle source
# File lib/tdiary/style/etdiary.rb, line 370
def delete_section(index)
        @sections.delete_at(index - 1)
end
each_paragraph() { |fragment| ... } click to toggle source
# File lib/tdiary/style/etdiary.rb, line 342
def each_paragraph
        @sections.each do |fragment|
                yield fragment
        end
end
each_section() { |section| ... } click to toggle source
# File lib/tdiary/style/etdiary.rb, line 348
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 ) click to toggle source
# File lib/tdiary/style/etdiary.rb, line 294
def replace( date, title, body )
        set_date( date )
        set_title( title )
        @sections = []
        append( body )
end
style() click to toggle source
# File lib/tdiary/style/etdiary.rb, line 290
def style
        'etDiary'
end
to_html( opt = {'anchor' => true, 'index' => true}, mode = :HTML ) click to toggle source
# File lib/tdiary/style/etdiary.rb, line 407
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) click to toggle source
# File lib/tdiary/style/etdiary.rb, line 382
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(/&/,"&amp;").gsub(/</,"&lt;").gsub(/>/,"&gt;")
                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() click to toggle source
# File lib/tdiary/style/etdiary.rb, line 424
def to_s
        "date=#{date.strftime('%Y%m%d')}, title=#{title}, " \
        + "body=[#{@sections.join('][')}]"
end
to_src() click to toggle source
# File lib/tdiary/style/etdiary.rb, line 374
def to_src
        src = ''
        each_paragraph do |fragment|
                src << fragment.to_src
        end
        src.sub(/\n*\z/,"\n")
end