Skip to content

Commit 41fc8da

Browse files
committed
Add normalization of callouts
1 parent ead0907 commit 41fc8da

7 files changed

+70
-3
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,22 @@ 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

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

150151
NB: This format of snippets is not currently compatible with Atlas
151152
from O'Reilly. However, you can always process the snippet and write
152153
out a normal Asciidoc file, a file which will be compatible with
153154
Atlas. See below for an example using Guard.
154155

156+
### Normalizing Callouts
157+
158+
If you use callouts, you might run into a situation where you write 10
159+
of them inside a snippet, and then you break that snippet into
160+
two. Your second snippet will have callouts 6-10. If you specify
161+
`normcallouts="true"` in your snippet, this module will rewrite those to
162+
start from 1.
163+
155164
### Using with Guard and live-reload
156165

157166
One nice workflow is to edit files in your editor, then have guard

lib/oreilly/snippets.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def self.config( opts )
1515
@@_config.merge!( opts )
1616
end
1717

18-
def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil, flatten=false )
18+
def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil, flatten=false, normcallouts=false )
1919
contents = nil
2020
line_numbers = nil
2121
error = false
@@ -62,11 +62,22 @@ def self.get_content_from_file( spec, identifier, language, sha=nil, numbers=nil
6262
end
6363
end
6464

65+
if normcallouts
66+
rv = normalize_callouts( rv )
67+
end
68+
6569
rv = "INVALID SNIPPET, WARNING" if error
6670
# rv = scrub_other_identifiers( contents, comments )
6771
rv
6872
end
6973

74+
def self.normalize_callouts( rv )
75+
# Find something that looks like comment character + whitespace + < + number + >
76+
index = 0
77+
rv.gsub!( /([\/#]) <\d+>/ ) { |c| index += 1; "#{$1} <#{index}>" }
78+
rv
79+
end
80+
7081
def self.skip_flattening( language )
7182
rv = ( !!@@_config[:skip_flattening] and language and !!@@_config[:skip_flattening][language.to_sym] )
7283
# rv = ( !!@@_config[:skip_flattening] and !!@@_config[:skip_flattening][language.to_sym] )
@@ -107,7 +118,7 @@ def self.process( input )
107118
rv = input
108119
if snippets and snippets.length > 0
109120
snippets.each do |s|
110-
content = get_content_from_file( s[:filename], s[:identifier], s[:language], s[:sha], s[:lines], s[:flatten] )
121+
content = get_content_from_file( s[:filename], s[:identifier], s[:language], s[:sha], s[:lines], s[:flatten], s[:normcallouts] )
111122
rv = rv.gsub( s[:full], content )
112123
end
113124
end

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.11"
3+
VERSION = "0.0.12"
44
end
55
end
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
OK().do( <2> ).it()
2+
another.thing( <3> ).bad()

spec/fixtures/normalize_callouts.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
something().is().here() // <2>
2+
another.thing().ok() // <3>

spec/fixtures/normalize_callouts.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
something.do.it # <2>
2+
yeah.it.works # <3>

spec/process_spec.rb

+41
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@
3737
3838
END
3939

40+
NORMALIZE_CALLOUTS_JS = <<"END"
41+
[filename="spec/fixtures/normalize_callouts.js", normcallouts="true"]
42+
snippet~~~~
43+
...
44+
snippet~~~~
45+
END
46+
47+
HONEYPOT_NORMALIZE_CALLOUTS = <<"END"
48+
[filename="spec/fixtures/normalize_callouts.honey", normcallouts="true"]
49+
snippet~~~~
50+
...
51+
snippet~~~~
52+
END
53+
54+
55+
NORMALIZE_CALLOUTS_RB = <<"END"
56+
[filename="spec/fixtures/normalize_callouts.rb", normcallouts="true"]
57+
snippet~~~~
58+
...
59+
snippet~~~~
60+
END
61+
4062

4163
FULL = <<END
4264
[filename="spec/fixtures/factorial.js", language="js", identifier="FACTORIAL_FUNC"]
@@ -195,6 +217,25 @@ def download_test_repository
195217
output.should_not match( /END FACTORIAL_FUNC/ )
196218
end
197219

220+
describe "#normcallouts" do
221+
it "should normalize callouts" do
222+
output = Oreilly::Snippets.process( NORMALIZE_CALLOUTS_JS )
223+
output.should match( /<1>/ )
224+
output.should_not match( /<3>/ )
225+
end
226+
227+
it "should normalize callouts with alternative comments" do
228+
output = Oreilly::Snippets.process( NORMALIZE_CALLOUTS_RB )
229+
output.should match( /<1>/ )
230+
output.should_not match( /<3>/ )
231+
end
232+
233+
it "should not mistakenly normalize callouts" do
234+
output = Oreilly::Snippets.process( HONEYPOT_NORMALIZE_CALLOUTS )
235+
output.should match( /<2>/ )
236+
end
237+
end
238+
198239
describe "#flatten" do
199240
before( :each ) do
200241
@with_spaces = File.read( "spec/fixtures/with_spaces.rb" )

0 commit comments

Comments
 (0)