Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix snippet reader tab and indent handling #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -88,6 +90,9 @@ public StringBuffer readSnippet(String snippetId) throws IOException {
int minIndent(List<String> lines) {
int minIndent = Integer.MAX_VALUE;
for (String line : lines) {
if (StringUtils.isBlank(line)) {
continue;
}
minIndent = Math.min(minIndent, indent(line));
}
return minIndent;
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,40 @@ public void testIgnoreDownloadError() throws Exception {
assertThat(snippet, CoreMatchers.containsString("Error during retrieving content"));
}

@Test
public void testIndentHandling() throws MacroExecutionException {
Map<String, Object> 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<String, Object> macroParameters)
throws MacroExecutionException {
File basedir = new File(getBasedir());
Expand Down
40 changes: 40 additions & 0 deletions doxia-core/src/test/resources/macro/snippet/testSnippet-indent.txt
Original file line number Diff line number Diff line change
@@ -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