aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/extensions/section_id_validator.rb
blob: 2ad496c4c52637725dc2a08695fc50bb41675b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Nuldoc
  module Extensions
    class SectionIdValidator < Asciidoctor::Extensions::TreeProcessor
      def process(doc)
        errors = []
        (doc.find_by(context: :section) {_1.level > 0}).each do |section|
          errors << validate_section(section)
        end
        error_message = errors.compact.join("\n")
        unless error_message.empty?
          raise "SectionIdValidator (#{doc.attributes['source-file-path']}):\n#{error_message}"
        end
      end

      private

      def validate_section(section)
        id = section.id
        unless id
          return "Section '#{section.title}': each section MUST have an id."
        end
        unless id.match?(/\A[-0-9a-z]+\z/)
          return "Section '#{section.title}' (##{id}): section id MUST consist of either hyphen, digits or lowercases."
        end
        nil
      end
    end
  end
end