# 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