You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2020/10/11 20:11:49 UTC

[freemarker-docgen] 02/02: Use [docgen ...] "to" and "toIfPresent" attributes, instead of to="?...".

This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/freemarker-docgen.git

commit ca0e062da17a19239acf0c10f3a41097880a55cc
Author: ddekany <dd...@apache.org>
AuthorDate: Sun Oct 11 19:40:59 2020 +0200

    Use [docgen ...] "to" and "toIfPresent" attributes, instead of to="?...".
---
 .../PrintTextWithDocgenSubstitutionsDirective.java | 44 ++++++++++++++--------
 1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/PrintTextWithDocgenSubstitutionsDirective.java b/freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/PrintTextWithDocgenSubstitutionsDirective.java
index 061bd16..628e62a 100644
--- a/freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/PrintTextWithDocgenSubstitutionsDirective.java
+++ b/freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/PrintTextWithDocgenSubstitutionsDirective.java
@@ -33,6 +33,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -148,6 +149,7 @@ public class PrintTextWithDocgenSubstitutionsDirective implements TemplateDirect
                     String charsetArg = null;
                     String fromArg = null;
                     String toArg = null;
+                    String toIfPresentArg = null;
                     Set<String> paramNamesSeen = new HashSet<>();
                     while (skipWS()) {
                         String paramName = fetchOptionalVariableName();
@@ -164,6 +166,8 @@ public class PrintTextWithDocgenSubstitutionsDirective implements TemplateDirect
                             fromArg = paramValue;
                         } else if (paramName.equals("to")) {
                             toArg = paramValue;
+                        } else if (paramName.equals("toIfPresent")) {
+                            toIfPresentArg = paramValue;
                         } else {
                             throw new TemplateException(
                                     "Unsupported " + StringUtil.jQuote(INSERT_FILE)
@@ -173,7 +177,7 @@ public class PrintTextWithDocgenSubstitutionsDirective implements TemplateDirect
                     skipRequiredToken(DOCGEN_TAG_END);
                     lastUnprintedIdx = cursor;
 
-                    insertFile(pathArg, charsetArg, fromArg, toArg);
+                    insertFile(pathArg, charsetArg, fromArg, toArg, toIfPresentArg);
                 } else {
                     throw new TemplateException(
                             "Unsupported docgen subvariable " + StringUtil.jQuote(subvarName) + ".", env);
@@ -235,7 +239,8 @@ public class PrintTextWithDocgenSubstitutionsDirective implements TemplateDirect
             }
         }
 
-        private void insertFile(String pathArg, String charsetArg, String fromArg, String toArg)
+        private void insertFile(String pathArg, String charsetArg, String fromArg,
+                String toArg, String toIfPresentArg)
                 throws TemplateException, IOException {
             int slashIndex = pathArg.indexOf("/");
             String symbolicNameStep = slashIndex != -1 ? pathArg.substring(0, slashIndex) : pathArg;
@@ -312,32 +317,39 @@ public class PrintTextWithDocgenSubstitutionsDirective implements TemplateDirect
                     }
                 }
 
+                String toStr;
+                boolean toPresenceOptional;
                 if (toArg != null) {
-                    boolean optional;
-                    String toArgCleaned;
-                    if (toArg.startsWith("?")) {
-                        optional = true;
-                        toArgCleaned = toArg.substring(1);
-                    } else {
-                        optional = false;
-                        toArgCleaned = toArg;
+                    if (toIfPresentArg != null) {
+                        throw newErrorInDocgenTag(
+                                "Can't use both \"to\" and \"toIfPresent\" argument.");
                     }
-                    Pattern from;
+                    toStr = toArg;
+                    toPresenceOptional = false;
+                } else if (toIfPresentArg != null) {
+                    toStr = toIfPresentArg;
+                    toPresenceOptional = true;
+                } else {
+                    toStr = null;
+                    toPresenceOptional = false;
+                }
+                if (toStr != null) {
+                    Pattern to;
                     try {
-                        from = Pattern.compile(toArgCleaned);
+                        to = Pattern.compile(toStr);
                     } catch (PatternSyntaxException e) {
-                        throw newErrorInDocgenTag("Invalid regular expression: " + toArgCleaned);
+                        throw newErrorInDocgenTag("Invalid regular expression: " + toStr);
                     }
-                    Matcher matcher = from.matcher(fileContent);
+                    Matcher matcher = to.matcher(fileContent);
                     if (matcher.find()) {
                         String remaining = fileContent.substring(0, matcher.start());
                         fileContent = remaining
                                 + (remaining.endsWith("\n") || remaining.endsWith("\r") ? "" : "\n")
                                 + "[\u2026]";
                     } else {
-                        if (!optional) {
+                        if (!toPresenceOptional) {
                             throw newErrorInDocgenTag(
-                                    "Regular expression has no match in the file content: " + fromArg);
+                                    "Regular expression has no match in the file content: " + toStr);
                         }
                     }
                 }