class Tm2Gsv::ThemeParser

Public Class Methods

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

Public Instance Methods

parse(file) click to toggle source
# File lib/tm2gsv/theme/parser.rb, line 11
def parse(file)
  @hash = ::Plist::parse_xml(file)
  @settings = @hash['settings']
  @theme = { version: '1.0' }

  # Get theme meta
  @theme[:name] = @hash['name']
  @theme[:id] = @hash['name'].dasherize
  @theme[:settings] = {}
  @theme[:styles] = {}

  parse_styles
  parse_scopes
  parse_settings

  return @theme
end

Private Instance Methods

load_data(file) click to toggle source
# File lib/tm2gsv/theme/parser.rb, line 91
def load_data(file)
  path = File.join(Tm2Gsv.root, 'data', 'theme', file)
  json = File.read File.expand_path(path)
  return ::JSON.parse(json)
end
parse_scopes() click to toggle source
# File lib/tm2gsv/theme/parser.rb, line 45
def parse_scopes
  data = load_data('styles.json')
  styles = @theme[:styles]

  @theme[:styles] = {}
  data.each do |key, value|
    style = styles.select { |k, v| value.include?(k) }
    @theme[:styles][key] = style.values[0] if style.any?
  end
end
parse_setting(opts) click to toggle source
# File lib/tm2gsv/theme/parser.rb, line 64
def parse_setting(opts)
  settings = @settings.first['settings']
  styles = @theme[:styles]
  setting = {}

  opts.each do |key, value|
    value = [value].flatten.reverse
    if key == 'fontStyle'
      style = settings.select { |k, v| value.include? k }.values[0]
      style.to_s.split.each { |i| setting[i] = true }
    else
      value.each do |item|
        if item.include? '.'
          item = item.split('.')
          style = styles.select { |k, v| k == item[0] }
          style = style[item[0]][item[1]] rescue nil
        else
          style = settings[item] rescue nil
        end
        setting[key] = style unless style.nil?
      end
    end
  end

  return setting
end
parse_settings() click to toggle source
# File lib/tm2gsv/theme/parser.rb, line 56
def parse_settings
  data = load_data('settings.json')

  data.each do |name, opts|
    @theme[:settings][name] = parse_setting(opts)
  end
end
parse_styles() click to toggle source
# File lib/tm2gsv/theme/parser.rb, line 31
def parse_styles
  @settings.drop(1).each do |style|
    settings = style['settings'] || {}

    settings['fontStyle'].to_s.split.each { |f| settings[f] = true }
    settings.delete('fontStyle')

    scopes = style['scope'].to_s.split(%r{,\s*})
    scopes.each do |scope|
      @theme[:styles][scope] = settings if settings.any?
    end
  end
end