Class: Uniword::Lint::BuiltinRules::MaxParagraphLength

Inherits:
Rule
  • Object
show all
Defined in:
lib/uniword/lint/builtin_rules/max_paragraph_length.rb

Overview

Rule: paragraphs longer than max words trigger a finding.

Constant Summary

Constants inherited from Rule

Rule::TYPES

Instance Attribute Summary

Attributes inherited from Rule

#name, #options, #severity

Instance Method Summary collapse

Methods inherited from Rule

#finding, register, type, types

Constructor Details

#initialize(max: 200, **rest) ⇒ MaxParagraphLength

Returns a new instance of MaxParagraphLength.

Parameters:

  • max (Integer) (defaults to: 200)

    word count threshold (default 200)



11
12
13
14
# File 'lib/uniword/lint/builtin_rules/max_paragraph_length.rb', line 11

def initialize(max: 200, **rest)
  super(**rest)
  @max = max
end

Instance Method Details

#check(document) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/uniword/lint/builtin_rules/max_paragraph_length.rb', line 16

def check(document)
  document.paragraphs.each_with_index do |paragraph, idx|
    text = paragraph.text.to_s
    word_count = text.split.length
    next if word_count <= @max

    yield finding(
      message: "Paragraph #{idx + 1} has #{word_count} words " \
               "(max #{@max})",
      path: "paragraph[#{idx}]",
    )
  end
end