Skip to content

Commit e9042e8

Browse files
committed
Allow callouts to be wrapped (when used in XML)
1 parent a95ed39 commit e9042e8

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

lib/oreilly/snippets.rb

+24-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ def self.get_content_from_file( s )
2525
flatten = s[:flatten]
2626
normcallouts = s[:normcallouts]
2727
callouts_prefix = s[:callouts_prefix]
28+
callouts_wrap = s[:callouts_wrap]
2829
callouts = s[:callouts]
2930

31+
if callouts_wrap
32+
if callouts_prefix
33+
raise "Cannot use both callout prefix and wrap together"
34+
end
35+
36+
if -1 == callouts_wrap.index( '{x}' )
37+
raise "Improper use of callouts: needs to be like '<!-- {x} -->' ('{x}' must be a placeholder)"
38+
end
39+
end
40+
3041
contents = nil
3142
line_numbers = nil
3243
error = false
@@ -74,7 +85,7 @@ def self.get_content_from_file( s )
7485
end
7586

7687
if callouts
77-
rv = process_callouts( rv, callouts, callouts_prefix )
88+
rv = process_callouts( rv, callouts, callouts_prefix, callouts_wrap )
7889
elsif normcallouts
7990
rv = normalize_callouts( rv )
8091
end
@@ -84,7 +95,7 @@ def self.get_content_from_file( s )
8495
rv
8596
end
8697

87-
def self.process_callouts( input, callouts, comment_character = nil )
98+
def self.process_callouts( input, callouts, comment_character = nil, wrap = nil )
8899
rv = nil
89100
# Strip them out and figure out the comment character
90101
rv = input.gsub( /([#\/]\/?) ?<\d+>/ ) { |c| comment_character ||= $1; '' }
@@ -107,7 +118,17 @@ def self.process_callouts( input, callouts, comment_character = nil )
107118
lines = rv.split /\n/
108119
list.each_with_index do |c,i|
109120
index = c.to_i - 1
110-
lines[index] += "#{comment_character} <#{i+1}>" if lines.length > index and index >= 0
121+
122+
if lines.length > index and index >= 0
123+
callout = nil
124+
if wrap
125+
callout = wrap.gsub( '{x}', "<#{i+1}>" )
126+
else
127+
callout = "#{comment_character} <#{i+1}>"
128+
end
129+
130+
lines[index] += callout
131+
end
111132
end
112133
rv = lines.join "\n"
113134
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.17"
3+
VERSION = "0.0.18"
44
end
55
end

spec/fixtures/some.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<xml>
2+
<item>
3+
<something attr="attr1"/>
4+
</item>
5+
</xml>

spec/process_spec.rb

+57
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@
8787
snippet~~~~
8888
END
8989

90+
CALLOUTS_WRAP_AND_PREFIX_JS = <<"END"
91+
[filename="spec/fixtures/normalize_callouts_long.js", callouts_prefix="#", callouts_wrap="# {1} #", callouts="1,10,15"]
92+
snippet~~~~
93+
...
94+
snippet~~~~
95+
END
96+
97+
CALLOUTS_WRAP_XML = <<"END"
98+
[filename="spec/fixtures/some.xml", callouts_wrap="<!-- {x} -->", callouts="1,2,3"]
99+
snippet~~~~
100+
...
101+
snippet~~~~
102+
END
103+
104+
INVALID_CALLOUTS_WRAP_XML = <<"END"
105+
[filename="spec/fixtures/some.xml", callouts_wrap="<!-- no x! -->", callouts="1,2,3"]
106+
snippet~~~~
107+
...
108+
snippet~~~~
109+
END
110+
111+
90112
NORMALIZE_CALLOUTS_JS = <<"END"
91113
[filename="spec/fixtures/normalize_callouts.js", normcallouts="true"]
92114
snippet~~~~
@@ -296,6 +318,41 @@ def download_test_repository
296318
lines[14].should match( /# <3>/ )
297319
end
298320

321+
it "should add a prefix character to a callout to makes sure the code is runnable" do
322+
output = Oreilly::Snippets.process( CALLOUTS_PREFIX_JS )
323+
lines = output.split /\n/
324+
lines[0].should match( /# <1>/ )
325+
lines[9].should match( /# <2>/ )
326+
lines[14].should match( /# <3>/ )
327+
end
328+
329+
it "should properly wrap callouts" do
330+
output = Oreilly::Snippets.process( CALLOUTS_WRAP_XML )
331+
lines = output.split /\n/
332+
lines[0].should match( /<!-- <1> -->/ )
333+
lines[1].should match( /<!-- <2> -->/ )
334+
lines[2].should match( /<!-- <3> -->/ )
335+
end
336+
337+
it "should raise an error when prefix and wrap are used together for callouts" do
338+
lambda {
339+
output = Oreilly::Snippets.process( CALLOUTS_WRAP_AND_PREFIX_JS )
340+
}.should raise_error
341+
end
342+
343+
it "should raise an error when wrap is misued" do
344+
lambda {
345+
output = Oreilly::Snippets.process( INVALID_CALLOUTS_WRAP_JS )
346+
}.should raise_error
347+
end
348+
349+
it "should raise an error when wrap is misued" do
350+
lambda {
351+
output = Oreilly::Snippets.process( CALLOUTS_WRAP_AND_PREFIX_JS )
352+
}.should raise_error
353+
end
354+
355+
299356
it "should add callouts using a default comment" do
300357
output = Oreilly::Snippets.process( REALLY_LONG_CALLOUTS_JS )
301358
lines = output.split /\n/

0 commit comments

Comments
 (0)