Pull out configuration into separate file,update homepage post count, default post index page

This commit is contained in:
Bradley Taunt 2024-07-23 09:36:35 -04:00
parent f7f421dd3a
commit d2a6023d2b
6 changed files with 72 additions and 21 deletions

View file

@ -18,7 +18,7 @@ gem install kramdown rss
## Getting Started
Make your changes in the top of the main `wruby.rb` file (site URL, your name,
Make your changes in the main configuration file `_config.yml` file (site URL, your name,
etc.). Remove the `.build.yml` unless you plan to host with sourcehut pages,
otherwise edit this file with your own details.
@ -28,6 +28,12 @@ otherwise edit this file with your own details.
* Pages go under the `pages` directory as markdown files
* Media (images, videos etc) go in the root `public` directory
## Defaults
* The homepage only displays the first `5` posts. You can configure this in `_config.yml` under `post_count`.
* The full blog post index will be generated at `yoursite.com/posts`
* This means you need to have a `posts.md` file in your `pages` directory (or change `posts_index` the core `_config.yml`)
## Running
1. Run `make build` in the root directory

21
_config.yml Normal file
View file

@ -0,0 +1,21 @@
site_url: 'https://wruby.btxx.org'
site_name: 'wruby'
author_name: 'Bradley Taunt'
directories:
posts: 'posts'
pages: 'pages'
public: 'public'
output: 'build'
posts_output: 'build/posts'
pages_output: 'build/'
files:
header: 'header.html'
footer: 'footer.html'
root_index: 'index.md'
posts_index: 'pages/posts.md'
rss: 'build/index.rss'
misc:
post_count: 5

View file

@ -4,6 +4,7 @@
<h2>Menu Navigation</h2>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/posts">Posts</a></li>
<li><a href="/about">About</a></li>
<li><a href="/websites">Websites</a></li>
<li><a href="/index.rss">RSS</a></li>

View file

@ -6,8 +6,7 @@
<meta name="color-scheme" content="dark light">
<link rel="icon" href="data:,">
<title>{{TITLE}}</title>
<link href="/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
<link href="/rss.xml" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" />
<link href="/index.rss" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" />
<style>*{box-sizing:border-box;}body{font-family:sans-serif;line-height:1.33;margin:0
auto;max-width:650px;padding:1rem;}blockquote{border-left:4px
solid;padding-left:5px;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.posts,#menu{list-style:none;padding:0;}.posts li{margin-bottom:8px;}.posts li span{display:block;font-size:90%;}#menu li{display:inline-block;margin-right:8px;}.footnotes{font-size:90%;}</style>

3
pages/posts.md Normal file
View file

@ -0,0 +1,3 @@
# All Posts
This page contains a list of all your blog posts.

View file

@ -4,24 +4,29 @@ require 'date'
require 'time'
require 'rss'
require 'find'
require 'yaml'
# Configuration all the things!
site_url = 'https://wruby.btxx.org'
site_name = 'wruby'
author_name = 'Bradley Taunt'
# Load configuration
config = YAML.load_file('_config.yml')
posts_dir = 'posts'
pages_dir = 'pages'
public_dir = 'public'
site_url = config['site_url']
site_name = config['site_name']
author_name = config['author_name']
output_dir = 'build'
posts_output_dir = "#{output_dir}/posts"
pages_output_dir = "#{output_dir}/"
posts_dir = config['directories']['posts']
pages_dir = config['directories']['pages']
public_dir = config['directories']['public']
output_dir = config['directories']['output']
posts_output_dir = config['directories']['posts_output']
pages_output_dir = config['directories']['pages_output']
header_file = 'header.html'
footer_file = 'footer.html'
root_index_file = 'index.md'
rss_file = "#{output_dir}/index.rss"
header_file = config['files']['header']
footer_file = config['files']['footer']
root_index_file = config['files']['root_index']
posts_index_file = config['files']['posts_index']
rss_file = config['files']['rss']
post_count = config['misc']['post_count']
# Make sure output directories exist
[posts_output_dir, pages_output_dir].each { |dir| FileUtils.mkdir_p(dir) }
@ -69,7 +74,7 @@ def process_markdown_files(input_directory, output_directory, header_content, fo
end
# Create the root index file
def generate_index(posts, header_content, footer_content, root_index_file, output_dir, posts_dir)
def generate_index(posts, header_content, footer_content, root_index_file, post_count, output_dir, posts_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
@ -77,12 +82,27 @@ def generate_index(posts, header_content, footer_content, root_index_file, outpu
header = replace_title_placeholder(header_content, root_title)
index_content = header + root_html + "<ul class=\"posts\">\n"
posts.each { |post| index_content << "<li><span>#{post[:date]}</span><a href='/#{posts_dir}/#{post[:link]}'>#{post[:title]}</a></li>\n" }
index_content << "</ul>\n" + footer_content
posts.first(post_count).each { |post| index_content << "<li><span>#{post[:date]}</span><a href='/#{posts_dir}/#{post[:link]}'>#{post[:title]}</a></li>\n" }
index_content << "</ul>\n<p><a href='/#{posts_dir}'>View all posts &rarr;</a></p>\n" + footer_content
File.write("#{output_dir}/index.html", index_content)
end
# Create the full posts list page
def generate_full_posts_list(posts, header_content, footer_content, posts_index_file, output_dir, posts_dir)
posts_index_content = File.read(posts_index_file)
posts_title = extract_title_from_md(posts_index_content.lines)
posts_html = Kramdown::Document.new(posts_index_content).to_html
header = replace_title_placeholder(header_content, posts_title)
list_content = header + posts_html + "<ul class=\"posts\">\n"
posts.each { |post| list_content << "<li><span>#{post[:date]}</span><a href='/#{posts_dir}/#{post[:link]}'>#{post[:title]}</a></li>\n" }
list_content << "</ul>\n" + footer_content
File.write("#{output_dir}/posts/index.html", list_content)
end
# Generate the RSS 2.0 feed
def generate_rss(posts, rss_file, author_name, site_name, site_url, posts_dir)
rss = RSS::Maker.make("2.0") do |maker|
@ -117,7 +137,8 @@ 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, posts_dir)
generate_index(posts, header_content, footer_content, root_index_file, post_count, output_dir, posts_dir)
generate_full_posts_list(posts, header_content, footer_content, posts_index_file, output_dir, posts_dir)
FileUtils.cp_r(public_dir, output_dir)
generate_rss(posts, rss_file, author_name, site_name, site_url, posts_dir)