summaryrefslogtreecommitdiff
path: root/new/lib/crys/builder.rb
blob: ee4fb8ce4a4af8abc9a2246d99d78b9e615256e8 (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
63
64
65
66
67
68
# frozen_string_literal: true

module Crys
  class Builder
    def initialize(file_path:, output_dir:, image_dir:)
      @file_path = file_path
      @output_dir = output_dir
      @image_dir = image_dir
    end

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

      process_content
      process_assets

      upsert_pages_db
    end

    private

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

    def pages_db_manager
      Crys::PagesDbManager.new
    end

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

    def process_content
      processor.parsed_pages.each do |page|
        content = page.content
        filename = page.filename
        output_path = "#{output_dir}/#{filename}"

        File.write(output_path, content)
      end
    end

    def processor_class
      case file_path
      when /html.erb$/
        html_processor
      when /xml.rb$/
        rss_processor
      else
        raise StandardError, "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