本文介绍了Java BBCode库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人使用过BBCode的优秀Java实现?我在看
Has anybody used a good Java implementation of BBCode? I am looking at
- :无人看到
- :列为alpha
- JBoss源代码中的BBcode解析器。
- javabbcode : nothing to see
- kefir-bb : Listed as alpha
- BBcode parser in JBoss source code.
是否有任何更好的选择?
Are there any better options?
推荐答案
当前版本的KefirBB 0.6不再列为测试版。我发现KefirBB解析器非常容易配置和扩展我自己的标签:
(这是最好的解析器我到目前为止已找到了)
(This is the best BBCode parser I've found so far)
我还在 fyhao.com ,但它确实可以防止错误的嵌套标记(因此不适合解析用户输入的输入):
I also found this code at fyhao.com, but it does protect you against incorrectly nested tags (thus not suitable for parsing user entered input):
public static String bbcode(String text) {
String html = text;
Map<String,String> bbMap = new HashMap<String , String>();
bbMap.put("(\r\n|\r|\n|\n\r)", "<br/>");
bbMap.put("\\[b\\](.+?)\\[/b\\]", "<strong>$1</strong>");
bbMap.put("\\[i\\](.+?)\\[/i\\]", "<span style='font-style:italic;'>$1</span>");
bbMap.put("\\[u\\](.+?)\\[/u\\]", "<span style='text-decoration:underline;'>$1</span>");
bbMap.put("\\[h1\\](.+?)\\[/h1\\]", "<h1>$1</h1>");
bbMap.put("\\[h2\\](.+?)\\[/h2\\]", "<h2>$1</h2>");
bbMap.put("\\[h3\\](.+?)\\[/h3\\]", "<h3>$1</h3>");
bbMap.put("\\[h4\\](.+?)\\[/h4\\]", "<h4>$1</h4>");
bbMap.put("\\[h5\\](.+?)\\[/h5\\]", "<h5>$1</h5>");
bbMap.put("\\[h6\\](.+?)\\[/h6\\]", "<h6>$1</h6>");
bbMap.put("\\[quote\\](.+?)\\[/quote\\]", "<blockquote>$1</blockquote>");
bbMap.put("\\[p\\](.+?)\\[/p\\]", "<p>$1</p>");
bbMap.put("\\[p=(.+?),(.+?)\\](.+?)\\[/p\\]", "<p style='text-indent:$1px;line-height:$2%;'>$3</p>");
bbMap.put("\\[center\\](.+?)\\[/center\\]", "<div align='center'>$1");
bbMap.put("\\[align=(.+?)\\](.+?)\\[/align\\]", "<div align='$1'>$2");
bbMap.put("\\[color=(.+?)\\](.+?)\\[/color\\]", "<span style='color:$1;'>$2</span>");
bbMap.put("\\[size=(.+?)\\](.+?)\\[/size\\]", "<span style='font-size:$1;'>$2</span>");
bbMap.put("\\[img\\](.+?)\\[/img\\]", "<img src='$1' />");
bbMap.put("\\[img=(.+?),(.+?)\\](.+?)\\[/img\\]", "<img width='$1' height='$2' src='$3' />");
bbMap.put("\\[email\\](.+?)\\[/email\\]", "<a href='mailto:$1'>$1</a>");
bbMap.put("\\[email=(.+?)\\](.+?)\\[/email\\]", "<a href='mailto:$1'>$2</a>");
bbMap.put("\\[url\\](.+?)\\[/url\\]", "<a href='$1'>$1</a>");
bbMap.put("\\[url=(.+?)\\](.+?)\\[/url\\]", "<a href='$1'>$2</a>");
bbMap.put("\\[youtube\\](.+?)\\[/youtube\\]", "<object width='640' height='380'><param name='movie' value='http://www.youtube.com/v/$1'></param><embed src='http://www.youtube.com/v/$1' type='application/x-shockwave-flash' width='640' height='380'></embed></object>");
bbMap.put("\\[video\\](.+?)\\[/video\\]", "<video src='$1' />");
for (Map.Entry entry: bbMap.entrySet()) {
html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
}
return html;
}
BTW javaBBcode是开源项目的一部分: JavaBB 。
BTW javaBBcode is part of opensource project: JavaBB.
这篇关于Java BBCode库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!