From bb7aecbfe554941c57c1221d55cdd3f60e5c6934 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 17 Oct 2019 15:40:52 +0100 Subject: [PATCH] Fix problem with operation block macro when using leveloffset Previously, when a document had a leveloffset attribute this was included in the attributes used when loading a snippet in the operation block macro. This could lead to warnings about section titles being out of sequence as the level used for the title did not take the leveloffset into consideration. This commit clones the documents attributes and removes any leveloffset attribute before loading a snippet. This removes the need for the leveloffset to be considered when loading a snippet fragment. As before, the level of each block in the fragment loaded from snippet is then set prior to adding the block to the main document. This takes the leveloffset into consideration. Closes gh-649 --- .../extensions/operation_block_macro.rb | 3 ++- .../AbstractOperationBlockMacroTests.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb b/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb index 8b37bc63..94f11801 100644 --- a/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb +++ b/spring-restdocs-asciidoctor-support/src/main/resources/extensions/operation_block_macro.rb @@ -46,7 +46,8 @@ class OperationBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor end def add_blocks(content, doc, parent) - options = { safe: doc.options[:safe], attributes: doc.attributes } + options = { safe: doc.options[:safe], attributes: doc.attributes.clone } + options[:attributes].delete 'leveloffset' fragment = Asciidoctor.load content, options # use a template to get the correct sectname and level for blocks to append template = create_section(parent, '', {}) diff --git a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java index 369c8266..c40f9344 100644 --- a/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java +++ b/spring-restdocs-asciidoctor/src/test/java/org/springframework/restdocs/asciidoctor/AbstractOperationBlockMacroTests.java @@ -122,6 +122,22 @@ public abstract class AbstractOperationBlockMacroTests { assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-in-section")); } + @Test + public void includeSnippetInSectionWithAbsoluteLevelOffset() throws Exception { + String result = this.asciidoctor + .convert("= A\n:doctype: book\n:sectnums:\n:leveloffset: 1\n\nAlpha\n\n= B\n\nBravo\n\n" + + "operation::some-operation[snippets='curl-request']\n\n= C\n", this.options); + assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-in-section")); + } + + @Test + public void includeSnippetInSectionWithRelativeLevelOffset() throws Exception { + String result = this.asciidoctor + .convert("= A\n:doctype: book\n:sectnums:\n:leveloffset: +1\n\nAlpha\n\n= B\n\nBravo\n\n" + + "operation::some-operation[snippets='curl-request']\n\n= C\n", this.options); + assertThat(result).isEqualTo(getExpectedContentFromFile("snippet-in-section")); + } + @Test public void includeSnippetInSectionWithPdfBackend() throws Exception { File output = configurePdfOutput();