Class: ParseSSI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web_root) ⇒ ParseSSI

Returns a new instance of ParseSSI.



5
6
7
8
9
# File 'lib/parse_ssi.rb', line 5

def initialize web_root
	@vars = {}
	@web_root = web_root
	return
end

Instance Attribute Details

#bottomObject (readonly)

Returns the value of attribute bottom.



3
4
5
# File 'lib/parse_ssi.rb', line 3

def bottom
  @bottom
end

#topObject (readonly)

Returns the value of attribute top.



3
4
5
# File 'lib/parse_ssi.rb', line 3

def top
  @top
end

#varsObject (readonly)

Returns the value of attribute vars.



3
4
5
# File 'lib/parse_ssi.rb', line 3

def vars
  @vars
end

#web_rootObject (readonly)

Returns the value of attribute web_root.



3
4
5
# File 'lib/parse_ssi.rb', line 3

def web_root
  @web_root
end

Instance Method Details

#echo_tag(var) ⇒ Object



69
70
71
# File 'lib/parse_ssi.rb', line 69

def echo_tag var
	[ '<!--#echo var="' + var + '" -->', "\n" ].join
end

#file_include_tag(path) ⇒ Object



65
66
67
# File 'lib/parse_ssi.rb', line 65

def file_include_tag path
	[ '<!--#include virtual="' + @web_root + path.to_s + '" -->', "\n" ].join
end

#parse(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/parse_ssi.rb', line 11

def parse file

	# the parse routine currently understands only 3 SSI attributes
	#
	# <!--#echo var="FOO" -->
	#
	# <!--#include virtual="/path/to/file" -->
	#
	# <!--#set var="FOO" value="bar" -->
	#
	# more may be added if required.

	result = File.read( @web_root + "/" + file )

	1 while result.sub!(/<!--#(\w+)\s(.*?)\s*-->(\n+)?/) do |match|
		attrs = parse_attributes($2)
		case $1
		when 'echo'
			@vars[ attrs['var'] ]
		when 'include'
			attrs['virtual'].gsub!(/\$\{(\w+)\}/) do |m|
				if $1
					@vars[$1] 
				else
					m
				end
			end
			File.read( @web_root + "/" + attrs['virtual'] )
		when 'set'
			@vars[ attrs['var'] ] = attrs['value']
			''
		else
			match
		end
	end
	result
end

#parse_attributes(source) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/parse_ssi.rb', line 49

def parse_attributes(source)
	attrs = Hash.new 
	source.scan( /(\w+)="([^"]*)"/ ) do |match|
		attrs[$1] = $2
	end
	return attrs
end

#set_tag(var, value) ⇒ Object



57
58
59
# File 'lib/parse_ssi.rb', line 57

def set_tag var, value
	[ '<!--#set var="' + var + '" value="', value.to_s, '" -->', "\n" ].join
end

#virtual_include_tag(path) ⇒ Object



61
62
63
# File 'lib/parse_ssi.rb', line 61

def virtual_include_tag path
	[ '<!--#include virtual="' + path.to_s + '" -->', "\n" ].join
end