summaryrefslogtreecommitdiff
path: root/new/lib/crys/builder.rb
blob: b8135c77ed4a7168b468d2fa11e6474093c2953d (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
51
52
53
54
55
56
57
58
59
60
61
62
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_content
      process_assets

      upsert_pages_db
    end

    private

    attr_reader :file_path, :output_dir, :processor, :db

    def pages_db_manager
      Crys::PagesDbManager.new
    end
    
    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_content
      content = processor.parsed_content
      filename = processor.filename
      output_path = output_dir + "/" + filename

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

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

    def html_processor
      Crys::HtmlProcessor
    end

    def rss_processor
      Crys::RssProcessor
    end

    def upsert_pages_db
      pages_db_manager.add_page(processor)
    end
  end
end