aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.build.yml2
-rw-r--r--blog.rb172
2 files changed, 59 insertions, 115 deletions
diff --git a/.build.yml b/.build.yml
index 0e29619..8cc2ce7 100644
--- a/.build.yml
+++ b/.build.yml
@@ -10,7 +10,7 @@ sources:
- https://git.sr.ht/~bt/btxx.org
tasks:
- install-gems: |
- sudo gem install kramdown fileutils rss
+ sudo gem install kramdown rss
- build: |
cd btxx.org
make build
diff --git a/blog.rb b/blog.rb
index 2841dee..0ccf450 100644
--- a/blog.rb
+++ b/blog.rb
@@ -3,168 +3,112 @@ require 'fileutils'
require 'date'
require 'rss'
-# Define all the things
+# Configuration all the things!
site_url = 'https://bt.srht.site'
site_name = 'btxx.org'
author_name = 'Bradley Taunt'
posts_dir = 'posts'
pages_dir = 'pages'
-wiki_dir = 'pages/wiki'
-recipes_dir = 'pages/recipes'
public_dir = 'public'
output_dir = 'build'
posts_output_dir = "#{output_dir}/posts"
pages_output_dir = "#{output_dir}/"
-wiki_output_dir = "#{output_dir}/wiki/"
-recipes_output_dir = "#{output_dir}/recipes/"
header_file = 'header.html'
footer_file = 'footer.html'
root_index_file = 'index.md'
-rss_file = "#{output_dir}/rss.xml"
+rss_file = "#{output_dir}/index.atom"
# Make sure output directories exist
-FileUtils.mkdir_p(posts_output_dir)
-FileUtils.mkdir_p(pages_output_dir)
-FileUtils.mkdir_p(wiki_output_dir)
-FileUtils.mkdir_p(recipes_output_dir)
+[posts_output_dir, pages_output_dir].each { |dir| FileUtils.mkdir_p(dir) }
-# Read footer
+# Read the footer content
footer_content = File.read(footer_file)
-# Initialize an array to hold post and page data
-posts = []
-pages = []
-wiki = []
-recipes = []
-
-# Replace title placeholder
+# Replace the title meta tag in the header.html
def replace_title_placeholder(header_content, title)
header_content.gsub('<title>{{TITLE}}</title>', "<title>#{title}</title>")
end
-# Grab title from the first line of a Markdown file
+# Grab the title from each markdown file
def extract_title_from_md(lines)
- first_line = lines[0]
- if first_line && first_line.start_with?('# ')
- title = first_line[2..-1].strip
- else
- title = 'Blog Index'
- end
- title
+ first_line = lines.first
+ first_line&.start_with?('# ') ? first_line[2..-1].strip : 'Blog Index'
end
-# Process Markdown files
-def process_markdown_files(input_directory, output_directory, content_array, header_file, footer_content)
- Dir.glob("#{input_directory}/*.md").each do |md_file|
- # Read the content of the Markdown file
+# Convert markdown files
+def process_markdown_files(input_directory, output_directory, header_content, footer_content)
+ items = []
+
+ Dir.glob("#{input_directory}/**/*.md").each do |md_file|
md_content = File.read(md_file)
lines = md_content.lines
- # Grab title from the first line
title = extract_title_from_md(lines)
-
- # Grab date from the third line
- third_line = lines[2]
- if third_line
- date = Date.parse(third_line.strip) rescue Date.today
- else
- date = Date.today
- end
-
- # Convert markdown to HTML
+ date = Date.parse(lines[2]&.strip || '') rescue Date.today
html_content = Kramdown::Document.new(md_content).to_html
- # Create output directory and file name
- file_name = File.basename(md_file, '.md')
- item_dir = "#{output_directory}/#{file_name}"
+ relative_path = md_file.sub(input_directory + '/', '').sub('.md', '')
+ item_dir = File.join(output_directory, relative_path)
output_file = "#{item_dir}/index.html"
-
- # Make sure item directory exists
FileUtils.mkdir_p(item_dir)
- # Read and replace title in header content
- header_content = replace_title_placeholder(File.read(header_file), title)
-
- # Write to output file and add header and footer templates
- File.open(output_file, 'w') do |file|
- file.write(header_content)
- file.write(html_content)
- file.write(footer_content)
- end
+ header = replace_title_placeholder(header_content, title)
+ File.write(output_file, header + html_content + footer_content)
- # Add item information to the array
- content_array << { title: title, date: date, link: "#{output_directory.split('/').last}/#{file_name}/", content: html_content }
+ items << { title: title, date: date, link: relative_path + '/', content: html_content }
end
-end
-
-# Process markdown files in posts and pages directories
-process_markdown_files(posts_dir, posts_output_dir, posts, header_file, footer_content)
-process_markdown_files(pages_dir, pages_output_dir, pages, header_file, footer_content)
-process_markdown_files(wiki_dir, wiki_output_dir, wiki, header_file, footer_content)
-process_markdown_files(recipes_dir, recipes_output_dir, recipes, header_file, footer_content)
-# Sort posts by date from newest to oldest
-posts.sort_by! { |post| -post[:date].to_time.to_i }
-
-# Read the root index.md file
-root_index_content = File.read(root_index_file)
-root_index_lines = root_index_content.lines
-
-# Grab title from root index.md
-root_title = extract_title_from_md(root_index_lines)
-
-# Convert root index.md to HTML
-root_index_html = Kramdown::Document.new(root_index_content).to_html
-
-# Read and replace title in header content for the root index
-header_content = replace_title_placeholder(File.read(header_file), root_title)
-
-# Build out index content with header and root index content
-index_content = header_content
-index_content << root_index_html
-index_content << "<ul class=\"posts\">\n"
-
-# Add posts to root index
-posts.each do |post|
- index_content << "<li><span>#{post[:date]}</span><a href='#{post[:link]}'>#{post[:title]}</a></li>\n"
+ items
end
-# Finalize index content with footer
-index_content << "</ul>\n"
-index_content << footer_content
+# Create the root index file
+def generate_index(posts, header_content, footer_content, root_index_file, output_dir)
+ root_index_content = File.read(root_index_file)
+ root_title = extract_title_from_md(root_index_content.lines)
+ root_html = Kramdown::Document.new(root_index_content).to_html
-# Copy the public directory to the build directory
-FileUtils.cp_r("#{public_dir}", output_dir)
+ header = replace_title_placeholder(header_content, root_title)
-# Write the index file
-File.open("#{output_dir}/index.html", 'w') do |file|
- file.write(index_content)
+ index_content = header + root_html + "<ul class=\"posts\">\n"
+ posts.each { |post| index_content << "<li><span>#{post[:date]}</span><a href='#{post[:link]}'>#{post[:title]}</a></li>\n" }
+ index_content << "</ul>\n" + footer_content
+
+ File.write("#{output_dir}/index.html", index_content)
end
-# Generate RSS feed
-rss = RSS::Maker.make("atom") do |maker|
- maker.channel.author = "#{author_name}"
- maker.channel.updated = Time.now.to_s
- maker.channel.about = "#{site_name}"
- maker.channel.title = "#{site_name} RSS Feed"
-
- posts.each do |post|
- maker.items.new_item do |item|
- item.link = "#{site_url}/#{post[:link]}"
- item.title = post[:title]
- item.updated = post[:date].to_s
- item.content.type = 'html'
- item.content.content = post[:content]
+# Generate the RSS feed
+def generate_rss(posts, rss_file, author_name, site_name, site_url)
+ rss = RSS::Maker.make("atom") do |maker|
+ maker.channel.author = author_name
+ maker.channel.updated = Time.now.to_s
+ maker.channel.about = site_name
+ maker.channel.title = "#{site_name} RSS Feed"
+
+ posts.each do |post|
+ maker.items.new_item do |item|
+ item.link = "#{site_url}/#{post[:link]}"
+ item.title = post[:title]
+ item.updated = post[:date].to_s
+ item.content.type = 'html'
+ item.content.content = post[:content]
+ end
end
end
-end
-# Write RSS file
-File.open(rss_file, 'w') do |file|
- file.write(rss)
+ File.write(rss_file, rss)
end
+# Process header, posts, pages, etc.
+header_content = File.read(header_file)
+
+posts = process_markdown_files(posts_dir, posts_output_dir, header_content, footer_content).sort_by { |post| -post[:date].to_time.to_i }
+pages = process_markdown_files(pages_dir, pages_output_dir, header_content, footer_content)
+
+generate_index(posts, header_content, footer_content, root_index_file, output_dir)
+FileUtils.cp_r(public_dir, output_dir)
+generate_rss(posts, rss_file, author_name, site_name, site_url)
+
puts "Blog built successfully in '#{output_dir}' folder. Have a great day!"