<commit_before>require 'middleman-spellcheck/spellchecker'
require 'middleman-spellcheck/cli'
require 'nokogiri'

module Middleman
  module Spellcheck
    class SpellcheckExtension < Extension
      REJECTED_EXTS = %w(.css .js .coffee)
      ALLOWED_WORDS = []
      option :page, "/*", "Run only pages that match the regex through the spellchecker"
      option :tags, [], "Run spellcheck only on some tags from the output"
      option :allow, [], "Allow specific words to be misspelled"
      option :ignored_exts, [], "Ignore specific extensions (ex: '.xml')"
      option :lang, "en", "Language for spellchecking"
      option :cmdargs, "", "Pass alternative command line arguments"
      option :debug, 0, "Enable debugging (for developers only)"
      option :dontfail, 0, "Don't fail when misspelled words are found"
      option :run_after_build, true, "Run Spellcheck after build"

      def after_build(builder)
        return if !options.run_after_build
        Spellchecker.cmdargs=(options.cmdargs)
        Spellchecker.debug_enabled=(options.debug)
        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 = spellcheck_resource(resource)
          current_misspelled.each do |misspell|
            builder.say_status :misspell, error_message(misspell), :red
          end
          total_misspelled += current_misspelled
        end

        unless total_misspelled.empty?
          estr = "Build failed. There are spelling errors."
          if options.dontfail != 0
            print "== :dontfail set! Will issue warning only, but not fail.\n"
            print estr, "\n"
          else
            raise Thor::Error, estr
          end
        end
      end

      def select_content(resource)
        rendered_resource = resource.render(layout: false)
        doc = Nokogiri::HTML.fragment(rendered_resource)
        doc.search('code,style,script').each(&:remove)

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

      def option_tags
        if options.tags.is_a? Array
          options.tags
        else
          [options.tags]
        end<commit_msg>Bugfix - `dontfail` was set to a truthy value by default added Spellcheck exit status modified output messages for clarity<commit_after>require 'middleman-spellcheck/spellchecker'
require 'middleman-spellcheck/cli'
require 'nokogiri'

module Middleman
  module Spellcheck
    class SpellcheckExtension < Extension
      REJECTED_EXTS = %w(.css .js .coffee)
      ALLOWED_WORDS = []
      option :page, "/*", "Run only pages that match the regex through the spellchecker"
      option :tags, [], "Run spellcheck only on some tags from the output"
      option :allow, [], "Allow specific words to be misspelled"
      option :ignored_exts, [], "Ignore specific extensions (ex: '.xml')"
      option :lang, "en", "Language for spellchecking"
      option :cmdargs, "", "Pass alternative command line arguments"
      option :debug, 0, "Enable debugging (for developers only)"
      option :dontfail, false, "Don't fail because misspelled words are found"
      option :run_after_build, true, "Run Spellcheck after build"

      def after_build(builder)
        return if !options.run_after_build
        Spellchecker.cmdargs=(options.cmdargs)
        Spellchecker.debug_enabled=(options.debug)
        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 = spellcheck_resource(resource)
          current_misspelled.each do |misspell|
            builder.say_status :misspell, error_message(misspell), :red
          end
          total_misspelled += current_misspelled
        end

        builder.say_status :spellcheck, "Spellchecks done. #{total_misspelled.length} misspelling(s) found.", :blue

        unless total_misspelled.empty? 
          if options.dontfail
            builder.say_status :spellcheck, "dontfail is set! Builder will ignore misspellings.", :yellow
          else
            desc = "Build failed. There are spelling errors."
            raise Thor::Error, desc
          end
        end
      end

      def select_content(resource)
        rendered_resource = resource.render(layout: false)
        doc = Nokogiri::HTML.fragment(rendered_resource)
        doc.search('code,style,script').each(&:remove)

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

      def option_tags
        if options.tags.is_a? Array
          options.tags
        else
          [options.tags]
        end
{'fraction_non_alphanumeric': 0.08679780977489353, 'fraction_numerical': 0.0008111944838775097, 'mean_word_length': 2.5947521865889214, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2349167', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1369, 'n_words': 393}