Top Level Namespace

Includes:
Term::ANSIColor

Constant Summary collapse

VERSION =
File.read(File.dirname(__FILE__) + '/../VERSION')
WEDATA_URL =
'http://wedata.net/databases/JHC/items.json'
CONFIG_PATH =
ENV['HOME'] + '/.jhc'

Instance Method Summary collapse

Instance Method Details

#help(config) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'bin/jhc', line 82

def help(config)
  buf = ''
  buf += "Usage: jhc -t HTML_TITLE -j jquery,underscore -c bootstrap -e url -r\n"
  buf += "Description:\n"
  buf += "\t-t: specify HTML title\n"
  buf += "\t-j: specify javascript libraries\n"
  buf += "\t-c: specify css\n"
  buf += "\t-e: specify url to extract body\n"
  buf += "\t-r: reload wedata config and exit\n"
  buf += "Available JavaScript Libraries:\n"
  config.select{|c| c['data']['type'] == 'js'}.each do |c|
    buf += "\t#{c['data']['key']}\t#{c['data']['url']}\n"
  end
  buf += "Available StyleSheets:\n"
  config.select{|c| c['data']['type'] == 'css'}.each do |c|
    buf += "\t#{c['data']['key']}\t#{c['data']['url']}\n"
  end
  red(buf)
end

#load_configObject



75
76
77
78
79
80
# File 'bin/jhc', line 75

def load_config
  json = JSON.parse(open(WEDATA_URL).read)
  file = File.open(CONFIG_PATH, 'w')
  file.write(json.to_yaml)
  file.close
end

#main(args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'bin/jhc', line 19

def main(args)
  puts white("# welcome to jhc v#{VERSION}")
  opt = parse_args(args)

  if !File.exists?(CONFIG_PATH) || opt['reload']
    puts white("# downloading: config data from wedata.net")
    load_config
    exit 0 if opt['reload']
  end
  cfg = YAML.load(File.read(CONFIG_PATH))

  dir = args[0]
  raise help(cfg) unless dir

  FileUtils.mkdir_p(dir)

  %w(js css).each do |key|
    next unless opt[key]
    FileUtils.mkdir_p(File.join(dir, key))
    opt[key].split(',').each do |lib|
      if item = cfg.find {|c| c['data']['key'] == lib && c['data']['type'] == key }
        content = open(item['data']['url']).read
        File.open(File.join(dir, key, "#{lib}.#{key}"), 'w').write(content)
        puts blue("add: " +  File.join(dir, key, "#{lib}.#{key}")) + white(" # #{item['data']['url']}")
      end
    end
  end

  body = nil
  if opt['extract']
    puts white("# extracting body of #{opt['extract']}")
    doc = Nokogiri::HTML.parse(open(opt['extract']).read)
    body = doc.search('/html/body').to_s.toutf8
  end

  haml = File.read(File.dirname(__FILE__) + '/../lib/index.haml')
  html = Haml::Engine.new(haml, :format => :html5).render(binding)
  puts blue("add: #{File.join(dir, 'index.html')}")
  File.open(File.join(dir, 'index.html'), 'w').write(html)
rescue StandardError => e
  puts e
  exit 1 
end

#parse_args(args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'bin/jhc', line 63

def parse_args(args)
  opt = {}
  OptionParser.new do |o|
    %w(title js css extract).each do |key|
      o.on("-#{key.chars.first} #{key.upcase}") {|v| opt[key] = v }
    end
    o.on("-r") {|v| opt['reload'] = v }
    o.parse!(args)
  end
  opt
end