Switch to a line oriented parsing.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1862 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
//
|
//
|
||||||
// samskivert library - useful routines for java programs
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2001 Michael Bayne
|
// Copyright (C) 2001 Michael Bayne
|
||||||
//
|
//
|
||||||
// This library is free software; you can redistribute it and/or modify it
|
// 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
|
// 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
|
// 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)
|
public static String simpleFormat (String text)
|
||||||
{
|
{
|
||||||
// first replace the image and other URLs
|
String[] lines = StringUtil.split(text, "\n");
|
||||||
Matcher m = _url.matcher(text);
|
StringBuilder tbuf = new StringBuilder();
|
||||||
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 = "<img src=\"" + match + "\">";
|
|
||||||
} else {
|
|
||||||
match = "<a href=\"" + match + "\">" + match + "</a>";
|
|
||||||
}
|
|
||||||
m.appendReplacement(tbuf, match);
|
|
||||||
}
|
|
||||||
m.appendTail(tbuf);
|
|
||||||
|
|
||||||
// then tackle the *s
|
boolean inpara = false, inlist = false;
|
||||||
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, "<ul><li>");
|
|
||||||
} else {
|
|
||||||
m.appendReplacement(tbuf, "<li>");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.appendTail(tbuf);
|
|
||||||
text = tbuf.toString();
|
|
||||||
|
|
||||||
// finally close the </ul>s and paragraphy
|
for (int ii = 0; ii < lines.length; ii++) {
|
||||||
String[] paras = _blank.split(text);
|
String line = lines[ii];
|
||||||
tbuf.setLength(0);
|
if (StringUtil.isBlank(lines[ii])) {
|
||||||
for (int ii = 0; ii < paras.length; ii++) {
|
if (inlist) {
|
||||||
String para = paras[ii].trim();
|
tbuf.append("</ul>");
|
||||||
if (para.startsWith("<ul>")) {
|
inlist = false;
|
||||||
tbuf.append(para).append(" </ul>\n");
|
}
|
||||||
} else {
|
if (inpara) {
|
||||||
tbuf.append("<p> ").append(para).append(" </p>\n");
|
tbuf.append("</p>\n");
|
||||||
}
|
inpara = false;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inpara) {
|
||||||
|
inpara = true;
|
||||||
|
tbuf.append("<p> ");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.startsWith("*")) {
|
||||||
|
if (inlist) {
|
||||||
|
tbuf.append("<li>");
|
||||||
|
} else {
|
||||||
|
tbuf.append("<ul><li>");
|
||||||
|
inlist = true;
|
||||||
|
}
|
||||||
|
line = line.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
tbuf.append(line).append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inlist) {
|
||||||
|
tbuf.append("</ul>");
|
||||||
|
}
|
||||||
|
if (inpara) {
|
||||||
|
tbuf.append("</p>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return tbuf.toString().trim();
|
return tbuf.toString().trim();
|
||||||
|
|||||||
Reference in New Issue
Block a user