class TDiary::Style::RDSection

Attributes

author[R]
body_to_html[R]
categories[R]
stripped_subtitle[R]
stripped_subtitle_to_html[R]
subtitle[R]
subtitle_to_html[R]

Public Class Methods

new( fragment, author = nil ) click to toggle source
# File lib/tdiary/style/rd.rb, line 180
def initialize( fragment, author = nil )
        @author = author
        if /\A=(?!=)/ =~ fragment then
                @subtitle, @body = fragment.split( /\n/, 2 )
                @subtitle.sub!( /^\=\s*/, '' )
        else
                @subtitle = nil
                @body = fragment.dup
        end
        @body = @body || ''
        @body.sub!( /[\n\r]+\Z/, '' )
        @body << "\n\n"

        @categories = get_categories
        @stripped_subtitle = strip_subtitle

        @subtitle_to_html = manufacture(@subtitle, true)
        @stripped_subtitle_to_html = manufacture(@stripped_subtitle, true)
        @body_to_html = manufacture(@body, false)
end

Public Instance Methods

body() click to toggle source
# File lib/tdiary/style/rd.rb, line 215
def body
        @body.dup
end
body=(str) click to toggle source
# File lib/tdiary/style/rd.rb, line 211
def body=(str)
        @body = str
end
categories=(categories) click to toggle source
# File lib/tdiary/style/rd.rb, line 219
def categories=(categories)
        @categories = categories
        cat_str = ""
        categories.each {|cat|
                cat_str << "[#{cat}]"
        }
        cat_str << " " unless cat_str.empty?
        @subtitle = @subtitle ? (cat_str + @stripped_subtitle) : nil
        @stripped_subtitle = strip_subtitle
end
html( date, idx, opt, mode = :HTML) click to toggle source
# File lib/tdiary/style/rd.rb, line 236
def html( date, idx, opt, mode = :HTML)
        if mode == :CHTML
                visitor = RD2tDiaryCHTMLVistor.new( date, idx, opt, @author)
                section_open = "<%=section_enter_proc( Time::at( #{date.to_i} ))%>\n"
                section_close = "<%=section_leave_proc( Time::at( #{date.to_i} ))%>\n"
        else
                visitor = RD2tDiaryVisitor.new( date, idx, opt, @author )
                section_open = %Q[<div class="section">\n<%=section_enter_proc( Time::at( #{date.to_i} ))%>\n]
                section_close = "<%=section_leave_proc( Time::at( #{date.to_i} ))%>\n</div>\n"
        end

        src = to_src.split(/^/)
        src.unshift("=begin\n").push("=end\n")
        tree = RDTree.new( src, nil, nil)
        begin
                tree.parse
        rescue ParseError
                raise SyntaxError, $!.message
        end

        r = "#{section_open}#{visitor.visit( tree )}#{section_close}"
end
subtitle=(subtitle) click to toggle source
# File lib/tdiary/style/rd.rb, line 201
def subtitle=(subtitle)
        cat_str = ""
        @categories.each {|cat|
                cat_str << "[#{cat}]"
        }
        cat_str << " " unless cat_str.empty?
        @subtitle = subtitle ? (cat_str + subtitle) : nil
        @stripped_subtitle = strip_subtitle
end
to_src() click to toggle source
# File lib/tdiary/style/rd.rb, line 230
def to_src
        r = ''
        r << "= #{@subtitle}\n" if @subtitle
        r << @body
end

Private Instance Methods

get_categories() click to toggle source
# File lib/tdiary/style/rd.rb, line 274
def get_categories
        return [] unless @subtitle
        cat = /^(\[(.*?)\])+/.match(@subtitle).to_a[0]
        return [] unless cat
        cat.scan(/\[(.*?)\]/).collect do |c|
                c[0].split(/,/)
        end.flatten
end
manufacture(str, subtitle = false) click to toggle source
# File lib/tdiary/style/rd.rb, line 260
def manufacture(str, subtitle = false)
        return nil unless str
        src = str.strip.split(/^/).unshift("=begin\n").push("=end\n")
        visitor = RD2tDiaryVisitor.new
        tree = RDTree.new(src, nil, nil)
        begin
                r = visitor.visit( tree.parse )
                r.gsub!(/<\/?p>/, '') if subtitle
                r
        rescue ParseError
                str
        end
end
strip_subtitle() click to toggle source
# File lib/tdiary/style/rd.rb, line 283
def strip_subtitle
        return nil unless @subtitle
        @subtitle.sub(/^(\[(.*?)\])+/,'')
end