diff --git a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java index 177a91ad..b31f44f8 100644 --- a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java +++ b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java @@ -76,53 +76,76 @@ public class ConfigInitializer Files.createFile(customSettingsPath); } } - private static Map extractEntries(List lines) { Map entries = new HashMap<>(); - String keyRegex = "^\\s*(\\w+)\\s*:\\s*(.*)"; // Capture key and value - Pattern pattern = Pattern.compile(keyRegex); + StringBuilder currentEntry = new StringBuilder(); + String currentKey = null; + int blockIndent = -1; for (String line : lines) { - Matcher matcher = pattern.matcher(line); - if (matcher.find() && !line.trim().startsWith("#")) { - String key = matcher.group(1).trim(); - String value = matcher.group(2).trim(); // Capture the value directly - entries.put(key, line); + if (line.trim().isEmpty()) { + if (currentKey != null) { + currentEntry.append(line).append("\n"); + } + continue; + } + + int indentLevel = getIndentationLevel(line); + if (line.trim().startsWith("#")) { + if (indentLevel <= blockIndent || blockIndent == -1) { + if (currentKey != null) { + entries.put(currentKey, currentEntry.toString().trim()); + currentEntry = new StringBuilder(); + } + currentKey = line.trim().replaceAll("#", "").split(":")[0].trim(); + blockIndent = indentLevel; + } + currentEntry.append(line).append("\n"); + } else if (indentLevel == 0 || indentLevel <= blockIndent) { + if (currentKey != null) { + entries.put(currentKey, currentEntry.toString().trim()); + currentEntry = new StringBuilder(); + } + currentKey = line.split(":")[0].trim(); + blockIndent = indentLevel; + currentEntry.append(line).append("\n"); + } else { + currentEntry.append(line).append("\n"); } } + + if (currentKey != null) { + entries.put(currentKey, currentEntry.toString().trim()); + } + return entries; } - private static List mergeConfigs( - List templateLines, - Map templateEntries, - Map userEntries) { + private static List mergeConfigs(List templateLines, Map templateEntries, Map userEntries) { List mergedLines = new ArrayList<>(); Set handledKeys = new HashSet<>(); - for (String line : templateLines) { - String cleanLine = line.split("#")[0].trim(); - if (!cleanLine.isEmpty() && cleanLine.contains(":")) { - String key = cleanLine.split(":")[0].trim(); - if (userEntries.containsKey(key)) { - // Always use user's entry if exists - mergedLines.add(userEntries.get(key)); - handledKeys.add(key); - } else { - // Use template's entry if no user entry - mergedLines.add(line); - } - } else { - // Add comments and other lines directly - mergedLines.add(line); - } - } + String currentBlockKey = null; + int blockIndent = -1; - // Add user entries not present in the template at the end - for (String key : userEntries.keySet()) { - if (!handledKeys.contains(key)) { - mergedLines.add(userEntries.get(key)); + for (String line : templateLines) { + if (line.trim().isEmpty()) { + mergedLines.add(line); + continue; + } + + int indentLevel = getIndentationLevel(line); + if (indentLevel == 0 || (indentLevel <= blockIndent && !line.trim().startsWith("#"))) { + currentBlockKey = line.split(":")[0].trim(); + blockIndent = indentLevel; + } + + if (userEntries.containsKey(currentBlockKey) && !handledKeys.contains(currentBlockKey)) { + mergedLines.add(userEntries.get(currentBlockKey)); + handledKeys.add(currentBlockKey); + } else if (!handledKeys.contains(currentBlockKey)) { + mergedLines.add(line); } } @@ -130,13 +153,13 @@ public class ConfigInitializer } + private static List cleanInvalidYamlEntries(List lines) { List cleanedLines = new ArrayList<>(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); String trimmedLine = line.trim(); - // Ignore commented lines and lines that don't look like key-only entries if (trimmedLine.startsWith("#") || !trimmedLine.endsWith(":") || trimmedLine.contains(" ")) { @@ -144,10 +167,7 @@ public class ConfigInitializer continue; } - // For potential key-only lines, check the next line to determine context if (isKeyWithoutChildrenOrValue(i, lines)) { - // Skip adding the current line since it's a key without any following value or - // children continue; } diff --git a/src/main/resources/settings.yml.template b/src/main/resources/settings.yml.template index 7d2047c3..45f1762d 100644 --- a/src/main/resources/settings.yml.template +++ b/src/main/resources/settings.yml.template @@ -21,10 +21,10 @@ system: showUpdate: true # see when a new update is available showUpdateOnlyAdmin: false # Only admins can see when a new update is available, depending on showUpdate it must be set to 'true' -#ui: -# appName: exampleAppName # Application's visible name -# homeDescription: I am a description # Short description or tagline shown on homepage. -# appNameNavbar: navbarName # Name displayed on the navigation bar +ui: + appName: null # Application's visible name + homeDescription: null # Short description or tagline shown on homepage. + appNameNavbar: null # Name displayed on the navigation bar endpoints: toRemove: [] # List endpoints to disable (e.g. ['img-to-pdf', 'remove-pages'])