Skip to content

Commit 1727b4e

Browse files
committed
Merge pull request #628 from ahus1
* gh-628: Polish "Avoid warnings when parsing AsciiDoc sub-content in Asciidoctor.load" Avoid warnings when parsing AsciiDoc sub-content in Asciidoctor.load Closes gh-628
2 parents 0e9f9fd + 9de06a3 commit 1727b4e

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ def read_snippets(snippets_dir, snippet_names, parent, operation,
3737

3838
def do_read_snippets(snippets, parent, operation, snippet_titles)
3939
content = StringIO.new
40-
section_level = parent.level + 1
4140
section_id = parent.id
4241
snippets.each do |snippet|
43-
append_snippet_block(content, snippet, section_level, section_id,
42+
append_snippet_block(content, snippet, section_id,
4443
operation, snippet_titles)
4544
end
4645
content.string
@@ -51,11 +50,12 @@ def add_blocks(content, doc, parent)
5150
fragment = Asciidoctor.load content, options
5251
fragment.blocks.each do |b|
5352
b.parent = parent
53+
b.level += parent.level
5454
parent << b
5555
end
5656
parent.find_by.each do |b|
57-
b.parent = b.parent unless b.is_a? Asciidoctor::Document
58-
end
57+
b.parent = b.parent unless b.is_a? Asciidoctor::Document
58+
end
5959
end
6060

6161
def snippets_to_include(snippet_names, snippets_dir, operation)
@@ -78,9 +78,9 @@ def all_snippets(snippets_dir, operation)
7878
.map { |file| Snippet.new(File.join(operation_dir, file), file[0..-6]) }
7979
end
8080

81-
def append_snippet_block(content, snippet, section_level, section_id,
81+
def append_snippet_block(content, snippet, section_id,
8282
operation, snippet_titles)
83-
write_title content, snippet, section_level, section_id, snippet_titles
83+
write_title content, snippet, section_id, snippet_titles
8484
write_content content, snippet, operation
8585
end
8686

@@ -96,8 +96,8 @@ def write_content(content, snippet, operation)
9696
end
9797
end
9898

99-
def write_title(content, snippet, level, id, snippet_titles)
100-
section_level = '=' * (level + 1)
99+
def write_title(content, snippet, id, snippet_titles)
100+
section_level = '=='
101101
title = snippet_titles.title_for_snippet snippet
102102
content.puts "[[#{id}_#{snippet.name.sub '-', '_'}]]"
103103
content.puts "#{section_level} #{title}"
@@ -141,4 +141,4 @@ def title_for_snippet(snippet)
141141
end
142142
end
143143
end
144-
end
144+
end

spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.asciidoctor.Options;
3636
import org.asciidoctor.OptionsBuilder;
3737
import org.asciidoctor.SafeMode;
38+
import org.junit.After;
3839
import org.junit.Before;
3940
import org.junit.Rule;
4041
import org.junit.Test;
@@ -64,6 +65,12 @@ public void setUp() throws IOException {
6465
prepareOperationSnippets(getBuildOutputLocation());
6566
this.options = OptionsBuilder.options().safe(SafeMode.UNSAFE).baseDir(getSourceLocation()).get();
6667
this.options.setAttributes(getAttributes());
68+
CapturingLogHandler.clear();
69+
}
70+
71+
@After
72+
public void verifyLogging() {
73+
assertThat(CapturingLogHandler.getLogRecords()).isEmpty();
6774
}
6875

6976
public void prepareOperationSnippets(File buildOutputLocation) throws IOException {
@@ -110,7 +117,8 @@ public void tableSnippetIncludeWithPdfBackend() throws Exception {
110117

111118
@Test
112119
public void includeSnippetInSection() throws Exception {
113-
String result = this.asciidoctor.convert("== Section\n" + "operation::some-operation[snippets='curl-request']",
120+
String result = this.asciidoctor.convert(
121+
"= A\n\nAlpha\n\n== B\n\nBravo\n\n" + "operation::some-operation[snippets='curl-request']",
114122
this.options);
115123
assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-in-section"));
116124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.asciidoctor.log.LogHandler;
23+
import org.asciidoctor.log.LogRecord;
24+
25+
public class CapturingLogHandler implements LogHandler {
26+
27+
private static final List<LogRecord> logRecords = new ArrayList<LogRecord>();
28+
29+
@Override
30+
public void log(LogRecord logRecord) {
31+
logRecords.add(logRecord);
32+
}
33+
34+
static List<LogRecord> getLogRecords() {
35+
return logRecords;
36+
}
37+
38+
static void clear() {
39+
logRecords.clear();
40+
}
41+
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.restdocs.asciidoctor.CapturingLogHandler

spring-restdocs-asciidoctor/src/test/resources/operations/snippet-in-section.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
<div id="preamble">
2+
<div class="sectionbody">
3+
<div class="paragraph">
4+
<p>Alpha</p>
5+
</div>
6+
</div>
7+
</div>
18
<div class="sect1">
2-
<h2 id="_section">Section</h2>
9+
<h2 id="_b">B</h2>
310
<div class="sectionbody">
11+
<div class="paragraph">
12+
<p>Bravo</p>
13+
</div>
414
<div class="sect2">
5-
<h3 id="_section_curl_request">Curl request</h3>
15+
<h3 id="_b_curl_request">Curl request</h3>
616
<div class="listingblock">
717
<div class="content">
818
<pre class="highlight"><code class="language-bash" data-lang="bash">$ curl 'http://localhost:8080/' -i</code></pre>

0 commit comments

Comments
 (0)