From a3b86375c946299c9216412e10c5bcf5234c7fcd Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Fri, 28 Jun 2019 19:12:42 +0200 Subject: Initial commit --- lib/blog.rb | 33 +++++++++++++++++++++++++++++++++ lib/pather.rb | 38 ++++++++++++++++++++++++++++++++++++++ lib/statics.rb | 7 +++++++ lib/templated.rb | 24 ++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 lib/blog.rb create mode 100644 lib/pather.rb create mode 100644 lib/statics.rb create mode 100644 lib/templated.rb (limited to 'lib') diff --git a/lib/blog.rb b/lib/blog.rb new file mode 100644 index 0000000..e4403a3 --- /dev/null +++ b/lib/blog.rb @@ -0,0 +1,33 @@ +require 'date' + +def parse_post filename + file_raw = File.read(src filename).force_encoding('UTF-8').split('---') + post = YAML.load file_raw[0] + post['content'] = file_raw[1].strip + post['filename'] = filename + post +end + +def generate_blog_post post + ERB.new(File.read(src 'post-template.html.erb')).result binding +end + +def posts + Dir.new(src 'blog').children.map do |filename| + post = parse_post "blog/#{filename}" + end.sort do |a, b| + Date.parse(b['date']) <=> Date.parse(a['date']) + end +end + +def write_blog_files + posts.each do |post| + content = ERB.new(File.read(src 'post-template.html.erb')).result binding + write_templated content, post['filename'], head_title: post['title'], h1_title: 'Blog.' + end +end + +def write_blog_archive + content = ERB.new(File.read(src 'blog.html.erb')).result + write_templated content, 'blog.html', title: 'Blog.' +end diff --git a/lib/pather.rb b/lib/pather.rb new file mode 100644 index 0000000..5ce7d02 --- /dev/null +++ b/lib/pather.rb @@ -0,0 +1,38 @@ +class Pather + def initialize + @assets = {} + @current_path = [] + end + + def add asset_id, path + @assets[asset_id] = @current_path + path.split('/') + end + + def path_to asset_id + other_path = @assets[asset_id] + find_path_between @current_path, other_path + end + + def find_path_between from, to + from, to = drop_common_prefix from, to + path = ('../' * from.length) + to.join('/') + end + + def drop_common_prefix path1, path2 + common_prefix_length = path1.zip(path2).take_while do |(segment1, segment2)| + segment1 == segment2 + end.length + + return path1[common_prefix_length..], path2[common_prefix_length..] + end + + def cd path + path.split('/').each do |segment| + if segment == '..' + @current_path.pop + else + @current_path.push segment + end + end + end +end diff --git a/lib/statics.rb b/lib/statics.rb new file mode 100644 index 0000000..3865439 --- /dev/null +++ b/lib/statics.rb @@ -0,0 +1,7 @@ +def write_statics statics + statics = statics.map do | filename | + src filename + end + + FileUtils.cp statics, BuildDir +end diff --git a/lib/templated.rb b/lib/templated.rb new file mode 100644 index 0000000..a7e0d63 --- /dev/null +++ b/lib/templated.rb @@ -0,0 +1,24 @@ +def template + ERB.new(File.read(src 'template.html.erb')) +end + +def write_templated_file content_filename, options + content = File.read(src content_filename) + write_templated content, content_filename, options +end + +def write_templated_erb erb_filename, options + content = ERB.new(File.read(src erb_filename)).result + cut_filename = erb_filename.sub /\.erb$/, '' + write_templated content, cut_filename, options +end + +# Options should include +# head_title: used for tag +# h1_title: used for <h1> on top of page +# title: used for both of the above, can be overridden with either +def write_templated content, filename, options + head_title = options[:head_title] || options[:title] || '' + h1_title = options[:h1_title] || options[:title] || '' + File.write build(filename), template.result(binding) +end -- cgit v1.2.3