Class: N::RubyShader

Inherits:
Shader show all
Defined in:
lib/nitro/shaders.rb

Overview

Convert the xhtml script to actual Ruby code, ready to be evaluated.

Instance Attribute Summary

Attributes inherited from Shader

#next_stage

Instance Method Summary collapse

Methods inherited from Shader

#<<, #initialize, #process_next

Constructor Details

This class inherits a constructor from N::Shader

Instance Method Details

#load_statically_included(filename) ⇒ Object

Loads and statically includes a file.



140
141
142
143
144
145
146
147
148
# File 'lib/nitro/shaders.rb', line 140

def load_statically_included(filename)
	Logger.debug "Statically including '#{filename}'" if $DBG
	
	text = File.read(filename)
	text.gsub!(/<\?xml.*\?>/, '')
	text.gsub!(/<\/?root(.*?)>/m, ' ');
	
	return text
end

#process(hash, text) ⇒ Object

Convert the xhtml script to actual Ruby code, ready to be evaluated.

– Investigate: perhaps xl:href should be used to be XLink compatible? ++



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/nitro/shaders.rb', line 68

def process(hash, text)
	# strip the xml header! (interracts with the following gsub!)
	text.gsub!(/<\?xml.*\?>/, "")

	# Statically include sub script files.
	# The target file is included at compile time.
	#
	# gmosx: must be xformed before the <?r pi.
	#
	# Example:		
	# <?include xl:href="root/myfile.sx" ?>
	
	text.gsub!(/<\?include href="(.*?)"(.*)\?>/) { |match|
		# gmosx: xmm match matches the whole string.
		# match = overload_path($1)				
		load_statically_included("#$root_dir/#$1")
	}

	# xform include instructions <include href="xxx" />
	# must be transformed before the processinc instructions.
	# Useful to include fragments cached on disk
  #
	# gmosx, FIXME: NOT TESTED! test and add caching.
	# add load_statically_included fixes.

	text.gsub!(/<include href="(.*?)"(.*)(.?)\/>/) { |match|
		"<?r File.read( '\#\{@dispatcher.root\}/#$1' ?>"
	}
	
	# xform render/inject instructions <render href="xxx" />
	# must be transformed before the processinc instructions.

	text.gsub!(/<inject href="(.*?)"(.*)(.?)\/>/) { |match|
		"<?r render '/#$1' ?>"
	}
	
	text.gsub!(/<render href="(.*?)"(.*)(.?)\/>/) { |match|
		"<?r render '/#$1' ?>"
	}
	
	# remove <root> elements. typically removed by xslt but lets
	# play it safe.
	
	text.gsub!(/<(\/)?root>/, '')
	
	# runtime ruby code.
	
	# xform the processing instructions, use <?r as
	# a marker.
	
	text.gsub!(/\?>/, "\n@out << %^")
	text.gsub!(/<\?r(\s?)/, "^\n")
	
	# xform alternative code tags (very useful in xsl stylesheets)
	
	text.gsub!(/<\/ruby>/, "\n@out << %^")
	text.gsub!(/<ruby>/, "^\n")
	
	# compile time ruby code.
	# Usefull for example to prevaluate localization.
	
	text.gsub!(/\#\[(.*?)\]/) { |match|
		eval($1)
	}
	
	text =  "@out << %^" + text + "^"

	process_next(hash, text)
end