middleman-spellcheck 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8608b5d6fd9049afe93af7ef3f005566bb10e537
4
- data.tar.gz: 00b148f8992c92b178992c485bea7a4275b80ea8
3
+ metadata.gz: fea98d00ab58220965c1e2b8118063d0fbf50b13
4
+ data.tar.gz: e35c46312d5c3a4afafea88635142e982d920368
5
5
  SHA512:
6
- metadata.gz: d86956a715506d4924060b035a3c8edd1f595b154d54b7a6e40ac4ebbb39b72f23a40d31939246e2a561d0f90a05e358975ce929ac0eec14a9c6bd1f8ab21701
7
- data.tar.gz: 376ba57130d2b77c563a2ee040adc564012e790ba964451d832d9ebe627e762bfcb0af4538da72bd70eff1496dcdb92e770302020e3dc36ee7e3dacf417225d0
6
+ metadata.gz: ae6225286fc01dbcfd6abe17f91b3b3b99f0250a1ed3ea2bbc8d2eb9b499af404826262b57e14146931318904b21dc65b51a3836cad6c7f3405b12e68c1cbfe6
7
+ data.tar.gz: 694260deefae946b9b7c8d76aeb3eb17ae7ffd4edbda7fb81c361113daac96dcacd32ac66917dd291b41cd30b4e461a19d682351f143bf60b49bad4d790a3c55
data/README.md CHANGED
@@ -16,6 +16,13 @@ Add the following to middleman's `config.rb`:
16
16
 
17
17
  activate :spellcheck
18
18
 
19
+ Spellcheck is run automatically after build, but you can also check individual files and subdirectories:
20
+
21
+ ```
22
+ middleman spellcheck source/about.html
23
+ middleman spellcheck source/blog/
24
+ ```
25
+
19
26
  ## Usage
20
27
 
21
28
  You can spellcheck only some resources using a regex with the URL:
@@ -51,12 +58,20 @@ example, to use Polish dictionary, use:
51
58
  activate :spellcheck, lang: "pl"
52
59
  ```
53
60
 
61
+ If you define the ``lang`` metadata in your pages / articles, then spellcheck will use those language.
62
+
54
63
  Middleman-spellcheck can issue many warnings if you run it over a new
55
64
  content. If you want to give yourself a chance to fix mistakes gradually and
56
65
  not fail each time you build, use :dontfail flag:
57
66
 
58
67
  ```ruby
59
- activate :spellcheck, lang: en, dontfail: 1
68
+ activate :spellcheck, lang: "en", dontfail: 1
69
+ ```
70
+
71
+ You can also disable the automatic spellcheck after build (and only run manual checks from the command line):
72
+
73
+ ```ruby
74
+ activate :spellcheck, run_after_build: false
60
75
  ```
61
76
 
62
77
  Advanced users wishing to invoke Middleman-spellcheck backend (Aspell) with
@@ -1,7 +1,8 @@
1
1
  require "middleman-core"
2
2
  require "middleman-spellcheck/version"
3
+ require "middleman-spellcheck/cli"
3
4
 
4
5
  ::Middleman::Extensions.register(:spellcheck) do
5
- require "middleman-spellcheck/extension"
6
- ::Middleman::Spellcheck::SpellcheckExtension
6
+ require "middleman-spellcheck/extension"
7
+ ::Middleman::Spellcheck::SpellcheckExtension
7
8
  end
@@ -0,0 +1,31 @@
1
+ module Middleman
2
+ module Cli
3
+ # This class provides an "spellchecl" command for the middleman CLI.
4
+ class Spellcheck < Thor
5
+ include Thor::Actions
6
+ namespace :spellcheck
7
+ desc "spellcheck FILE", "Run spellcheck on given file or path"
8
+ def spellcheck(*paths)
9
+ app = ::Middleman::Application.server.inst
10
+
11
+ resources = app.sitemap.resources.select{|resource|
12
+ paths.any? {|path|
13
+ resource.source_file.sub(Dir.pwd,'').sub(%r{^/},'')[/^#{Regexp.escape(path)}/]
14
+ }
15
+ }
16
+ if resources.empty?
17
+ $stderr.puts "File / Directory #{path} not exist"
18
+ exit 1
19
+ end
20
+ ext = app.extensions[:spellcheck]
21
+ resources.each do |resource|
22
+ say_status :spellcheck, "Running spell checker for #{resource.url}", :blue
23
+ current_misspelled = ext.spellcheck_resource(resource)
24
+ current_misspelled.each do |misspell|
25
+ say_status :misspell, ext.error_message(misspell), :red
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,4 +1,5 @@
1
1
  require 'middleman-spellcheck/spellchecker'
2
+ require 'middleman-spellcheck/cli'
2
3
  require 'nokogiri'
3
4
 
4
5
  module Middleman
@@ -13,8 +14,10 @@ module Middleman
13
14
  option :cmdargs, "", "Pass alternative command line arguments"
14
15
  option :debug, 0, "Enable debugging (for developers only)"
15
16
  option :dontfail, 0, "Don't fail when misspelled words are found"
17
+ option :run_after_build, true, "Run Spellcheck after build"
16
18
 
17
19
  def after_build(builder)
20
+ return if !options.run_after_build
18
21
  Spellchecker.cmdargs=(options.cmdargs)
19
22
  Spellchecker.debug_enabled=(options.debug)
20
23
  filtered = filter_resources(app, options.page)
@@ -22,7 +25,7 @@ module Middleman
22
25
 
23
26
  filtered.each do |resource|
24
27
  builder.say_status :spellcheck, "Running spell checker for #{resource.url}", :blue
25
- current_misspelled = run_check(select_content(resource), options.lang)
28
+ current_misspelled = spellcheck_resource(resource)
26
29
  current_misspelled.each do |misspell|
27
30
  builder.say_status :misspell, error_message(misspell), :red
28
31
  end
@@ -30,19 +33,20 @@ module Middleman
30
33
  end
31
34
 
32
35
  unless total_misspelled.empty?
33
- estr = "Build failed. There are spelling errors."
34
- if options.dontfail
35
- print "== :dontfail set! Will issue warning only, but not fail.\n"
36
- print estr, "\n"
37
- else
36
+ estr = "Build failed. There are spelling errors."
37
+ if options.dontfail
38
+ print "== :dontfail set! Will issue warning only, but not fail.\n"
39
+ print estr, "\n"
40
+ else
38
41
  raise Thor::Error, estr
39
- end
42
+ end
40
43
  end
41
44
  end
42
45
 
43
46
  def select_content(resource)
44
47
  rendered_resource = resource.render(layout: false)
45
- doc = Nokogiri::HTML(rendered_resource)
48
+ doc = Nokogiri::HTML.fragment(rendered_resource)
49
+ doc.search('code,style,script').each(&:remove)
46
50
 
47
51
  if options.tags.empty?
48
52
  doc.text
@@ -69,7 +73,19 @@ module Middleman
69
73
 
70
74
  def filter_resources(app, pattern)
71
75
  app.sitemap.resources.select { |resource| resource.url.match(pattern) }
72
- .reject { |resource| option_ignored_exts.include? resource.ext }
76
+ .reject { |resource| option_ignored_exts.include? resource.ext }
77
+ end
78
+
79
+ def spellcheck_resource(resource)
80
+ lang =
81
+ if options.lang.respond_to?(:call)
82
+ options.lang.call(resource)
83
+ elsif resource.respond_to?(:lang) and resource.lang
84
+ resource.lang.to_s
85
+ else
86
+ options.lang
87
+ end
88
+ run_check(select_content(resource), lang)
73
89
  end
74
90
 
75
91
  def run_check(text, lang)
@@ -54,10 +54,11 @@ class Spellchecker
54
54
  # should be * or &
55
55
  word_check_res = f.gets.strip()
56
56
  sdbg "<- got result '#{word_check_res}'"
57
-
58
- # skip the empty line
59
- val = f.gets()
60
- sdbg "Expected empty line, got '#{val}'"
57
+ if word_check_res != ""
58
+ # skip the empty line
59
+ val = f.gets()
60
+ sdbg "Expected empty line, got '#{val}'"
61
+ end
61
62
 
62
63
  result << word_check_res
63
64
  end
@@ -75,9 +76,9 @@ class Spellchecker
75
76
  text.gsub! '’', '\''
76
77
  sdbg "self.check got raw text:\n#{text}\n"
77
78
 
78
- words = text.split(/[^A-Za-z']+/).select { |s|
79
- s != "" and s != "'s" and s != "'"
80
- }
79
+ words = text.split(/[^\p{L}']+/).select { |s|
80
+ s != "" and s != "'s" and s != "'"
81
+ }.uniq
81
82
  sdbg "self.check word array:\n#{words}\n"
82
83
 
83
84
  results = query(words, lang).map do |query_result|
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module Spellcheck
3
- VERSION = "0.8.0"
3
+ VERSION = "0.9.0"
4
4
  end
5
5
  end
@@ -1 +1,2 @@
1
1
  require "middleman-spellcheck"
2
+ require 'middleman-core/cli'
@@ -3,7 +3,7 @@ require_relative '../../../lib/middleman-spellcheck/spellchecker'
3
3
 
4
4
  describe Spellchecker do
5
5
  let(:text) { "hello, world! of txet" }
6
- let(:result) { Spellchecker.check(text) }
6
+ let(:result) { Spellchecker.check(text, "en") }
7
7
 
8
8
  context "with one wrong word" do
9
9
  it "can spell check words" do
@@ -55,8 +55,27 @@ describe Spellchecker do
55
55
 
56
56
  it "whitelists the word" do
57
57
  result.should == [{ word: "A", correct: true },
58
- { word: "users", correct: true },
58
+ { word: "user's", correct: true },
59
59
  { word: "page", correct: true }]
60
60
  end
61
61
  end
62
+
63
+ context "Bugs" do
64
+ let(:text) { "'http user" }
65
+
66
+ it "don't crash" do
67
+ result.should == [{ word: "'http", correct: false },
68
+ { word: "user", correct: true }]
69
+ end
70
+ end
71
+
72
+ context "Unicode" do
73
+ let(:text) { "café hånk 你好" }
74
+
75
+ it "splits correctly on unicode chars, doesnt crash on Chinese chars" do
76
+ result.should == [{ word: "café", correct: false },
77
+ { word: "hånk", correct: false },
78
+ { word: "你好", correct: false }]
79
+ end
80
+ end
62
81
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-spellcheck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Zarea
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-17 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleman-core
@@ -100,6 +100,7 @@ files:
100
100
  - fixtures/spelling_path_app/source/check/index.html
101
101
  - fixtures/spelling_path_app/source/no_check/index.html
102
102
  - lib/middleman-spellcheck.rb
103
+ - lib/middleman-spellcheck/cli.rb
103
104
  - lib/middleman-spellcheck/extension.rb
104
105
  - lib/middleman-spellcheck/spellchecker.rb
105
106
  - lib/middleman-spellcheck/version.rb