@@ -48,39 +48,59 @@ def process_block(block)
48
48
return unless %w[ console sense kibana ] . include? lang
49
49
50
50
snippet = block . attr 'snippet'
51
- if snippet
52
- # If you specify the snippet path then we should copy it into the
53
- # destination directory so it is available for Kibana.
54
- snippet_path = "snippets/#{ snippet } "
55
- normalized = block . normalize_system_path ( snippet_path , block . document . base_dir )
56
- if File . readable? normalized
57
- copy_snippet block , normalized , snippet_path
58
- logger . warn message_with_context "reading snippets from a path makes the book harder to read" , :source_location => block . source_location
51
+ snippet_path =
52
+ if snippet
53
+ handle_override_snippet block , snippet
59
54
else
60
- logger . error message_with_context "can't read snippet from #{ normalized } " , :source_location => block . source_location
55
+ handle_implicit_snippet block , lang
61
56
end
62
- else
63
- # If you don't specify the snippet then we assign it a number and read
64
- # the contents of the source listing, copying it to the destination
65
- # directory so it is available for Kibana.
66
- snippet_number = block . document . attr 'snippet_number' , 1
67
- snippet = "#{ snippet_number } .#{ lang } "
68
- block . document . set_attr 'snippet_number' , snippet_number + 1
69
-
70
- snippet_path = "snippets/#{ snippet } "
71
- source = block . source . gsub ( CALLOUT_SCAN_RX , '' ) + "\n "
72
- write_snippet block , source , snippet_path
73
- end
74
- block . set_attr 'snippet_link' , "<ulink type=\" snippet\" url=\" #{ snippet_path } \" />"
57
+ block . set_attr 'snippet_link' ,
58
+ "<ulink type=\" snippet\" url=\" #{ snippet_path } \" />"
75
59
block . document . register :links , snippet_path
76
60
77
61
def block . content
78
62
"#{ @attributes [ 'snippet_link' ] } #{ super } "
79
63
end
80
64
end
81
65
82
- def copy_snippet ( block , source , uri )
83
- logger . info message_with_context "copying snippet #{ source } " , :source_location => block . source_location
66
+ ##
67
+ # Copy explicitly configured snippets to the right path so kibana can pick
68
+ # them up and warn the user that they are lame.
69
+ def handle_override_snippet ( block , snippet )
70
+ snippet_path = "snippets/#{ snippet } "
71
+ normalized = block . normalize_system_path ( snippet_path ,
72
+ block . document . base_dir )
73
+ if File . readable? normalized
74
+ copy_override_snippet block , normalized , snippet_path
75
+ message = "reading snippets from a path makes the book harder to read"
76
+ logger . warn message_with_context message ,
77
+ source_location : block . source_location
78
+ else
79
+ logger . error message_with_context "can't read snippet from #{ normalized } " ,
80
+ source_location : block . source_location
81
+ end
82
+ snippet_path
83
+ end
84
+
85
+ ##
86
+ # Handles non-override snippets by assigning them a number and copying them
87
+ # some place that kibana can read them.
88
+ def handle_implicit_snippet ( block , lang )
89
+ snippet_number = block . document . attr 'snippet_number' , 1
90
+ snippet = "#{ snippet_number } .#{ lang } "
91
+ block . document . set_attr 'snippet_number' , snippet_number + 1
92
+
93
+ snippet_path = "snippets/#{ snippet } "
94
+ source = block . source . gsub ( CALLOUT_SCAN_RX , '' ) + "\n "
95
+ write_snippet block , source , snippet_path
96
+ snippet_path
97
+ end
98
+
99
+ ##
100
+ # Copies an override snippet from the filesystem into the snippets directory.
101
+ def copy_override_snippet ( block , source , uri )
102
+ logger . info message_with_context "copying snippet #{ source } " ,
103
+ source_location : block . source_location
84
104
copy_proc = block . document . attr 'copy_snippet'
85
105
if copy_proc
86
106
# Delegate to a proc for copying if one is defined. Used for testing.
@@ -93,8 +113,12 @@ def copy_snippet(block, source, uri)
93
113
end
94
114
end
95
115
116
+ ##
117
+ # Writes a snippet extracted from the asciidoc file into the
118
+ # snippets directory.
96
119
def write_snippet ( block , snippet , uri )
97
- logger . info message_with_context "writing snippet #{ uri } " , :source_location => block . source_location
120
+ logger . info message_with_context "writing snippet #{ uri } " ,
121
+ source_location : block . source_location
98
122
write_proc = block . document . attr 'write_snippet'
99
123
if write_proc
100
124
# Delegate to a proc for copying if one is defined. Used for testing.
0 commit comments