From af8b61cf2816ea330ee35b1e3fcb3d2b24a0f05d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 20 Nov 2022 22:36:04 +0900 Subject: refactor: organize template files --- NOTE.md | 2 +- lib/command.rb | 91 +++++++++++++++++++++------------- lib/parser.rb | 5 +- templates/document__post.html.erb | 9 ++-- templates/document__post_list.html.erb | 58 ++++++++++++++++++++++ templates/document__tag.html.erb | 58 ++++++++++++++++++++++ templates/posts_list.html.erb | 59 ---------------------- templates/tag.html.erb | 59 ---------------------- 8 files changed, 182 insertions(+), 159 deletions(-) create mode 100644 templates/document__post_list.html.erb create mode 100644 templates/document__tag.html.erb delete mode 100644 templates/posts_list.html.erb delete mode 100644 templates/tag.html.erb diff --git a/NOTE.md b/NOTE.md index 8cc3a17..dd60fbd 100644 --- a/NOTE.md +++ b/NOTE.md @@ -48,7 +48,7 @@ $ touch content/posts/$(date +'%Y-%m-%d')/[TITLE].md * https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap?hl=ja * [ ] Admonitions * [x] Stylesheet cache busting -* [ ] templates +* [x] templates * Define custom converter using ERB diff --git a/lib/command.rb b/lib/command.rb index 5662eec..dc2561a 100644 --- a/lib/command.rb +++ b/lib/command.rb @@ -12,9 +12,6 @@ module NulDoc 'author' => @config[:author], 'site-copyright-year' => @config[:site_copyright_year], 'site-name' => @config[:site_name], - 'source-highlighter' => 'rouge', - 'reproducible' => true, - 'sectids' => false, }, @content_dir, @template_dir, @@ -24,7 +21,7 @@ module NulDoc def run posts = generate_posts(@content_dir + '/posts') generate_tags(posts) - generate_posts_list(posts) + generate_post_list(posts) end private @@ -94,18 +91,32 @@ module NulDoc end def build_tag_doc(tag, posts) - 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], - site_copyright_year: @config[:site_copyright_year], - site_name: @config[:site_name], - lang: 'ja-JP', # TODO - copyright_year: posts.last.attributes['revision-history'].first.date.year, - description: "タグ「#{tag.label}」のついた記事一覧", - }) + converter = NulDoc::HTMLConverter.new(nil, { template_dirs: [@template_dir] }) + converter.convert_document( + (Class.new do + def initialize(config, tag, posts, stylesheets) + @config = config + @tag = tag + @posts = posts + @stylesheets = stylesheets + end + def attr(name) + case name + when 'document-type'; 'tag' + when 'stylesheets'; @stylesheets + when 'author'; @config[:author] + when 'site-copyright-year'; @config[:site_copyright_year] + when 'site-name'; @config[:site_name] + when 'lang'; 'ja-JP' # TODO + when 'copyright-year'; @posts.last.attributes['revision-history'].first.date.year + when 'description'; "タグ「#{@tag.label}」のついた記事一覧" + else raise "Unknown attr: #{name}" + end + end + def title; @tag.label; end + def posts; @posts; end + end).new(@config, tag, posts, stylesheets) + ) end def output_tags(tag_docs) @@ -121,28 +132,40 @@ module NulDoc end end - def generate_posts_list(posts) - html = build_posts_list_doc(posts) - output_posts_list(html) + def generate_post_list(posts) + html = build_post_list_doc(posts) + output_post_list(html) end - def build_posts_list_doc(posts) - 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], - site_name: @config[:site_name], - lang: 'ja-JP', # TODO - copyright_year: @config[:site_copyright_year], - description: "記事一覧", - doctitle: "Posts", - header_title: "Posts", - }) + def build_post_list_doc(posts) + converter = NulDoc::HTMLConverter.new(nil, { template_dirs: [@template_dir] }) + converter.convert_document( + (Class.new do + def initialize(config, posts, stylesheets) + @config = config + @posts = posts + @stylesheets = stylesheets + end + def attr(name) + case name + when 'document-type'; 'post_list' + when 'stylesheets'; @stylesheets + when 'author'; @config[:author] + when 'site-copyright-year'; @config[:site_copyright_year] + when 'site-name'; @config[:site_name] + when 'lang'; 'ja-JP' # TODO + when 'copyright-year'; @config[:site_copyright_year] + when 'description'; '記事一覧' + else raise "Unknown attr: #{name}" + end + end + def title; 'Posts'; end + def posts; @posts; end + end).new(@config, posts.reverse, stylesheets) + ) end - def output_posts_list(html) + def output_post_list(html) destination_file_path = "#{@dest_dir}/posts/index.html" destination_dir = File.dirname(destination_file_path) unless Dir.exist?(destination_dir) diff --git a/lib/parser.rb b/lib/parser.rb index e644dc6..f241c4d 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -17,9 +17,12 @@ module NulDoc template_engine: 'erb', template_engine_options: { erb: { trim: '<>' } }, attributes: @common_attributes.merge({ + 'document-type' => document_type, 'source-file-path' => file_path, 'href' => file_path.sub(@content_dir, '').sub('.adoc', '/'), - 'document-type' => document_type, + 'source-highlighter' => 'rouge', + 'reproducible' => true, + 'sectids' => false, }), extension_registry: Asciidoctor::Extensions.create do tree_processor Nuldoc::Extensions::RevisionHistoryProcessor diff --git a/templates/document__post.html.erb b/templates/document__post.html.erb index bc9a842..a52ac9a 100644 --- a/templates/document__post.html.erb +++ b/templates/document__post.html.erb @@ -4,11 +4,10 @@ <% _lang = attr 'lang' %> <% _site_copyright_year = attr 'site-copyright-year' %> <% _copyright_year = attr 'copyright-year' %> -<% _revisions = attr 'revision-history' %> <% _site_name = attr 'site-name' %> +<% _title = title %> +<% _revisions = attr 'revision-history' %> <% _tags = attr 'tags' %> -<% _doctitle = doctitle %> -<% _header_title = header.title %> <% _content = content %> @@ -20,7 +19,7 @@ - <%= _doctitle %> | <%= _site_name %> + <%= _title %> | <%= _site_name %> <% for stylesheet in _stylesheets %> <% end %> @@ -36,7 +35,7 @@
-

<%= _header_title %>

+

<%= _title %>

<% if not _tags.empty? %>