Class: Eggshell::Bundles::Basic::SectionBlocks

Inherits:
Object
  • Object
show all
Includes:
Eggshell::BlockHandler, Eggshell::BlockHandler::BlockParams, Eggshell::BlockHandler::HtmlUtils
Defined in:
lib/eggshell/bundles/basics.rb

Constant Summary collapse

SECTION =
'section'
SECTION_END =
'section-end'
TOC_TEMPLATE =
{
  :default => "<div class='toc-h$level'><a href='\#$id'>$title</a></div>"
}
START =
/^(h[1-6]|section|toc)[(.]/

Constants included from Eggshell::BlockHandler::HtmlUtils

Eggshell::BlockHandler::HtmlUtils::HASH_HTML_ESCAPE

Constants included from Eggshell::BlockHandler::BlockParams

Eggshell::BlockHandler::BlockParams::ATTRIBUTES, Eggshell::BlockHandler::BlockParams::CLASS, Eggshell::BlockHandler::BlockParams::ID, Eggshell::BlockHandler::BlockParams::KEYS, Eggshell::BlockHandler::BlockParams::STYLE

Constants included from Eggshell::BlockHandler

Eggshell::BlockHandler::COLLECT, Eggshell::BlockHandler::COLLECT_RAW, Eggshell::BlockHandler::DONE, Eggshell::BlockHandler::RETRY

Instance Method Summary collapse

Methods included from Eggshell::BlockHandler::HtmlUtils

#attrib_string, #create_tag, #css_string, #html_escape, #inject_attribs

Methods included from Eggshell::BlockHandler::BlockParams

#get_block_params, #set_block_params

Methods included from Eggshell::BlockHandler

#continue_with, #current_type, #equal?, #reset, #set_processor

Methods included from Eggshell::BaseHandler

#set_processor

Constructor Details

#initializeSectionBlocks

Returns a new instance of SectionBlocks.



447
448
449
450
451
# File 'lib/eggshell/bundles/basics.rb', line 447

def initialize
  @block_types = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', SECTION, SECTION_END, 'toc']
  @header_list = []
  @header_idx = {}
end

Instance Method Details

#can_handle(line) ⇒ Object



455
456
457
458
459
460
461
462
# File 'lib/eggshell/bundles/basics.rb', line 455

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) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/eggshell/bundles/basics.rb', line 464

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