class SublimeDSL::TextMate::Theme

Constants

COLOR_NAMES_HASH

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

Attributes

author[RW]
base_colors[R]

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
comment[RW]
items[R]

array of defined items

license[RW]
name[RW]
uuid[RW]

Public Class Methods

import(file) click to toggle source

Create from a PList file.

# 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
new(name) click to toggle source
# 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

Public Instance Methods

add_item(e) click to toggle source
# 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_hash() click to toggle source

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

# 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) click to toggle source
# File lib/sublime_dsl/textmate/theme.rb, line 172
def export(dir)
  file = "#{dir}/#{basename}.tmTheme"
  PListWriter.new(self).export(file)
end
to_dsl() click to toggle source
# 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) click to toggle source
# 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