Skip to content

Commit 1b75aab

Browse files
committed
Add support for flattening
1 parent 32936ea commit 1b75aab

File tree

6 files changed

+105
-31
lines changed

6 files changed

+105
-31
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ test/tmp
1616
test/version_tmp
1717
tmp
1818
spec/oreilly-snippets-sample-content
19+
*~

lib/oreilly/snippets.rb

+37-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
COMMENTS = {
44
:js => "\/\/",
5-
:ruby => "#"
5+
:ruby => "#",
6+
:python => "#"
67
}
78

89
module Oreilly
910
module Snippets
1011

11-
def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil )
12+
def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil, flatten=false )
1213
contents = nil
1314
line_numbers = nil
1415
error = false
1516

17+
if numbers
18+
sae = numbers.split( ".." ).map { |d| Integer(d)-1 }
19+
line_numbers = [sae[0], sae[1]]
20+
end
21+
1622
if sha
1723
if sha[0..2].eql? "xxx"
1824
contents = "PLACEHOLDER TEXT, UPDATE WITH CORRECT SHA HASH"
1925
else
20-
if numbers
21-
sae = numbers.split( ".." ).map { |d| Integer(d)-1 }
22-
line_numbers = [sae[0], sae[1]]
23-
end
2426
# Use the filename to change into the directory and use git-show
2527
cwd = Dir.pwd
2628
Dir.chdir spec if spec
@@ -33,7 +35,9 @@ def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil
3335
end
3436

3537
# If line numbers are there, provide only that content
36-
contents = contents.split( /\n/ )[line_numbers[0]..line_numbers[1]].join( "\n" ) if line_numbers
38+
if line_numbers
39+
contents = contents.split( /\n/ )[line_numbers[0]..line_numbers[1]].join( "\n" )
40+
end
3741

3842
rv = nil
3943
if identifier
@@ -45,11 +49,35 @@ def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil
4549
rv = contents
4650
end
4751

52+
if flatten
53+
rv = self.flatten_it( rv )
54+
end
55+
4856
rv = "INVALID SNIPPET, WARNING" if error
4957
# rv = scrub_other_identifiers( contents, comments )
5058
rv
5159
end
5260

61+
def self.flatten_it( content )
62+
# find the smallest indent level, and then strip that off the beginning of all lines
63+
smallest = nil
64+
lines = content.split "\n"
65+
lines.each do |l|
66+
if l =~ /^(\s+)/
67+
if smallest
68+
if $1.length < smallest.length
69+
smallest = $1
70+
end
71+
else
72+
smallest = $1
73+
end
74+
end
75+
end
76+
77+
replaced = content.gsub( /^#{smallest}/, '' )
78+
replaced
79+
end
80+
5381
def self.scrub_other_identifiers( s, comments )
5482
puts s
5583
re = /#{comments} BEGIN \S+\n(.*)\n#{comments} END \S+\n/m
@@ -62,7 +90,7 @@ def self.process( input )
6290
rv = input
6391
if snippets and snippets.length > 0
6492
snippets.each do |s|
65-
content = get_content_from_file( s[:filename], s[:identifier], s[:language], s[:sha], s[:lines] )
93+
content = get_content_from_file( s[:filename], s[:identifier], s[:language], s[:sha], s[:lines], s[:flatten] )
6694
rv = rv.gsub( s[:full], content )
6795
end
6896
end
@@ -78,7 +106,7 @@ def self.parse( input )
78106
m[0].scan( /([^=\[,\s]*)="([^"]*)"/ ) do |kv|
79107
match[kv[0].to_sym] = kv[1]
80108
end
81-
match[:full] = full.strip
109+
match[:full] = full
82110
output << match
83111
end
84112
output

spec/fixtures/with_spaces.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def foo
2+
'hi'
3+
end
4+
5+
def bar; 'hi'; end

spec/fixtures/with_tabs.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def foo
2+
'hi'
3+
end
4+
5+
def bar; 'hi'; end

spec/process_spec.rb

+55-21
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@
2727
snippet~~~~
2828
END
2929

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-
4230
LOTS_OF_IDENTIFIERS = <<END
4331
4432
[filename="spec/fixtures/coffeetech.js", language="js"]
@@ -88,6 +76,30 @@
8876
8977
END
9078

79+
FLATTEN_WITH_SPACES =<<END
80+
[filename="spec/fixtures/with_spaces.rb", language="ruby", flatten="true", lines="1..3"]
81+
snippet~~~~
82+
Put any descriptive text you want here. It will be replaced with the
83+
specified code snippet when you build ebook outputs
84+
snippet~~~~
85+
END
86+
87+
FLATTEN_NO_LINE_NUMBERS =<<END
88+
[filename="spec/fixtures/with_tabs.rb", language="ruby", flatten="true"]
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_WITH_TABS =<<END
96+
[filename="spec/fixtures/with_tabs.rb", language="ruby", flatten="true", lines="1..3"]
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+
91103
def download_test_repository
92104
root = File.join( "spec", TEST_REPO )
93105
unless File.exists? root
@@ -124,6 +136,7 @@ def download_test_repository
124136
output = outputs[0]
125137
output[:sha].should == "c863f786f5959799d7c:test.js"
126138
end
139+
127140
end
128141

129142
# describe "#scrub_other_identifiers" do
@@ -150,6 +163,30 @@ def download_test_repository
150163
output.should_not match( /END FACTORIAL_FUNC/ )
151164
end
152165

166+
describe "#flatten" do
167+
before( :each ) do
168+
@with_spaces = File.read( "spec/fixtures/with_spaces.rb" )
169+
@with_tabs = File.read( "spec/fixtures/with_tabs.rb" )
170+
@spaces_flattened = @with_spaces.split( "\n" )[0..3].join( "\n" ).gsub( /^ /, "" )
171+
@tabs_flattened = @with_tabs.split( "\n" )[0..3].join( "\n" ).gsub( /^\t\t/, "" )
172+
end
173+
174+
it "should not flatten when indentation level is zero" do
175+
output = Oreilly::Snippets.process( FLATTEN_NO_LINE_NUMBERS )
176+
output
177+
end
178+
179+
it "should support flattening with tabs" do
180+
output = Oreilly::Snippets.process( FLATTEN_WITH_TABS )
181+
output.should == @tabs_flattened
182+
end
183+
184+
it "should support flattening with spaces" do
185+
output = Oreilly::Snippets.process( FLATTEN_WITH_SPACES )
186+
output.should == @spaces_flattened
187+
end
188+
end
189+
153190
# NYI
154191
# it "should remove all identifiers when processing" do
155192
# output = Oreilly::Snippets.process( LOTS_OF_IDENTIFIERS )
@@ -169,22 +206,19 @@ def download_test_repository
169206

170207
it "should retrieve by SHA and give us only certain lines" do
171208
output = Oreilly::Snippets.process( WITH_SHA_LINE_NUMBERS )
172-
cwd = Dir.getwd
173-
Dir.chdir File.join( ROOT )
174-
original = `git show c863f786f5959799d7c11312a7ba1d603ff16339:test.js`
175-
Dir.chdir cwd
209+
original = nil
210+
Dir.chdir File.join( ROOT ) do
211+
original = `git show c863f786f5959799d7c11312a7ba1d603ff16339:test.js`
212+
end
176213
lines = original.split /\n/
177-
original = lines[0..2].join "\n"
178-
output.strip.should == original.strip
214+
original = lines[0..2].join( "\n" ) + "\n"
215+
output.should == original
179216
end
180217

181218
it "should indicate placeholder if using xxx as the sha" do
182219
output = Oreilly::Snippets.process( WITH_PLACEHOLDER_SHA )
183220
output.should match( /PLACEHOLDER/ )
184221
end
185222
end
186-
187-
188-
189223
end
190224
end

spec/spec_helper.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
require 'oreilly/snippets'
99

1010
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
1212
config.run_all_when_everything_filtered = true
1313
config.filter_run :focus
14+
config.expect_with(:rspec) { |c| c.syntax = :should }
1415

1516
# Run specs in random order to surface order dependencies. If you find an
1617
# order dependency and want to debug it, you can fix the order by providing

0 commit comments

Comments
 (0)