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
This commit is contained in:
Andy Wilkinson
2019-08-30 12:31:01 +01:00
5 changed files with 74 additions and 13 deletions

View File

@@ -37,10 +37,9 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
def do_read_snippets(snippets, parent, operation, snippet_titles)
content = StringIO.new
section_level = parent.level + 1
section_id = parent.id
snippets.each do |snippet|
append_snippet_block(content, snippet, section_level, section_id,
append_snippet_block(content, snippet, section_id,
operation, snippet_titles)
end
content.string
@@ -51,11 +50,12 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
fragment = Asciidoctor.load content, options
fragment.blocks.each do |b|
b.parent = parent
b.level += parent.level
parent << b
end
parent.find_by.each do |b|
b.parent = b.parent unless b.is_a? Asciidoctor::Document
end
b.parent = b.parent unless b.is_a? Asciidoctor::Document
end
end
def snippets_to_include(snippet_names, snippets_dir, operation)
@@ -78,9 +78,9 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
.map { |file| Snippet.new(File.join(operation_dir, file), file[0..-6]) }
end
def append_snippet_block(content, snippet, section_level, section_id,
def append_snippet_block(content, snippet, section_id,
operation, snippet_titles)
write_title content, snippet, section_level, section_id, snippet_titles
write_title content, snippet, section_id, snippet_titles
write_content content, snippet, operation
end
@@ -96,8 +96,8 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
end
end
def write_title(content, snippet, level, id, snippet_titles)
section_level = '=' * (level + 1)
def write_title(content, snippet, id, snippet_titles)
section_level = '=='
title = snippet_titles.title_for_snippet snippet
content.puts "[[#{id}_#{snippet.name.sub '-', '_'}]]"
content.puts "#{section_level} #{title}"
@@ -141,4 +141,4 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
end
end
end
end
end

View File

@@ -35,6 +35,7 @@ import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -64,6 +65,12 @@ public abstract class AbstractOperationBlockMacroTests {
prepareOperationSnippets(getBuildOutputLocation());
this.options = OptionsBuilder.options().safe(SafeMode.UNSAFE).baseDir(getSourceLocation()).get();
this.options.setAttributes(getAttributes());
CapturingLogHandler.clear();
}
@After
public void verifyLogging() {
assertThat(CapturingLogHandler.getLogRecords()).isEmpty();
}
public void prepareOperationSnippets(File buildOutputLocation) throws IOException {
@@ -110,7 +117,8 @@ public abstract class AbstractOperationBlockMacroTests {
@Test
public void includeSnippetInSection() throws Exception {
String result = this.asciidoctor.convert("== Section\n" + "operation::some-operation[snippets='curl-request']",
String result = this.asciidoctor.convert(
"= A\n\nAlpha\n\n== B\n\nBravo\n\n" + "operation::some-operation[snippets='curl-request']",
this.options);
assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-in-section"));
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.asciidoctor;
import java.util.ArrayList;
import java.util.List;
import org.asciidoctor.log.LogHandler;
import org.asciidoctor.log.LogRecord;
public class CapturingLogHandler implements LogHandler {
private static final List<LogRecord> logRecords = new ArrayList<LogRecord>();
@Override
public void log(LogRecord logRecord) {
logRecords.add(logRecord);
}
static List<LogRecord> getLogRecords() {
return logRecords;
}
static void clear() {
logRecords.clear();
}
}

View File

@@ -0,0 +1 @@
org.springframework.restdocs.asciidoctor.CapturingLogHandler

View File

@@ -1,8 +1,18 @@
<div class="sect1">
<h2 id="_section">Section</h2>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Alpha</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_b">B</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Bravo</p>
</div>
<div class="sect2">
<h3 id="_section_curl_request">Curl request</h3>
<h3 id="_b_curl_request">Curl request</h3>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">$ curl 'http://localhost:8080/' -i</code></pre>