# frozen_string_literal: true

#
# Script that generates the embedded svg icons block for premonition.scss
#
# 1. Export SVG files from Sketch (premonition-icons.sketch) to the source folder upon changes.
# 2. Run this scripts: ruby generate.rb
# 3. Replace the new generated block with the one found in ../premonition.scss
#
# Remember to regenerate the css file: sass premonition.scss > premonition.css
#
require 'uri'

def create(f, name, color)
  text = File.read(f)
  text = text.gsub(/\n/, '')
  text = text.gsub(/<\?xml version.*\?>/, '')
  text = text.gsub(/<!--.*-->/, '')
  text = text.gsub(/\"/, "'")
  text = text.gsub(/\<title>.*<\/title>/, '')
  text = text.gsub(/\<desc>.*<\/desc>/, '')
  text = text.gsub(/>\s*</, '><')
  text = "data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8'%3F%3E#{URI::encode(text)}"
  text = text.gsub(/%20/, ' ')
  one, two = text.match(/^(.*fill\=').*('.*)$/i).captures
  "&.pn-#{name}  { @include pn-icon(\"#{one}\", #{color}, \"#{two}\"); }"
end

puts '/* Autogenerated code */'
puts create('source/Note.svg', 'note', '$svg-default-color')
puts create('source/Info.svg', 'info', '$svg-info-color')
puts create('source/Warning.svg', 'warn', '$svg-warning-color')
puts create('source/Error.svg', 'error', '$svg-error-color')
puts create('source/Quote.svg', 'quote', '$svg-citation-color')
puts create('source/Default.svg', 'square', '$svg-default-color')
puts '/* End of Autogenerated code */'
