diff --git a/projects/samskivert/src/java/com/samskivert/servlet/util/HTMLUtil.java b/projects/samskivert/src/java/com/samskivert/servlet/util/HTMLUtil.java
index 7df37fc0..96683009 100644
--- a/projects/samskivert/src/java/com/samskivert/servlet/util/HTMLUtil.java
+++ b/projects/samskivert/src/java/com/samskivert/servlet/util/HTMLUtil.java
@@ -20,6 +20,9 @@
package com.samskivert.servlet.util;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import com.samskivert.util.StringUtil;
/**
@@ -79,4 +82,65 @@ public class HTMLUtil
text = StringUtil.replace(text, "\n", "
\n");
return text;
}
+
+ /**
+ * Does some simple HTML markup, matching bare image URLs and wrapping
+ * them in image tags, matching other URLs and wrapping them in href
+ * tags, and wrapping * prefixed lists into ul-style HTML lists.
+ */
+ public static String simpleFormat (String text)
+ {
+ // first replace the image and other URLs
+ Matcher m = _url.matcher(text);
+ StringBuffer tbuf = new StringBuffer();
+ while (m.find()) {
+ String match = m.group();
+ String lmatch = match.toLowerCase();
+ if (lmatch.endsWith(".png") ||
+ lmatch.endsWith(".jpg") ||
+ lmatch.endsWith(".gif")) {
+ match = "";
+ } else {
+ match = "" + match + "";
+ }
+ m.appendReplacement(tbuf, match);
+ }
+ m.appendTail(tbuf);
+
+ // then tackle the *s
+ text = tbuf.toString();
+ m = _star.matcher(text);
+ tbuf.setLength(0);
+ while (m.find()) {
+ String match = m.group();
+ int start = m.start();
+ if (start == 0 || (start >= 2 && text.charAt(start-1) == '\n' &&
+ text.charAt(start-2) == '\n')) {
+ m.appendReplacement(tbuf, "
").append(para).append("
\n"); + } + } + + return tbuf.toString().trim(); + } + + protected static Pattern _url = + Pattern.compile("http://\\S+", Pattern.MULTILINE); + protected static Pattern _star = Pattern.compile("^\\*", Pattern.MULTILINE); + protected static Pattern _blank = Pattern.compile("^$", Pattern.MULTILINE); } diff --git a/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java b/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java index a035d05e..72d2df92 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java @@ -88,6 +88,14 @@ public class StringTool return HTMLUtil.makeLinear(text); } + /** + * Does some simple markup of the supplied text. + */ + public static String simpleFormat (String text) + { + return HTMLUtil.simpleFormat(text); + } + /** * Converts a float to a reasonably formatted string. */