class Eggshell::Bundles::Basic::SectionBlocks

Constants

SECTION
SECTION_END
START
TOC_TEMPLATE

Public Class Methods

new() click to toggle source
# File lib/eggshell/bundles/basics.rb, line 452
def initialize
        @block_types = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', SECTION, SECTION_END, 'toc']
        @header_list = []
        @header_idx = {}
end

Public Instance Methods

can_handle(line) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 460
def can_handle(line)
        match = START.match(line)
        if match
                @block_type = match[1]
                return @block_type != 'toc' ? BH::DONE : BH::COLLECT
        end
        return BH::RETRY
end
process(type, args, lines, out, call_depth = 0) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 469
def process(type, args, lines, out, call_depth = 0)
        bp = get_block_params(type, args[0])
        line = lines[0]
        line = line.line.strip if line.is_a?(Eggshell::Line)

        if type[0] == 'h'
                if type == 'hr'
                        out << create_tag(type, bp, false)
                else
                        lvl = type[1].to_i

                        # assign unique id
                        id = bp['id'] || line.downcase.strip.gsub(/[^a-z0-9_-]+/, '-')
                        lid = id
                        i = 1
                        while @header_idx[lid] != nil
                                lid = "#{id}-#{i}"
                                i += 1
                        end
                        id = lid
                        bp['id'] = id
                        title = @eggshell.expand_formatting(line)

                        out << "#{create_tag(type, bp)}#{title}</#{type}>"

                        @header_list << {:level => lvl, :id => lid, :title => title, :tag => type}
                        @header_idx[lid] = @header_list.length - 1
                end
        elsif type == SECTION
                out << create_tag(SECTION, bp)
                @header_list << type
        elsif type == SECTION_END
                out << '</section>'
                @header_list << type
        elsif type == 'toc'
                # first, parse the toc definitions from lines
                toc_template = TOC_TEMPLATE.clone
                lines.each do |line_obj|
                        line = line_obj.is_a?(Eggshell::Line) ? line_obj.line : line
                        key, val = line.split(':', 2)
                        toc_template[key.to_sym] = val
                end

                # now go through collected headers and sections and generate toc
                out << @eggshell.expand_formatting(toc_template[:start]) if toc_template[:start]
                @header_list.each do |entry|
                        if entry == SECTION
                                out << @eggshell.expand_formatting(toc_template[:section]) if toc_template[:section]
                        elsif entry == SECTION_END
                                out << @eggshell.expand_formatting(toc_template[:section_end]) if toc_template[:section_end]
                        elsif entry.is_a?(Hash)
                                tpl = toc_template[entry[:tag]] || toc_template[:default]
                                out << @eggshell.expand_formatting(
                                        tpl.gsub('$id', entry[:id]).gsub('$title', entry[:title]).gsub('$level', entry[:level].to_s)
                                )
                        end
                end
                out << @eggshell.expand_formatting(toc_template[:end]) if toc_template[:end]
        end
end