Java BBCode 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/849396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:03:12  来源:igfitidea点击:

Java BBCode library

javaparsingbbcode

提问by Rohit

Has anybody used a good Java implementation of BBCode? I am looking at

有没有人使用过 BBCode 的一个很好的 Java 实现?我在看

  1. javabbcode: nothing to see
  2. kefir-bb: Listed as alpha
  3. BBcode parser in JBoss source code.
  1. javabbcode:没什么可看的
  2. 开菲尔-bb: 列为 alpha
  3. JBoss 源代码中的 BBcode 解析器。

Are there any better options?

有没有更好的选择?

采纳答案by Kdeveloper

The current version of KefirBB 0.6 is not listed as beta anymore. I find the KefirBB parser very easy to configure and extend with my own tags:

当前版本的 KefirBB 0.6 不再列为测试版。我发现 KefirBB 解析器很容易配置和扩展我自己的标签:

kefir-bb.sourceforge.net

开菲尔-bb.sourceforge.net

(This is the best BBCodeparser I've found so far)

(这是迄今为止我发现的最好的BBCode解析器)

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):

我还在fyhao.com 上找到了这段代码,但它确实可以保护您免受错误嵌套的标签(因此不适合解析用户输入的输入):

    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></strong>");
        bbMap.put("\[i\](.+?)\[/i\]", "<span style='font-style:italic;'></span>");
        bbMap.put("\[u\](.+?)\[/u\]", "<span style='text-decoration:underline;'></span>");
        bbMap.put("\[h1\](.+?)\[/h1\]", "<h1></h1>");
        bbMap.put("\[h2\](.+?)\[/h2\]", "<h2></h2>");
        bbMap.put("\[h3\](.+?)\[/h3\]", "<h3></h3>");
        bbMap.put("\[h4\](.+?)\[/h4\]", "<h4></h4>");
        bbMap.put("\[h5\](.+?)\[/h5\]", "<h5></h5>");
        bbMap.put("\[h6\](.+?)\[/h6\]", "<h6></h6>");
        bbMap.put("\[quote\](.+?)\[/quote\]", "<blockquote></blockquote>");
        bbMap.put("\[p\](.+?)\[/p\]", "<p></p>");
        bbMap.put("\[p=(.+?),(.+?)\](.+?)\[/p\]", "<p style='text-indent:px;line-height:%;'></p>");
        bbMap.put("\[center\](.+?)\[/center\]", "<div align='center'>");
        bbMap.put("\[align=(.+?)\](.+?)\[/align\]", "<div align=''>");
        bbMap.put("\[color=(.+?)\](.+?)\[/color\]", "<span style='color:;'></span>");
        bbMap.put("\[size=(.+?)\](.+?)\[/size\]", "<span style='font-size:;'></span>");
        bbMap.put("\[img\](.+?)\[/img\]", "<img src='' />");
        bbMap.put("\[img=(.+?),(.+?)\](.+?)\[/img\]", "<img width='' height='' src='' />");
        bbMap.put("\[email\](.+?)\[/email\]", "<a href='mailto:'></a>");
        bbMap.put("\[email=(.+?)\](.+?)\[/email\]", "<a href='mailto:'></a>");
        bbMap.put("\[url\](.+?)\[/url\]", "<a href=''></a>");
        bbMap.put("\[url=(.+?)\](.+?)\[/url\]", "<a href=''></a>");
        bbMap.put("\[youtube\](.+?)\[/youtube\]", "<object width='640' height='380'><param name='movie' value='http://www.youtube.com/v/'></param><embed src='http://www.youtube.com/v/' type='application/x-shockwave-flash' width='640' height='380'></embed></object>");
        bbMap.put("\[video\](.+?)\[/video\]", "<video src='' />");

        for (Map.Entry entry: bbMap.entrySet()) {
            html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
        }

        return html;
    }

BTW javaBBcode is part of opensource project: JavaBB.

顺便说一句,javaBBcode 是开源项目的一部分:JavaBB

回答by Kdeveloper

I believe a better option is to use wiki markup. You can try the Mylyn Wikitext package which I currently use with great success for our Documentation system.

我相信更好的选择是使用 wiki 标记。您可以尝试 Mylyn Wikitext 包,我目前在我们的文档系统中使用该包取得了巨大成功。

However, this is not an answer to your problem, if what you try is to parse some forum text you already have automatically :-I

但是,这不是您问题的答案,如果您尝试的是自动解析一些您已经拥有的论坛文本:-I

回答by Kdeveloper

I believe a better option is to use wiki markup

我相信更好的选择是使用 wiki 标记

Unless you have non technical users who are used to bbcode and aren't terribly interested in learning another arcane markup format.

除非您的非技术用户习惯了 bbcode 并且对学习另一种神秘的标记格式不太感兴趣。

I have successfully used regular expressions in the past to flip between html and bbcode. Define a list of expressions that cover each tag, and precompile the Patterns for each one. Then iteratively transform the string with each expression. Works both ways. Performance is actually pretty good, and you can adapt to your own dialect/extensions easily.

我过去曾成功地使用正则表达式在 html 和 bbcode 之间切换。定义覆盖每个标签的表达式列表,并为每个标签预编译模式。然后用每个表达式迭代转换字符串。双向工作。性能其实还不错,你可以很容易地适应你自己的方言/扩展。

Downside is that regex aren't always straightforward to work out.

缺点是正则表达式并不总是很容易解决。

回答by robotdan

Another Java option for BBCode parsing and transforming to HTML is Prime Transformer.

用于 BBCode 解析和转换为 HTML 的另一个 Java 选项是 Prime Transformer。

https://github.com/inversoft/prime-transformer

https://github.com/inversoft/prime-transformer

We built this for some specific uses but tried to keep it generic enough for general consumption. I've got a basic example of how to put it together on the landing page on GitHub.

我们为某些特定用途构建了它,但试图保持它足够通用以供一般消费。我在 GitHub 上的登录页面上有一个基本示例,说明如何将其放在一起。