summaryrefslogtreecommitdiff
path: root/new/lib/crys/builder.rb
blob: a3f55b1521f1025962fde8f51e6ed18bb05175cd (plain)
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
43
44
45
46
47
48
49
50

module Crys
  class Builder
    

    def initialize(file_path:, output_dir:, image_dir:)
      @file_path = file_path
      @output_dir = output_dir
      @processor = processor_class.new(file_path: file_path, image_dir: image_dir)

    end

    def build
      process_html
      process_assets
    end

    private

    attr_reader :file_path, :output_dir, :processor

    def process_assets
      processor.assets.each do |asset|
        output_path = output_dir + "/" + asset.relative_path
        File.open(output_path, 'w') { |file| file.write(asset.processed_asset) }
      end
    end

    def process_html
      html = processor.to_html
      filename = processor.filename
      output_path = output_dir + "/" + filename

      File.open(output_path, 'w') { |file| file.write(html) }
    end

      def processor_class
        case file_path
        when /html.erb$/
          html_processor
        else
        raise StandardError.new("No processor for #{file_path}")
      end
    end

    def html_processor
      Crys::HtmlProcessor
    end
  end
end