Skip to content

Commit 8db9b84

Browse files
committedAug 17, 2015
Add capability to ignore language flattening on case by case basis
1 parent 938a19c commit 8db9b84

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ if anotherVar == "anotherVar"
144144
If you want the default to be to flatten (avoiding setting it each
145145
snippet declaration), you can set that using the config method: `Oreilly::Snippets.config( flatten: true )`
146146

147+
At the moment, flattening does not work perfectly for Java files. You can ignore java with `Oreilly::Snippets.config( flatten: true, flatten_exceptions: { java: true } )`
148+
147149
#### Incompatibilities with Atlas from O'Reilly
148150

149151
NB: This format of snippets is not currently compatible with Atlas

‎lib/oreilly/snippets.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil
5555
rv = contents
5656
end
5757

58-
if flatten or @@_config[:flatten]
58+
if ( flatten or @@_config[:flatten] ) and not flatten_exceptions( language )
5959
rv = flatten_it( rv )
6060
end
6161

@@ -64,6 +64,10 @@ def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil
6464
rv
6565
end
6666

67+
def self.flatten_exceptions( language )
68+
@@_config[:flatten_exceptions] and @@_config[:flatten_exceptions][language.to_sym]
69+
end
70+
6771
def self.flatten_it( content )
6872
# find the smallest indent level, and then strip that off the beginning of all lines
6973
smallest = nil

‎lib/oreilly/snippets/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Oreilly
22
module Snippets
3-
VERSION = "0.0.8"
3+
VERSION = "0.0.9"
44
end
55
end

‎spec/fixtures/factorial.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
public class Factorial {
3+
public static void main( String[] args ) {
4+
// Do something
5+
}
6+
}

‎spec/process_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
snippet~~~~
5555
END
5656

57+
DONT_USE_JAVA_FOR_FLATTENING = <<END
58+
[filename="spec/fixtures/factorial.java", language="java", lines="3..5"]
59+
snippet~~~~
60+
Put any descriptive text you want here. It will be replaced with the
61+
specified code snippet when you build ebook outputs
62+
snippet~~~~
63+
END
64+
5765
TEMPLATE = <<END
5866
5967
ABC
@@ -202,6 +210,17 @@ def download_test_repository
202210
lines[-1][0].should match /\s/ # Last line is not indented
203211
end
204212

213+
it "should not flatten if java is used and properly configured" do
214+
string = <<END
215+
public static void main( String[] args ) {
216+
// Do something
217+
}
218+
END
219+
Oreilly::Snippets.config( flatten: true, flatten_exceptions: { java: true } )
220+
output = Oreilly::Snippets.process( DONT_USE_JAVA_FOR_FLATTENING )
221+
string.should eq( output )
222+
end
223+
205224
it "should support flattening with tabs" do
206225
output = Oreilly::Snippets.process( FLATTEN_WITH_TABS )
207226
output.should == @tabs_flattened

0 commit comments

Comments
 (0)
Please sign in to comment.