oreilly-snippets 0.0.1 → 0.0.2

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: 6c1f282b603c87774de48d0528a5a5c502b7f781
4
- data.tar.gz: 1107766b02889b4bd04852cc6010594ffd347d1c
3
+ metadata.gz: c6d5f18cb671bae8c31619c1d908d7c0636575b1
4
+ data.tar.gz: 5c717da0a54e70e02225244fb6e553099a45a91f
5
5
  SHA512:
6
- metadata.gz: 0cfb0a42084e9391dfcbf575c327a48e3cab0fbf18214b4d7227f3c4d008dae3c65a3692d335faff429ac8ff81016b94c5e37efda3c7a0efdbf16eb521858f85
7
- data.tar.gz: f8ea02d98b973a31bc8dcbedf52a471b40e0673646e16c12a7be2b3fbefbe489f240a03bbbe99e4039a3a8ba3f638973c2d58f70aa569b61e45fe0cc36bf294c
6
+ metadata.gz: 80c79c398493b13a443c206109c3bab60611539a25a6d426e19352579ade4d99eeb9c3a01c4df750c27e36fc37f530d0b23861d5d532cfc38478b548135d2dfa
7
+ data.tar.gz: 8d8b76b771f3bcfc01bf4c0be3b0192df890bae7713d9283e58359b51f5d8b5d34138b2f3b69f3518f1724d267f742f14dfb7b9d0916f4e146b699a976842b57
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ spec/oreilly-snippets-sample-content
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]
@@ -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]
@@ -1,5 +1,5 @@
1
1
  module Oreilly
2
2
  module Snippets
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
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="../../github.js.test", language="js", sha="8e05a916fe0b1a9d3e:coffeetech.js"]
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
- describe "#parse" do
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
- it "should parse wrapped items" do
74
- outputs = Oreilly::Snippets.parse( WRAPPED_BY_SOURCE )
75
- output = outputs[0]
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
- it "should parse the file and extract the correct things" do
82
- outputs = Oreilly::Snippets.parse( TEMPLATE )
83
- output = outputs[0]
84
- output[:filename].should == "spec/fixtures/factorial.js"
85
- output[:language].should == "js"
86
- output[:identifier].should == "FACTORIAL_FUNC"
87
- output[:full].strip.should == FULL.strip
88
- end
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
- it "should get the SHA when specified" do
91
- outputs = Oreilly::Snippets.parse( WITH_SHA )
92
- output = outputs[0]
93
- output[:sha].should == "8e05a916fe0b1a9d3e:coffeetech.js"
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
- it "should process a complex file" do
107
- output = Oreilly::Snippets.process( WRAPPED_BY_SOURCE )
108
- output.should_not match( /MODULE_DEFINITION/ )
109
- output.should_not match( /\/\/$/ )
110
- end
111
-
112
- it "should process a simple file" do
113
- output = Oreilly::Snippets.process( TEMPLATE )
114
- output.should match( /ABC/ )
115
- output.should match( /DEF/ )
116
- output.should match( /function factorial\(number\)/ )
117
- output.should_not match( /BEGIN FACTORIAL_FUNC/ )
118
- output.should_not match( /END FACTORIAL_FUNC/ )
119
- end
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
- # NYI
122
- # it "should remove all identifiers when processing" do
123
- # output = Oreilly::Snippets.process( LOTS_OF_IDENTIFIERS )
124
- # output.should_not match( /BEGIN/ )
125
- # end
126
-
127
- describe "#git" do
128
- it "should retrieve by SHA if specified" do
129
- output = Oreilly::Snippets.process( WITH_SHA )
130
- ORIGINAL_CONTENTS.strip.should == output.strip
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.1
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-04 00:00:00.000000000 Z
11
+ date: 2014-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler