diff options
Diffstat (limited to 'new/lib/crys')
21 files changed, 337 insertions, 76 deletions
diff --git a/new/lib/crys/_code_processor.rb b/new/lib/crys/_code_processor.rb deleted file mode 100644 index e69de29b..00000000 --- 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 8333669b..00000000 --- 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 e69de29b..00000000 --- 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 e69de29b..00000000 --- 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 e69de29b..00000000 --- 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 e69de29b..00000000 --- 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 e69de29b..00000000 --- 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 e69de29b..00000000 --- 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 00000000..d8f1d7b6 --- /dev/null +++ b/new/lib/crys/batch_builder.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Crys + class BatchBuilder + def initialize(pages_db_manager:, output_dir:, image_dir:, builder_class:) + @pages_db_manager = pages_db_manager + @output_dir = output_dir + @image_dir = image_dir + @builder_class = builder_class + + end + + def build + pages_db_manager.pages.each do |page| + builder_class.new( + file_path: page.source, + output_dir: output_dir, + image_dir: image_dir, + pages_db_manager: pages_db_manager + ).build + puts "processed: #{page.filename}" + end + end + + private + + attr_reader :pages_db_manager, :output_dir, :image_dir, :builder_class + + end +end diff --git a/new/lib/crys/builder.rb b/new/lib/crys/builder.rb index a3f55b15..123935a6 100644 --- a/new/lib/crys/builder.rb +++ b/new/lib/crys/builder.rb @@ -1,50 +1,71 @@ +# frozen_string_literal: true module Crys class Builder - - - def initialize(file_path:, output_dir:, image_dir:) + def initialize(file_path:, output_dir:, image_dir:, pages_db_manager:) @file_path = file_path @output_dir = output_dir - @processor = processor_class.new(file_path: file_path, image_dir: image_dir) - + @image_dir = image_dir + @pages_db_manager = pages_db_manager end - def build - process_html + 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 + 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.open(output_path, 'w') { |file| file.write(asset.processed_asset) } + output_path = "#{output_dir}/#{asset.relative_path}" + File.write(output_path, asset.processed_asset) end end - def process_html - html = processor.to_html - filename = processor.filename - output_path = output_dir + "/" + filename + 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 - File.open(output_path, 'w') { |file| file.write(html) } + pages_db_manager.save end - def processor_class - case file_path - when /html.erb$/ - html_processor - else - raise StandardError.new("No processor for #{file_path}") + 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 diff --git a/new/lib/crys/deployer.rb b/new/lib/crys/deployer.rb new file mode 100644 index 00000000..501ea046 --- /dev/null +++ b/new/lib/crys/deployer.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'rsync' + +module Crys + class Deployer + DEST_PREFIX = '/usr/local/sites/' + + def initialize(dest:, source:) + @dest = dest + @source = source + + Rsync.configure do |config| + config.host = 'mms@10.0.7.0' + end + end + + def run + puts "source: #{source}" + full_dest = DEST_PREFIX + dest + puts "dest: #{full_dest}" + + Rsync.run(source, full_dest, ['-rtz']) do |result| + if result.success? + result.changes.each do |change| + puts "#{change.filename} (#{change.summary})" + end + else + puts result.error + end + end + end + + private + + attr_reader :dest, :source + end +end diff --git a/new/lib/crys/image_processor.rb b/new/lib/crys/image_processor.rb index b7e2fd67..306c0769 100644 --- a/new/lib/crys/image_processor.rb +++ b/new/lib/crys/image_processor.rb @@ -1,26 +1,24 @@ +# frozen_string_literal: true + module Crys class ImageProcessor def initialize(path:, filename:, width: :auto) @path = path @filename = filename @file = File.read(path) + @width = width end def relative_path filename end - def filename - filename - end - def processed_asset file.to_s end - private + private - attr_reader :path, :filename, :file - + attr_reader :path, :filename, :file 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 00000000..d8da7e70 --- /dev/null +++ b/new/lib/crys/pages_db_manager.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module Crys + class PagesDbManager + PAGES_KEY = :pages + PAGE = Struct.new( + :filename, + :source, + + :last_updated_at, + :created_at, + + :in_rss, + :in_all, + + keyword_init: true) + + + def initialize(db_path:) + @db_path = db_path + end + + + def add_page(page) + @page = page + hash = Digest::MD5.hexdigest(page.source) + known_page = known_page(hash) + + upsert_db(hash, known_page) + end + + def save + save_db + end + + def pages + db.fetch(:pages).map do |_, data| + PAGE.new(data) + end + end + + private + + attr_reader :hash, :page, :db_path + + def db + @db ||= YAML.load_file( + db_path, + permitted_classes: [Time, Symbol] + ) + end + + def upsert_db(hash, known_page) + record = PAGE.new(known_page || { filename: page.filename, + source: page.source, + created_at: Time.now, + in_rss: true, + in_all: true}) + + record.last_updated_at = page.last_updated_at + + @db[PAGES_KEY][hash] = record.to_h + end + + def save_db + File.write(db_path, db.to_yaml) + end + + def known_page(hash) + db[PAGES_KEY][hash] + 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 1dbf6d82..00000000 --- a/new/lib/crys/processors/common_parts.rb +++ /dev/null @@ -1,20 +0,0 @@ -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 -<a href="<%=url%>"> -<img src="<%=image.relative_path%>" width="88" height="31" class="webbutton" alt="<%=alt%>"> -</a> -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 d5ea73d6..eccc748b 100644 --- a/new/lib/crys/processors/html_processor.rb +++ b/new/lib/crys/processors/html_processor.rb @@ -1,38 +1,48 @@ -require 'erb' -require_relative "common_functions" -require_relative "common_parts" +# frozen_string_literal: true + +require_relative 'mixins/common_functions' +require_relative 'mixins/common_parts' module Crys class HtmlProcessor - include CommonFunctions include CommonParts - + def initialize(file_path:, image_dir:) @file_path = file_path @image_dir = image_dir @assets = [] end - def to_html - html_file = ERB.new(File.read(file_path)) - html_file.result(local_binding) + def parsed_pages + [ + ProcessedPage.new( + filename: filename, + content: html_content, + source: file_path, + last_updated_at: Time.now + ) + ] end - def filename - file_path.scan(/.*\/(.*)\.erb/).flatten.first + 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 + attr_reader :assets, :file_path private - attr_reader :file_path, :image_dir + attr_reader :image_dir attr_writer :assets def local_binding - local_binding ||= binding.clone + @local_binding ||= binding.clone end end end diff --git a/new/lib/crys/processors/common_functions.rb b/new/lib/crys/processors/mixins/common_functions.rb index f33c7681..52ce65ac 100644 --- a/new/lib/crys/processors/common_functions.rb +++ b/new/lib/crys/processors/mixins/common_functions.rb @@ -1,13 +1,15 @@ +# 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__))))))}" + 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 + def process_image(file:, width: :auto) + full_path = "#{image_dir}/#{file}" image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: width) assets << image @@ -19,10 +21,13 @@ module Crys if name path = case name when :main - "/public/index.xml" + '/public/index.xml' end rss = File.read(project_root + path) + else + puts url + end RSS::Parser.parse(rss) 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 + <a href="<%=url%>"><img src="<%=image.relative_path%>" width="88" height="31" class="webbutton" alt="<%=alt%>"></a> + 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..8627c10e --- /dev/null +++ b/new/lib/crys/processors/processed_page.rb @@ -0,0 +1,8 @@ +module Crys + ProcessedPage = Struct.new( + :filename, + :content, + :source, + :last_updated_at + ) +end diff --git a/new/lib/crys/processors/rss_processor.rb b/new/lib/crys/processors/rss_processor.rb new file mode 100644 index 00000000..8254bf4a --- /dev/null +++ b/new/lib/crys/processors/rss_processor.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative 'mixins/common_functions' +require_relative 'mixins/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 = [] + @image_dir = image_dir # just for api consistency + end + + def parsed_pages + [ + ProcessedPage.new( + filename: rss.filename, + content: rss.content, + source: file_path, + last_updated_at: Time.now + ) + ] + end + + def filename + rss.filename + end + + attr_reader :assets, :file_path + + private + + attr_reader :rss + end +end diff --git a/new/lib/crys/processors/yaml_batch_processor.rb b/new/lib/crys/processors/yaml_batch_processor.rb new file mode 100644 index 00000000..ef0257b8 --- /dev/null +++ b/new/lib/crys/processors/yaml_batch_processor.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "yaml" + +require_relative 'mixins/common_functions' +require_relative 'mixins/common_parts' + +module Crys + class YamlBatchProcessor + def initialize(file_path:, image_dir:) + @file_path = file_path + rss_file = File.read(file_path) + + @assets = [] + @image_dir = image_dir # just for api consistency + end + + def parsed_pages + pages.map do |page| + ProcessedPage.new( + filename: page[:filename], + content: "all your base are belong to us", + source: file_path + "#" + page[:uid].to_s , + last_updated_at: page[:last_updated_at] + ) + end + end + + def filename + rss.filename + end + + attr_reader :assets, :file_path + + private + + def pages + @pages ||= YAML.load_file(file_path, + permitted_classes: [Time, Symbol] + ) + end + + attr_reader :rss + end +end diff --git a/new/lib/crys/server.rb b/new/lib/crys/server.rb index aa0c19c3..cc43b396 100644 --- a/new/lib/crys/server.rb +++ b/new/lib/crys/server.rb @@ -1,4 +1,6 @@ -require 'webrick' +# frozen_string_literal: true + +require 'webrick' module Crys class Server def initialize(root:) @@ -6,16 +8,16 @@ module Crys end def start - server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root - trap 'INT' do server.shutdown end + server = WEBrick::HTTPServer.new Port: 8000, DocumentRoot: root + trap 'INT' do + server.shutdown + end server.start end - private - - attr_reader :root + private + attr_reader :root end end - |