class AddTOCFilter < Nanoc::Filter identifier :add_toc def run(content, params={}) content.gsub('{{TOC}}') do # Find all top-level sections doc = Nokogiri::HTML(content) headers = doc.xpath('//h2').map do |header| { :title => header.inner_html, :id => header['id'] } end if headers.empty? next '' end # Build table of contents res = '