deadlink 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb5e6436cde33615811d3788e82efa4aae463b0b
4
- data.tar.gz: c4b3acda36bf178319b056384504370b5fa22802
3
+ metadata.gz: 7538d034a5581e58ea5b20677fcf57ef79a79297
4
+ data.tar.gz: d5cab5c98c9546972f54ee9148aa6d276294d923
5
5
  SHA512:
6
- metadata.gz: bafda7f50283eb4ecc701de7bcf60436697dc5611da533e7dcff1044fdd04804089ccb3a7b324898f5e32770c6ecaf1cd02594494b884670601ba3ddfd3c6151
7
- data.tar.gz: a7e9dc7d73724d5af0eb690a1ec90a94b3cfadf017b4254d4f2e3fea6f26e77ae511323d0a178e46045a9f7bd36fb8170557a544fd5d4338a30fab0f62ad6a1d
6
+ metadata.gz: f5a53cb6ee7664b250e60c8aedc052c97af63afd3fd4a2accc6c225b4645bc8dc96c058a5fb0ab918712fc5c60eb7fbfa8b6f3bf2f7f54c44cef54234b2c0480
7
+ data.tar.gz: 9c7fccd5a8a65db23b05f2a1eb72c85967e13d44e43d64605eb9ef6f284e1b7e363a1c99704698232544a570e1bf9a263a092567dd6d0d19fd59b4cd2cf3d49c
data/.travis.yml CHANGED
@@ -1,10 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.2
4
-
3
+ - 2.2.2
5
4
  before_install: gem install bundler -v 1.10.6
6
-
7
5
  cache: bundler
8
-
9
6
  notifications:
10
- email: false
7
+ email: false
8
+ slack:
9
+ secure: wXu1Ozr6IS53djD3ISOALmVx2DTgpnHstFdCi+COtbFXEQgskF2R2esAfd22k3/muWdmOE4pyPzQ8kljOjBnP+9uupBfkt+U50ZKry1M4Hygy7hbUES+iUpjxKnF6RvmEodrQ8H9vFQi6Ookeh3x5cEiUyikJ72XDLPd17gpd0AhvwcH2dqBDq7AgJU/BqgM8MF/87k1O9sLrQd8ED9zHBG6fj0SLPy4EzkbF9NpkmMnzjDNbliuQ1Q5ty0Uhe4GfJFAQ3FBbOOjL2LLpohxuJHLV0JI7VixwAYz+3N8Xl5+t2CSYVf1BTEZKMPa7kQ64hy8EQLMW0b6tghcHE0LLNzh83euIYO/ayCQ5s9urjZbZFn2Q0QUPj7RPbGMKtdO9h2OVyM1PMZqLLEvEw9rmxEDN42sPczgagZmiGd7xwhxavwT2qXlg0K2aLXmgOqTEOc/yDwrKXz4/5f+Vw2b0BOhOQlf2M6D8x3xIF2MqCdkm+lCcTh3Nxn2Vlh2z6YmuPbvFxrhzEMFjSY9kYSzqiBFt7K9flDRqpWWAVLn9ra2rScyEhS4+tA/CLBi4zt6cP7T+AUVsi8hZLPbz6j7iiLdRag7SXpv34nxuB9rJdo3DK45zr5MfpDEvgFUu2930gbNidb03yOzm7k/YwbvabQGoWnXpMCEuYwYPC7Txoc=
10
+ on_success: change
11
+ on_failure: change
data/README.md CHANGED
@@ -29,7 +29,6 @@ $ deadlink deadlink/
29
29
  result is this
30
30
 
31
31
  ```
32
- example.md#title1 in ./README.md line: 43
33
32
  contributor-covenant.org in ./README.md line: 73
34
33
  nothing_file3.md in ./test/files/dir1/nest_file1.md line: 2
35
34
  dir1/nothing_nest_file2.md in ./test/files/top.md line: 3
@@ -42,10 +41,7 @@ nothing.txt in ./test/files/top.md line: 8
42
41
  this gem not check following patterns yet. applying these patterns to gem is future task.
43
42
 
44
43
  - http(s) link
45
- - anchor link ( only check file path part )
46
- - e.g. `[anhor link](example.md#title1)` => check example.md
47
44
  - multibyte characters
48
- - specify file.
49
45
 
50
46
  ## Installation
51
47
 
@@ -53,7 +49,7 @@ this gem not check following patterns yet. applying these patterns to gem is fut
53
49
 
54
50
  ## Usage
55
51
 
56
- specify path of directory
52
+ ### specify path of directory
57
53
 
58
54
  ```
59
55
  $ deadlink <dir>
@@ -71,6 +67,12 @@ if you not specify `<dir>` path, deadlink scan top directory of current git repo
71
67
  $ deadlink
72
68
  ```
73
69
 
70
+ ### specify a file
71
+
72
+ ```
73
+ $ deadlink <file path>
74
+ ```
75
+
74
76
  ### Option
75
77
 
76
78
  `-p` option output for editor. In this repository case
data/deadlink.gemspec CHANGED
@@ -23,5 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "minitest"
25
25
  spec.add_development_dependency "fakefs"
26
+ spec.add_development_dependency "minitest-reporters"
26
27
 
27
28
  end
@@ -0,0 +1,42 @@
1
+ module Deadlink
2
+ class MdFile
3
+
4
+ attr_reader :path, :headers, :link_paths
5
+
6
+ def initialize(file_path, repo_root)
7
+ @path = file_path
8
+ @headers = []
9
+ @link_paths = []
10
+ init(repo_root)
11
+ end
12
+
13
+ private
14
+
15
+ def init(repo_root)
16
+ File.open(@path) do |f|
17
+ f.each_with_index do |line,index|
18
+ attribute(line, index, repo_root)
19
+ end
20
+ end
21
+ end
22
+
23
+ def attribute(line, index, repo_root)
24
+ if line =~ heading_pattern # capture sharp header part
25
+ header = Regexp.last_match[:header].downcase.rstrip.gsub(/\s+/," ")
26
+ @headers.push header.gsub(" ", "-")
27
+ end
28
+ line.scan link_pattern do |link| # capthure links path part
29
+ @link_paths.push Path.new(@path, link[0], index + 1, repo_root)
30
+ end
31
+ end
32
+
33
+ def heading_pattern
34
+ /^\#{1,6} +(?<header>.+)/
35
+ end
36
+
37
+ def link_pattern
38
+ /\[[^\]]*\]\((?<link>[^)]+)\)/
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ require 'forwardable'
2
+
3
+ module Deadlink
4
+ class MdFiles
5
+
6
+ include Enumerable
7
+ extend Forwardable
8
+ def_delegators :@files,:[],:empty?, :each
9
+
10
+ def initialize(files)
11
+ @files = files
12
+ end
13
+
14
+ def header_include?(path, anchor)
15
+ file = find_by(path)
16
+ return file.headers.include?(anchor) unless file.nil?
17
+ false
18
+ end
19
+
20
+ private
21
+
22
+ def find_by(path)
23
+ @files.each { |file| return file if file.path == path }
24
+ end
25
+
26
+ end
27
+ end
data/lib/deadlink/path.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Deadlink
2
2
  class Path
3
3
 
4
- attr_reader :link, :cur_file_path, :index
4
+ attr_reader :cur_file_path, :link, :index, :anchor
5
5
 
6
6
  def initialize(cur_file_path, link, index, repo_root)
7
7
  @cur_file_path = cur_file_path
@@ -12,23 +12,29 @@ module Deadlink
12
12
  hash = split_link(link)
13
13
  @link_file_path = hash[:filepath]
14
14
  @anchor = hash[:anchor]
15
- end
16
15
 
17
- def deadlink?
18
- !FileTest.exist?(abusolute_path) && ignore
16
+ @abusolute_link_file_path = abusolute_link_file_path
19
17
  end
20
18
 
19
+ def deadlink?(files)
20
+ (not_exist? || anchor_invalid?(files)) && not_ignore?
21
+ end
22
+
21
23
  private
22
24
 
23
- def ignore
24
- !url?
25
+ def anchor_invalid?(files)
26
+ return false if @anchor.empty?
27
+ if @link_file_path.empty?
28
+ return !files.header_include?(@cur_file_path, @anchor)
29
+ end
30
+ !files.header_include?(@abusolute_link_file_path, @anchor)
25
31
  end
26
-
27
- def url?
28
- @link =~ /https?:\/\/[\S]+/
32
+
33
+ def exist?
34
+ FileTest.exist?(@abusolute_link_file_path)
29
35
  end
30
36
 
31
- def abusolute_path
37
+ def abusolute_link_file_path
32
38
  if specify_root?(@link_file_path)
33
39
  return File.join(@repo_root, @link_file_path)
34
40
  else
@@ -38,12 +44,30 @@ module Deadlink
38
44
 
39
45
  def split_link(link)
40
46
  # split <filenpath>#<anchor>
41
- hash = link.match(/(?<filepath>[^#]*)#*(?<anchor>.*)/)
47
+ link.match(/(?<filepath>[^#]*)#*(?<anchor>.*)/)
42
48
  end
43
49
 
44
50
  def specify_root?(path)
45
51
  path[0] == "/"
46
52
  end
47
53
 
54
+ def ignore?
55
+ url?
56
+ end
57
+
58
+ def url?
59
+ @link =~ /https?:\/\/[\S]+/
60
+ end
61
+
62
+ ## negative wrap
63
+
64
+ def not_exist?
65
+ !exist?
66
+ end
67
+
68
+ def not_ignore?
69
+ !ignore?
70
+ end
71
+
48
72
  end
49
73
  end
@@ -3,11 +3,14 @@ module Deadlink
3
3
 
4
4
  attr_reader :deadlinks
5
5
 
6
- def initialize(paths)
7
- @paths = paths
8
- @deadlinks = paths.select { |path| path.deadlink? }
9
- end
6
+ def initialize(files)
7
+ @files = files
8
+ @paths = []
9
+ @files.each { |file| @paths.concat(file.link_paths) }
10
10
 
11
+ @deadlinks = @paths.select { |path| path.deadlink?(@files) }
12
+ end
13
+
11
14
  def deadlink_include?
12
15
  @deadlinks.any?
13
16
  end
@@ -15,34 +15,33 @@ module Deadlink
15
15
  puts @target_path + ": No such file or directory"
16
16
  return false
17
17
  end
18
- return true
18
+ true
19
19
  end
20
20
 
21
21
  def md_files
22
+ files = []
22
23
  if File.directory?(@target_path)
23
- Dir.glob(File.join(@target_path, '/**/*.{md,markdown}'))
24
+ glob do |file_path|
25
+ files.push(MdFile.new(file_path, @repo_root))
26
+ end
24
27
  else
25
- files = []
26
- files.push(@target_path)
28
+ files.push(MdFile.new(@target_path, @repo_root))
27
29
  end
30
+ MdFiles.new(files)
28
31
  end
29
32
 
30
33
  def paths(files)
31
- paths = []
32
- files.each do |file|
33
- File.open(file) do |f|
34
- f.each_with_index do |line, index|
35
- line.scan /\[[^\]]*\]\(([^)]+)\)/ do |link|
36
- paths.push Path.new(f.path, link[0], index + 1, @repo_root)
37
- end
38
- end
39
- end
40
- end
41
- Paths.new(paths)
34
+ Paths.new(files)
42
35
  end
43
36
 
44
37
  private
45
38
 
39
+ def glob
40
+ Dir.glob(File.join(@target_path, '/**/*.{md,markdown}')) do |file_path|
41
+ yield file_path
42
+ end
43
+ end
44
+
46
45
  def repo_root(target_path)
47
46
  dir = target_path
48
47
  until dir.empty? do
@@ -1,3 +1,3 @@
1
1
  module Deadlink
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/deadlink.rb CHANGED
@@ -1,8 +1,12 @@
1
1
  require "deadlink/version"
2
2
  require 'deadlink/scanner'
3
+ require 'deadlink/md_file'
3
4
  require 'deadlink/path'
4
5
  require 'deadlink/paths'
5
6
  require 'deadlink/decorator'
7
+ require 'deadlink/md_files.rb'
8
+
9
+
6
10
  require 'optparse'
7
11
 
8
12
  module Deadlink
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deadlink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yutakakinjyo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-19 00:00:00.000000000 Z
11
+ date: 2015-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: check deadlink from markdown file in your git repository
70
84
  email:
71
85
  - yutakakinjyo@gmail.com
@@ -87,6 +101,8 @@ files:
87
101
  - exe/deadlink
88
102
  - lib/deadlink.rb
89
103
  - lib/deadlink/decorator.rb
104
+ - lib/deadlink/md_file.rb
105
+ - lib/deadlink/md_files.rb
90
106
  - lib/deadlink/path.rb
91
107
  - lib/deadlink/paths.rb
92
108
  - lib/deadlink/scanner.rb