sloane.sh/lib/filters/tufte.rb
2025-01-03 17:17:18 -05:00

28 lines
808 B
Ruby

Nanoc::Filter.define(:tufte) do |content, params|
doc = Nokogiri::HTML.fragment(content)
# Convert to array to avoid modifying during iteration
nodes = doc.children.to_a
# Track the current section we're building
current_section = nil
# Process each node
nodes.each do |node|
if node.name == 'h2'
# Start a new section
current_section = Nokogiri::XML::Node.new('section', doc)
node.add_previous_sibling(current_section)
current_section.add_child(node)
elsif current_section && node.name != 'h2'
# Add this node to the current section
current_section.add_child(node)
end
end
doc.to_html.gsub(/\[\^([^\]]+)\]:\s*(.+)$/) do |match|
ref = $1
note_content = $2.strip
"<span class=\"sidenote\">#{note_content}</span>"
end
end