class Webgen::Page

A Page object wraps a meta information hash and a hash of {block name => block content} associations.

It is normally generated from a file or string in Webgen Page Format using the provided class methods.

Attributes

blocks[R]

The hash of blocks for the page.

meta_info[R]

The contents of the meta information block.

Public Class Methods

from_data(data) click to toggle source

Parse the given string data in Webgen Page Format.

This method returns a Page object containing the hash with the meta information and the parsed blocks.

   # File lib/webgen/page.rb
35 def from_data(data)
36   md = RE_PAGE.match(data)
37   meta_info = parse_meta_info(md[1], data =~ RE_META_INFO_START)
38   blocks = parse_blocks(md[2] || '', meta_info)
39   new(meta_info, blocks)
40 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
109 def initialize(meta_info = {}, blocks = {})
110   @meta_info = meta_info
111   @blocks = blocks
112 end

Private Class Methods

parse_blocks(data, meta_info) click to toggle source

Parse all blocks in data and return them.

The key 'blocks' of the meta information hash is updated with information found on block starting lines.

   # File lib/webgen/page.rb
67 def parse_blocks(data, meta_info)
68   scanned = data.scan(RE_BLOCKS)
69   raise(FormatError, 'No content blocks specified') if scanned.length == 0
70 
71   blocks = {}
72   scanned.each_with_index do |block_data, index|
73     index += 1
74     options, content = *block_data
75     if md = RE_BLOCKS_START_SIMPLE.match(options.to_s)
76       options = {'name' => md[1]}
77     else
78       md = RE_BLOCKS_START_COMPLEX.match(options.to_s)
79       raise(FormatError, "Found invalid blocks starting line for block #{index}: #{options}") if content =~ /\A---/ || md.nil?
80       options = Hash[*md[1].to_s.scan(/(\w+):([^\s]*)/).map {|k,v| [k, (v == '' ? nil : YAML::load(v))]}.flatten]
81     end
82 
83     name = options.delete('name') || (index == 1 ? 'content' : 'block' + (index).to_s)
84     raise(FormatError, "Previously used name '#{name}' also used for block #{index}") if blocks.has_key?(name)
85     content ||= ''
86     content.gsub!(/^(\\+)(---.*?)$/) {|m| "\\" * ($1.length / 2) + $2}
87     content.chomp! unless index == scanned.length
88 
89     blocks[name] = content
90     ((meta_info['blocks'] ||= {})[name] ||= {}).merge!(options) unless options.empty?
91   end
92   meta_info['blocks'].delete_if {|k,v| v.empty?} if meta_info.has_key?('blocks')
93   meta_info.delete('blocks') if meta_info.has_key?('blocks') && meta_info['blocks'].empty?
94   blocks
95 end
parse_meta_info(mi_data, has_mi_start) 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
44 def parse_meta_info(mi_data, has_mi_start)
45   if mi_data.nil? && has_mi_start
46     raise FormatError, 'Found start line for meta information block but no valid meta information block'
47   elsif mi_data.nil?
48     {}
49   else
50     begin
51       meta_info = YAML::load(mi_data.to_s)
52       unless meta_info.kind_of?(Hash)
53         raise FormatError, "Invalid structure of meta information block: expected YAML hash but found #{meta_info.class}"
54       end
55     rescue ArgumentError, SyntaxError, YAML::SyntaxError => e
56       raise FormatError, "Invalid YAML syntax in meta information block: #{e.message}"
57     end
58     meta_info
59   end
60 end

Public Instance Methods

to_s() click to toggle source

Convert the Page object back into a string.

    # File lib/webgen/page.rb
115 def to_s
116   str = ""
117   str << @meta_info.to_yaml
118   blocks.each do |name, value|
119     str << "--- #{name}\n" << value.gsub(/^---.*?$/) {|m| "\\#{m}" } << (value =~ /\n$/ ? "" : "\n")
120   end
121   str
122 end