class Eggshell::Bundles::Basic::TableBlock

Constants

CELL_ATTR_END
CELL_ATTR_IDX
CELL_ATTR_START
DELIM1

@todo support opening row with !@ (which would then get applied to <tr>) @todo support thead/tbody/tfoot attributes? maybe these things can be handled same as how caption/colgroup is defined

DELIM2
SPECIAL_DEF
T_ROW

Public Class Methods

new() click to toggle source
# File lib/eggshell/bundles/basics.rb, line 152
def initialize
        @block_types = ['table']
end

Public Instance Methods

can_handle(line) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 166
def can_handle(line)
        if !@block_type
                if line.match(/^(table[(.]?|\||\|>|\/|!@)/) || line.match(SPECIAL_DEF)
                        @block_type = 'table'
                        return BH::COLLECT
                end
        end
        return BH::RETRY
end
continue_with(line) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 176
def continue_with(line)
        if line.match(/^(\||\|>|\/|!@)/) || line.match(SPECIAL_DEF)
                return BH::COLLECT
        end
        return BH::RETRY
end
fmt_cell(val, header = false, colnum = 0) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 304
def fmt_cell(val, header = false, colnum = 0)
        tag = header ? 'th' : 'td'
        buff = []
        attribs = ''

        if val[0] == '\\'
                val = val[1..val.length]
        elsif val[0..CELL_ATTR_IDX] == CELL_ATTR_START
                rt = val.index(CELL_ATTR_END)
                if rt
                        attribs = val[CELL_ATTR_IDX+1...rt]
                        val = val[rt+CELL_ATTR_END.length..val.length]
                end
        end

        # inject column position via class
        attribs = inject_attribs(attribs, {:class=>"td-col-#{colnum}"})

        buff << "<#{tag} #{attribs}>"
        cclass = 
        if val[0] == '\\'
                val = val[1..val.length]
        end

        buff << @eggshell.expand_formatting(val)
        buff << "</#{tag}>"
        return buff.join('')
end
process(type, args, lines, out, call_depth = 0) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 184
def process(type, args, lines, out, call_depth = 0)
        args = [] if !args
        bp = get_block_params(type, args[0])
        row_classes = bp['row.classes']
        row_classes = ['odd', 'even'] if !row_classes.is_a?(Array)
        
        o_out = out
        out = []

        @eggshell.vars[T_ROW] = 0
        out << create_tag('table', bp)
        out << "%caption%\n%colgroup%"
        caption = {:text => '', :atts => {}}
        colgroup = []

        cols = []
        rows = 0
        rc = 0
        data_started = false

        lines.each do |line_obj|
                ccount = 0
                line = line_obj.line
                special = line.match(SPECIAL_DEF)
                if special
                        type = special[1]
                        atts = special[2]
                        if type == 'caption'
                                text, atts = atts.split('|', 2)
                                caption[:text] = text
                                caption[:attributes] = parse_args(atts, true)[0]
                        else
                                atts = parse_args(atts, true)[0]
                                colgroup << create_tag('col', attrib_string(atts), false)
                        end
                elsif line[0] == '/' && rc == 0
                        cols = line[1..line.length].split('|')
                        out << "<thead><tr class='#{bp['head.class']}'>"
                        cols.each do |col|
                                col = col.strip
                                if col == '' && bp['emptycell']
                                        col = bp['emptycell']
                                end
                                out << "\t#{fmt_cell(col, true, ccount)}"
                                ccount += 1
                        end
                        out << '</tr></thead>'
                elsif line[0] == '/'
                        # implies footer
                        out << '</tbody>' if rc > 0
                        cols = line[1..line.length].split('|')
                        out << "<tfoot><tr class='#{bp['foot.class']}'>"
                        cols.each do |col|
                                col = col.strip
                                if col == '' && bp['emptycell']
                                        col = bp['emptycell']
                                end
                                out << "\t#{fmt_cell(col, true, ccount)}"
                                ccount += 1
                        end
                        out << '</tr></tfoot>'
                        break
                elsif line[0] == DELIM1 || line[0..1] == DELIM2 || line[0..1] == CELL_ATTR_START
                        out << '<tbody>' if rc == 0
                        idx = 1
                        rattribs = ''
                        
                        if line[0..1] == CELL_ATTR_START
                                # @todo look for delim instead of '@!'?
                                rt = line.index(CELL_ATTR_END)
                                if rt
                                        rattribs = line[2...rt]
                                        line = line[rt+CELL_ATTR_END.length..line.length]
                                end
                        end

                        sep = /(?<!\\)\|/
                        if line[1] == '>'
                                idx = 2
                                sep = /(?<!\\)\|\>/
                        end
                        cols = line[idx..line.length].split(sep)
                        @eggshell.vars[T_ROW] = rc
                        rclass = row_classes[rc % row_classes.length]
                        rattribs = inject_attribs(rattribs, {:class=>"tr-row-#{rc} #{rclass}"})

                        out << "<tr #{rattribs}>"
                        cols.each do |col|
                                col = col.strip
                                if col == '' && bp['emptycell']
                                        col = bp['emptycell']
                                end
                                out << "\t#{fmt_cell(col, false, ccount)}"
                                ccount += 1
                        end
                        out << '</tr>'
                        rc += 1
                else
                        cols = line[1..line.length].split('|') if line[0] == '\\'
                end
                rows += 1
        end

        out << "</table>"

        if caption[:text] != ''
                out[1].gsub!('%caption%', create_tag('caption', attrib_string(caption[:attributes]), true, caption[:text]))
        else
                out[1].gsub!("%caption%\n", '')
        end

        if colgroup.length > 0
                out[1].gsub!('%colgroup%', "<colgroup>\n\t#{colgroup.join("\n\t")}\n</colgroup>")
        else
                out[1].gsub!('%colgroup%', '')
        end
        
        o_out << out.join("\n")
end