diff options
| author | nsfisis <nsfisis@gmail.com> | 2022-11-19 14:23:32 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2022-11-19 14:25:59 +0900 |
| commit | 6209453817da9922f28bac1bb1522c6d380630ab (patch) | |
| tree | 19e0699e751af387d549d6720ca215c8065b3c0c /lib/extensions/section_id_validator.rb | |
| parent | 0cafa073914b5e0b162b735a7f8445fb2aa8a604 (diff) | |
| download | blog.nsfisis.dev-6209453817da9922f28bac1bb1522c6d380630ab.tar.gz blog.nsfisis.dev-6209453817da9922f28bac1bb1522c6d380630ab.tar.zst blog.nsfisis.dev-6209453817da9922f28bac1bb1522c6d380630ab.zip | |
Hugo to Asciidoctor
Diffstat (limited to 'lib/extensions/section_id_validator.rb')
| -rw-r--r-- | lib/extensions/section_id_validator.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/extensions/section_id_validator.rb b/lib/extensions/section_id_validator.rb new file mode 100644 index 0000000..2ad496c --- /dev/null +++ b/lib/extensions/section_id_validator.rb @@ -0,0 +1,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 |
