Class: General::GMeta

Inherits:
GTemplate show all
Defined in:
lib/gtemplates/gmeta.rb

Overview

Created: 1 - 31 - 2016

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GTemplate

#match, #regex

Methods inherited from GBaseTemplate

#apply, #apply_all, #to_s

Constructor Details

#initialize(top, bottom, source = nil) ⇒ GMeta

Creates a new GMeta

Parameter: top - the top end of the meta (in preparts) Parameter: bottom - the bottom end of the data (in preparts) Parameter: source - the source of the file



95
96
97
98
99
# File 'lib/gtemplates/gmeta.rb', line 95

def initialize top, bottom, source=nil
  @top    = top
  @bottom = bottom
  @source = source
end

Class Method Details

.load(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gtemplates/gmeta.rb', line 29

def self.load path
  # Read file

  string = IO.read path

  # Prepartial types

  ptypes = [
    General::GExtend,
    General::GInclude,
    General::GYield,
    General::GPretext
  ]

  # Breakdown algorithm

  preparts = []
  m = nil
  loop do
    # Match the front of the string to a preprocessor

    prepart = ptypes.find { |ptype| m = ptype::REGEX.match(string) }

    # Raise error if no matching prepart can be found

    if prepart.nil?
      raise GError.new "Unmatched prepartial at #{(string.length <= 5 ? string : string[0..5] + "...").inspect}"
    end

    # Add the partial to the array

    preparts << prepart.new(m)

    # Trim the front of the string

    string = string[m.end(0)..-1]

    # End when string is empty

    break if string.length == 0
  end

  # Find an extend

  extindex = preparts.index { |prepart| prepart.is_a? General::GExtend }

  # Run extend algorithm (throw error if extend is found elsewhere)

  if extindex == 0
    preparts = GMeta.load(preparts[extindex].filename+General::GIO::EXTENSION).gextend(preparts[1..-1])
  elsif !extindex.nil? && extindex > 0
    raise GError.new "@@extend prepartial needs to be at beginning of template."
  end

  # Check for multiple yields

  yields = preparts.find_all{ |prepart| prepart.is_a? General::GYield  }
  if yields.length > 1
    raise GError.new "@@yield prepartial can only appear ONCE!"
  end

  # Find the yield

  yindex = preparts.index{ |prepart| prepart.is_a? General::GYield }

  # Return a new meta file if yield is found. Else default is end.

  unless yindex.nil?
    return self.new preparts[0...yindex], preparts[(yindex + 1)..-1], path
  else
    return self.new preparts, [], path
  end
end

Instance Method Details

#gextend(preparts) ⇒ Object

Extends the given preparts from the GMeta

Parameter: preparts - the preparts array to extend

Returns: the extended preparts



106
107
108
# File 'lib/gtemplates/gmeta.rb', line 106

def gextend preparts
  return @top + preparts + [General::GPretext.new("\r\n")] + @bottom
end