1
1
require 'asciidoctor/extensions'
2
2
require 'stringio'
3
+ require 'asciidoctor/logging'
3
4
4
5
# Spring REST Docs block macro to import multiple snippet of an operation at
5
6
# once
11
12
class OperationBlockMacro < Asciidoctor ::Extensions ::BlockMacroProcessor
12
13
use_dsl
13
14
named :operation
15
+ include Asciidoctor ::Logging
14
16
15
17
def process ( parent , operation , attributes )
16
18
snippets_dir = parent . document . attributes [ 'snippets' ] . to_s
@@ -27,8 +29,9 @@ def read_snippets(snippets_dir, snippet_names, parent, operation,
27
29
snippet_titles )
28
30
snippets = snippets_to_include ( snippet_names , snippets_dir , operation )
29
31
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
32
35
"No snippets found for operation::#{ operation } "
33
36
else
34
37
do_read_snippets ( snippets , parent , operation , snippet_titles )
@@ -41,7 +44,7 @@ def do_read_snippets(snippets, parent, operation, snippet_titles)
41
44
section_id = parent . id
42
45
snippets . each do |snippet |
43
46
append_snippet_block ( content , snippet , section_id ,
44
- operation , snippet_titles )
47
+ operation , snippet_titles , parent )
45
48
end
46
49
content . string
47
50
end
@@ -87,17 +90,18 @@ def all_snippets(snippets_dir, operation)
87
90
end
88
91
89
92
def append_snippet_block ( content , snippet , section_id ,
90
- operation , snippet_titles )
93
+ operation , snippet_titles , parent )
91
94
write_title content , snippet , section_id , snippet_titles
92
- write_content content , snippet , operation
95
+ write_content content , snippet , operation , parent
93
96
end
94
97
95
- def write_content ( content , snippet , operation )
98
+ def write_content ( content , snippet , operation , parent )
96
99
if File . file? snippet . path
97
100
content . puts File . readlines ( snippet . path , :encoding => 'UTF-8' ) . join
98
101
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
101
105
content . puts "Snippet #{ snippet . name } not found for" \
102
106
" operation::#{ operation } "
103
107
content . puts ''
@@ -149,4 +153,4 @@ def title_for_snippet(snippet)
149
153
end
150
154
end
151
155
end
152
- end
156
+ end
0 commit comments