Class: Webgen::Page
- Inherits:
-
Object
- Object
- Webgen::Page
- Defined in:
- lib/webgen/page.rb
Overview
A Page object wraps a meta information hash and a hash of name => block content associations.
It is normally generated from a file or string in Webgen Page Format using the provided class methods.
Defined Under Namespace
Classes: FormatError
Constant Summary collapse
- RE_NEWLINE =
:stopdoc:
/\r?\n/- RE_META_INFO_START =
/\A---[ \t]*#{RE_NEWLINE}/- RE_META_INFO =
/#{RE_META_INFO_START}.*?#{RE_NEWLINE}(?=---.*?#{RE_NEWLINE}|\Z)/m- RE_BLOCKS_START_SIMPLE =
/^---[ \t]*$|^---[ \t]+(\w+)[ \t]*(?:[ \t]+-+[ \t]*)?$|^$/- RE_BLOCKS_START_COMPLEX =
/^---[ \t]+?(?:[ \t]*((?:\w+:\S*[ \t]*)*))?(?:[ \t]+-+[ \t]*)?$/- RE_BLOCKS_START =
/^---(?:[ \t]+.*?|)(?=#{RE_NEWLINE})/- RE_BLOCKS =
/(?:(#{RE_BLOCKS_START})|\A)#{RE_NEWLINE}?(.*?)(?:(?=#{RE_BLOCKS_START})|\z)/m- RE_PAGE =
/(#{RE_META_INFO})?(.*)/m
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
The hash of blocks for the page.
-
#meta_info ⇒ Object
readonly
The contents of the meta information block.
Class Method Summary collapse
-
.from_data(data) ⇒ Object
Parse the given string
datain Webgen Page Format. -
.parse_blocks(data, meta_info) ⇒ Object
Parse all blocks in
dataand return them. -
.parse_meta_info(mi_data, has_mi_start) ⇒ Object
Parse the meta info string in
mi_dataand return the hash with the meta information.
Instance Method Summary collapse
-
#initialize(meta_info = {}, blocks = {}) ⇒ Page
constructor
Create a new Page object with the meta information provided in
meta_infoand the givenblocks. -
#to_s ⇒ Object
Convert the Page object back into a string.
Constructor Details
#initialize(meta_info = {}, blocks = {}) ⇒ Page
Create a new Page object with the meta information provided in meta_info and the given
blocks.
111 112 113 114 |
# File 'lib/webgen/page.rb', line 111 def initialize( = {}, blocks = {}) @meta_info = @blocks = blocks end |
Instance Attribute Details
#blocks ⇒ Object (readonly)
The hash of blocks for the page.
107 108 109 |
# File 'lib/webgen/page.rb', line 107 def blocks @blocks end |
#meta_info ⇒ Object (readonly)
The contents of the meta information block.
104 105 106 |
# File 'lib/webgen/page.rb', line 104 def @meta_info end |
Class Method Details
.from_data(data) ⇒ Object
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.
37 38 39 40 41 42 |
# File 'lib/webgen/page.rb', line 37 def from_data(data) md = RE_PAGE.match(data) = (md[1], data =~ RE_META_INFO_START) blocks = parse_blocks(md[2] || '', ) new(, blocks) end |
.parse_blocks(data, meta_info) ⇒ Object
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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/webgen/page.rb', line 69 def parse_blocks(data, ) scanned = data.scan(RE_BLOCKS) raise(FormatError, 'No content blocks specified') if scanned.length == 0 blocks = {} scanned.each_with_index do |block_data, index| index += 1 , content = *block_data if md = RE_BLOCKS_START_SIMPLE.match(.to_s) = {'name' => md[1]} else md = RE_BLOCKS_START_COMPLEX.match(.to_s) raise(FormatError, "Found invalid blocks starting line for block #{index}: #{}") if content =~ /\A---/ || md.nil? = Hash[*md[1].to_s.scan(/(\w+):([^\s]*)/).map {|k,v| [k, (v == '' ? nil : Utils.yaml_load(v))]}.flatten] end name = .delete('name') || (index == 1 ? 'content' : 'block' + (index).to_s) raise(FormatError, "Previously used name '#{name}' also used for block #{index}") if blocks.has_key?(name) content ||= '' content.gsub!(/^(\\+)(---.*?)$/) {|m| "\\" * ($1.length / 2) + $2} content.chomp! unless index == scanned.length blocks[name] = content ((['blocks'] ||= {})[name] ||= {}).merge!() unless .empty? end ['blocks'].delete_if {|k,v| v.empty?} if .has_key?('blocks') .delete('blocks') if .has_key?('blocks') && ['blocks'].empty? blocks end |
.parse_meta_info(mi_data, has_mi_start) ⇒ Object
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/webgen/page.rb', line 46 def (mi_data, has_mi_start) if mi_data.nil? && has_mi_start raise FormatError, 'Found start line for meta information block but no valid meta information block' elsif mi_data.nil? {} else begin = Utils.yaml_load(mi_data.to_s) unless .kind_of?(Hash) raise FormatError, "Invalid structure of meta information block: expected YAML hash but found #{.class}" end rescue ArgumentError, SyntaxError, YAML::SyntaxError => e raise FormatError, "Invalid YAML syntax in meta information block: #{e.}" end end end |
Instance Method Details
#to_s ⇒ Object
Convert the Page object back into a string.
117 118 119 120 121 122 123 124 |
# File 'lib/webgen/page.rb', line 117 def to_s str = "" str << @meta_info.to_yaml blocks.each do |name, value| str << "--- #{name}\n" << value.gsub(/^---.*?$/) {|m| "\\#{m}" } << (value =~ /\n$/ ? "" : "\n") end str end |