11require 'asciidoctor/extensions'
22require 'stringio'
3+ require 'asciidoctor/logging'
34
45# Spring REST Docs block macro to import multiple snippet of an operation at
56# once
1112class OperationBlockMacro < Asciidoctor ::Extensions ::BlockMacroProcessor
1213 use_dsl
1314 named :operation
15+ include Asciidoctor ::Logging
1416
1517 def process ( parent , operation , attributes )
1618 snippets_dir = parent . document . attributes [ 'snippets' ] . to_s
@@ -27,8 +29,9 @@ def read_snippets(snippets_dir, snippet_names, parent, operation,
2729 snippet_titles )
2830 snippets = snippets_to_include ( snippet_names , snippets_dir , operation )
2931 if snippets . empty?
30- warn "No snippets were found for operation #{ operation } in" \
31- "#{ snippets_dir } "
32+ location = parent . document . reader . cursor_at_mark
33+ logger . warn message_with_context "No snippets were found for operation #{ operation } in " \
34+ "#{ snippets_dir } " , source_location : location
3235 "No snippets found for operation::#{ operation } "
3336 else
3437 do_read_snippets ( snippets , parent , operation , snippet_titles )
@@ -41,7 +44,7 @@ def do_read_snippets(snippets, parent, operation, snippet_titles)
4144 section_id = parent . id
4245 snippets . each do |snippet |
4346 append_snippet_block ( content , snippet , section_id ,
44- operation , snippet_titles )
47+ operation , snippet_titles , parent )
4548 end
4649 content . string
4750 end
@@ -87,17 +90,18 @@ def all_snippets(snippets_dir, operation)
8790 end
8891
8992 def append_snippet_block ( content , snippet , section_id ,
90- operation , snippet_titles )
93+ operation , snippet_titles , parent )
9194 write_title content , snippet , section_id , snippet_titles
92- write_content content , snippet , operation
95+ write_content content , snippet , operation , parent
9396 end
9497
95- def write_content ( content , snippet , operation )
98+ def write_content ( content , snippet , operation , parent )
9699 if File . file? snippet . path
97100 content . puts File . readlines ( snippet . path , :encoding => 'UTF-8' ) . join
98101 else
99- warn "Snippet #{ snippet . name } not found at #{ snippet . path } for" \
100- " operation #{ operation } "
102+ location = parent . document . reader . cursor_at_mark
103+ logger . warn message_with_context "Snippet #{ snippet . name } not found at #{ snippet . path } for" \
104+ " operation #{ operation } " , source_location : location
101105 content . puts "Snippet #{ snippet . name } not found for" \
102106 " operation::#{ operation } "
103107 content . puts ''
@@ -149,4 +153,4 @@ def title_for_snippet(snippet)
149153 end
150154 end
151155 end
152- end
156+ end
0 commit comments