Class: Middleman::Spellcheck::SpellcheckExtension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-spellcheck/extension.rb

Constant Summary collapse

REJECTED_EXTS =
%w(.css .js .coffee)

Instance Method Summary collapse

Instance Method Details

#after_build(builder) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/middleman-spellcheck/extension.rb', line 12

def after_build(builder)
  filtered = filter_resources(app, options.page)
  total_misspelled = []

  filtered.each do |resource|
    builder.say_status :spellcheck, "Running spell checker for #{resource.url}", :blue
    current_misspelled = run_check(select_content(resource))
    current_misspelled.each do |misspell|
      builder.say_status :misspell, error_message(misspell), :red
    end
    total_misspelled += current_misspelled
  end

  unless total_misspelled.empty?
    raise Thor::Error, "Build failed. There are spelling errors."
  end
end

#error_message(misspell) ⇒ Object



81
82
83
# File 'lib/middleman-spellcheck/extension.rb', line 81

def error_message(misspell)
  "The word '#{misspell[:word]}' is misspelled"
end

#exclude_allowed(results) ⇒ Object



68
69
70
# File 'lib/middleman-spellcheck/extension.rb', line 68

def exclude_allowed(results)
  results.reject { |entry| option_allowed.include? entry[:word].downcase }
end

#filter_resources(app, pattern) ⇒ Object



57
58
59
60
# File 'lib/middleman-spellcheck/extension.rb', line 57

def filter_resources(app, pattern)
  app.sitemap.resources.select { |resource| resource.url.match(pattern) }
                       .reject { |resource| REJECTED_EXTS.include? resource.ext }
end

#option_allowedObject



72
73
74
75
76
77
78
79
# File 'lib/middleman-spellcheck/extension.rb', line 72

def option_allowed
  allowed = if options.allow.is_a? Array
              options.allow
            else
              [options.allow]
            end
  allowed.map(&:downcase)
end

#option_tagsObject



41
42
43
44
45
46
47
# File 'lib/middleman-spellcheck/extension.rb', line 41

def option_tags
  if options.tags.is_a? Array
    options.tags
  else
    [options.tags]
  end
end

#run_check(text, dictionary = "en") ⇒ Object



62
63
64
65
66
# File 'lib/middleman-spellcheck/extension.rb', line 62

def run_check(text, dictionary="en")
  results = Spellchecker.check(text, dictionary)
  results = exclude_allowed(results)
  results.reject { |entry| entry[:correct] }
end

#select_content(resource) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/middleman-spellcheck/extension.rb', line 30

def select_content(resource)
  rendered_resource = resource.render(layout: false)
  doc = Nokogiri::HTML(rendered_resource)

  if options.tags.empty?
    doc.text
  else
    select_tagged_content(doc, option_tags)
  end
end

#select_tagged_content(doc, tags) ⇒ Object



49
50
51
# File 'lib/middleman-spellcheck/extension.rb', line 49

def select_tagged_content(doc, tags)
  tags.map { |tag| texts_for_tag(doc, tag.to_s) }.flatten.join(' ')
end

#texts_for_tag(doc, tag) ⇒ Object



53
54
55
# File 'lib/middleman-spellcheck/extension.rb', line 53

def texts_for_tag(doc, tag)
  doc.css(tag).map(&:text)
end