Class: General::GIO

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

Overview

Created: 3 - 4 - 2016

Constant Summary collapse

EXTENSION =

The general file extension

".general"

Instance Attribute Summary collapse

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(string, source = nil) ⇒ GIO

Creates a GIO with the given template string and source filename

Parameter: string - the string being converted to a template Parameter: source - the name of the source file of the template



127
128
129
130
# File 'lib/gtemplates/gio.rb', line 127

def initialize string, source=nil
	super string
	@source = source
end

Instance Attribute Details

#sourceObject (readonly)

Public read source



39
40
41
# File 'lib/gtemplates/gio.rb', line 39

def source
  @source
end

Class Method Details

.load(path) ⇒ Object

Loads a GIO from a file with the given path

Parameter: path - the path of the file to load

Return: GIO loaded from the file



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gtemplates/gio.rb', line 46

def self.load path
	# Get current working directory
	cwd = Dir.getwd

	# Change to path of file
	Dir.chdir File.dirname(path)

	# Read raw text
	string = IO.read File.basename(path)

	# Prepartial types
	ptypes = [
		General::GExtend,
		General::GInclude,
		General::GYield,
		General::GPretext
	]

	# Breakdown algorithm
	preparts = []
	m = nil
	while string.length > 0
		# 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?
			Dir.chdir cwd # Make sure to change back to current directory
			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

	# 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
		begin
			preparts = GMeta.load(preparts[extindex].filename+General::GIO::EXTENSION).gextend(preparts[1..-1])
		rescue GError => e
			Dir.chdir cwd # Make sure to change back to current directory
			raise e
		end
	elsif !extindex.nil?
		Dir.chdir cwd # Make sure to change back to current directory
		raise GError.new "@@extend prepartial needs to be at beginning of template."
	end

	# Find a yield
	yindex = preparts.index{ |prepart| prepart.is_a? General::GYield }

	# Raise error if yield is found
	unless yindex.nil?
		Dir.chdir cwd # Make sure to change back to current directory
		raise GError.new "#{path} is a meta template and cannot be parsed. Must be extended by other GIO template"
	end

	# Combine text
	text = preparts.collect{ |prepart| prepart.apply }.join

	# Change to current directory
	Dir.chdir cwd

	# Return new GIO
	begin
		return self.new text, path
	rescue GError => e
		raise GError.new "Error when parsing template file #{path}"
	end
end

Instance Method Details

#write(ios, data = {}) ⇒ Object

Writes the template with the given data applied to the target stream

Parameter: ios - if String, is the name of the file to write to if IO, is the stream to write to Parameter: data - the data to be applied (merges with defaults)



137
138
139
140
141
142
143
144
145
# File 'lib/gtemplates/gio.rb', line 137

def write ios, data={}
	if ios.is_a? String
		IO.write ios, apply(data)
	elsif ios.is_a? IO
		ios.write apply(data)
	else
		raise TypeError.new "Expected IO or String, got: #{ios.class}"
	end
end