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:47 UTC

[freemarker-docgen] branch master updated (0f23e85 -> ca0e062)

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

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


    from 0f23e85  Added optional "from" and "to" parameters [docgen.insertFile ...].
     new 472d132  Fixed text escaping issue newly introduced
     new ca0e062  Use [docgen ...] "to" and "toIfPresent" attributes, instead of to="?...".

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../PrintTextWithDocgenSubstitutionsDirective.java | 48 ++++++++++++++--------
 1 file changed, 30 insertions(+), 18 deletions(-)


[freemarker-docgen] 01/02: Fixed text escaping issue newly introduced

Posted by dd...@apache.org.
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 472d132f06d8f88a7fa0f44b796834e6f014ab37
Author: ddekany <dd...@apache.org>
AuthorDate: Sun Oct 11 19:40:03 2020 +0200

    Fixed text escaping issue newly introduced
---
 .../docgen/core/PrintTextWithDocgenSubstitutionsDirective.java        | 4 ++--
 1 file changed, 2 insertions(+), 2 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 369d2f3..061bd16 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
@@ -128,7 +128,7 @@ public class PrintTextWithDocgenSubstitutionsDirective implements TemplateDirect
                     lastDocgenTagStart = cursor;
                 }
 
-                out.write(text, lastUnprintedIdx, cursor - lastUnprintedIdx);
+                HTMLOutputFormat.INSTANCE.output(text.substring(lastUnprintedIdx, cursor), out);
                 lastUnprintedIdx = cursor;
 
                 cursor += DOCGEN_TAG_START.length();
@@ -180,7 +180,7 @@ public class PrintTextWithDocgenSubstitutionsDirective implements TemplateDirect
                 }
 
             }
-            out.write(text, lastUnprintedIdx, text.length() - lastUnprintedIdx);
+            HTMLOutputFormat.INSTANCE.output(text.substring(lastUnprintedIdx, text.length()), out);
         }
 
         private void insertCustomVariable(String customVarName) throws TemplateException, IOException {


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

Posted by dd...@apache.org.
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);
                         }
                     }
                 }