oreilly-snippets 0.0.1 → 0.0.2
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 +6 -0
- data/lib/oreilly/snippets.rb +18 -2
- data/lib/oreilly/snippets/version.rb +1 -1
- data/spec/process_spec.rb +95 -53
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6d5f18cb671bae8c31619c1d908d7c0636575b1
|
4
|
+
data.tar.gz: 5c717da0a54e70e02225244fb6e553099a45a91f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80c79c398493b13a443c206109c3bab60611539a25a6d426e19352579ade4d99eeb9c3a01c4df750c27e36fc37f530d0b23861d5d532cfc38478b548135d2dfa
|
7
|
+
data.tar.gz: 8d8b76b771f3bcfc01bf4c0be3b0192df890bae7713d9283e58359b51f5d8b5d34138b2f3b69f3518f1724d267f742f14dfb7b9d0916f4e146b699a976842b57
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -18,6 +18,12 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
### Library usage
|
22
|
+
|
23
|
+
Send a string with proper snippet code into the process method:
|
24
|
+
|
25
|
+
`Oreilly::Snippets.process( asciidoc_string )`
|
26
|
+
|
21
27
|
### Snippets Usage
|
22
28
|
|
23
29
|
Check out (Code Snippets)[http://chimera.labs.oreilly.com/books/1230000000065/ch04.html#code_explanation]
|
data/lib/oreilly/snippets.rb
CHANGED
@@ -10,16 +10,32 @@ module Oreilly
|
|
10
10
|
|
11
11
|
def self.get_content_from_file( spec, identifier, language, sha=nil )
|
12
12
|
contents = nil
|
13
|
+
line_numbers = nil
|
14
|
+
|
13
15
|
if sha
|
16
|
+
|
17
|
+
if sha.count( ":" ) > 1
|
18
|
+
# Strip off the line numbers and use them later
|
19
|
+
first = sha.index ":"
|
20
|
+
second = (sha.index ":", first+1)+1
|
21
|
+
numbers = sha[second..-1]
|
22
|
+
sae = numbers.split( ".." ).map { |d| Integer(d) }
|
23
|
+
sha = sha[0..second-2]
|
24
|
+
line_numbers = [sae[0], sae[1]]
|
25
|
+
end
|
14
26
|
# Use the filename to change into the directory and use git-show
|
15
27
|
cwd = Dir.pwd
|
16
|
-
Dir.chdir spec
|
28
|
+
Dir.chdir spec if spec
|
17
29
|
contents = `git show #{sha}`
|
18
|
-
Dir.chdir cwd
|
30
|
+
Dir.chdir cwd if spec
|
31
|
+
|
19
32
|
else
|
20
33
|
contents = File.read( spec )
|
21
34
|
end
|
22
35
|
|
36
|
+
# If line numbers are there, provide only that content
|
37
|
+
contents = contents.split( /\n/ )[line_numbers[0]..line_numbers[1]].join( "\n" ) if line_numbers
|
38
|
+
|
23
39
|
rv = nil
|
24
40
|
if identifier
|
25
41
|
comments = COMMENTS[language.to_sym]
|
data/spec/process_spec.rb
CHANGED
@@ -1,7 +1,20 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
TEST_REPO = "oreilly-snippets-sample-content"
|
6
|
+
GITHUB_REPO = "https://github.com/xrd/#{TEST_REPO}.git"
|
7
|
+
ROOT = File.join "spec", TEST_REPO
|
2
8
|
|
3
9
|
WITH_SHA = <<END
|
4
|
-
[filename="
|
10
|
+
[filename="#{ROOT}", language="js", sha="c863f786f5959799d7c:test.js"]
|
11
|
+
snippet~~~~
|
12
|
+
Put any descriptive text you want here. It will be replaced with the
|
13
|
+
snippet~~~~
|
14
|
+
END
|
15
|
+
|
16
|
+
WITH_SHA_LINE_NUMBERS = <<END
|
17
|
+
[filename="#{ROOT}", language="js", sha="c863f786f5959799d7c:test.js:1..3"]
|
5
18
|
snippet~~~~
|
6
19
|
Put any descriptive text you want here. It will be replaced with the
|
7
20
|
snippet~~~~
|
@@ -68,67 +81,96 @@ snippet~~~~~
|
|
68
81
|
|
69
82
|
END
|
70
83
|
|
71
|
-
|
84
|
+
def download_test_repository
|
85
|
+
root = File.join( "spec", TEST_REPO )
|
86
|
+
unless File.exists? root
|
87
|
+
`git clone #{GITHUB_REPO} #{root}`
|
88
|
+
end
|
89
|
+
end
|
72
90
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
output[:filename].should == "spec/fixtures/coffeetech.js"
|
77
|
-
output[:language].should == "js"
|
78
|
-
output[:identifier].should == "MODULE_DEFINITION"
|
91
|
+
describe Oreilly::Snippets do
|
92
|
+
before( :all ) do
|
93
|
+
download_test_repository()
|
79
94
|
end
|
80
95
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
96
|
+
describe "#parse" do
|
97
|
+
|
98
|
+
it "should parse wrapped items" do
|
99
|
+
outputs = Oreilly::Snippets.parse( WRAPPED_BY_SOURCE )
|
100
|
+
output = outputs[0]
|
101
|
+
output[:filename].should == "spec/fixtures/coffeetech.js"
|
102
|
+
output[:language].should == "js"
|
103
|
+
output[:identifier].should == "MODULE_DEFINITION"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should parse the file and extract the correct things" do
|
107
|
+
outputs = Oreilly::Snippets.parse( TEMPLATE )
|
108
|
+
output = outputs[0]
|
109
|
+
output[:filename].should == "spec/fixtures/factorial.js"
|
110
|
+
output[:language].should == "js"
|
111
|
+
output[:identifier].should == "FACTORIAL_FUNC"
|
112
|
+
output[:full].strip.should == FULL.strip
|
113
|
+
end
|
89
114
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
115
|
+
it "should get the SHA when specified" do
|
116
|
+
outputs = Oreilly::Snippets.parse( WITH_SHA )
|
117
|
+
output = outputs[0]
|
118
|
+
output[:sha].should == "c863f786f5959799d7c:test.js"
|
119
|
+
end
|
94
120
|
end
|
95
|
-
end
|
96
121
|
|
97
|
-
# describe "#scrub_other_identifiers" do
|
98
|
-
# it "should scrub everything that looks like an identifier" do
|
99
|
-
# out = Oreilly::Snippets.scrub_other_identifiers( File.read( "spec/fixtures/coffeetech.js" ), "//" )
|
100
|
-
# out.should_not match( /FOOBAR/ )
|
101
|
-
# end
|
102
|
-
# end
|
122
|
+
# describe "#scrub_other_identifiers" do
|
123
|
+
# it "should scrub everything that looks like an identifier" do
|
124
|
+
# out = Oreilly::Snippets.scrub_other_identifiers( File.read( "spec/fixtures/coffeetech.js" ), "//" )
|
125
|
+
# out.should_not match( /FOOBAR/ )
|
126
|
+
# end
|
127
|
+
# end
|
103
128
|
|
104
|
-
describe "#process" do
|
129
|
+
describe "#process" do
|
105
130
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
131
|
+
it "should process a complex file" do
|
132
|
+
output = Oreilly::Snippets.process( WRAPPED_BY_SOURCE )
|
133
|
+
output.should_not match( /MODULE_DEFINITION/ )
|
134
|
+
output.should_not match( /\/\/$/ )
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should process a simple file" do
|
138
|
+
output = Oreilly::Snippets.process( TEMPLATE )
|
139
|
+
output.should match( /ABC/ )
|
140
|
+
output.should match( /DEF/ )
|
141
|
+
output.should match( /function factorial\(number\)/ )
|
142
|
+
output.should_not match( /BEGIN FACTORIAL_FUNC/ )
|
143
|
+
output.should_not match( /END FACTORIAL_FUNC/ )
|
144
|
+
end
|
120
145
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
146
|
+
# NYI
|
147
|
+
# it "should remove all identifiers when processing" do
|
148
|
+
# output = Oreilly::Snippets.process( LOTS_OF_IDENTIFIERS )
|
149
|
+
# output.should_not match( /BEGIN/ )
|
150
|
+
# end
|
151
|
+
|
152
|
+
describe "#git" do
|
153
|
+
it "should retrieve by SHA if specified" do
|
154
|
+
output = Oreilly::Snippets.process( WITH_SHA )
|
155
|
+
# strip the whitespace, makes comparison easier..
|
156
|
+
cwd = Dir.getwd
|
157
|
+
Dir.chdir File.join( ROOT )
|
158
|
+
original = `git show c863f786f5959799d7c11312a7ba1d603ff16339:test.js`
|
159
|
+
Dir.chdir cwd
|
160
|
+
output.strip.should == original.strip
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should retrieve by SHA and give us only certain lines" do
|
164
|
+
output = Oreilly::Snippets.process( WITH_SHA_LINE_NUMBERS )
|
165
|
+
cwd = Dir.getwd
|
166
|
+
Dir.chdir File.join( ROOT )
|
167
|
+
original = `git show c863f786f5959799d7c11312a7ba1d603ff16339:test.js`
|
168
|
+
Dir.chdir cwd
|
169
|
+
lines = original.split /\n/
|
170
|
+
original = lines[1..3].join "\n"
|
171
|
+
output.strip.should == original.strip
|
172
|
+
end
|
131
173
|
end
|
174
|
+
|
132
175
|
end
|
133
|
-
|
134
176
|
end
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Dawson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|