starter 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/starter/markdown.rb +86 -0
  2. metadata +19 -2
@@ -0,0 +1,86 @@
1
+ require "pathname"
2
+ require "term/ansicolor"
3
+
4
+ module Starter
5
+
6
+ class Markdown
7
+
8
+ # From http://stackoverflow.com/questions/9268407/how-to-convert-markdown-style-links-using-regex
9
+ InlineLinkRegex = %r{
10
+ \[ # Literal opening bracket
11
+ ( # Capture what we find in here
12
+ [^\]]+ # One or more characters other than close bracket
13
+ ) # Stop capturing
14
+ \] # Literal closing bracket
15
+ \( # Literal opening parenthesis
16
+ ( # Capture what we find in here
17
+ [^)]+ # One or more characters other than close parenthesis
18
+ ) # Stop capturing
19
+ \) # Literal closing parenthesis
20
+ }x
21
+
22
+ LinkDefRegex= %r{^\[(.+)\]:(.+)$}
23
+
24
+ attr_reader :linted_docs
25
+
26
+ def initialize(root_doc, options={})
27
+ @linted_docs = {}
28
+ @root_doc = root_doc
29
+ @directory = options[:directory]
30
+ end
31
+
32
+ def lint
33
+ lint_doc(@root_doc)
34
+ if @directory
35
+ files = Dir["#{@directory}/**/*.md"]
36
+ unseen = files.to_a - @linted_docs.keys
37
+ puts Term::ANSIColor.yellow("Unreferenced documents:")
38
+ unseen.each do |path|
39
+ puts " " + Term::ANSIColor.yellow(path)
40
+ end
41
+ end
42
+ end
43
+
44
+ def lint_doc(path, source=nil)
45
+ # no point in repeating a check
46
+ return if @linted_docs[path]
47
+
48
+ begin
49
+ string = File.read(path)
50
+ puts " Linting doc: #{path}"
51
+ lint_markdown(string, path)
52
+ @linted_docs[path] = true
53
+ rescue Errno::ENOENT
54
+ puts Term::ANSIColor.red("Broken link in #{source}: #{path}")
55
+ rescue => e
56
+ pp e
57
+ end
58
+ end
59
+
60
+
61
+ def lint_markdown(string, source=nil)
62
+ string.each_line do |line|
63
+ normal_link = InlineLinkRegex.match(line)
64
+ link_def = LinkDefRegex.match(line)
65
+ if normal_link
66
+ lint_link(normal_link, source)
67
+ end
68
+ if link_def
69
+ lint_link(link_def, source)
70
+ end
71
+ end
72
+ end
73
+
74
+ def lint_link(match, source)
75
+ _m, text, url = match.to_a
76
+ if url && url =~ /\.md$/ && url !~ /^https?:\/\/|#/
77
+ url = (Pathname.new(source).dirname + url).to_s
78
+ url.sub!(/#.*$/, "")
79
+ lint_doc(url, source)
80
+ end
81
+ end
82
+
83
+
84
+ end
85
+
86
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-07 00:00:00.000000000 Z
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: git
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: term-ansicolor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description:
47
63
  email:
48
64
  executables: []
@@ -55,6 +71,7 @@ files:
55
71
  - lib/starter/extensions/module.rb
56
72
  - lib/starter/extensions/string.rb
57
73
  - lib/starter/http.rb
74
+ - lib/starter/markdown.rb
58
75
  - lib/starter/misc.rb
59
76
  - lib/starter/mixins/statistics.rb
60
77
  - lib/starter/password.rb