oreilly-snippets 0.0.12 → 0.0.15

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,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YTAwOGQ0YTdhODBiMjI1MThjMTZkNTQyMzkwMjdlN2ViYmRjYjE3YQ==
5
- data.tar.gz: !binary |-
6
- ZDlmMjc1YTY5MWZlMmJiM2MwYjA0OGY4NWQzNDFlYzE5M2UyYzY2ZQ==
2
+ SHA1:
3
+ metadata.gz: 1d3e3c29a72469016a1400732a8e020cddee9599
4
+ data.tar.gz: e9377684d7ba0e2bc9f4f5ae65e8514be72b57a1
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- YWMyOTRmNGQ4NDhhMGE1MTk5ZTMzZTA3MDA1MTdhZDRhMzhmMzVmMDYyNzg1
10
- NGRmYWE3M2MxNmZjODk0MWE0YWY1ZWU5MzUwYTlhYzE1MTNlZjFiYzBjZmEz
11
- YzJjMDI2MDJlYjY3ZTI2YWNiMDFlMzc0NTBlMTliMDQ0MGVkOTY=
12
- data.tar.gz: !binary |-
13
- MGE0NjY5MTA5ZmIzYTI3YzVjMTcxNzdiMDQ2ZjQxZjRiZTk2NmRmOWY3M2I5
14
- MjUwY2ZmNjE4ZWNiOGYyZmVmMzJkZjU2YWI2YjM3OWZjMDllNTAwNDBiYmFm
15
- MmQzZjc3ZDllMDlhZWJlMGYwZDMwZTU0ZWEwYTEwNDQ3Y2ZiN2Q=
6
+ metadata.gz: cd8d103b247334041ef4b2e487b5f4002ff56444495c2d5ca32b10c846c4911cea0648d63c1e3e61af3be9aaaba6381505e3d24377a3a3fce964892da09435a1
7
+ data.tar.gz: b8fd891e2fbbb6c5becc3429d8916bbd84ca7853431d9a282da3ad86d0da91f200d2ec4cb13c19395f9225bc0b0ba2a815e87948291a5dcf957a9850eec1ce93
@@ -15,7 +15,17 @@ module Oreilly
15
15
  @@_config.merge!( opts )
16
16
  end
17
17
 
18
- def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil, flatten=false, normcallouts=false )
18
+ def self.get_content_from_file( s )
19
+
20
+ spec = s[:filename]
21
+ identifier = s[:identifier]
22
+ language = s[:language]
23
+ sha = s[:sha]
24
+ numbers = s[:lines]
25
+ flatten = s[:flatten]
26
+ normcallouts = s[:normcallouts]
27
+ callouts = s[:callouts]
28
+
19
29
  contents = nil
20
30
  line_numbers = nil
21
31
  error = false
@@ -62,7 +72,9 @@ module Oreilly
62
72
  end
63
73
  end
64
74
 
65
- if normcallouts
75
+ if callouts
76
+ rv = process_callouts( rv, callouts )
77
+ elsif normcallouts
66
78
  rv = normalize_callouts( rv )
67
79
  end
68
80
 
@@ -71,10 +83,41 @@ module Oreilly
71
83
  rv
72
84
  end
73
85
 
86
+ def self.process_callouts( input, callouts )
87
+ rv = nil
88
+ # Strip them out and figure out the comment character
89
+ comment_character = nil
90
+ rv = input.gsub( /([#\/]\/?) ?<\d+>/ ) { |c| comment_character = $1; '' }
91
+
92
+ unless comment_character
93
+ # OK, we need to scan for it and hope to figure it out
94
+ if rv =~ /\/\/[\/]+$/
95
+ comment_character = '/'
96
+ elsif rv =~ /#[^#]+$/
97
+ comment_character = '#'
98
+ end
99
+ end
100
+
101
+ unless comment_character
102
+ comment_character = '//'
103
+ end
104
+
105
+ unless callouts.eql? ""
106
+ list = callouts.split /,/
107
+ lines = rv.split /\n/
108
+ list.each_with_index do |c,i|
109
+ index = c.to_i - 1
110
+ lines[index] += "#{comment_character} <#{i+1}>" if lines.length > index and index >= 0
111
+ end
112
+ rv = lines.join "\n"
113
+ end
114
+ rv
115
+ end
116
+
74
117
  def self.normalize_callouts( rv )
75
118
  # Find something that looks like comment character + whitespace + < + number + >
76
119
  index = 0
77
- rv.gsub!( /([\/#]) <\d+>/ ) { |c| index += 1; "#{$1} <#{index}>" }
120
+ rv.gsub!( /([\/#]) <\d*>/ ) { |c| index += 1; "#{$1} <#{index}>" }
78
121
  rv
79
122
  end
80
123
 
@@ -107,7 +150,7 @@ module Oreilly
107
150
 
108
151
 
109
152
  def self.scrub_other_identifiers( s, comments )
110
- puts s
153
+ # puts s
111
154
  re = /#{comments} BEGIN \S+\n(.*)\n#{comments} END \S+\n/m
112
155
  s.gsub!( re, $1 )
113
156
  s
@@ -118,7 +161,7 @@ module Oreilly
118
161
  rv = input
119
162
  if snippets and snippets.length > 0
120
163
  snippets.each do |s|
121
- content = get_content_from_file( s[:filename], s[:identifier], s[:language], s[:sha], s[:lines], s[:flatten], s[:normcallouts] )
164
+ content = get_content_from_file( s ) # s[:filename], s[:identifier], s[:language], s[:sha], s[:lines], s[:flatten], s[:normcallouts] )
122
165
  rv = rv.gsub( s[:full], content )
123
166
  end
124
167
  end
@@ -1,5 +1,5 @@
1
1
  module Oreilly
2
2
  module Snippets
3
- VERSION = "0.0.12"
3
+ VERSION = "0.0.15"
4
4
  end
5
5
  end
@@ -0,0 +1,3 @@
1
+ something().is().here() //<2>
2
+ another.thing().ok() //<10>
3
+ another.thing().ok()
@@ -1,2 +1,3 @@
1
1
  something().is().here() // <2>
2
2
  another.thing().ok() // <3>
3
+ another.thing().ok() // <12>
@@ -0,0 +1,21 @@
1
+ something().is().here()
2
+ another.thing().ok()
3
+ another.thing().ok()
4
+ something().is().here()
5
+ another.thing().ok()
6
+ another.thing().ok()
7
+ something().is().here()
8
+ another.thing().ok()
9
+ another.thing().ok()
10
+ something().is().here()
11
+ another.thing().ok()
12
+ another.thing().ok()
13
+ something().is().here()
14
+ another.thing().ok()
15
+ another.thing().ok()
16
+ something().is().here()
17
+ another.thing().ok()
18
+ another.thing().ok()
19
+ something().is().here()
20
+ another.thing().ok()
21
+ another.thing().ok()
data/spec/process_spec.rb CHANGED
@@ -6,6 +6,14 @@ TEST_REPO = "oreilly-snippets-sample-content"
6
6
  GITHUB_REPO = "https://github.com/xrd/#{TEST_REPO}.git"
7
7
  ROOT = File.join "spec", TEST_REPO
8
8
 
9
+ WITH_DIRECTORY_AND_SHA = <<END
10
+ [filename="../oreilly-snippets", language="js", sha="2f35461ff68c92c2e554c953:spec/fixtures/factorial.js"]
11
+ snippet~~~~
12
+ Put any descriptive text you want here. It will be replaced with the
13
+ snippet~~~~
14
+ END
15
+
16
+
9
17
  WITH_SHA = <<END
10
18
  [filename="#{ROOT}", language="js", sha="c863f786f5959799d7c:test.js"]
11
19
  snippet~~~~
@@ -37,6 +45,36 @@ snippet~~~~
37
45
 
38
46
  END
39
47
 
48
+ STRIP_CALLOUTS_JS = <<"END"
49
+ [filename="spec/fixtures/normalize_callouts.js", callouts=""]
50
+ snippet~~~~
51
+ ...
52
+ snippet~~~~
53
+ END
54
+
55
+ ADD_CALLOUTS_JS = <<"END"
56
+ [filename="spec/fixtures/normalize_callouts.js", callouts="1,2,3"]
57
+ snippet~~~~
58
+ ...
59
+ snippet~~~~
60
+ END
61
+
62
+ NO_SPACED_CALLOUTS_JS = <<"END"
63
+ [filename="spec/fixtures/add-callouts-no-space.js", callouts="1"]
64
+ snippet~~~~
65
+ ...
66
+ snippet~~~~
67
+ END
68
+
69
+
70
+ REALLY_LONG_CALLOUTS_JS = <<"END"
71
+ [filename="spec/fixtures/normalize_callouts_long.js", callouts="1,10,15"]
72
+ snippet~~~~
73
+ ...
74
+ snippet~~~~
75
+ END
76
+
77
+
40
78
  NORMALIZE_CALLOUTS_JS = <<"END"
41
79
  [filename="spec/fixtures/normalize_callouts.js", normcallouts="true"]
42
80
  snippet~~~~
@@ -51,7 +89,6 @@ snippet~~~~
51
89
  snippet~~~~
52
90
  END
53
91
 
54
-
55
92
  NORMALIZE_CALLOUTS_RB = <<"END"
56
93
  [filename="spec/fixtures/normalize_callouts.rb", normcallouts="true"]
57
94
  snippet~~~~
@@ -217,11 +254,47 @@ describe Oreilly::Snippets do
217
254
  output.should_not match( /END FACTORIAL_FUNC/ )
218
255
  end
219
256
 
257
+ describe "#callouts" do
258
+ it "should strip callouts completely" do
259
+ output = Oreilly::Snippets.process( STRIP_CALLOUTS_JS )
260
+ output.should_not match( /<\d+>/ )
261
+ end
262
+
263
+ it "should add callouts" do
264
+ output = Oreilly::Snippets.process( ADD_CALLOUTS_JS )
265
+ lines = output.split /\n/
266
+ lines[0].should match( /<1>/ )
267
+ lines[1].should match( /<2>/ )
268
+ lines[2].should match( /<3>/ )
269
+ end
270
+
271
+ it "should add callouts into a long file" do
272
+ output = Oreilly::Snippets.process( REALLY_LONG_CALLOUTS_JS )
273
+ lines = output.split /\n/
274
+ lines[0].should match( /<1>/ )
275
+ lines[9].should match( /<2>/ )
276
+ lines[14].should match( /<3>/ )
277
+ end
278
+
279
+ it "should add callouts using a default comment" do
280
+ output = Oreilly::Snippets.process( REALLY_LONG_CALLOUTS_JS )
281
+ lines = output.split /\n/
282
+ lines[0].should match( /\/\/ <1>/ )
283
+ end
284
+
285
+ it "should add callouts without a space character in between comment and callout" do
286
+ output = Oreilly::Snippets.process( NO_SPACED_CALLOUTS_JS )
287
+ lines = output.split /\n/
288
+ lines[0].should match( /\/\/ <1>/ )
289
+ lines[1].should_not match( /\/\/<10>/ )
290
+ end
291
+ end
292
+
220
293
  describe "#normcallouts" do
221
294
  it "should normalize callouts" do
222
295
  output = Oreilly::Snippets.process( NORMALIZE_CALLOUTS_JS )
223
296
  output.should match( /<1>/ )
224
- output.should_not match( /<3>/ )
297
+ output.should_not match( /<12>/ )
225
298
  end
226
299
 
227
300
  it "should normalize callouts with alternative comments" do
@@ -302,7 +375,7 @@ END
302
375
  Dir.chdir File.join( ROOT )
303
376
  original = `git show c863f786f5959799d7c11312a7ba1d603ff16339:test.js`
304
377
  Dir.chdir cwd
305
- output.strip.should == original.strip
378
+ output.rstrip.should == original.rstrip
306
379
  end
307
380
 
308
381
  it "should retrieve by SHA and give us only certain lines" do
@@ -316,6 +389,16 @@ END
316
389
  output.should == original
317
390
  end
318
391
 
392
+ it "should parse directory specifications" do
393
+ output = Oreilly::Snippets.process( WITH_DIRECTORY_AND_SHA )
394
+ original = nil
395
+ path = ENV['REPO_NAME'] || "../oreilly-snippets"
396
+ Dir.chdir path do
397
+ original = `git show 2f35461ff68c92c2e554c953:spec/fixtures/factorial.js`
398
+ end
399
+ output.should == ( original + "\n" )
400
+ end
401
+
319
402
  it "should indicate placeholder if using xxx as the sha" do
320
403
  output = Oreilly::Snippets.process( WITH_PLACEHOLDER_SHA )
321
404
  output.should match( /PLACEHOLDER/ )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oreilly-snippets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Dawson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-24 00:00:00.000000000 Z
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,28 +28,28 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Use O'Reilly style snippets inside your markup (asciidoc or markdown)
@@ -69,12 +69,14 @@ files:
69
69
  - lib/oreilly/snippets.rb
70
70
  - lib/oreilly/snippets/version.rb
71
71
  - oreilly-snippets.gemspec
72
+ - spec/fixtures/add-callouts-no-space.js
72
73
  - spec/fixtures/coffeetech.js
73
74
  - spec/fixtures/factorial.java
74
75
  - spec/fixtures/factorial.js
75
76
  - spec/fixtures/normalize_callouts.honey
76
77
  - spec/fixtures/normalize_callouts.js
77
78
  - spec/fixtures/normalize_callouts.rb
79
+ - spec/fixtures/normalize_callouts_long.js
78
80
  - spec/fixtures/with_spaces.rb
79
81
  - spec/fixtures/with_tabs.rb
80
82
  - spec/process_spec.rb
@@ -89,27 +91,29 @@ require_paths:
89
91
  - lib
90
92
  required_ruby_version: !ruby/object:Gem::Requirement
91
93
  requirements:
92
- - - ! '>='
94
+ - - '>='
93
95
  - !ruby/object:Gem::Version
94
96
  version: '0'
95
97
  required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  requirements:
97
- - - ! '>='
99
+ - - '>='
98
100
  - !ruby/object:Gem::Version
99
101
  version: '0'
100
102
  requirements: []
101
103
  rubyforge_project:
102
- rubygems_version: 2.2.5
104
+ rubygems_version: 2.2.2
103
105
  signing_key:
104
106
  specification_version: 4
105
107
  summary: See the README
106
108
  test_files:
109
+ - spec/fixtures/add-callouts-no-space.js
107
110
  - spec/fixtures/coffeetech.js
108
111
  - spec/fixtures/factorial.java
109
112
  - spec/fixtures/factorial.js
110
113
  - spec/fixtures/normalize_callouts.honey
111
114
  - spec/fixtures/normalize_callouts.js
112
115
  - spec/fixtures/normalize_callouts.rb
116
+ - spec/fixtures/normalize_callouts_long.js
113
117
  - spec/fixtures/with_spaces.rb
114
118
  - spec/fixtures/with_tabs.rb
115
119
  - spec/process_spec.rb