class Tm2Gsv::ThemeWriter

Public Class Methods

new(task) click to toggle source
# File lib/tm2gsv/theme/writer.rb, line 7
def initialize(task)
  @task = task || ::Thor.new()
end

Public Instance Methods

write(theme, destination) click to toggle source
# File lib/tm2gsv/theme/writer.rb, line 11
def write(theme, destination)
  @theme = theme
  @destination = destination
  @file = File.join(@destination, "#{@theme[:id]}.xml")

  create_doc
  create_scheme

  create_styles(@theme[:settings])
  create_styles(@theme[:styles])

  write_file

  return @file
end

Private Instance Methods

create_doc() click to toggle source
# File lib/tm2gsv/theme/writer.rb, line 29
def create_doc
  @doc = ::Ox::Document.new(version: '1.0', encoding: 'UTF-8')
end
create_scheme() click to toggle source
# File lib/tm2gsv/theme/writer.rb, line 33
def create_scheme
  @scheme = ::Ox::Element.new('style-scheme')
  @scheme[:id] = @theme[:id]
  @scheme[:_name] = @theme[:name]
  @scheme[:version] = @theme[:version]
  @doc << @scheme

  author = ::Ox::Element.new('author')
  author << 'TM2GSV'
  @scheme << author

  desc = ::Ox::Element.new('description')
  desc << 'A GTKSourceView color scheme converted with TM2GSV.'
  @scheme << desc
end
create_styles(styles) click to toggle source
# File lib/tm2gsv/theme/writer.rb, line 49
def create_styles(styles)
  styles.each do |key, value|
    next if value.empty?

    elem = ::Ox::Element.new('style')
    elem[:name] = key
    value.each do |k, v|
      next if v.nil?
      if v.to_s.color?
        elem[k] = normalize_color(v)
      else
        elem[k] = v
      end
    end

    @scheme << elem
  end
end
normalize_color(value) click to toggle source
# File lib/tm2gsv/theme/writer.rb, line 73
def normalize_color(value)
  bg = @theme[:settings]['text']['background']

  if value.color_with_alpha?
    bg = ::Color::RGB.from_html(bg.to_hex_color)
    color = ::Color::RGB.from_html(value.to_hex_color)
    return color.mix_with(bg, value.to_hex_alpha).html
  else
    return ::Color::RGB.from_html(value).html
  end
end
write_file() click to toggle source
# File lib/tm2gsv/theme/writer.rb, line 68
def write_file
  ::Ox.default_options = { with_xml: true }
  ::Ox.to_file @file, @doc
end