Class: Atomizr
- Inherits:
-
Object
- Object
- Atomizr
- Defined in:
- lib/atomizr.rb
Class Method Summary collapse
- .add_trailing_tabstop(input) ⇒ Object
- .copy_file(src, dest, del = false) ⇒ Object
- .delete_file(input) ⇒ Object
- .filter_str(input, filter) ⇒ Object
-
.fix_scope(scope) ⇒ Object
prepend dot to scope.
- .get_outname(type, item) ⇒ Object
- .get_scope(scope) ⇒ Object
- .info ⇒ Object
- .init_hashes ⇒ Object
- .mkdir(folder) ⇒ Object
- .read_file(input, type) ⇒ Object
- .read_json(item) ⇒ Object
- .read_sublime_xml(item, data) ⇒ Object
- .read_textmate_xml(item, data) ⇒ Object
- .read_xml(item) ⇒ Object
- .valid_json?(json) ⇒ Boolean
- .valid_xml?(xml) ⇒ Boolean
- .version ⇒ Object
- .write_cson(data, item, many = false) ⇒ Object
- .write_data(item) ⇒ Object
- .write_json(data, file, many = false) ⇒ Object
Class Method Details
.add_trailing_tabstop(input) ⇒ Object
412 413 414 415 416 417 418 419 |
# File 'lib/atomizr.rb', line 412 def self.add_trailing_tabstop(input) unless input.match(/\$\d+$/) == nil # nothing to do here return input end return "#{input}$0" end |
.copy_file(src, dest, del = false) ⇒ Object
452 453 454 455 456 457 458 |
# File 'lib/atomizr.rb', line 452 def self.copy_file(src, dest, del = false) File.write(dest, File.read(src)) if del == true File.delete(src) end end |
.delete_file(input) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/atomizr.rb', line 369 def self.delete_file(input) if File.directory?(input) puts "\nDeleting '#{input[0..-2]}'" unless $silent == true FileUtils.rm_rf(input) else Dir.glob(input) do |item| puts "Deleting '#{item}'" unless $silent == true File.delete(item) end end end |
.filter_str(input, filter) ⇒ Object
430 431 432 433 434 435 436 437 438 439 |
# File 'lib/atomizr.rb', line 430 def self.filter_str(input, filter) if filter.any? filter.each do |needle, replacement| input = input.to_s.gsub(needle, replacement) end end return input end |
.fix_scope(scope) ⇒ Object
prepend dot to scope
422 423 424 425 426 427 428 |
# File 'lib/atomizr.rb', line 422 def self.fix_scope(scope) if scope[0] != "." scope = "."+ scope end return scope end |
.get_outname(type, item) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/atomizr.rb', line 383 def self.get_outname(type, item) if $output == type if $input[0].include?("*") file = item else file = $input[0] end output = File.basename(file, ".*")+"."+type else output = $output end return output end |
.get_scope(scope) ⇒ Object
399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/atomizr.rb', line 399 def self.get_scope(scope) if $scope == nil scope = fix_scope(filter_str(scope, @scope_filter)) puts "Using default scope '"+scope+"'" unless $silent == true else scope = fix_scope(filter_str($scope, @scope_filter)) puts "Override scope using '"+scope+"'" unless $silent == true end return scope end |
.info ⇒ Object
42 43 44 |
# File 'lib/atomizr.rb', line 42 def self.info puts "\n#{@name}, version #{@version}\nThe MIT License\nCopyright (c) 2015, 2016 #{@author.join(", ")}" end |
.init_hashes ⇒ Object
441 442 443 444 |
# File 'lib/atomizr.rb', line 441 def self.init_hashes() @data = Hash.new @completions = Hash.new end |
.mkdir(folder) ⇒ Object
446 447 448 449 450 |
# File 'lib/atomizr.rb', line 446 def self.mkdir(folder) if !Dir.exists?(folder) FileUtils.mkdir_p(folder) end end |
.read_file(input, type) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/atomizr.rb', line 50 def self.read_file(input, type) if $merge == true init_hashes() end Dir.glob(input) do |item| if $merge == false init_hashes() end @data = send("read_#{type}", item) if $merge == false write_data(item) end end if $merge == true write_data($output) end end |
.read_json(item) ⇒ Object
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/atomizr.rb', line 162 def self.read_json(item) puts "\nReading completion file '#{item}'" unless $silent == true # read file file = File.read(item) # validate file if (valid_json?(file) == false) && $no_validation == false abort("\nError: Invalid JSON file '#{item}'") unless $silent == true else data = JSON.load(file) end # get scope @data['scope'] = get_scope( data["scope"] ) data["completions"].each do |line| trigger = line["trigger"] # Next if JSON contains non-standard keys if trigger == nil puts " >> Ignoring line #{line}" unless $silent == true next end contents = line["contents"] title = filter_str(trigger, @title_filter) prefix = filter_str(trigger, @prefix_filter) body = filter_str(contents, @body_filter) if @completions.has_key?(title) if $dupes == false puts " !! Duplicate trigger #{title.dump} in #{item}" unless $silent == true else abort("\nError: duplicate trigger '#{title.dump}' in #{item}. Triggers must be unique.") unless $silent == true end end @completions[title] = { :prefix => prefix, :body => body } end @data['completions'] = @completions $input_counter += 1 return @data end |
.read_sublime_xml(item, data) ⇒ Object
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 |
# File 'lib/atomizr.rb', line 99 def self.read_sublime_xml(item, data) # get scope @data['scope'] = get_scope( data.xpath("//scope")[0].text.strip ) trigger = data.xpath("//tabTrigger")[0].text.strip if data.xpath("//description").empty? description = trigger else description = data.xpath("//description")[0].text.strip end data.xpath("//content").each do |node| title = filter_str(trigger, @title_filter) prefix = filter_str(trigger, @prefix_filter) body = filter_str(node.text.strip, @body_filter) if @completions.has_key?(title) if $dupes == false puts " !! Duplicate trigger #{title.dump} in #{item}" unless $silent == true else abort("\nError: duplicate trigger '#{title.dump}' in #{item}. Triggers must be unique.") end end @completions[description] = { :prefix => prefix, :body => body } end return @completions end |
.read_textmate_xml(item, data) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/atomizr.rb', line 135 def self.read_textmate_xml(item, data) data.xpath('//dict').each do | node | node.element_children.map(&:content).each_slice(2) do | k, v | case k when 'scope' @data['scope'] = get_scope(v.to_s) when 'name' @title = filter_str(v.to_s, @title_filter) when 'tabTrigger' @prefix = filter_str(v.to_s, @prefix_filter) when 'content' @body = filter_str(v.to_s.strip, @body_filter) else next end end end @completions[@title] = { :prefix => @prefix, :body => @body } return @completions end |
.read_xml(item) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/atomizr.rb', line 74 def self.read_xml(item) puts "\nReading snippet file '#{item}'" unless $silent == true # read file, parse data file = File.read(item) # validate file if (valid_xml?(file) == false) && ($no_validation == false) abort("\nError: Invalid XML file '#{item}'") unless $silent == true end data = Nokogiri::XML(file) if (item.end_with? ".tmSnippet") || ($is_tm == true) @data['completions'] = read_textmate_xml(item, data) else @data['completions'] = read_sublime_xml(item, data) end $input_counter += 1 return @data end |
.valid_json?(json) ⇒ Boolean
215 216 217 218 219 220 |
# File 'lib/atomizr.rb', line 215 def self.valid_json?(json) JSON.parse(json) true rescue false end |
.valid_xml?(xml) ⇒ Boolean
222 223 224 225 226 227 |
# File 'lib/atomizr.rb', line 222 def self.valid_xml?(xml) Nokogiri::XML(xml) { |config| config. = Nokogiri::XML::ParseOptions::STRICT } true rescue false end |
.version ⇒ Object
46 47 48 |
# File 'lib/atomizr.rb', line 46 def self.version puts "#{@version}" end |
.write_cson(data, item, many = false) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/atomizr.rb', line 286 def self.write_cson(data, item, many = false) if many == false if $no_comment == true comment = "" else comment = "# Generated with #{@name} - #{@homepage}\n" end cson = comment cson += "'"+data['scope']+"':\n" data['completions'].each do |item| title = item[0] prefix = item[1][:prefix] body = item[1][:body] if $no_tabstops == false body = add_trailing_tabstop(body) end cson += " '"+title+"':\n" cson += " 'prefix': '"+prefix+"'\n" if body.lines.count <= 1 cson += " 'body': '"+body+"'\n" else cson += " 'body': \"\"\"\n" body.each_line do |line| cson += " "+line end cson +="\n \"\"\"\n" end end if File.directory?($input[0]) mkdir("#{$folder}/#{$output}/snippets") file = $output + '/snippets/' + item + '.cson' else file = get_outname('cson', item) end puts "Writing '#{file}'" unless $silent == true File.open("#{$folder}/#{file}","w") do |f| f.write(cson) end $output_counter += 1 elsif many == true scope = data['scope'] data['completions'].each do |item| cson = "'"+scope+"':\n" title = item[0] prefix = item[1][:prefix] body = item[1][:body] file = filter_str(prefix, @filename_filter) cson += " '"+title+"':\n" cson += " 'prefix': '"+prefix+"'\n" if body.lines.count <= 1 cson += " 'body': '"+body+"'\n" else cson += " 'body': \"\"\"\n" body.each_line do |line| cson += " "+line end cson +="\n \"\"\"\n" end puts "Writing '#{file}.cson'" unless $silent == true File.open("#{$folder}/#{file}.cson","w") do |f| f.write(cson) end $output_counter += 1 end end end |
.write_data(item) ⇒ Object
229 230 231 232 233 234 235 236 237 |
# File 'lib/atomizr.rb', line 229 def self.write_data(item) if $output == "json" file = get_outname('json', item) write_json(@data, file, $split) else file = get_outname('cson', item) write_cson(@data, file, $split) end end |
.write_json(data, file, many = false) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/atomizr.rb', line 239 def self.write_json(data, file, many = false) if many == false if $no_comment == true json = { data['scope'] => data['completions'] } else json = { :"#" => "# Generated with #{@name} - #{@homepage}", data['scope'] => data['completions'] } end puts "Writing '#{file}'" unless $silent == true File.open("#{$folder}/#{file}","w") do |f| f.write(JSON.pretty_generate(json)) end $output_counter += 1 elsif many == true scope = data['scope'] data['completions'].each do |item| file = filter_str(item[1][:prefix], @filename_filter) json = { data['scope'] => { item[0] => { 'prefix' => item[1][:prefix], 'body' => item[1][:body] } } } puts "Writing '#{file}.json'" unless $silent == true File.open("#{$folder}/#{file}.json","w") do |f| f.write(JSON.pretty_generate(json)) end $output_counter += 1 end end end |