File: parser.rb

package info (click to toggle)
ruby-prawn-icon 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,228 kB
  • sloc: ruby: 1,370; makefile: 5
file content (42 lines) | stat: -rw-r--r-- 1,052 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

# tool/scss/parser.rb: Convert SCSS variables to YAML legend.
#
# Copyright September 2017, Jesse Doyle. All rights reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.

require 'yaml'

module SCSS
  class Parser
    attr_accessor :path, :output, :data, :prefix, :version, :specifier

    def initialize(opts = {})
      self.specifier = opts.fetch(:specifier)
      self.prefix = opts.fetch(:prefix)
      self.version = opts.fetch(:version)
      self.path = opts.fetch(:path)
      self.data = File.read(path)
      self.output = opts.fetch(:output)
      parse
    end

    private

    def parse
      matches = data.scan(prefix)
      initial = {
        specifier => {
          '__font_version__' => data.scan(version).flatten.first
        }
      }
      yaml = matches.each_with_object(initial) do |match, hash|
        key, value = match
        hash[specifier][key] = [value.hex].pack('U')
        hash
      end
      File.write(output, yaml.to_yaml)
    end
  end
end