Skip to content

Commit

Permalink
Update id of figures on loops
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandobrito committed Jul 3, 2017
1 parent 6ca2454 commit eebd895
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/sablon/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ def evaluate(env)
value = value.to_ary if value.respond_to?(:to_ary)
raise ContextError, "The expression #{list_expr.inspect} should evaluate to an enumerable but was: #{value.inspect}" unless value.is_a?(Enumerable)

content = value.flat_map do |item|
contents = value.flat_map do |item|
iter_env = env.alter_context(iterator_name => item)
block.process(iter_env)
end
block.replace(content.reverse)

locate_and_update_ids(contents)
block.replace(contents.reverse)
end

def locate_and_update_ids(contents)
contents.each_with_index do |content, index|
elements_with_id = content.search('.//*[@id]')
elements_with_id.each { |element| element['id'] += index.to_s }
end
end
end

Expand Down
90 changes: 90 additions & 0 deletions test/fixtures/xml/figure_loop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<w:p w:rsidR="00F14945" w:rsidRDefault="001B67B3">
<w:fldSimple w:instr=" MERGEFIELD numbers:each(number) \* MERGEFORMAT ">
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>«numbers:each(number)»</w:t>
</w:r>
</w:fldSimple>
</w:p>
<w:p w:rsidR="001B67B3" w:rsidRDefault="001B67B3">
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<mc:Choice Requires="wps">
<w:drawing>
<wp:anchor xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251659264" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1" wp14:anchorId="6945DEE5" wp14:editId="3884E855">
<wp:simplePos x="0" y="0" />
<wp:positionH relativeFrom="column">
<wp:posOffset>0</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="paragraph">
<wp:posOffset>-9525</wp:posOffset>
</wp:positionV>
<wp:extent cx="228600" cy="200025" />
<wp:effectExtent l="0" t="0" r="19050" b="28575" />
<wp:wrapNone />
<wp:docPr id="1" name="Rectangle 1" />
<wp:cNvGraphicFramePr />
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:wsp xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:cNvSpPr />
<wps:spPr>
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="228600" cy="200025" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
</wps:spPr>
<wps:style>
<a:lnRef idx="2">
<a:schemeClr val="accent1">
<a:shade val="50000" />
</a:schemeClr>
</a:lnRef>
<a:fillRef idx="1">
<a:schemeClr val="accent1" />
</a:fillRef>
<a:effectRef idx="0">
<a:schemeClr val="accent1" />
</a:effectRef>
<a:fontRef idx="minor">
<a:schemeClr val="lt1" />
</a:fontRef>
</wps:style>
<wps:bodyPr rot="0" spcFirstLastPara="0" vertOverflow="overflow" horzOverflow="overflow" vert="horz" wrap="square" lIns="91440" tIns="45720" rIns="91440" bIns="45720" numCol="1" spcCol="0" rtlCol="0" fromWordArt="0" anchor="ctr" anchorCtr="0" forceAA="0" compatLnSpc="1">
<a:prstTxWarp prst="textNoShape">
<a:avLst />
</a:prstTxWarp>
<a:noAutofit />
</wps:bodyPr>
</wps:wsp>
</a:graphicData>
</a:graphic>
</wp:anchor>
</w:drawing>
</mc:Choice>
<mc:Fallback>
<w:pict>
<v:rect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" w14:anchorId="79EAB187" id="Rectangle 1" o:spid="_x0000_s1026" style="position:absolute;margin-left:0;margin-top:-.75pt;width:18pt;height:15.75pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:middle" o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF&amp;#xA;90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6RA&amp;#xA;0s5Y6ir5vt9lD1JwBDIwOMJKHpHlpr69KfdHjyxSmriSfYz+USnWPY7AufNIadK6MEJMx9ApD/oD&amp;#xA;OlTrorhX2lFEilmcO2RdNtjC5xDF9pCuTyYBB5bi6bQ4syoJ3g9WQ0ymaiLzg5KdCXlKLjvcW893&amp;#xA;SUOqXwnz5DrgnHtJTxOsQfEKIT7DmDSUCaxw7Rqn8787ZsmRM9e2VmPeBN4uqYvTtW7jvijg9N/y&amp;#xA;JsXecLq0q+WD6m8AAAD//wMAUEsDBBQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAX3JlbHMvLnJl&amp;#xA;bHOkkMFqwzAMhu+DvYPRfXGawxijTi+j0GvpHsDYimMaW0Yy2fr2M4PBMnrbUb/Q94l/f/hMi1qR&amp;#xA;JVI2sOt6UJgd+ZiDgffL8ekFlFSbvV0oo4EbChzGx4f9GRdb25HMsYhqlCwG5lrLq9biZkxWOiqY&amp;#xA;22YiTra2kYMu1l1tQD30/bPm3wwYN0x18gb45AdQl1tp5j/sFB2T0FQ7R0nTNEV3j6o9feQzro1i&amp;#xA;OWA14Fm+Q8a1a8+Bvu/d/dMb2JY5uiPbhG/ktn4cqGU/er3pcvwCAAD//wMAUEsDBBQABgAIAAAA&amp;#x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&amp;#xA;vssJTqvRrGbeVNs5eHHBIXWRNBRrBQKpibajVsPX5/vqBUTKhqzxkVDDggm29e1NZUobJzrg5Zhb&amp;#xA;wSGUSqPB5dyXUqbGYTBpHXsk9r7jEExmObTSDmbi8ODlg1IbGUxH3OBMjzuHzfk4Bi4xeFiK52l3&amp;#xA;3rv5o0O//OC4aH1/N7+9gsg4579n+MVndKiZ6RRHskl4DTwka1gVTyDYfdywPvFVCmRdyf/09RUA&amp;#xA;AP//AwBQSwECLQAUAAYACAAAACEAtoM4kv4AAADhAQAAEwAAAAAAAAAAAAAAAAAAAAAAW0NvbnRl&amp;#xA;bnRfVHlwZXNdLnhtbFBLAQItABQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAAAAAAAAAAAAAAC8B&amp;#xA;AABfcmVscy8ucmVsc1BLAQItABQABgAIAAAAIQDz/KtwcAIAADgFAAAOAAAAAAAAAAAAAAAAAC4C&amp;#xA;AABkcnMvZTJvRG9jLnhtbFBLAQItABQABgAIAAAAIQC6u+OI2AAAAAUBAAAPAAAAAAAAAAAAAAAA&amp;#xA;AMoEAABkcnMvZG93bnJldi54bWxQSwUGAAAAAAQABADzAAAAzwUAAAAA&amp;#xA;" fillcolor="#5b9bd5 [3204]" strokecolor="#1f4d78 [1604]" strokeweight="1pt" />
</w:pict>
</mc:Fallback>
</mc:AlternateContent>
</w:r>
</w:p>
<w:p w:rsidR="001B67B3" w:rsidRDefault="001B67B3">
<w:fldSimple w:instr=" MERGEFIELD numbers:endEach \* MERGEFORMAT ">
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>«numbers:endEach»</w:t>
</w:r>
</w:fldSimple>
</w:p>
12 changes: 12 additions & 0 deletions test/processor/document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ def test_single_row_table_loop
document
end

# Figures have unique identifiers
# Loop should assign random identifiers to all figures
def test_loop_with_figure
original = snippet("figure_loop")
result = process(original, { numbers: [1, 2] })

result_ids = Nokogiri::XML(result).search("//@id").map(&:value)

# asserts there are no repeated ids
assert_equal result_ids.length, result_ids.uniq.length
end

def test_loop_over_collection_convertable_to_an_enumerable
style_collection = Class.new do
def to_ary
Expand Down

0 comments on commit eebd895

Please sign in to comment.