Class: Pakman::Page

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

Overview

Jekyll-style page

with optional front-matter (yaml block)

Constant Summary collapse

HEADERS_PATTERN =

check if s includes newline too? fix/check ^ - just means start of newline (use /A or something — MUST always be first

note: include --- in headers
  e.g. ---  results in nil
       empty string (without leading ---) results in false! (we want nil if no headers for empty block)
/
 ^(?<headers>---\s*\n
    .*?)
 ^(---\s*$\n?)
/xm

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, opts = {}) ⇒ Page

Returns a new instance of Page.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pakman/page.rb', line 39

def initialize( text, opts={} )
  ## todo/fix: check regex in jekyll (add link to source etc.)
  if m=HEADERS_PATTERN.match( text )
    @contents  = m.post_match
    pp m
    pp m[:headers]
    @headers  = YAML.load( m[:headers] )
    pp @headers
    @headers = {}  if @headers.nil?  ##  check if headers is nil use/assign empty hash
  else
    @contents = text
    @headers  = nil
  end
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



21
22
23
# File 'lib/pakman/page.rb', line 21

def contents
  @contents
end

#headersObject (readonly)

Returns the value of attribute headers.



22
23
24
# File 'lib/pakman/page.rb', line 22

def headers
  @headers
end

Class Method Details

.from_file(path) ⇒ Object



11
12
13
14
15
# File 'lib/pakman/page.rb', line 11

def self.from_file( path )
  puts "  Loading page (from file) >#{path}<..."
  text = File.open( path, 'r:bom|utf-8' ).read     ## note: assume utf8
  self.new( text, path: path )   ## note: pass along path as an option
end

.from_string(text) ⇒ Object

use parse as alias - why?? why not??



17
18
19
# File 'lib/pakman/page.rb', line 17

def self.from_string( text )  ### use parse as alias - why?? why not??
  self.new( text )
end

Instance Method Details

#headers?Boolean

has headers/metadata (front matter block) - yes/no - use hash for check for now

Returns:

  • (Boolean)


25
# File 'lib/pakman/page.rb', line 25

def headers?()  @headers.kind_of?( Hash ); end