diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-24 21:53:46 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-24 21:53:46 +0100 |
commit | 639fece16245dac8c24277cc1ce2f1321f683920 (patch) | |
tree | e51960ee0aec339051fda39e72b4ef7693aaf6f0 /_plugins/toc.rb | |
parent | 126fa877f904a894e43c423fe19427e9221e36d1 (diff) |
clean up ruby code
Diffstat (limited to '_plugins/toc.rb')
-rw-r--r-- | _plugins/toc.rb | 92 |
1 files changed, 45 insertions, 47 deletions
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 '<aside id="toc" class="plainlink">%s</aside>' % [ 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 '<aside id="toc" class="plainlink">%s</aside>' % [ toc ] + end - output = '<ul>' + def output_toc + # empty toc (this check prevents crash) + return "" if @els.length == 0 - current_level = el_level(@els[0]) + output = '<ul>' - while @els.length > 0 - el = @els[0] - el_next = @els[1] - level = el_level(el) - level_next = el_level(el_next || el) # || el to prevent crash on end of list + current_level = el_level(@els[0]) - if level >= level_next - output += '<li>' - else - output += '<li class="stub"><details>' - output += '<summary>' - end + while @els.length > 0 + el = @els[0] + el_next = @els[1] + level = el_level(el) + level_next = el_level(el_next || el) # || el to prevent crash on end of list - output += '<a href="#%s">%s</a>' % [ el['id'], el.inner_html ] - @els.shift() + if level >= level_next + output += '<li>' + else + output += '<li class="stub"><details>' + output += '<summary>' + end - if level >= level_next - output += '</li>' - else - output += '</summary>' - output += output_toc - output += '</details></li>' - end + output += '<a href="#%s">%s</a>' % [ el['id'], el.inner_html ] + @els.shift() - break if level_next < level + if level >= level_next + output += '</li>' + else + output += '</summary>' + output += output_toc + output += '</details></li>' end - output += '</ul>' - - return output + break if level_next < level end - def el_level el - return Integer(el.name[1..]) - end + output += '</ul>' + + 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) |