From 9caa69da348230856616059894f3b5f49bc470b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=BCth?= Date: Thu, 30 Mar 2023 11:21:57 +0200 Subject: [PATCH] Fix snippet reader tab and indent handling --- .../doxia/macro/snippet/SnippetReader.java | 9 ++++- .../doxia/macro/snippet/SnippetMacroTest.java | 34 ++++++++++++++++ .../macro/snippet/testSnippet-indent.txt | 40 +++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 doxia-core/src/test/resources/macro/snippet/testSnippet-indent.txt diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetReader.java b/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetReader.java index dd38d5e0f..5845eef49 100644 --- a/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetReader.java +++ b/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetReader.java @@ -73,7 +73,9 @@ public StringBuffer readSnippet(String snippetId) throws IOException { int minIndent = minIndent(lines); StringBuffer result = new StringBuffer(); for (String line : lines) { - result.append(line.substring(minIndent)); + if (line.length() > minIndent) { + result.append(line.substring(minIndent)); + } result.append(EOL); } return result; @@ -88,6 +90,9 @@ public StringBuffer readSnippet(String snippetId) throws IOException { int minIndent(List lines) { int minIndent = Integer.MAX_VALUE; for (String line : lines) { + if (StringUtils.isBlank(line)) { + continue; + } minIndent = Math.min(minIndent, indent(line)); } return minIndent; @@ -103,7 +108,7 @@ int indent(String line) { char[] chars = line.toCharArray(); int indent = 0; for (; indent < chars.length; indent++) { - if (chars[indent] != ' ') { + if (chars[indent] != ' ' && chars[indent] != '\t') { break; } } diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java index 6f3fdd38c..af6bbd22d 100644 --- a/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java +++ b/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java @@ -151,6 +151,40 @@ public void testIgnoreDownloadError() throws Exception { assertThat(snippet, CoreMatchers.containsString("Error during retrieving content")); } + @Test + public void testIndentHandling() throws MacroExecutionException { + Map macroParameters = new HashMap<>(); + macroParameters.put("file", "src/test/resources/macro/snippet/testSnippet-indent.txt"); + macroParameters.put("encoding", "UTF-8"); + macroParameters.put("id", "firstId"); + macroParameters.put("verbatim", "false"); + + // 1. Ensure leading tabs are trimmed + SinkEventTestingSink sink = executeSnippetMacro(macroParameters); + String snippet = (String) sink.getEventList().get(0).getArgs()[0]; + assertEquals(1, sink.getEventList().size(), "There should only be one event"); + assertEquals("first snippet\n", snippet); + + // 2. Ensure leading spaces are trimmed + macroParameters.put("id", "secondId"); + sink = executeSnippetMacro(macroParameters); + snippet = (String) sink.getEventList().get(0).getArgs()[0]; + assertEquals("second snippet\n", snippet); + + // 3. Ensure empty lines don't break the minIndent calculation + macroParameters.put("id", "thirdId"); + sink = executeSnippetMacro(macroParameters); + snippet = (String) sink.getEventList().get(0).getArgs()[0]; + String[] lines = snippet.split(System.getProperty("line.separator")); + assertEquals("Line1", lines[0], "There should not be any leading indentation since the minIndent is two"); + assertEquals(" Line2", lines[1]); + assertEquals("", lines[2]); + assertEquals("", lines[3]); + assertEquals("", lines[4]); + assertEquals(" ", lines[5], "The tab should be preserved"); + assertEquals(" Line6", lines[6]); + } + private SinkEventTestingSink executeSnippetMacro(Map macroParameters) throws MacroExecutionException { File basedir = new File(getBasedir()); diff --git a/doxia-core/src/test/resources/macro/snippet/testSnippet-indent.txt b/doxia-core/src/test/resources/macro/snippet/testSnippet-indent.txt new file mode 100644 index 000000000..17b791087 --- /dev/null +++ b/doxia-core/src/test/resources/macro/snippet/testSnippet-indent.txt @@ -0,0 +1,40 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +# +# http://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. + +preamble + +SNIPPET START firstId + first snippet +SNIPPET END firstId + +interlude + +another snippet to start: secondId + second snippet +another snippet to end: secondId + +snippet start thirdId + Line1 + Line2 + + + + + Line6 +snippet end thirdId + +conclusion