Module: Malt

Extended by:
Kernel
Defined in:
lib/malt.rb,
lib/malt/kernel.rb,
lib/malt/engines.rb,
lib/malt/formats.rb,
lib/malt/meta/data.rb,
lib/malt/engines/abstract.rb,
lib/malt/formats/abstract.rb

Defined Under Namespace

Modules: Engines, Formats, Kernel

Constant Summary collapse

DIRECTORY =
File.dirname(__FILE__)

Class Method Summary collapse

Class Method Details

.const_missing(name) ⇒ Object



21
22
23
24
# File 'lib/malt/meta/data.rb', line 21

def self.const_missing(name)
  key = name.to_s.downcase
  gemfile[key] || profile[key] || super(name)
end

.file(file, options = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/malt.rb', line 25

def self.file(file, options={})
  type = options[:type] || options[:format] || File.extname(file)
  type = ext_to_type(type)
  malt_class = registry[type]
  raise "unkown type -- #{type}" unless malt_class
  malt_class.new(options.merge(:file=>file,:type=>type))
end

.gemfileObject



7
8
9
10
11
12
# File 'lib/malt/meta/data.rb', line 7

def self.gemfile
  @gemfile ||= (
    require 'yaml'
    YAML.load(File.new(DIRECTORY + '/gemfile'))
  )
end

.main(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/malt.rb', line 55

def self.main(*args)
  require 'optparse'
  itype, otype = nil, nil
  OptionParser.new{|o|
    o.on('-t TYPE', 'input type'){  |t| itype  = t }
    o.on('-o TYPE', 'output type'){ |t| otype = t }
    o.on('--help', '-h'       , 'display this help message'){ puts o; exit }
  }.parse!
  db, files = *args.partition{ |x| x.index('=') }
  db = db.inject({}){ |h,kv| k,v = kv.split('='); h[k] = v; h}
  files.each do |file|
    puts Malt.render(:file=>file, :type=>itype, :format=>otype)
    #file = itype ? Malt.file(file, :type=>itype) : Malt.file(file)
    #if otype
    #  puts file.render(otype, db)
    #else
    #  puts file.render(db)
    #end
  end
end

.open(url, options = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/malt.rb', line 47

def self.open(url, options={})
  require 'open-uri'
  text = open(url).read
  file = File.basename(url) # parse better with URI.parse
  text(text, :file=>file)
end

.profileObject



14
15
16
17
18
19
# File 'lib/malt/meta/data.rb', line 14

def self.profile
  @profile ||= (
    require 'yaml'
    YAML.load(File.new(DIRECTORY + '/profile'))
  )
end

.register(malt_class, *exts) ⇒ Object



11
12
13
14
15
16
# File 'lib/malt.rb', line 11

def self.register(malt_class, *exts)
  exts.each do |ext|
    type = ext_to_type(ext)
    registry[type] = malt_class
  end
end

.registryObject



19
20
21
# File 'lib/malt.rb', line 19

def self.registry
  @registry ||= {}
end

.text(text, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/malt.rb', line 34

def self.text(text, options={})
  if file = options[:file]
    ext = File.extname(file)
    ext = nil if ext.empty?
  end
  type = options[:type] || options[:format] || ext
  type = ext_to_type(type)
  malt_class = registry[type] || Formats::Text
  #raise "unkown type -- #{type}" unless malt_class
  malt_class.new(options.merge(:text=>text,:file=>file,:type=>type))
end