public
Description: A little script to convert color palettes to Sass constant sets
Homepage: http://yapok.org/blog/
Clone URL: git://github.com/madx/gp2sass.git
gp2sass / gp2sass
100644 158 lines (132 sloc) 4.381 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env ruby
 
# Author: MadX <root+gp2sass@yapok.org>
# License: Public domain
 
require 'optparse'
require 'ostruct'
 
# Command line option parser (relies on optparse)
class G2SOptionParser
 
  Version = [1, 0]
 
  def self.parse(args)
    options = OpenStruct.new
    options.output = $stdout
    options.mode = :overwrite
    options.verbose = false
    options.notify = true
    
    opts = OptionParser.new do |opts|
      opts.banner = "Usage: gp2sass [options] source_file"
      
      opts.separator ""
      opts.separator "Specific options:"
      
      opts.on('-o', '--output [FILE]', 'Output contents in FILE') do |file|
        options.output = file
      end
      
      opts.on('-p', '--prepend', 'Prepend to the selected file') do |p|
        options.mode = :prepend
      end
      
      opts.on('-a', '--append', 'Append to the seleced file') do |a|
        options.mode = :append
      end
      
      opts.on('-n', '--[no-]notify', 'Put a loud comment in the sass output',
        'to notify the success') do |n|
        options.notify = n
      end
      
      opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
        options.verbose = v
      end
      
      opts.separator ""
      opts.separator "Common options:"
      
      opts.on_tail("-h", "--help", "Show this message") do
        puts opts
        puts "\nDescription:",
          " gp2sass is a command line tool to convert Gimp palette files (gpl)",
          " to a Sass constant set."
          
        puts "\nExample:",
          " Try the following command (if you have The Gimp installed):",
          "", " gp2sass Tango",
          ""
        exit
      end
 
      opts.on_tail("--version", "Show version") do
        puts "gp2sass v#{Version.join('.')}"
        exit
      end
      
    end
    
    opts.parse!(args)
    options
  end # self.parse()
 
end # G2SOptionParser
 
# Parse ARGV :)
source = ARGV[-1]
options = G2SOptionParser.parse(ARGV[0..(ARGV.length - 1)])
 
# Yes, the program begins \o/
begin
 
  # Guess the source or raise an error if no file matches
  if source.nil? then raise "Usage: gp2sass [options] source_file" end
  searchable_dirs = [
    '.',
    '~/.gimp-2.4/palettes/',
    '~/.gimp-2.2/palettes/',
    '/usr/share/gimp/2.0/palettes'
  ].collect{|path| File.expand_path(path) }
  path = nil
  source = File.basename(source, '.gpl') + '.gpl'
  if options.verbose
    puts "Searching %s in" % source
    searchable_dirs.each {|d| puts " #{File.expand_path(d)}"}
  end
  searchable_dirs.each do |dir|
    path = File.join(dir, source )
    if File.exists?(path) then break else path = nil end
  end
  raise 'File "%s" not found' % source if path.nil?
  
  puts "File found: %s" % path if options.verbose
  
  # Open the palette file and parse it
  header, *data = File.read(path).split('#')
  palette = OpenStruct.new
  palette.name = header.gsub(/.*Name: ([^\n]+).*/m, '\1')
  palette.colors = {}
  data.join.strip.each_line do |line|
    digits, name = line.split("\t")
    name = name.downcase.gsub(/ +/, '_').strip
    palette.colors[name] = digits.scan(/\S+/).collect {|c| "%02x" % c.to_i }
  end
  output = options.notify ? "/* %s palette imported#{$/}" % palette.name : ''
  palette.colors.each do |name, hexes|
    output += "!%s = #%s%s%s#{$/}" % ([name] + hexes)
  end
  if options.verbose
    puts "Palette file parsed, output will look like :"
    puts output.split($/).collect {|l| l = " #{l}"}.join($/)
  end
  if options.output == $stdout
    puts output
  else
    if options.verbose && !File.exists?(options.output)
      puts "File %s doesn't exist, creating it" % options.output
    end
    if File.exists?(options.output)
      buf = File.read(options.output)
      puts "Saving file contents" if options.verbose
    else
      buf = nil
    end
    f = File.open(options.output, 'w+')
    puts 'Opening "%s"' % options.output if options.verbose
    case options.mode
      when :prepend
        msg = "Prepending output to file contents"
        output = "#{output}#{$/}#{buf}"
      when :append
        msg = "Appending output to file contents"
        output = "#{buf}#{$/}#{output}"
      else
        msg = "Overwriting file contents"
    end
    puts msg if options.verbose
    f.write(output)
    f.close
    puts "Finished." if options.verbose
  end
  
rescue => e
  puts e.message
  exit
end