From 639fece16245dac8c24277cc1ce2f1321f683920 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 24 Jan 2025 21:53:46 +0100 Subject: clean up ruby code --- _plugins/authors.rb | 38 ---------------------- _plugins/datefmt.rb | 8 ----- _plugins/filters.rb | 14 ++++++++ _plugins/meta.rb | 5 +++ _plugins/toc.rb | 92 ++++++++++++++++++++++++++--------------------------- 5 files changed, 64 insertions(+), 93 deletions(-) delete mode 100644 _plugins/authors.rb delete mode 100644 _plugins/datefmt.rb create mode 100644 _plugins/filters.rb (limited to '_plugins') diff --git a/_plugins/authors.rb b/_plugins/authors.rb deleted file mode 100644 index a2e0a53..0000000 --- a/_plugins/authors.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'set' - -module Jekyll - class FormatAuthors < Liquid::Tag - def initialize(tag_name, input, tokens) - super - @args = input.split(" ").map { |arg| arg.strip() } - end - - def transform_authors(author_meta, git_authors) - authors = Set[] - for substitute in author_meta do - for name in substitute['git'] do - if git_authors.include?(name) - authors.add(substitute['name']) - end - end - end - return authors.to_a() - end - - def join_names(authors) - return "nobody?" if authors.length == 0 - return "#{authors[0]}" if authors.length == 1 - return "#{authors[0..-2].join(", ")} and #{authors[-1]}" - end - - def render(context) - author_meta = context[@args[0]] - git_authors = context[@args[1]] - authors = transform_authors(author_meta, git_authors) - return join_names(authors) - end - end -end - -Liquid::Template.register_tag('fmt_authors', Jekyll::FormatAuthors) - diff --git a/_plugins/datefmt.rb b/_plugins/datefmt.rb deleted file mode 100644 index 7c2274f..0000000 --- a/_plugins/datefmt.rb +++ /dev/null @@ -1,8 +0,0 @@ -module DateFormatter - def datefmt(input) - return input.strftime("%F") - end -end - -Liquid::Template.register_filter(DateFormatter) - diff --git a/_plugins/filters.rb b/_plugins/filters.rb new file mode 100644 index 0000000..2526573 --- /dev/null +++ b/_plugins/filters.rb @@ -0,0 +1,14 @@ +module Filters + def datefmt(input) + return input.strftime("%F") + end + + def sentence_join(items) + return "" if items == nil or items.length == 0 + return "#{items[0]}" if items.length == 1 + return "#{items[0..-2].join(", ")} and #{items[-1]}" + end +end + +Liquid::Template.register_filter(Filters) + diff --git a/_plugins/meta.rb b/_plugins/meta.rb index f0d420f..85f3a75 100644 --- a/_plugins/meta.rb +++ b/_plugins/meta.rb @@ -8,6 +8,11 @@ module Meta # directly add generated page metadata to `page.meta` in liquid page.data['meta'] = site.data['post'][page.slug] + # set page.authors to author metadata from git+yaml + page.data['authors'] = site.data['authors'].filter { |author| + author['git'].intersect?(page.data['meta']['authors']) + } + # set page.date to generated date_initial page.data['date'] = page.data['meta']['date_initial'] end diff --git a/_plugins/toc.rb b/_plugins/toc.rb index 80acc82..09ef946 100644 --- a/_plugins/toc.rb +++ b/_plugins/toc.rb @@ -1,64 +1,62 @@ require 'nokogiri' -module Jekyll - class TOC < Liquid::Tag - def render context - # load HTML into nokogiri - html = context.registers[:page]['content'] - doc = Nokogiri::HTML(html) - - # enumerate over all h1-4 headings - @els = doc.css("h1, h2, h3, h4") - toc = output_toc() - return '' if toc.empty? - return '' % [ toc ] - end - - def output_toc - # empty toc (this check prevents crash) - return "" if @els.length == 0 +class TOC < Liquid::Tag + def render context + # load HTML into nokogiri + html = context.registers[:page]['content'] + doc = Nokogiri::HTML(html) + + # enumerate over all h1-4 headings + @els = doc.css("h1, h2, h3, h4") + toc = output_toc() + return '' if toc.empty? + return '' % [ toc ] + end - output = '' + + return output + end + + def el_level el + return Integer(el.name[1..]) end end -Liquid::Template.register_tag('toc', Jekyll::TOC) +Liquid::Template.register_tag('toc', TOC) -- cgit v1.2.3