Class: SublimeDSL::TextMate::Theme

Inherits:
Object
  • Object
show all
Includes:
CustomBaseName
Defined in:
lib/sublime_dsl/textmate/theme.rb,
lib/sublime_dsl/textmate/theme/item.rb,
lib/sublime_dsl/textmate/theme/dsl_reader.rb,
lib/sublime_dsl/textmate/theme/plist_writer.rb

Defined Under Namespace

Classes: DSLReader, Item, PListWriter

Constant Summary collapse

COLOR_NAMES_HASH =

Hash { ‘#xxxxxx’ => color_name }, read from the DATA section of this file.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CustomBaseName

#basename, #basename=, #custom_basename, #dsl_file_arg

Constructor Details

#initialize(name) ⇒ Theme

Returns a new instance of Theme.



103
104
105
106
107
108
109
110
111
# File 'lib/sublime_dsl/textmate/theme.rb', line 103

def initialize(name)
  @name = name
  @author = nil
  @uuid = nil
  @comment = nil
  @license = nil
  @base_colors = {}
  @items = []
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



75
76
77
# File 'lib/sublime_dsl/textmate/theme.rb', line 75

def author
  @author
end

#base_colorsObject (readonly)

base color hash:

background
caret
foreground
invisibles
line_highlight
selection
selection_border
inactive_selection

Monokai* also have:

inactive_selection
inactive_selection_foreground
find_highlight
find_highlight_foreground
active_guide
brackets_foreground
brackets_options
bracket_contents_foreground
bracket_contents_options


98
99
100
# File 'lib/sublime_dsl/textmate/theme.rb', line 98

def base_colors
  @base_colors
end

#commentObject

Returns the value of attribute comment.



75
76
77
# File 'lib/sublime_dsl/textmate/theme.rb', line 75

def comment
  @comment
end

#itemsObject (readonly)

array of defined items



101
102
103
# File 'lib/sublime_dsl/textmate/theme.rb', line 101

def items
  @items
end

#licenseObject

Returns the value of attribute license.



75
76
77
# File 'lib/sublime_dsl/textmate/theme.rb', line 75

def license
  @license
end

#nameObject

Returns the value of attribute name.



75
76
77
# File 'lib/sublime_dsl/textmate/theme.rb', line 75

def name
  @name
end

#uuidObject

Returns the value of attribute uuid.



75
76
77
# File 'lib/sublime_dsl/textmate/theme.rb', line 75

def uuid
  @uuid
end

Class Method Details

.color_dataObject

Returns the content of this file after __END__ (DATA is not available for required files).



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sublime_dsl/textmate/theme.rb', line 46

def self.color_data # :nodoc:
  data = ''
  File.open(__FILE__) do |f|
    in_data = false
    f.each_line do |line|
      if line =~ /^__END__/
        in_data = true
      elsif in_data
        data << line
      end
    end
  end
  data
end

.import(file) ⇒ Object

Create from a PList file.



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
# File 'lib/sublime_dsl/textmate/theme.rb', line 13

def self.import(file)
  p = PList.import(file)
  t = new(p.delete('name'))
  t.basename = File.basename(file, File.extname(file))
  t.author = p.delete('author')
  t.uuid = p.delete('uuid')
  t.comment = p.delete('comment')
  t.license = p.delete('license')
  p.delete('settings').each do |h|
    n = h['name']
    s = h['scope'] && h['scope'].strip
    if n || s
      e = Item.new(n, s)
      h['settings'].each_pair do |prop, value|
        next if value.empty?
        e.send "#{prop.snake_case}=", value
      end
      t.add_item e
    else
      h['settings'].each_pair do |prop, color|
        key = prop.snake_case #.to_sym
        color.upcase! if color =~ /^#/
        t.base_colors[key] = color
      end
    end
  end
  p.empty? or raise Error, "unknown theme keys: #{p.inspect}"
  t
end

Instance Method Details

#add_item(e) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/sublime_dsl/textmate/theme.rb', line 113

def add_item(e)
  warn "unnamed item in theme #{name.inspect}" unless e.name
  warn "empty scope for item '#{e.name}' in theme #{name.inspect}" unless e.scope
  e.name && items.any? { |el| el.name == e.name } and
    warn "duplicate item name '#{e.name}' in theme #{name.inspect}"
  items << e
end

#colors_hashObject

A Hash { <color value> => <color name> }.



178
179
180
181
182
183
184
185
186
# File 'lib/sublime_dsl/textmate/theme.rb', line 178

def colors_hash
  @colors_hash ||= begin
    colors = base_colors.values + items.flat_map(&:colors)
    colors.uniq!
    h = {}
    colors.each_with_index { |c, i| h[c] = COLOR_NAMES_HASH[c] || "color#{i}" }
    h
  end
end

#export(dir) ⇒ Object



172
173
174
175
# File 'lib/sublime_dsl/textmate/theme.rb', line 172

def export(dir)
  file = "#{dir}/#{basename}.tmTheme"
  PListWriter.new(self).export(file)
end

#to_dslObject



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
159
160
161
162
# File 'lib/sublime_dsl/textmate/theme.rb', line 121

def to_dsl
  out = StringIO.new('', 'wb:utf-8')

  args = name.to_source
  args << dsl_file_arg

  out.puts comment.strip.lines.map { |l| "# #{l}" }.join << "\n\n" if comment
  out.puts "theme #{args} do"
  out.puts
  out.puts "  author '#{author}'" if author
  out.puts "  uuid '#{uuid}'" if uuid
  if license
    out.puts
    out.puts "  license <<-TXT"
    license.lines.each { |l| out.puts l.strip.empty? ? '' : l.strip.wrap.indent(4) }
    out.puts "  TXT"
  end

  out.puts
  max = colors_hash.values.map(&:length).max
  colors_hash.each_pair do |color, name|
    out.puts "  %*1$2$s = '%3$s'" % [-max, name, color]
  end

  out.puts
  out.puts "  base_colors("
  base_colors.each_pair do |k,v|
    out.puts "    #{k}: #{colors_hash[v]},"
  end
  out.puts "  )"

  out.puts
  names_hash = items.group_by(&:name)
  items.each do |e|
    out.puts '  # FIXME: duplicate name:' if e.name && names_hash[e.name].length > 1
    out.puts e.to_dsl(colors_hash).indent(2)
  end

  out.puts "\nend"

  out.string
end

#write(dir) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/sublime_dsl/textmate/theme.rb', line 164

def write(dir)
  file = "#{dir}/#{basename}.tmTheme.rb"
  File.open(file, 'wb:utf-8') do |f|
    f.puts '# encoding: utf-8'
    f.puts "\n" << to_dsl
  end
end