diff options
author | mms <git@sapka.me> | 2025-01-06 22:17:43 +0100 |
---|---|---|
committer | mms <git@sapka.me> | 2025-01-06 22:17:43 +0100 |
commit | b50e4a5f40bc64fa1893d249d83c05ecd217de1d (patch) | |
tree | d98b5c3a378975d095aa3dad8d1a642c3cf0f3d2 /new/lib/crys | |
parent | bfd8209ad0d80b5027bea8a1a095dadc8bffdc61 (diff) |
feat(new): rss + batch
Diffstat (limited to 'new/lib/crys')
-rw-r--r-- | new/lib/crys/_code_processor.rb | 0 | ||||
-rw-r--r-- | new/lib/crys/_default_data.rb | 4 | ||||
-rw-r--r-- | new/lib/crys/_file_watcher.rb | 0 | ||||
-rw-r--r-- | new/lib/crys/_listing.rb | 0 | ||||
-rw-r--r-- | new/lib/crys/_new_file.rb | 0 | ||||
-rw-r--r-- | new/lib/crys/_page_from_erb.rb | 0 | ||||
-rw-r--r-- | new/lib/crys/_page_from_org.rb | 0 | ||||
-rw-r--r-- | new/lib/crys/_resource_from_css.rb | 0 | ||||
-rw-r--r-- | new/lib/crys/batch_builder.rb | 34 | ||||
-rw-r--r-- | new/lib/crys/builder.rb | 40 | ||||
-rw-r--r-- | new/lib/crys/pages_db_manager.rb | 62 | ||||
-rw-r--r-- | new/lib/crys/processors/html_processor.rb | 7 | ||||
-rw-r--r-- | new/lib/crys/processors/rss_processor.rb | 28 |
13 files changed, 153 insertions, 22 deletions
diff --git a/new/lib/crys/_code_processor.rb b/new/lib/crys/_code_processor.rb deleted file mode 100644 index e69de29..0000000 --- a/new/lib/crys/_code_processor.rb +++ /dev/null diff --git a/new/lib/crys/_default_data.rb b/new/lib/crys/_default_data.rb deleted file mode 100644 index 8333669..0000000 --- a/new/lib/crys/_default_data.rb +++ /dev/null @@ -1,4 +0,0 @@ -module Crys - module DefaultData - end -end diff --git a/new/lib/crys/_file_watcher.rb b/new/lib/crys/_file_watcher.rb deleted file mode 100644 index e69de29..0000000 --- a/new/lib/crys/_file_watcher.rb +++ /dev/null diff --git a/new/lib/crys/_listing.rb b/new/lib/crys/_listing.rb deleted file mode 100644 index e69de29..0000000 --- a/new/lib/crys/_listing.rb +++ /dev/null diff --git a/new/lib/crys/_new_file.rb b/new/lib/crys/_new_file.rb deleted file mode 100644 index e69de29..0000000 --- a/new/lib/crys/_new_file.rb +++ /dev/null diff --git a/new/lib/crys/_page_from_erb.rb b/new/lib/crys/_page_from_erb.rb deleted file mode 100644 index e69de29..0000000 --- a/new/lib/crys/_page_from_erb.rb +++ /dev/null diff --git a/new/lib/crys/_page_from_org.rb b/new/lib/crys/_page_from_org.rb deleted file mode 100644 index e69de29..0000000 --- a/new/lib/crys/_page_from_org.rb +++ /dev/null diff --git a/new/lib/crys/_resource_from_css.rb b/new/lib/crys/_resource_from_css.rb deleted file mode 100644 index e69de29..0000000 --- a/new/lib/crys/_resource_from_css.rb +++ /dev/null diff --git a/new/lib/crys/batch_builder.rb b/new/lib/crys/batch_builder.rb new file mode 100644 index 0000000..61fdcf2 --- /dev/null +++ b/new/lib/crys/batch_builder.rb @@ -0,0 +1,34 @@ +module Crys + class BatchBuilder + def db_path + output_dir = "#{File.dirname(File.dirname(File.dirname(__FILE__)))}/db/pages.yaml" + end + + def output_dir + "#{File.dirname(File.dirname(__FILE__))}/output" + end + + def image_dir + "#{File.dirname(File.dirname(File.expand_path(__FILE__)))}/assets/images" + end + + def builder_class + Crys::Builder + end + + def db + Crys::PagesDbManager.new + end + + def build + db.pages.each do |page| + builder_class.new( + file_path: page.file_path, + output_dir: output_dir, + image_dir: image_dir, + ).build + puts "processed: " + page.filename + end + end + end +end diff --git a/new/lib/crys/builder.rb b/new/lib/crys/builder.rb index a3f55b1..b8135c7 100644 --- a/new/lib/crys/builder.rb +++ b/new/lib/crys/builder.rb @@ -1,24 +1,26 @@ - 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_html + process_content process_assets + + upsert_pages_db end private - attr_reader :file_path, :output_dir, :processor + 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 @@ -26,19 +28,21 @@ module Crys end end - def process_html - html = processor.to_html + def process_content + content = processor.parsed_content filename = processor.filename output_path = output_dir + "/" + filename - File.open(output_path, 'w') { |file| file.write(html) } + File.open(output_path, 'w') { |file| file.write(content) } end - def processor_class - case file_path - when /html.erb$/ - html_processor - else + 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 @@ -46,5 +50,13 @@ module Crys 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 diff --git a/new/lib/crys/pages_db_manager.rb b/new/lib/crys/pages_db_manager.rb new file mode 100644 index 0000000..6a5e71c --- /dev/null +++ b/new/lib/crys/pages_db_manager.rb @@ -0,0 +1,62 @@ +module Crys + class PagesDbManager + PAGES_KEY = :pages + PAGE = Struct.new(:filename, :file_path, :last_update, :in_rss, keyword_init: true) + + def db_path + end + + def add_page(processor) + @processor = processor + hash = Digest::MD5.hexdigest(processor.file_path) + known_page = known_page(hash) + + upsert_db(hash, known_page) + save_db + end + + def pages + db.fetch(:pages).map do | _, data| + PAGE.new(data) + end + end + + private + + attr_reader :hash, :processor + + + def db + @db ||= YAML.load_file( + db_path, + permitted_classes: [Time, Symbol] + ) + end + + def upsert_db(hash, known_page) + if known_page + record = PAGE.new(known_page) + else + record = PAGE.new( + filename: processor.filename, + file_path: processor.file_path, + in_rss: true + ) + end + + record.last_update = Time.now + + @db[PAGES_KEY][hash] = record.to_h + end + + def save_db + File.open(db_path, 'w') do |f| + f.write db.to_yaml + end + end + + def known_page(hash) + db[PAGES_KEY][hash] + end + end +end diff --git a/new/lib/crys/processors/html_processor.rb b/new/lib/crys/processors/html_processor.rb index d5ea73d..bcc9768 100644 --- a/new/lib/crys/processors/html_processor.rb +++ b/new/lib/crys/processors/html_processor.rb @@ -14,21 +14,20 @@ module Crys @assets = [] end - def to_html + def parsed_content html_file = ERB.new(File.read(file_path)) html_file.result(local_binding) end - def filename file_path.scan(/.*\/(.*)\.erb/).flatten.first end - attr_reader :assets + attr_reader :assets, :file_path private - attr_reader :file_path, :image_dir + attr_reader :image_dir attr_writer :assets def local_binding diff --git a/new/lib/crys/processors/rss_processor.rb b/new/lib/crys/processors/rss_processor.rb new file mode 100644 index 0000000..bce48ad --- /dev/null +++ b/new/lib/crys/processors/rss_processor.rb @@ -0,0 +1,28 @@ +require_relative "common_functions" +require_relative "common_parts" + +module Crys + class RssProcessor + def initialize(file_path:, image_dir:) + @file_path = file_path + rss_file = File.read(file_path) + instance_eval(rss_file) + @assets = [] + end + + def parsed_content + rss.content + end + + def filename + rss.filename + end + + attr_reader :assets, :file_path + + private + + attr_reader :rss + + end +end |