aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/extensions/source_id_validator.rb
blob: 6e04deb01062ed832cff9f12f3a5c1668e083437 (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
30
31
32
module Nuldoc
  module Extensions
    class SourceIdValidator < Asciidoctor::Extensions::TreeProcessor
      def process(doc)
        errors = []
        (doc.find_by(context: :listing) {_1.style == 'source'}).each do |source|
          errors << validate_section(source)
        end
        error_message = errors.compact.join("\n")
        unless error_message.empty?
          raise "SourceIdValidator (#{doc.attributes['source-file-path']}):\n#{error_message}"
        end
      end

      private

      def validate_section(source)
        id = source.id
        unless id
          return "Each source MUST have an id."
        end
        if id.start_with?('source.')
          return "Source id (##{id}) MUST NOT start with 'source.', which is appended by `nul`."
        end
        unless id.match?(/\A[-0-9a-z]+\z/)
          return "Source id (##{id}) MUST consist of either hypen, digits or lowercases."
        end
        nil
      end
    end
  end
end