From 3d13bedaaffdae466621788d808a6b71b9ed9f59 Mon Sep 17 00:00:00 2001 From: mms Date: Wed, 8 Jan 2025 22:20:17 +0100 Subject: feat: batch-first --- new/lib/crys/builder.rb | 18 ++++++----- new/lib/crys/processors/common_functions.rb | 36 ---------------------- new/lib/crys/processors/common_parts.rb | 18 ----------- new/lib/crys/processors/html_processor.rb | 20 ++++++++---- new/lib/crys/processors/mixins/common_functions.rb | 36 ++++++++++++++++++++++ new/lib/crys/processors/mixins/common_parts.rb | 18 +++++++++++ new/lib/crys/processors/processed_page.rb | 6 ++++ new/lib/crys/processors/rss_processor.rb | 13 +++++--- 8 files changed, 94 insertions(+), 71 deletions(-) delete mode 100644 new/lib/crys/processors/common_functions.rb delete mode 100644 new/lib/crys/processors/common_parts.rb create mode 100644 new/lib/crys/processors/mixins/common_functions.rb create mode 100644 new/lib/crys/processors/mixins/common_parts.rb create mode 100644 new/lib/crys/processors/processed_page.rb (limited to 'new/lib/crys') diff --git a/new/lib/crys/builder.rb b/new/lib/crys/builder.rb index 3b1ed388..ee4fb8ce 100644 --- a/new/lib/crys/builder.rb +++ b/new/lib/crys/builder.rb @@ -5,10 +5,12 @@ module Crys 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) + @image_dir = image_dir end - def build + def build + @processor = processor_class.new(file_path: file_path, image_dir: image_dir) + process_content process_assets @@ -17,7 +19,7 @@ module Crys private - attr_reader :file_path, :output_dir, :processor, :db + attr_reader :file_path, :output_dir, :processor, :db, :image_dir def pages_db_manager Crys::PagesDbManager.new @@ -31,11 +33,13 @@ module Crys end def process_content - content = processor.parsed_content - filename = processor.filename - output_path = "#{output_dir}/#{filename}" + processor.parsed_pages.each do |page| + content = page.content + filename = page.filename + output_path = "#{output_dir}/#{filename}" - File.write(output_path, content) + File.write(output_path, content) + end end def processor_class diff --git a/new/lib/crys/processors/common_functions.rb b/new/lib/crys/processors/common_functions.rb deleted file mode 100644 index 9cf56692..00000000 --- a/new/lib/crys/processors/common_functions.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -require 'rss' - -module Crys - module CommonFunctions - def project_root - File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))))).to_s - end - - def process_image(file:, width: :auto) - full_path = "#{image_dir}/#{file}" - - image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: width) - assets << image - - image - end - - def fetch_rss(url: nil, name: nil) - if name - path = case name - when :main - '/public/index.xml' - end - - rss = File.read(project_root + path) - else - puts url - - end - - RSS::Parser.parse(rss) - end - end -end diff --git a/new/lib/crys/processors/common_parts.rb b/new/lib/crys/processors/common_parts.rb deleted file mode 100644 index a6c2dc16..00000000 --- a/new/lib/crys/processors/common_parts.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module Crys - module CommonParts - def webbutton(file:, url:, alt:) - full_path = "#{image_dir}/buttons/#{file}" - - image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: 88) - assets << image - - code = ERB.new <<~EOF - <%=alt%> - EOF - - code.result(binding) - end - end -end diff --git a/new/lib/crys/processors/html_processor.rb b/new/lib/crys/processors/html_processor.rb index 33116cef..5dd1387f 100644 --- a/new/lib/crys/processors/html_processor.rb +++ b/new/lib/crys/processors/html_processor.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require 'erb' -require_relative 'common_functions' -require_relative 'common_parts' +require_relative 'mixins/common_functions' +require_relative 'mixins/common_parts' module Crys class HtmlProcessor @@ -15,15 +14,24 @@ module Crys @assets = [] end - def parsed_content - html_file = ERB.new(File.read(file_path)) - html_file.result(local_binding) + def parsed_pages + [ + ProcessedPage.new( + filename: filename, + content: html_content + ) + ] end def filename file_path.scan(%r{.*/(.*)\.erb}).flatten.first end + def html_content + html_file = ERB.new(File.read(file_path)) + html_file.result(local_binding) + end + attr_reader :assets, :file_path private diff --git a/new/lib/crys/processors/mixins/common_functions.rb b/new/lib/crys/processors/mixins/common_functions.rb new file mode 100644 index 00000000..52ce65ac --- /dev/null +++ b/new/lib/crys/processors/mixins/common_functions.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'rss' + +module Crys + module CommonFunctions + def project_root + File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))))).to_s + end + + def process_image(file:, width: :auto) + full_path = "#{image_dir}/#{file}" + + image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: width) + assets << image + + image + end + + def fetch_rss(url: nil, name: nil) + if name + path = case name + when :main + '/public/index.xml' + end + + rss = File.read(project_root + path) + else + puts url + + end + + RSS::Parser.parse(rss) + end + end +end diff --git a/new/lib/crys/processors/mixins/common_parts.rb b/new/lib/crys/processors/mixins/common_parts.rb new file mode 100644 index 00000000..a6c2dc16 --- /dev/null +++ b/new/lib/crys/processors/mixins/common_parts.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Crys + module CommonParts + def webbutton(file:, url:, alt:) + full_path = "#{image_dir}/buttons/#{file}" + + image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: 88) + assets << image + + code = ERB.new <<~EOF + <%=alt%> + EOF + + code.result(binding) + end + end +end diff --git a/new/lib/crys/processors/processed_page.rb b/new/lib/crys/processors/processed_page.rb new file mode 100644 index 00000000..5bab3566 --- /dev/null +++ b/new/lib/crys/processors/processed_page.rb @@ -0,0 +1,6 @@ +module Crys + ProcessedPage = Struct.new( + :filename, + :content + ) +end diff --git a/new/lib/crys/processors/rss_processor.rb b/new/lib/crys/processors/rss_processor.rb index de79f11b..4806c594 100644 --- a/new/lib/crys/processors/rss_processor.rb +++ b/new/lib/crys/processors/rss_processor.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require_relative 'common_functions' -require_relative 'common_parts' +require_relative 'mixins/common_functions' +require_relative 'mixins/common_parts' module Crys class RssProcessor @@ -13,8 +13,13 @@ module Crys @image_dir = image_dir # just for api consistency end - def parsed_content - rss.content + def parsed_pages + [ + ProcessedPage.new( + filename: rss.filename, + content: rss.content + ) + ] end def filename -- cgit v1.2.3