Class: Webgen::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/webgen/page.rb

Overview

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.

Defined Under Namespace

Classes: Block, FormatError

Constant Summary collapse

RE_META_INFO_START =

:stopdoc:

/\A---\s*(?:\n|\r|\r\n)/m
RE_META_INFO =
/\A---\s*(?:\n|\r|\r\n).*?(?:\n|\r|\r\n)(?=---.*?(?:\n|\r|\r\n)|\Z)/m
RE_BLOCKS_OPTIONS =
/^--- *?(?: *((?:\w+:[^\s]* *)*))?$|^$/
RE_BLOCKS_START =
/^--- .*?$|^--- *$/
RE_BLOCKS =
/(?:(#{RE_BLOCKS_START})|\A)(.*?)(?:(?=#{RE_BLOCKS_START})|\Z)/m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta_info = {}, blocks = {}) ⇒ Page

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



147
148
149
150
# File 'lib/webgen/page.rb', line 147

def initialize(meta_info = {}, blocks = {})
  @meta_info = meta_info
  @blocks = blocks
end

Instance Attribute Details

#blocksObject (readonly)

The hash of blocks for the page.



143
144
145
# File 'lib/webgen/page.rb', line 143

def blocks
  @blocks
end

#meta_infoObject (readonly)

The contents of the meta information block.



140
141
142
# File 'lib/webgen/page.rb', line 140

def meta_info
  @meta_info
end

Class Method Details

.from_data(data, meta_info = {}) ⇒ Object

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.



67
68
69
70
71
72
# File 'lib/webgen/page.rb', line 67

def from_data(data, meta_info = {})
  md = /(#{RE_META_INFO})?(.*)/m.match(normalize_eol(data))
  meta_info = meta_info.merge(parse_meta_info(md[1], data))
  blocks = parse_blocks(md[2] || '', meta_info)
  new(meta_info, blocks)
end

.meta_info_from_data(data) ⇒ Object

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



75
76
77
78
# File 'lib/webgen/page.rb', line 75

def meta_info_from_data(data)
  md = /(#{RE_META_INFO})?/m.match(normalize_eol(data))
  parse_meta_info(md[1], data)
end