summaryrefslogtreecommitdiff
path: root/new/lib
diff options
context:
space:
mode:
Diffstat (limited to 'new/lib')
-rw-r--r--new/lib/crys.rb11
-rw-r--r--new/lib/crys/batch_builder.rb34
-rw-r--r--new/lib/crys/builder.rb62
-rw-r--r--new/lib/crys/code_processor.rb0
-rw-r--r--new/lib/crys/default_data.rb4
-rw-r--r--new/lib/crys/file_watcher.rb0
-rw-r--r--new/lib/crys/image_processor.rb20
-rw-r--r--new/lib/crys/listing.rb0
-rw-r--r--new/lib/crys/new_file.rb0
-rw-r--r--new/lib/crys/page_from_erb.rb0
-rw-r--r--new/lib/crys/page_from_org.rb0
-rw-r--r--new/lib/crys/pages_db_manager.rb62
-rw-r--r--new/lib/crys/processors/common_functions.rb31
-rw-r--r--new/lib/crys/processors/common_parts.rb20
-rw-r--r--new/lib/crys/processors/html_processor.rb37
-rw-r--r--new/lib/crys/processors/rss_processor.rb28
-rw-r--r--new/lib/crys/renderer.rb32
-rw-r--r--new/lib/crys/resource_from_css.rb0
18 files changed, 303 insertions, 38 deletions
diff --git a/new/lib/crys.rb b/new/lib/crys.rb
index 333fcfb..bd0d298 100644
--- a/new/lib/crys.rb
+++ b/new/lib/crys.rb
@@ -1,4 +1,15 @@
+require 'erb'
+require 'rss'
+
+require_relative "crys/processors/html_processor.rb"
+require_relative "crys/processors/rss_processor.rb"
+
require_relative "crys/server.rb"
+require_relative "crys/builder.rb"
+require_relative "crys/batch_builder.rb"
+require_relative "crys/image_processor.rb"
+require_relative "crys/pages_db_manager.rb"
module Crys
end
+
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
new file mode 100644
index 0000000..b8135c7
--- /dev/null
+++ b/new/lib/crys/builder.rb
@@ -0,0 +1,62 @@
+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_content
+ process_assets
+
+ upsert_pages_db
+ end
+
+ private
+
+ 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
+ File.open(output_path, 'w') { |file| file.write(asset.processed_asset) }
+ end
+ end
+
+ def process_content
+ content = processor.parsed_content
+ filename = processor.filename
+ output_path = output_dir + "/" + filename
+
+ File.open(output_path, 'w') { |file| file.write(content) }
+ end
+
+ 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
+
+ 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/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/image_processor.rb b/new/lib/crys/image_processor.rb
index 1a33d04..b7e2fd6 100644
--- a/new/lib/crys/image_processor.rb
+++ b/new/lib/crys/image_processor.rb
@@ -1,10 +1,26 @@
module Crys
class ImageProcessor
- def initialize(path:, width:, height:, format:)
+ def initialize(path:, filename:, width: :auto)
+ @path = path
+ @filename = filename
+ @file = File.read(path)
end
- def path
+ def relative_path
+ filename
+ end
+
+ def filename
+ filename
+ end
+ def processed_asset
+ file.to_s
end
+
+ private
+
+ attr_reader :path, :filename, :file
+
end
end
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/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/common_functions.rb b/new/lib/crys/processors/common_functions.rb
new file mode 100644
index 0000000..f33c768
--- /dev/null
+++ b/new/lib/crys/processors/common_functions.rb
@@ -0,0 +1,31 @@
+require 'rss'
+
+module Crys
+ module CommonFunctions
+ def project_root
+ "#{File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))))}"
+ 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)
+ 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
new file mode 100644
index 0000000..1dbf6d8
--- /dev/null
+++ b/new/lib/crys/processors/common_parts.rb
@@ -0,0 +1,20 @@
+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
new file mode 100644
index 0000000..bcc9768
--- /dev/null
+++ b/new/lib/crys/processors/html_processor.rb
@@ -0,0 +1,37 @@
+require 'erb'
+require_relative "common_functions"
+require_relative "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 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, :file_path
+
+ private
+
+ attr_reader :image_dir
+ attr_writer :assets
+
+ def local_binding
+ local_binding ||= binding.clone
+ end
+ end
+end
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
diff --git a/new/lib/crys/renderer.rb b/new/lib/crys/renderer.rb
deleted file mode 100644
index c490330..0000000
--- a/new/lib/crys/renderer.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-require 'erb'
-require 'pry'
-
-module Crys
- module Splash
- class Theme
- PARENT_DIR = File.expand_path(".", Dir.pwd)
-
- def render
- template = ERB.new(File.read(PARENT_DIR + "/theme/" + self.class::PAGE))
- template.result(local_binding)
- end
-
- def body
- self.class::BODY.result(local_binding)
- end
-
- private
-
- attr_accessor :template, :data
-
- def local_binding
- local_binding = binding.clone
- self.class::DATA.each do |k, v|
- local_binding.local_variable_set(k,v)
- end
- local_binding
-
- end
- end
- end
-end
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