Skip to content

Commit 937e148

Browse files
authored
Asciidoctor: Break up another method (#775)
Rubocop identified the `OpenInWidget`'s `process_block` method as being particularly long and twisty. This breaks it apart into two more named methods which should *hopefully* make it a bit more readable. I also dropped the hash rockets while I was there.
1 parent bad9a93 commit 937e148

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

resources/asciidoctor/lib/open_in_widget/extension.rb

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,59 @@ def process_block(block)
4848
return unless %w[console sense kibana].include? lang
4949

5050
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
5954
else
60-
logger.error message_with_context "can't read snippet from #{normalized}", :source_location => block.source_location
55+
handle_implicit_snippet block, lang
6156
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}\"/>"
7559
block.document.register :links, snippet_path
7660

7761
def block.content
7862
"#{@attributes['snippet_link']}#{super}"
7963
end
8064
end
8165

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
84104
copy_proc = block.document.attr 'copy_snippet'
85105
if copy_proc
86106
# Delegate to a proc for copying if one is defined. Used for testing.
@@ -93,8 +113,12 @@ def copy_snippet(block, source, uri)
93113
end
94114
end
95115

116+
##
117+
# Writes a snippet extracted from the asciidoc file into the
118+
# snippets directory.
96119
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
98122
write_proc = block.document.attr 'write_snippet'
99123
if write_proc
100124
# Delegate to a proc for copying if one is defined. Used for testing.

0 commit comments

Comments
 (0)