File: bundle_processor.rb

package info (click to toggle)
ruby-sprockets-export 1.0.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 124 kB
  • sloc: ruby: 117; sh: 4; makefile: 3
file content (40 lines) | stat: -rw-r--r-- 921 bytes parent folder | download | duplicates (2)
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
require "sprockets/export/template"
require "ostruct"

module Sprockets::Export::BundleProcessor
  extend self

  PATTERN = /(.*)\/\* !START EXPORT (\S*)\s*(\S*) \*\/(.*)\/\* !END EXPORT \*\/(.*)/m

  def call(input)
    Sprockets::Export::Store.reset
    data = input[:data]

    if data =~ PATTERN
      data = Sprockets::Export::Template.new(extract_template_data(data)).render
    end

    { data: data }
  end

  private
    def extract_template_data(js)
      _, head, namespace, flags, export, tail = *js.match(PATTERN)

      { namespace: namespace,
        flags: create_flags(flags),
        export: format(export),
        head: format(head),
        tail: format(tail) }
    end

    def format(part)
      part.strip.gsub(/\A\s*;\n*|\n;\s*\z/, "").strip
    end

    def create_flags(string = "")
      flags = {}
      string.split.each { |flag| flags[flag] = true }
      OpenStruct.new(flags)
    end
end