Skip to content

Commit

Permalink
Enable end- as additional block end statement
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed Mar 15, 2022
1 parent 5ae6dde commit 5735d42
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.4.0-alpha.21'
version = '1.4.0-alpha.22'

sourceCompatibility = "17"
targetCompatibility = "17"
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/docutools/jocument/impl/ParsingUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.docutools.jocument.impl;

import java.util.List;

public class ParsingUtils {

private ParsingUtils() {
Expand All @@ -18,8 +20,7 @@ public static String stripBrackets(String value) {
return value.substring(2, value.length() - 2);
}

public static String getMatchingLoopEnd(String placeholder) {
return String.format("{{/%s}}", placeholder);
public static List<String> getMatchingLoopEnds(String placeholder) {
return List.of("{{/%s}}".formatted(placeholder), "{{end-%s}}".formatted(placeholder));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

private ExcelUtils() {
}

private static final Logger logger = LogManager.getLogger();

public static String getPlaceholder(Cell cell) {
Expand Down Expand Up @@ -72,11 +76,11 @@ public static String getPlaceholderFromLoopEnd(Row row) {
* @return Whether the matching loop-end string for the placeholder was found in this row
*/
public static boolean isMatchingLoopEnd(Row row, String placeholder) {
var endPlaceholder = ParsingUtils.getMatchingLoopEnd(placeholder);
var endPlaceholders = ParsingUtils.getMatchingLoopEnds(placeholder);
if (getNumberOfNonEmptyCells(row) == 1) {
var cell = row.getCell(row.getFirstCellNum());
if (cell.getCellType() == CellType.STRING) {
return cell.getStringCellValue().equals(endPlaceholder);
return endPlaceholders.stream().anyMatch(endPlaceholder -> cell.getStringCellValue().equals(endPlaceholder));
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ private void removeLoop(IBodyElement start, List<IBodyElement> content, List<IBo
}

private List<IBodyElement> getLoopBody(String placeholderName, List<IBodyElement> remaining) {
var endLoopMarker = String.format("{{/%s}}", placeholderName);
logger.debug("Getting loop body from {} to {}", placeholderName, endLoopMarker);
var endLoopMarkers = ParsingUtils.getMatchingLoopEnds(placeholderName);
logger.debug("Getting loop body from {}", placeholderName);
return remaining.stream()
//Could be written nice with `takeUntil(element -> (element instanceof XP xp && eLM.equals(WU.toString(xp)))
.takeWhile(element -> !(element instanceof XWPFParagraph xwpfParagraph
&& endLoopMarker.equals(WordUtilities.toString(xwpfParagraph))))
&& endLoopMarkers.stream().anyMatch(endLoopMarker -> endLoopMarker.equals(WordUtilities.toString(xwpfParagraph)))))
.toList();
}

Expand Down

0 comments on commit 5735d42

Please sign in to comment.