summaryrefslogtreecommitdiff
path: root/new/lib/crys/builder.rb
blob: 123935a60467fc8a304485769ceeb7132f8ff067 (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
69
70
71
# frozen_string_literal: true

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

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

      process_content
      process_assets
    end

    private

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


    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|
        path = output_dir + "/" + File.dirname(page.filename)
        FileUtils.mkdir_p(path)
        
        content = page.content
        filename = page.filename
        output_path = "#{output_dir}/#{filename}"
        pages_db_manager.add_page(page)
        File.write(output_path, content)
      end

      pages_db_manager.save
    end

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

    def html_processor
      Crys::HtmlProcessor
    end

    def rss_processor
      Crys::RssProcessor
    end

    def yaml_batch_processor
      Crys::YamlBatchProcessor
    end
  end
end