class Eggshell::Bundles::Basic::ListBlocks

Constants

START_LIST
START_LIST_SHORT

Public Class Methods

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

Public Instance Methods

can_handle(line) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 346
def can_handle(line)
        match = START_LIST.match(line)
        if match
                @block_type = match[1]
                return BH::COLLECT
        end

        match = START_LIST_SHORT.match(line)
        if match
                if match[1] == '>'
                        @block_type = 'dl'
                else
                        @block_type = match[1] == '-' ? 'ul' : 'ol'
                end
                return BH::COLLECT
        end
        
        return BH::RETRY
end
continue_with(line) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 366
def continue_with(line)
        if line == nil || line == "" || !START_LIST_SHORT.match(line)
                return BH::RETRY
        else
                return BH::COLLECT
        end
end
equal?(handler, type) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 436
def equal?(handler, type)
        self.class == handler.class && (type == 'ol' || type == 'ul')
end
process(type, args, lines, out, call_depth = 0) click to toggle source

@todo support ability to add attributes to sub-lists (maybe '[-#] @(.…)')

# File lib/eggshell/bundles/basics.rb, line 375
def process(type, args, lines, out, call_depth = 0)
        if type == 'dl'
                # @todo
        else
                order_stack = []
                otype_stack = []
                last = nil
                first_type = nil

                if lines[0] && !lines[0].match(/[-#]/)
                        line = lines.shift
                end

                lines.each do |line_obj|
                        line = nil
                        indent = 0
                        if line_obj.is_a?(Eggshell::Line)
                                line = line_obj.line
                                indent = line_obj.indent_lvl
                        else
                                line = line_obj
                        end
                        ltype = line[0] == '-' ? 'ul' : 'ol'
                        line = line[1..line.length].strip

                        if order_stack.length == 0
                                # use the given type to start; infer for sub-lists
                                order_stack << create_tag(type, get_block_params(type, args[0]))
                                otype_stack << type
                        # @todo make sure that previous item was a list
                        # remove closing li to enclose sublist
                        elsif indent > (otype_stack.length-1) && order_stack.length > 0
                                last = order_stack[-1]
                                last = last[0...last.length-5]
                                order_stack[-1] = last

                                order_stack << "#{"\t"*indent}<#{ltype}>"
                                otype_stack << ltype
                        elsif indent < (otype_stack.length-1)
                                count = otype_stack.length - 1
                                while count > indent
                                        ltype = otype_stack.pop  
                                        order_stack << "#{"\t"*count}</#{ltype}>\n#{"\t"*(count-1)}</li>"
                                        count -= 1
                                end
                        end
                        order_stack << "#{"\t"*indent}<li>#{line}</li>"
                end

                # close nested lists
                d = otype_stack.length
                c = 1
                otype_stack.reverse.each do |ltype|
                        ident = d - c
                        order_stack << "#{"\t" * ident}</#{ltype}>\n#{ident-1 >= 0 ? "\t"*(ident-1) : ''}#{c == d ? '' : "</li>"}"
                        c += 1
                end
                out << @eggshell.expand_all(order_stack.join("\n"))
        end
end