r9910 - helma/helma/trunk/src/helma/util

[email protected] Wed, 16 Sep 2009 15:35:21 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090916133521.E8AA93D0E3@mia>
Author: hannes
Date: 2009-09-16 15:35:21 +0200 (Wed, 16 Sep 2009)
New Revision: 9910

Modified:
   helma/helma/trunk/src/helma/util/MarkdownProcessor.java
Log:
More link parsing fixes

Details at http://dev.helma.org/trac/helma/changeset/9910

Modified: helma/helma/trunk/src/helma/util/MarkdownProcessor.java
===================================================================
--- helma/helma/trunk/src/helma/util/MarkdownProcessor.java	2009-09-16 10:03:51 UTC (rev 9909)
+++ helma/helma/trunk/src/helma/util/MarkdownProcessor.java	2009-09-16 13:35:21 UTC (rev 9910)
@@ -190,8 +190,8 @@
                         linkValue[0] = buffer.toString().trim();
                         buffer.setLength(0);
                         int j = i + 1;
-                        int newlines = 0;
-                        while (j < length && chars[j] != ')' && Character.isWhitespace(chars[j])) {
+                        int newlines = c == '\n' ? 1 : 0;
+                        while (j < length && Character.isWhitespace(chars[j])) {
                             if (chars[j] == '\n') {
                                 newlines += 1;
                                 if (newlines > 1) {
@@ -203,7 +203,7 @@
                             }
                             j += 1;
                         }
-                        if ((chars[j] == '"' || chars[j] == '\'' || chars[j] == '(') && newlines <= 1) {
+                        if (j < length && newlines <= 1 && isLinkQuote(chars[j])) {
                             char quoteChar = chars[j] == '(' ? ')' : chars[j];
                             int start = j = j + 1;
                             int len = -1;
@@ -221,13 +221,16 @@
                                 c = chars[j];
                             }
                         }
-                        if (c == '\n') {
+                        if (c == '\n' && state != NONE) {
                             links.put(linkId.toLowerCase(), linkValue);
                             System.arraycopy(chars, i, chars, linestart, length - i);
                             length -= (i - linestart);
                             i = linestart;
                             buffer.setLength(0);
                             linkId = null;
+                        } else {
+                            // no valid link title - escape
+                            state = NONE;
                         }
                     } else {
                         buffer.append(c);
@@ -985,6 +988,10 @@
         return b.toString();
     }
 
+    boolean isLinkQuote(char c) {
+        return c == '"' || c == '\'' || c == '(';
+    }
+
     boolean isSpace(char c) {
         return c == ' ' || c == '\t';
     }