| Class | WebPageData |
| In: |
lib/webgen/content.rb
|
| Parent: | Object |
A WebPageData object contains the parsed data of a file/string in the WebPage Description Format.
| blocks | [R] | The content blocks. Access via index or name. |
| meta_info | [R] | The contents of the meta information block. |
Parses the given String data and initializes a new WebPageData object with the found values. The blocks are converted to HTML by using the provided formatters hash. A key in this hash has to be a format name and the value and object which responds to the +call(content)+ method. You can set default_meta_info to provide default entries for the meta information block.
# File lib/webgen/content.rb, line 130
130: def initialize( data, formatters = {'default' => proc {|c| c} }, default_meta_info = {} )
131: @meta_info = default_meta_info
132: @formatters = formatters
133: parse( data )
134: end
# File lib/webgen/content.rb, line 167
167: def convert( content, format )
168: raise( WebPageDataInvalid, "Invalid content format specified: #{format}" ) unless @formatters.has_key?( format )
169: @formatters[format].call( content )
170: end
# File lib/webgen/content.rb, line 140
140: def parse( data )
141: @blocks = {}
142: blocks = data.gsub(/\r\n?/, "\n").scan( /(?:(?:^--- *(?:(\w+) *(?:, *(\w+) *)?)?$)|\A)(.*?)(?:(?=^---.*?$)|\Z)/m )
143: if data =~ /\A---\s*$/
144: begin
145: meta = YAML::load( blocks.shift[2] )
146: raise( WebPageDataInvalid, 'Invalid structure of meta information part') unless meta.kind_of?( Hash )
147: @meta_info.update( meta )
148: rescue ArgumentError => e
149: raise WebPageDataInvalid, e.message
150: end
151: end
152:
153: raise( WebPageDataInvalid, 'No content blocks specified' ) if blocks.length == 0
154:
155: blocks.each_with_index do |block_data, index|
156: name, format, content = *block_data
157: name = name || (@meta_info['blocks'] && @meta_info['blocks'][index] && @meta_info['blocks'][index][0]) || 'content'
158: raise( WebPageDataInvalid, "Same name used for more than one block: #{name}" ) if @blocks.has_key?( name )
159: content ||= ''
160: content.gsub!( /^(\\+)(---.*?)$/ ) {|m| "\\" * ($1.length / 2) + $2 }
161: content.strip!
162: format = format || (@meta_info['blocks'] && @meta_info['blocks'][index] && @meta_info['blocks'][index][1]) || 'default'
163: @blocks[name] = @blocks[index] = HtmlBlock.new( name, convert( content, format ) )
164: end
165: end