Class: Eggshell::Bundles::Basic::SectionBlocks
- Inherits:
-
Object
- Object
- Eggshell::Bundles::Basic::SectionBlocks
- 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
- #can_handle(line) ⇒ Object
-
#initialize ⇒ SectionBlocks
constructor
A new instance of SectionBlocks.
- #process(type, args, lines, out, call_depth = 0) ⇒ Object
Methods included from Eggshell::BlockHandler::HtmlUtils
#attrib_string, #create_tag, #css_string, #html_escape
Methods included from Eggshell::BlockHandler::BlockParams
#get_block_params, #set_block_params
Methods included from Eggshell::BlockHandler
#continue_with, #current_type, #reset, #set_processor
Methods included from Eggshell::BaseHandler
Constructor Details
#initialize ⇒ SectionBlocks
422 423 424 425 426 |
# File 'lib/eggshell/bundles/basics.rb', line 422 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
430 431 432 433 434 435 436 437 |
# File 'lib/eggshell/bundles/basics.rb', line 430 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
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 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 |
# File 'lib/eggshell/bundles/basics.rb', line 439 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.(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.(toc_template[:start]) if toc_template[:start] @header_list.each do |entry| if entry == SECTION out << @eggshell.(toc_template[:section]) if toc_template[:section] elsif entry == SECTION_END out << @eggshell.(toc_template[:section_end]) if toc_template[:section_end] elsif entry.is_a?(Hash) tpl = toc_template[entry[:tag]] || toc_template[:default] out << @eggshell.( tpl.gsub('$id', entry[:id]).gsub('$title', entry[:title]).gsub('$level', entry[:level].to_s) ) end end out << @eggshell.(toc_template[:end]) if toc_template[:end] end end |