Parent

Class Index [+]

Quicksearch

Webgen::Page

A Page object wraps a meta information hash and an array of Block objects. It is normally generated from a file or string in Webgen Page Format using the provided class methods.

Attributes

meta_info[R]

The contents of the meta information block.

blocks[R]

The hash of blocks for the page.

Public Class Methods

from_data(data, meta_info = {}) click to toggle source

Parse the given string data in Webgen Page Format and initialize a new Page object with the information. The meta_info parameter can be used to provide default meta information.

    # File lib/webgen/page.rb, line 67
67:       def from_data(data, meta_info = {})
68:         md = /(#{RE_META_INFO})?(.*)/.match(normalize_eol(data))
69:         meta_info = meta_info.merge(parse_meta_info(md[1], data))
70:         blocks = parse_blocks(md[2] || '', meta_info)
71:         new(meta_info, blocks)
72:       end
meta_info_from_data(data) click to toggle source

Parse the given string data in Webgen Page Format and return the found meta information.

    # File lib/webgen/page.rb, line 75
75:       def meta_info_from_data(data)
76:         md = /(#{RE_META_INFO})?/.match(normalize_eol(data))
77:         parse_meta_info(md[1], data)
78:       end
new(meta_info = {}, blocks = {}) click to toggle source

Create a new Page object with the meta information provided in meta_info and the given blocks.

     # File lib/webgen/page.rb, line 147
147:     def initialize(meta_info = {}, blocks = {})
148:       @meta_info = meta_info
149:       @blocks = blocks
150:     end

Private Class Methods

normalize_eol(data) click to toggle source

Normalize the end-of-line encodings to Unix style.

    # File lib/webgen/page.rb, line 85
85:       def normalize_eol(data)
86:         data.gsub(/\r\n?/, "\n")
87:       end
parse_blocks(data, meta_info) click to toggle source

Parse all blocks in data and return them. Meta information can be provided in meta_info which is used for setting the block names and options.

     # File lib/webgen/page.rb, line 111
111:       def parse_blocks(data, meta_info)
112:         scanned = data.scan(RE_BLOCKS)
113:         raise(FormatError, 'No content blocks specified') if scanned.length == 0
114: 
115:         blocks = {}
116:         scanned.each_with_index do |block_data, index|
117:           options, content = *block_data
118:           md = RE_BLOCKS_OPTIONS.match(options.to_s)
119:           raise(FormatError, "Found invalid blocks starting line for block #{index+1}: #{options}") if content =~ /\A---/ || md.nil?
120:           options = Hash[*md[1].to_s.scan(/(\w+):([^\s]*)/).map {|k,v| [k, (v == '' ? nil : YAML::load(v))]}.flatten]
121:           options = (meta_info['blocks']['default'] || {} rescue {}).
122:             merge((meta_info['blocks'][index+1] || {} rescue {})).
123:             merge(options)
124: 
125:           name = options.delete('name') || (index == 0 ? 'content' : 'block' + (index + 1).to_s)
126:           raise(FormatError, "Previously used name '#{name}' also used for block #{index+1}") if blocks.has_key?(name)
127:           content ||= ''
128:           content.gsub!(/^(\\+)(---.*?)$/) {|m| "\\" * ($1.length / 2) + $2}
129:           content.chomp!("\n") unless index + 1 == scanned.length
130:           blocks[name] = blocks[index+1] = Block.new(name, content, options)
131:         end
132:         meta_info.delete('blocks')
133:         blocks
134:       end
parse_meta_info(mi_data, data) click to toggle source

Parse the meta info string in mi_data and return the hash with the meta information. The original data is used for checking the validness of the meta information block.

     # File lib/webgen/page.rb, line 91
 91:       def parse_meta_info(mi_data, data)
 92:         if mi_data.nil? && data =~ RE_META_INFO_START
 93:           raise FormatError, 'Found start line for meta information block but no valid meta information block'
 94:         elsif mi_data.nil?
 95:           {}
 96:         else
 97:           begin
 98:             meta_info = YAML::load(mi_data.to_s)
 99:             unless meta_info.kind_of?(Hash)
100:               raise FormatError, "Invalid structure of meta information block: expected YAML hash but found #{meta_info.class}"
101:             end
102:           rescue ArgumentError => e
103:             raise FormatError, "Invalid YAML syntax in meta information block: #{e.message}"
104:           end
105:           meta_info
106:         end
107:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.