From a9a3cdf220122f9b260ad323a3590d251c91cc18 Mon Sep 17 00:00:00 2001
From: mdb
Date: Sun, 25 Jun 2006 16:11:29 +0000
Subject: [PATCH] Switch to a line oriented parsing.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1862 6335cc39-0255-0410-8fd6-9bcaacd3b74c
---
.../com/samskivert/servlet/util/HTMLUtil.java | 84 +++++++++----------
1 file changed, 41 insertions(+), 43 deletions(-)
diff --git a/src/java/com/samskivert/servlet/util/HTMLUtil.java b/src/java/com/samskivert/servlet/util/HTMLUtil.java
index 96683009..d30706b7 100644
--- a/src/java/com/samskivert/servlet/util/HTMLUtil.java
+++ b/src/java/com/samskivert/servlet/util/HTMLUtil.java
@@ -3,7 +3,7 @@
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
-//
+//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
@@ -90,50 +90,48 @@ public class HTMLUtil
*/
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);
+ String[] lines = StringUtil.split(text, "\n");
+ StringBuilder tbuf = new StringBuilder();
- // 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, "- ");
- } else {
- m.appendReplacement(tbuf, "
- ");
- }
- }
- m.appendTail(tbuf);
- text = tbuf.toString();
+ boolean inpara = false, inlist = false;
- // finally close the
s and paragraphy
- String[] paras = _blank.split(text);
- tbuf.setLength(0);
- for (int ii = 0; ii < paras.length; ii++) {
- String para = paras[ii].trim();
- if (para.startsWith("")) {
- tbuf.append(para).append("
\n");
- } else {
- tbuf.append(" ").append(para).append("
\n");
- }
+ for (int ii = 0; ii < lines.length; ii++) {
+ String line = lines[ii];
+ if (StringUtil.isBlank(lines[ii])) {
+ if (inlist) {
+ tbuf.append("");
+ inlist = false;
+ }
+ if (inpara) {
+ tbuf.append("
\n");
+ inpara = false;
+ }
+ continue;
+ }
+
+ if (!inpara) {
+ inpara = true;
+ tbuf.append(" ");
+ }
+
+ if (line.startsWith("*")) {
+ if (inlist) {
+ tbuf.append("
");
+ } else {
+ tbuf.append("- ");
+ inlist = true;
+ }
+ line = line.substring(1);
+ }
+
+ tbuf.append(line).append("\n");
+ }
+
+ if (inlist) {
+ tbuf.append("
");
+ }
+ if (inpara) {
+ tbuf.append("\n");
}
return tbuf.toString().trim();