Class HtmlBlock
In: lib/webgen/content.rb
Parent: Object
WebPageDataInvalid WebPageData HtmlBlock RuntimeError dot/f_3.png

A single block within a page file. The content of the block gets automatically parsed for HTML headers with the id attribute set and converts them into sections for later use.

Methods

Classes and Modules

Class HtmlBlock::Section

Constants

SECTION_REGEXP = /<h([123456])(?:>|\s([^>]*)>)(.*?)<\/h\1\s*>/i  
ATTR_REGEXP = /\s*(\w+)\s*=\s*('|")([^\2]+)\2\s*/

Attributes

content  [R]  The content of the block.
name  [R]  The name of the block.
sections  [R]  The parsed sections as array of Section objects.

Public Class methods

Creates a new block with the name name and the given content. The content gets parsed for sections automatically.

[Source]

    # File lib/webgen/content.rb, line 64
64:   def initialize( name, content )
65:     @name, @content = name, content
66:     @sections = self.class.parse_sections( content )
67:   end

Private Class methods

[Source]

     # File lib/webgen/content.rb, line 82
 82:   def self.parse_sections( content )
 83:     sections = []
 84:     stack = []
 85:     content.scan( SECTION_REGEXP ).each do |level,attrs,title|
 86:       next if attrs.nil?
 87:       id_attr = attrs.scan( ATTR_REGEXP ).find {|name,sep,value| name == 'id'}
 88:       next if id_attr.nil?
 89:       id = id_attr[2]
 90: 
 91:       section = Section.new( level.to_i, id, title )
 92:       success = false
 93:       while !success
 94:         if stack.empty?
 95:           sections << section
 96:           stack << section
 97:           success = true
 98:         elsif stack.last.level < section.level
 99:           stack.last.subsections << section
100:           stack << section
101:           success = true
102:         else
103:           stack.pop
104:         end
105:       end
106:     end
107:     sections
108:   end

Public Instance methods

Renders the block using ERB and context as binding.

[Source]

    # File lib/webgen/content.rb, line 70
70:   def render_with_erb( context )
71:     @compiled_block = ERB.new( @content ) unless defined?( @compiled_block )
72:     @compiled_block.result( context )
73:   end

[Validate]