Class: Golf::Compiler
- Inherits:
-
Object
- Object
- Golf::Compiler
- Defined in:
- lib/golf/compiler.rb
Instance Attribute Summary collapse
-
#golfpath ⇒ Object
Returns the value of attribute golfpath.
Class Method Summary collapse
Instance Method Summary collapse
-
#compile_res(dir) ⇒ Object
compile_res: This function creates a hash representation of the file structure relative to <dir>.
- #component_json ⇒ Object
- #extract_parts(data, path) ⇒ Object
- #filter_by_block(data) ⇒ Object
- #filter_by_extension(data, path) ⇒ Object
- #filtered_read(path) ⇒ Object
- #generate_componentsjs ⇒ Object
-
#initialize(golfpath = '.') ⇒ Compiler
constructor
A new instance of Compiler.
- #package_name(path) ⇒ Object
- #path_valid_for_filtering?(path) ⇒ Boolean
- #plugin_json ⇒ Object
- #res_json ⇒ Object
- #script_json ⇒ Object
- #style_json ⇒ Object
- #traverse(dir, type) ⇒ Object
- #traverse_components ⇒ Object
Constructor Details
#initialize(golfpath = '.') ⇒ Compiler
Returns a new instance of Compiler.
19 20 21 22 23 24 25 26 27 |
# File 'lib/golf/compiler.rb', line 19 def initialize(golfpath = '.') self.golfpath = "#{golfpath}/golfapp" puts "golf #{Golf::VERSION}: starting compiler in #{@golfpath}..." puts "golf #{Golf::VERSION}: is valid golfapp?: #{Golf::Compiler.valid?(@golfpath)}" puts "golf #{Golf::VERSION}: loading filters in #{golfpath}/filters" Dir["#{golfpath}/filters/*.rb"].each do |path| require path end end |
Instance Attribute Details
#golfpath ⇒ Object
Returns the value of attribute golfpath.
17 18 19 |
# File 'lib/golf/compiler.rb', line 17 def golfpath @golfpath end |
Class Method Details
.valid?(dir) ⇒ Boolean
29 30 31 |
# File 'lib/golf/compiler.rb', line 29 def self.valid?(dir) File.exists?("#{dir}/golfapp/components") end |
Instance Method Details
#compile_res(dir) ⇒ Object
compile_res: This function creates a hash representation of the file structure relative to <dir>. The values are either hashes (for directories) or strings (for files). The string values are the path to the file relative to @golfpath.
Example: compile_res(“#@golfpath/dir1”)
@golfpath/
|--dir1/
| |--file1
| |--file2
| +--dir2/
| |--file3
| |--file4
| +--dir3/
| |--file5
| +--file6
+--dir4/
{
file1 => "dir1/file1",
file2 => "dir1/file2",
dir2 => {
file3 => "dir1/dir2/file3",
file4 => "dir1/dir2/file4"
dir3 => {
file5 => "dir1/dir2/dir3/file5",
file6 => "dir1/dir2/dir3/file6"
}
}
}
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 |
# File 'lib/golf/compiler.rb', line 78 def compile_res(dir) results = {} mypath = dir.split('').last == "/" ? dir : dir+"/" myroot = @golfpath.split('').last == "/" ? @golfpath : @golfpath+"/" if File.exists?(mypath) Find.find(mypath) do |path| e = path.slice(mypath.length, path.length-mypath.length) r = path.slice(myroot.length, path.length-myroot.length) f = URI.escape(e) g = File.basename(e) h = File.dirname(r) == "." ? [] : File.dirname(r).split("/") if FileTest.directory?(path) next else r2 = results h.each { |i| if ! r2[i] r2[i] = {} end r2 = r2[i] } r2[g] = f end end end results end |
#component_json ⇒ Object
38 39 40 |
# File 'lib/golf/compiler.rb', line 38 def component_json traverse_components end |
#extract_parts(data, path) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/golf/compiler.rb', line 145 def extract_parts(data, path) component_name = path.split('/').last component_dir = path.gsub(component_name, '') #load from file doc = Hpricot(data) arr = {} css = (doc/'//style').first if css arr["css"] = css.inner_html else arr["css"] = "" end js = (doc/'//script').first if js arr["js"] = js.inner_html else arr["js"] = "" end (doc/'//style').remove (doc/'//script').remove arr["html"] = doc.to_s #load from files, ".js.coffee", etc Dir["#{component_dir}/*"].each do |file_path| next if file_path.include?('~') valid_arr = path_valid_for_filtering?(file_path) if valid_arr filter_name = valid_arr[1] output_type = valid_arr[0] arr[output_type] = filtered_read(file_path) else extension = file_path.split('/').last.split('.').last arr[extension] = File.read(file_path) unless extension == "html" end end arr end |
#filter_by_block(data) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/golf/compiler.rb', line 197 def filter_by_block(data) doc = Hpricot(data) if doc unfiltered_elements = doc.search('//*[@filter]') if unfiltered_elements.count == 0 data else unfiltered_elements.each do |element| filter = element.attributes["filter"] filter_name = filter.capitalize.to_sym if Golf::Filter.constants.include?(filter_name) element.remove_attribute('filter') res = Golf::Filter.const_get(filter_name).transform(element.to_s) element.swap(res) end end return doc.to_s end else data end end |
#filter_by_extension(data, path) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/golf/compiler.rb', line 230 def filter_by_extension(data, path) valid_arr = path_valid_for_filtering?(path) if valid_arr filter_name = valid_arr[1].capitalize.to_sym if Golf::Filter.constants.include?(filter_name) Golf::Filter.const_get(filter_name).transform(data) else data end else data end end |
#filtered_read(path) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/golf/compiler.rb', line 188 def filtered_read(path) data = File.read(path) if path.split('.').last == 'html' data = filter_by_block(data) end data = filter_by_extension(data, path) data end |
#generate_componentsjs ⇒ Object
34 35 36 |
# File 'lib/golf/compiler.rb', line 34 def generate_componentsjs "jQuery.golf.components=#{component_json};;jQuery.golf.res=#{res_json};;jQuery.golf.plugins=#{plugin_json};;jQuery.golf.scripts=#{script_json};;jQuery.golf.styles=#{style_json};;jQuery.golf.setupComponents();" end |
#package_name(path) ⇒ Object
244 245 246 247 248 249 250 |
# File 'lib/golf/compiler.rb', line 244 def package_name(path) if path.include?('golfapp/components') path.match(/golfapp\/components\/(.*)/) component_path = $1 component_path.split('/')[0...-1].join('.') end end |
#path_valid_for_filtering?(path) ⇒ Boolean
220 221 222 223 224 225 226 227 228 |
# File 'lib/golf/compiler.rb', line 220 def path_valid_for_filtering?(path) path_arr = path.split('/').last.split('.') if path_arr.count > 2 last_two = path_arr[path_arr.length - 2..path_arr.length] if last_two[0] == "js" or last_two[0] == "css" or last_two[0] == "html" last_two end end end |
#plugin_json ⇒ Object
106 107 108 |
# File 'lib/golf/compiler.rb', line 106 def plugin_json traverse("#{@golfpath}/plugins", "js") end |
#res_json ⇒ Object
42 43 44 |
# File 'lib/golf/compiler.rb', line 42 def res_json JSON.dump(compile_res(@golfpath)) end |
#script_json ⇒ Object
110 111 112 |
# File 'lib/golf/compiler.rb', line 110 def script_json traverse("#{@golfpath}/scripts", "js") end |
#style_json ⇒ Object
114 115 116 |
# File 'lib/golf/compiler.rb', line 114 def style_json traverse("#{@golfpath}/styles", "css") end |
#traverse(dir, type) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/golf/compiler.rb', line 132 def traverse(dir, type) results = {} if File.exists?(dir) and File.directory?(dir) Dir["#{dir}/**/*"].sort.reverse.each do |path| next if path.include?('~') or !path.include?(".#{type}") name = path.split('/').last.gsub(".#{type}",'') data = filtered_read(path) results = results.merge({ name => { "name" => name, "#{type}" => data }}) end end JSON.dump(results) end |
#traverse_components ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/golf/compiler.rb', line 118 def traverse_components results = {} Dir["#{@golfpath}/components/**/*"].each do |path| name = package_name(path) valid_arr = path_valid_for_filtering?(path) next if FileTest.directory?(path) or !path.include?('.html') or path.include?('~') data = filtered_read(path) data_arr = extract_parts(data, path) results = results.merge({ name => { "name" => name, "html" => data_arr["html"], "css" => data_arr["css"], "js" => data_arr["js"] }}) end JSON.dump(results) end |