class Html::Elem

Constants

IndentAdd
LineLenMax

Attributes

attrs[R]
content[R]
name[R]
style[R]

Public Class Methods

new(name, attrs=nil, content=nil) click to toggle source
# File misc/txt2html.rb, line 19
def initialize(name, attrs=nil, content=nil)
        @name = name
        @attrs = Hash.new
        @style = Hash.new
        attrs.each { |k, v| set_attr(k, v) } if attrs
        if content == false
                @content = Array.new
                @uniq = true
        else
                @content = content ? content : Array.new
                @uniq = false
        end
        self
end

Public Instance Methods

<<(*content)
Alias for: add
add(*content) click to toggle source
# File misc/txt2html.rb, line 43
def add(*content)
        content.each { |e|
                if (e.class == Array)
                        add(*e)
                        next
                end
                if e.class.ancestors.include? Elem
                        @content << e
                else
                        @content << e.to_s.gsub(Regexp.new("(#{@@quotechars.keys.join('|')})", 'm')) { |x| @@quotechars[x] }
                end
        }
        self
end
Also aliased as: <<
add_style(k, v) click to toggle source
# File misc/txt2html.rb, line 59
def add_style(k, v)
        @style[k] = v
        self
end
bg(c) click to toggle source
# File misc/txt2html.rb, line 75
def bg(c)
        @style['background'] = c
        self
end
hclass(c) click to toggle source
# File misc/txt2html.rb, line 80
def hclass(c)
        @attrs['class'] = c
        self
end
inspect() click to toggle source
# File misc/txt2html.rb, line 181
def inspect
        "<#{@name}"+@content.map{|c|"\n"+c.inspect}.join+"\n/#{@name}>"
end
length(start=nil) click to toggle source
# File misc/txt2html.rb, line 85
def length(start=nil)
        # text length on one line w/o indent
        if start
                l = start.length
        else
                # '<name>'
                l = @name.length + 2
                @attrs.each{ |k, v|
                        l += " #{k}=\"#{v}\"".length
                }
                # ' style=""' - last '; '
                l += 9-2 unless @style.empty?
                # 'k: v; '
                @style.each{ |k, v|
                        l += "#{k}: #{v}; ".length
                }
                # ' /'
                l += 2 if @uniq
        end
        @content.each{ |c|
                l += c.length
        }
        # '</name>'
        l += 3+@name.length unless @uniq
        return l
end
set_attr(k, v) click to toggle source
# File misc/txt2html.rb, line 64
def set_attr(k, v)
        if k == 'style'
                v.split(/\s*;\s*/).each { |s|
                        add_style($1, $2) if s =~ /^\s*(\S+)\s*:\s*(.*?)\s*$/
                }
        else
                @attrs[k]=v
        end
        self
end
to_s(indent = '') click to toggle source
# File misc/txt2html.rb, line 112
def to_s(indent = '')
        attrs = @attrs.map { |k, v| " #{k}=\"#{v}\"" }.join
        attrs += ' style="' + @style.map{ |k, v| "#{k}: #{v}" }.join('; ') + '"' unless @style.empty?
        s = '' << indent << '<' << @name << attrs << (@uniq ? ' />' : '>')
        if @uniq
                s
        elsif @name == 'pre'
                s << @content.map { |c| c.to_s }.join.chomp << '</pre>'
        else
                if length(s) > LineLenMax
                        sindent = indent + IndentAdd
                        sep = "\n"
                        @content.each { |c|
                                case c
                                when Elem
                                        if sep == ''
                                                s << c.to_s(sindent).sub(/^\s+/, '')
                                        else
                                                news = c.to_s(sindent)
                                                plen = s.length - (s.rindex("\n") || -1) - 1
                                                plen -= 1 if s[-1, 1] == ' '
                                                newss = news.sub(/^\s+/, '')
                                                if not news.include?("\n") and s[-1] != ?> and
                                                                plen + 1 + newss.length <= LineLenMax
                                                        # concat inline tag to previous String
                                                        s << ' ' if s[-1, 1] != ' '
                                                        s << newss
                                                else
                                                        s << sep if c.name =~ /^h\d$/ and c != @content.first
                                                        s << sep << news
                                                end
                                        end
                                when String
                                        cw = c.split(/\s+/)
                                        if @name == 'p' and c.object_id == @content.first.object_id
                                                cw.shift if cw[0] == ''
                                                s << "\n" << sindent
                                        else
                                                s << cw.shift.to_s
                                        end
                                        plen = s.length - (s.rindex("\n") || -1) - 1
                                        while w = cw.shift
                                                plen -= 1 if s[-1, 1] == ' '
                                                if plen + 1 + w.length > LineLenMax
                                                        s << "\n" << sindent
                                                        plen = sindent.length
                                                end
                                                s << ' ' if s[-1, 1] != ' '
                                                s << w
                                                plen += w.length+1
                                        end
                                        if c !~ /\s+$/
                                                sep = ''
                                                next
                                        end
                                else
                                        s << sep << sindent << c.to_s
                                end
                                sep = "\n"
                        }
                        sep = "\n" if @name == 'p'
                        sep << indent if sep != ''
                        s << sep << "</#@name>"
                else
                        s << @content.map { |c| c.to_s }.join << "</#@name>"
                end
        end
end