arrfab / centos / centos.org

Forked from centos/centos.org 4 years ago
Clone

Blame lib/filters/add_ids_to_headers.rb

e8ec7a
require 'nokogiri'
e8ec7a
e8ec7a
module Nanoc3::Filters
e8ec7a
e8ec7a
  # TODO document
e8ec7a
  class AddIDsToHeaders < Nanoc3::Filter
e8ec7a
e8ec7a
    identifiers :add_ids_to_headers
e8ec7a
e8ec7a
    def run(content, arguments={})
e8ec7a
      doc = Nokogiri::HTML.parse(content)
e8ec7a
      doc.css("h1, h2, h3, h4, h5, h6").each do |header|
e8ec7a
        header['id'] = header.content.downcase.gsub(/\W+/, '-').gsub(/^-|-$/, '')
e8ec7a
      end
e8ec7a
      doc.to_s
e8ec7a
    end
e8ec7a
e8ec7a
  end
e8ec7a
e8ec7a
end