oreilly-snippets 0.0.5 → 0.0.6
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 +4 -4
- data/.gitignore +1 -0
- data/README.md +51 -2
- data/lib/oreilly/snippets/version.rb +1 -1
- data/lib/oreilly/snippets.rb +35 -14
- data/spec/fixtures/with_spaces.rb +5 -0
- data/spec/fixtures/with_tabs.rb +5 -0
- data/spec/process_spec.rb +81 -21
- data/spec/spec_helper.rb +2 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67d27cdaa420bb60920b3d025fbf2fd1d1801d74
|
4
|
+
data.tar.gz: 3598b75bc1051bca2581724e64cf894c66c6f3bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb4578a7500000a4711f9cd15c6957f781a8d0c809a2bb2f83bedb77723d19b06ced9b9852ad723e186f3bfccf0dbb2c9e952fe190a112ce990ec4eec91fe662
|
7
|
+
data.tar.gz: 0d0adae1b813dbcf5d02efd72ad3202203e498fc577ee8b1120a521155e0adfe1128d10bdb45d3246fb750736d080a6d75ac17c3157b1be684e57a6770366c13
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -26,7 +26,7 @@ Send a string with proper snippet code into the process method:
|
|
26
26
|
|
27
27
|
### Snippets Usage
|
28
28
|
|
29
|
-
Check out
|
29
|
+
Check out [Code Snippets](http://chimera.labs.oreilly.com/books/1230000000065/ch04.html#code_explanation)
|
30
30
|
from O'Reilly.
|
31
31
|
|
32
32
|
A snippet looks like this inside your markup.
|
@@ -69,6 +69,8 @@ mod.factory( 'Github', function() {
|
|
69
69
|
|
70
70
|
### Special additions to snippets
|
71
71
|
|
72
|
+
#### Retrieve content from the local git repository
|
73
|
+
|
72
74
|
You can also use syntax like this to pull from a specific commit in
|
73
75
|
your git repository:
|
74
76
|
|
@@ -81,22 +83,69 @@ directory, then grab the file at the specific SHA hash. This means you
|
|
81
83
|
can write code inside a repository, and add a snippet pointing to that
|
82
84
|
exact revision in the repository.
|
83
85
|
|
86
|
+
#### Line Numbers
|
87
|
+
|
84
88
|
Also, you can specify line numbers and use just certain lines within the file retrieved:
|
85
89
|
|
86
90
|
```
|
87
91
|
[filename="../../github.js.test", language="js", sha="8e05a916fe0b1a9d3e:coffeetech.js, lines="1..5"]
|
88
92
|
```
|
89
93
|
|
94
|
+
This is equivalent to a range in ruby like `[0..4]`. So, we use human indexes, which are converted to zero-based numbering.
|
95
|
+
|
96
|
+
#### Placeholders for future SHA hashes
|
97
|
+
|
90
98
|
If you want to use a placeholder to remind you to put the correct
|
91
99
|
content in later once you have made the correct commit, use "xxx" as
|
92
100
|
the sha hash.
|
93
101
|
|
94
102
|
```
|
95
|
-
[filename="../../github.js.test", language="js", sha="
|
103
|
+
[filename="../../github.js.test", language="js", sha="xxx:coffeetech.js, lines="1..5"]
|
96
104
|
```
|
97
105
|
|
98
106
|
This will get replaced with `PLACEHOLDER TEXT, UPDATE WITH CORRECT SHA HASH`.
|
99
107
|
|
108
|
+
#### Flattening Identation
|
109
|
+
|
110
|
+
You can specify `flatten=true` and oreilly-snippets will flatten out
|
111
|
+
indentation. For example, if you are including a snippet python
|
112
|
+
content of python content, you might not want to keep the indentation
|
113
|
+
level as it is in the file, but display the content "flattened" to the
|
114
|
+
smallest indentation level.
|
115
|
+
|
116
|
+
For example, imagine this content:
|
117
|
+
|
118
|
+
```
|
119
|
+
def barfoo():
|
120
|
+
print "barfoo"
|
121
|
+
if someVar == "someVar"
|
122
|
+
if anotherVar == "anotherVar"
|
123
|
+
if thirdVar == "thirdVar"
|
124
|
+
print( "all of them" )
|
125
|
+
```
|
126
|
+
|
127
|
+
Then, imagine if you take the snippet from lines 4-6. You probably
|
128
|
+
don't want to display the snippet like this:
|
129
|
+
|
130
|
+
```
|
131
|
+
if anotherVar == "anotherVar"
|
132
|
+
if thirdVar == "thirdVar"
|
133
|
+
print( "all of them" )
|
134
|
+
```
|
135
|
+
|
136
|
+
You probably want it like this:
|
137
|
+
|
138
|
+
```
|
139
|
+
if anotherVar == "anotherVar"
|
140
|
+
if thirdVar == "thirdVar"
|
141
|
+
print( "all of them" )
|
142
|
+
```
|
143
|
+
|
144
|
+
If you want the default to be to flatten (avoiding setting it each
|
145
|
+
snippet declaration), you can set that using the config method: `Oreilly::Snippets.config( flatten: true )`
|
146
|
+
|
147
|
+
#### Incompatibilities with Atlas from O'Reilly
|
148
|
+
|
100
149
|
NB: This format of snippets is not currently compatible with Atlas
|
101
150
|
from O'Reilly. However, you can always process the snippet and write
|
102
151
|
out a normal Asciidoc file, a file which will be compatible with
|
data/lib/oreilly/snippets.rb
CHANGED
@@ -2,38 +2,55 @@ require "oreilly/snippets/version"
|
|
2
2
|
|
3
3
|
COMMENTS = {
|
4
4
|
:js => "\/\/",
|
5
|
-
:ruby => "#"
|
5
|
+
:ruby => "#",
|
6
|
+
:python => "#"
|
6
7
|
}
|
7
8
|
|
9
|
+
class String
|
10
|
+
def unindent
|
11
|
+
gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
8
15
|
module Oreilly
|
9
16
|
module Snippets
|
10
17
|
|
11
|
-
|
18
|
+
@@_config = {}
|
19
|
+
|
20
|
+
def self.config( opts )
|
21
|
+
@@_config.merge!( opts )
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil, flatten=false )
|
12
25
|
contents = nil
|
13
26
|
line_numbers = nil
|
14
27
|
error = false
|
15
28
|
|
29
|
+
if numbers
|
30
|
+
sae = numbers.split( ".." ).map { |d| Integer(d)-1 }
|
31
|
+
line_numbers = [sae[0], sae[1]]
|
32
|
+
end
|
33
|
+
|
16
34
|
if sha
|
17
35
|
if sha[0..2].eql? "xxx"
|
18
36
|
contents = "PLACEHOLDER TEXT, UPDATE WITH CORRECT SHA HASH"
|
19
37
|
else
|
20
|
-
if numbers
|
21
|
-
sae = numbers.split( ".." ).map { |d| Integer(d)-1 }
|
22
|
-
line_numbers = [sae[0], sae[1]]
|
23
|
-
end
|
24
38
|
# Use the filename to change into the directory and use git-show
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
39
|
+
if spec
|
40
|
+
Dir.chdir spec do
|
41
|
+
contents = `git show #{sha}`
|
42
|
+
error = true unless contents
|
43
|
+
end
|
44
|
+
end
|
30
45
|
end
|
31
46
|
else
|
32
47
|
contents = File.read( spec )
|
33
48
|
end
|
34
49
|
|
35
50
|
# If line numbers are there, provide only that content
|
36
|
-
|
51
|
+
if line_numbers
|
52
|
+
contents = contents.split( /\n/ )[line_numbers[0]..line_numbers[1]].join( "\n" )
|
53
|
+
end
|
37
54
|
|
38
55
|
rv = nil
|
39
56
|
if identifier
|
@@ -45,6 +62,10 @@ module Oreilly
|
|
45
62
|
rv = contents
|
46
63
|
end
|
47
64
|
|
65
|
+
if flatten or @@_config[:flatten]
|
66
|
+
rv = rv.unindent()
|
67
|
+
end
|
68
|
+
|
48
69
|
rv = "INVALID SNIPPET, WARNING" if error
|
49
70
|
# rv = scrub_other_identifiers( contents, comments )
|
50
71
|
rv
|
@@ -62,7 +83,7 @@ module Oreilly
|
|
62
83
|
rv = input
|
63
84
|
if snippets and snippets.length > 0
|
64
85
|
snippets.each do |s|
|
65
|
-
content = get_content_from_file( s[:filename], s[:identifier], s[:language], s[:sha], s[:lines] )
|
86
|
+
content = get_content_from_file( s[:filename], s[:identifier], s[:language], s[:sha], s[:lines], s[:flatten] )
|
66
87
|
rv = rv.gsub( s[:full], content )
|
67
88
|
end
|
68
89
|
end
|
@@ -78,7 +99,7 @@ module Oreilly
|
|
78
99
|
m[0].scan( /([^=\[,\s]*)="([^"]*)"/ ) do |kv|
|
79
100
|
match[kv[0].to_sym] = kv[1]
|
80
101
|
end
|
81
|
-
match[:full] = full
|
102
|
+
match[:full] = full
|
82
103
|
output << match
|
83
104
|
end
|
84
105
|
output
|
data/spec/process_spec.rb
CHANGED
@@ -27,18 +27,6 @@ Put any descriptive text you want here. It will be replaced with the
|
|
27
27
|
snippet~~~~
|
28
28
|
END
|
29
29
|
|
30
|
-
ORIGINAL_CONTENTS = <<END
|
31
|
-
var mod = angular.module( 'coffeetech', [] )
|
32
|
-
mod.controller( 'ShopsCtrl', function( $scope ) {
|
33
|
-
var github = new Github({} );
|
34
|
-
var repo = github.getRepo( "xrd", "spa.coffeete.ch" );
|
35
|
-
repo.contents( "gh-pages", "portland.json", function(err, data) {
|
36
|
-
$scope.shops = JSON.parse( data );
|
37
|
-
$scope.$digest();
|
38
|
-
}, false );
|
39
|
-
})
|
40
|
-
END
|
41
|
-
|
42
30
|
LOTS_OF_IDENTIFIERS = <<END
|
43
31
|
|
44
32
|
[filename="spec/fixtures/coffeetech.js", language="js"]
|
@@ -58,6 +46,14 @@ specified code snippet when you build ebook outputs
|
|
58
46
|
snippet~~~~
|
59
47
|
END
|
60
48
|
|
49
|
+
FOR_FLATTENING = <<END
|
50
|
+
[filename="spec/fixtures/factorial.js", language="js", lines="6..9"]
|
51
|
+
snippet~~~~
|
52
|
+
Put any descriptive text you want here. It will be replaced with the
|
53
|
+
specified code snippet when you build ebook outputs
|
54
|
+
snippet~~~~
|
55
|
+
END
|
56
|
+
|
61
57
|
TEMPLATE = <<END
|
62
58
|
|
63
59
|
ABC
|
@@ -88,6 +84,38 @@ snippet~~~~~
|
|
88
84
|
|
89
85
|
END
|
90
86
|
|
87
|
+
FLATTEN_WITH_SPACES =<<END
|
88
|
+
[filename="spec/fixtures/with_spaces.rb", language="ruby", flatten="true", lines="1..3"]
|
89
|
+
snippet~~~~
|
90
|
+
Put any descriptive text you want here. It will be replaced with the
|
91
|
+
specified code snippet when you build ebook outputs
|
92
|
+
snippet~~~~
|
93
|
+
END
|
94
|
+
|
95
|
+
FLATTEN_NO_LINE_NUMBERS =<<END
|
96
|
+
[filename="spec/fixtures/with_tabs.rb", language="ruby", flatten="true"]
|
97
|
+
snippet~~~~
|
98
|
+
Put any descriptive text you want here. It will be replaced with the
|
99
|
+
specified code snippet when you build ebook outputs
|
100
|
+
snippet~~~~
|
101
|
+
END
|
102
|
+
|
103
|
+
# Look at this: http://stackoverflow.com/questions/3772864/how-do-i-remove-leading-whitespace-chars-from-ruby-heredoc
|
104
|
+
|
105
|
+
# class String
|
106
|
+
# def unindent
|
107
|
+
# gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
|
108
|
+
# end
|
109
|
+
# end
|
110
|
+
|
111
|
+
FLATTEN_WITH_TABS =<<END
|
112
|
+
[filename="spec/fixtures/with_tabs.rb", language="ruby", flatten="true", lines="1..3"]
|
113
|
+
snippet~~~~
|
114
|
+
Put any descriptive text you want here. It will be replaced with the
|
115
|
+
specified code snippet when you build ebook outputs
|
116
|
+
snippet~~~~
|
117
|
+
END
|
118
|
+
|
91
119
|
def download_test_repository
|
92
120
|
root = File.join( "spec", TEST_REPO )
|
93
121
|
unless File.exists? root
|
@@ -124,6 +152,7 @@ describe Oreilly::Snippets do
|
|
124
152
|
output = outputs[0]
|
125
153
|
output[:sha].should == "c863f786f5959799d7c:test.js"
|
126
154
|
end
|
155
|
+
|
127
156
|
end
|
128
157
|
|
129
158
|
# describe "#scrub_other_identifiers" do
|
@@ -150,6 +179,40 @@ describe Oreilly::Snippets do
|
|
150
179
|
output.should_not match( /END FACTORIAL_FUNC/ )
|
151
180
|
end
|
152
181
|
|
182
|
+
describe "#flatten" do
|
183
|
+
before( :each ) do
|
184
|
+
@with_spaces = File.read( "spec/fixtures/with_spaces.rb" )
|
185
|
+
@with_tabs = File.read( "spec/fixtures/with_tabs.rb" )
|
186
|
+
@spaces_flattened = @with_spaces.split( "\n" )[0..3].join( "\n" ).gsub( /^ /, "" )
|
187
|
+
@tabs_flattened = @with_tabs.split( "\n" )[0..3].join( "\n" ).gsub( /^\t\t/, "" )
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should not flatten when indentation level is zero" do
|
191
|
+
output = Oreilly::Snippets.process( FLATTEN_NO_LINE_NUMBERS )
|
192
|
+
# remove one newline, consequence of embedding inside a template, it adds a newline
|
193
|
+
output = output[0...-1]
|
194
|
+
output.should == @with_tabs
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should support flattening as a configuration option" do
|
198
|
+
Oreilly::Snippets.config( flatten: true )
|
199
|
+
output = Oreilly::Snippets.process( FOR_FLATTENING )
|
200
|
+
lines = output.split "\n"
|
201
|
+
lines[0][0].should_not match /\s/ # First line has no whitespace starting
|
202
|
+
lines[-1][0].should match /\s/ # Last line is not indented
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should support flattening with tabs" do
|
206
|
+
output = Oreilly::Snippets.process( FLATTEN_WITH_TABS )
|
207
|
+
output.should == @tabs_flattened
|
208
|
+
end
|
209
|
+
|
210
|
+
it "should support flattening with spaces" do
|
211
|
+
output = Oreilly::Snippets.process( FLATTEN_WITH_SPACES )
|
212
|
+
output.should == @spaces_flattened
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
153
216
|
# NYI
|
154
217
|
# it "should remove all identifiers when processing" do
|
155
218
|
# output = Oreilly::Snippets.process( LOTS_OF_IDENTIFIERS )
|
@@ -169,13 +232,13 @@ describe Oreilly::Snippets do
|
|
169
232
|
|
170
233
|
it "should retrieve by SHA and give us only certain lines" do
|
171
234
|
output = Oreilly::Snippets.process( WITH_SHA_LINE_NUMBERS )
|
172
|
-
|
173
|
-
Dir.chdir File.join( ROOT )
|
174
|
-
|
175
|
-
|
235
|
+
original = nil
|
236
|
+
Dir.chdir File.join( ROOT ) do
|
237
|
+
original = `git show c863f786f5959799d7c11312a7ba1d603ff16339:test.js`
|
238
|
+
end
|
176
239
|
lines = original.split /\n/
|
177
|
-
original = lines[0..2].join "\n"
|
178
|
-
output.
|
240
|
+
original = lines[0..2].join( "\n" ) + "\n"
|
241
|
+
output.should == original
|
179
242
|
end
|
180
243
|
|
181
244
|
it "should indicate placeholder if using xxx as the sha" do
|
@@ -183,8 +246,5 @@ describe Oreilly::Snippets do
|
|
183
246
|
output.should match( /PLACEHOLDER/ )
|
184
247
|
end
|
185
248
|
end
|
186
|
-
|
187
|
-
|
188
|
-
|
189
249
|
end
|
190
250
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,9 +8,10 @@
|
|
8
8
|
require 'oreilly/snippets'
|
9
9
|
|
10
10
|
RSpec.configure do |config|
|
11
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
# config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
12
|
config.run_all_when_everything_filtered = true
|
13
13
|
config.filter_run :focus
|
14
|
+
config.expect_with(:rspec) { |c| c.syntax = :should }
|
14
15
|
|
15
16
|
# Run specs in random order to surface order dependencies. If you find an
|
16
17
|
# order dependency and want to debug it, you can fix the order by providing
|
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.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Dawson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,8 @@ files:
|
|
71
71
|
- oreilly-snippets.gemspec
|
72
72
|
- spec/fixtures/coffeetech.js
|
73
73
|
- spec/fixtures/factorial.js
|
74
|
+
- spec/fixtures/with_spaces.rb
|
75
|
+
- spec/fixtures/with_tabs.rb
|
74
76
|
- spec/process_spec.rb
|
75
77
|
- spec/spec_helper.rb
|
76
78
|
homepage: ''
|
@@ -93,12 +95,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
95
|
version: '0'
|
94
96
|
requirements: []
|
95
97
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.2.2
|
97
99
|
signing_key:
|
98
100
|
specification_version: 4
|
99
101
|
summary: See the README
|
100
102
|
test_files:
|
101
103
|
- spec/fixtures/coffeetech.js
|
102
104
|
- spec/fixtures/factorial.js
|
105
|
+
- spec/fixtures/with_spaces.rb
|
106
|
+
- spec/fixtures/with_tabs.rb
|
103
107
|
- spec/process_spec.rb
|
104
108
|
- spec/spec_helper.rb
|