From 75402b673c0f630b620904ce3153f8645d89d700 Mon Sep 17 00:00:00 2001
From: nsfisis
Date: Sun, 20 Nov 2022 21:42:50 +0900
Subject: implement cache busting for stylesheets
---
NOTE.md | 5 +
lib/command.rb | 19 ++-
lib/html_converter.rb | 16 +++
lib/parser.rb | 4 +-
nuldoc.rb | 4 +
public/posts/2021-03-05/my-first-post/index.html | 17 ++-
public/posts/2021-03-30/phperkaigi-2021/index.html | 111 ++--------------
.../index.html | 10 +-
.../python-unbound-local-error/index.html | 10 +-
.../ruby-detect-running-implementation/index.html | 10 +-
.../ruby-then-keyword-and-case-in/index.html | 40 +-----
.../rust-where-are-primitive-types-from/index.html | 25 +---
.../index.html | 35 +----
.../vim-swap-order-of-selected-lines/index.html | 52 ++------
.../2022-04-09/phperkaigi-2022-tokens/index.html | 126 ++----------------
.../index.html | 37 ++----
public/posts/2022-05-01/phperkaigi-2022/index.html | 49 ++-----
.../php-conference-okinawa-code-golf/index.html | 54 ++------
.../index.html | 27 ++--
.../index.html | 74 ++---------
.../phperkaigi-2023-unused-token-quiz-1/index.html | 37 ++----
.../setup-server-for-this-site/index.html | 143 ++-------------------
.../phperkaigi-2023-unused-token-quiz-2/index.html | 37 ++----
public/posts/index.html | 20 +--
public/tags/conference/index.html | 20 +--
public/tags/cpp/index.html | 20 +--
public/tags/cpp17/index.html | 20 +--
public/tags/note-to-self/index.html | 20 +--
public/tags/php/index.html | 20 +--
public/tags/phpcon/index.html | 20 +--
public/tags/phperkaigi/index.html | 20 +--
public/tags/python/index.html | 20 +--
public/tags/python3/index.html | 20 +--
public/tags/ruby/index.html | 20 +--
public/tags/ruby3/index.html | 20 +--
public/tags/rust/index.html | 20 +--
public/tags/vim/index.html | 20 +--
templates/document.html.erb | 69 ----------
templates/document__post.html.erb | 69 ++++++++++
templates/posts_list.html.erb | 8 +-
templates/tag.html.erb | 8 +-
41 files changed, 367 insertions(+), 1009 deletions(-)
create mode 100644 lib/html_converter.rb
delete mode 100644 templates/document.html.erb
create mode 100644 templates/document__post.html.erb
diff --git a/NOTE.md b/NOTE.md
index 7adbcb2..8cc3a17 100644
--- a/NOTE.md
+++ b/NOTE.md
@@ -20,6 +20,7 @@ $ touch content/posts/$(date +'%Y-%m-%d')/[TITLE].md
* [x] Add /posts/ page
* [x] Stylesheets
* [x] Syntax highlight
+* [ ] Add /tags/ page
* [ ] Add / page
* [ ] Add /about/ page
* [ ] Add navigation bar
@@ -46,6 +47,10 @@ $ touch content/posts/$(date +'%Y-%m-%d')/[TITLE].md
* https://www.sitemaps.org/protocol.html
* https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap?hl=ja
* [ ] Admonitions
+* [x] Stylesheet cache busting
+* [ ] templates
+ * Define custom converter using ERB
+
## Structure
diff --git a/lib/command.rb b/lib/command.rb
index 81692b3..5662eec 100644
--- a/lib/command.rb
+++ b/lib/command.rb
@@ -4,9 +4,11 @@ module NulDoc
@config = config
@content_dir = @config[:content_dir]
@dest_dir = @config[:dest_dir]
+ @static_dir = @config[:static_dir]
@template_dir = @config[:template_dir]
@parser = NulDoc::Parser.new(
{
+ 'stylesheets' => stylesheets,
'author' => @config[:author],
'site-copyright-year' => @config[:site_copyright_year],
'site-name' => @config[:site_name],
@@ -43,7 +45,7 @@ module NulDoc
end
def parse_posts(post_file_paths)
- post_file_paths.map { @parser.parse_file(_1) }
+ post_file_paths.map { @parser.parse_file(_1, 'post') }
end
def output_posts(posts)
@@ -92,8 +94,9 @@ module NulDoc
end
def build_tag_doc(tag, posts)
- erb = ERB.new(File.read(@template_dir + '/tag.html.erb'))
+ erb = ERB.new(File.read(@template_dir + '/tag.html.erb'), trim_mode: '<>')
erb.result_with_hash({
+ stylesheets: stylesheets,
tag: tag,
posts: posts,
author: @config[:author],
@@ -124,8 +127,9 @@ module NulDoc
end
def build_posts_list_doc(posts)
- erb = ERB.new(File.read(@template_dir + '/posts_list.html.erb'))
+ erb = ERB.new(File.read(@template_dir + '/posts_list.html.erb'), trim_mode: '<>')
erb.result_with_hash({
+ stylesheets: stylesheets,
posts: posts.reverse,
author: @config[:author],
site_copyright_year: @config[:site_copyright_year],
@@ -148,5 +152,14 @@ module NulDoc
f.puts(html)
end
end
+
+ def stylesheets
+ stylesheet_file_names = %w[hl.css style.css custom.css]
+ stylesheet_file_names.map {|ss_file_name|
+ ss_file_path = "#{@static_dir}/#{ss_file_name}"
+ hash = Digest::MD5.file(ss_file_path).hexdigest
+ "/#{ss_file_name}?#{hash}"
+ }
+ end
end
end
diff --git a/lib/html_converter.rb b/lib/html_converter.rb
new file mode 100644
index 0000000..126d72a
--- /dev/null
+++ b/lib/html_converter.rb
@@ -0,0 +1,16 @@
+module NulDoc
+ class HTMLConverter < (Asciidoctor::Converter.for 'html5')
+ register_for 'html5'
+
+ def initialize(backend, opts)
+ super
+ @template_dir = opts[:template_dirs].first
+ end
+
+ def convert_document(node)
+ template_file_name = "document__#{node.attr('document-type')}.html.erb"
+ erb = Tilt::ERBTemplate.new("#{@template_dir}/#{template_file_name}")
+ erb.render(node, {})
+ end
+ end
+end
diff --git a/lib/parser.rb b/lib/parser.rb
index 8ae3303..e644dc6 100644
--- a/lib/parser.rb
+++ b/lib/parser.rb
@@ -6,7 +6,7 @@ module NulDoc
@template_dir = template_dir
end
- def parse_file(file_path)
+ def parse_file(file_path, document_type)
Asciidoctor.load_file(
file_path,
backend: :html5,
@@ -15,9 +15,11 @@ module NulDoc
safe: :unsafe,
template_dirs: [@template_dir],
template_engine: 'erb',
+ template_engine_options: { erb: { trim: '<>' } },
attributes: @common_attributes.merge({
'source-file-path' => file_path,
'href' => file_path.sub(@content_dir, '').sub('.adoc', '/'),
+ 'document-type' => document_type,
}),
extension_registry: Asciidoctor::Extensions.create do
tree_processor Nuldoc::Extensions::RevisionHistoryProcessor
diff --git a/nuldoc.rb b/nuldoc.rb
index 363383e..c702981 100644
--- a/nuldoc.rb
+++ b/nuldoc.rb
@@ -1,8 +1,11 @@
require 'date'
+require 'digest/md5'
require 'fileutils'
+
require 'asciidoctor'
require_relative 'lib/command'
+require_relative 'lib/html_converter'
require_relative 'lib/parser'
require_relative 'lib/extensions/document_title_processor'
require_relative 'lib/extensions/lang_attribute_processor'
@@ -20,5 +23,6 @@ NulDoc::Command.new({
site_copyright_year: 2021,
content_dir: __dir__ + '/content',
dest_dir: __dir__ + '/public',
+ static_dir: __dir__ + '/static',
template_dir: __dir__ + '/templates',
}).run
diff --git a/public/posts/2021-03-05/my-first-post/index.html b/public/posts/2021-03-05/my-first-post/index.html
index fbf7237..6ca0ae8 100644
--- a/public/posts/2021-03-05/my-first-post/index.html
+++ b/public/posts/2021-03-05/my-first-post/index.html
@@ -9,9 +9,13 @@
My First Post | REPL: Rest-Eat-Program Loop
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-