Module: Lightr

Defined in:
lib/lightr/mixins.rb,
lib/lightr/version.rb,
lib/lightr/grammars.rb,
lib/lightr/grammars/ruby.rb,
lib/lightr/mixins/grammar.rb,
lib/lightr/grammars/javascript.rb

Defined Under Namespace

Modules: Grammar Classes: JavaScript, Ruby

Constant Summary collapse

VERSION =
'0.0.7'

Class Method Summary collapse

Class Method Details

.associate(grammar, *extensions) ⇒ Object

Associate grammar with extensions passed.



11
12
13
# File 'lib/lightr/grammars.rb', line 11

def self.associate grammar, *extensions
  (@associations ||= []) << [grammar, extensions]
end

.grammar_for(path) ⇒ Object

Return grammar symbol for the extension associated to path, or nil.



18
19
20
21
22
23
# File 'lib/lightr/grammars.rb', line 18

def self.grammar_for path
  @associations.each do |(grammar, extensions)|
    return grammar if extensions.include? File.extname(path)
  end
  nil
end

.parse_file(path, options = {}) ⇒ Object

Parse a path, auto-detecting the grammar, or using the grammar passed using the :as option.

Examples

Lightr.parse_file 'foo.rb'
Lightr.parse_file 'foo.js', :as => :JavaScript


35
36
37
38
# File 'lib/lightr/grammars.rb', line 35

def self.parse_file path, options = {}
  grammar = options.delete(:as) || grammar_for(path)
  eval(grammar.to_s).parse File.read(path)
end