class ZendeskAppsTools::Theming::ZassFormatter

Constants

COLOR
COLOR_COMMAND_REGEXP
COMMAND
PERCENTAGE

Public Class Methods

color_adjust(rgb, percentage, op) click to toggle source
# File lib/zendesk_apps_tools/theming/zass_formatter.rb, line 39
def self.color_adjust(rgb, percentage, op)
  color = Sass::Script::Value::Color.from_hex(rgb)

  # Copied from:
  # https://github.com/sass/sass/blob/3.4.21/lib/sass/script/functions.rb#L2671
  new_color = color.with(lightness: color.lightness.public_send(op, percentage))

  # We set the Sass Output to compressed
  new_color.options = { style: :compressed }
  new_color.to_sass
rescue ArgumentError
  nil
end
format(raw, variables) click to toggle source
# File lib/zendesk_apps_tools/theming/zass_formatter.rb, line 14
def self.format(raw, variables)
  joined_keys = "(#{variables.keys.join('|')})"

  keys_regex = /(\$#{joined_keys}\b)|(#\{\$#{joined_keys}\})/

  substitution_hash = variables.each_with_object({}) do |(k, v), hash|
    hash["$#{k}"] = v.to_s
    hash["\#{$#{k}}"] = v.to_s
  end

  body = raw.to_s.dup
  body.gsub!(keys_regex, substitution_hash)

  # Color manipulation
  body.gsub!(COLOR_COMMAND_REGEXP) do |whole_match|
    if $LAST_MATCH_INFO[:command] == 'lighten'
      color_adjust($LAST_MATCH_INFO[:color], $LAST_MATCH_INFO[:percentage].to_i, :+) || whole_match
    else
      color_adjust($LAST_MATCH_INFO[:color], $LAST_MATCH_INFO[:percentage].to_i, :-) || whole_match
    end
  end

  body
end