Object
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.
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
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 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 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.
Generated with the Darkfish Rdoc Generator 1.1.6.