Java BBCode library [closed]
Asked Answered
M

4

16

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

  1. javabbcode : nothing to see
  2. kefir-bb : Listed as alpha
  3. BBcode parser in JBoss source code.

Are there any better options?

Markos answered 11/5, 2009 at 18:26 Comment(2)
Did you find any BBCode parsers/libraries?Descendible
I tried KefirBB and it very easy to work with and the current 0.6 version is not listed as beta.Descendible
D
17

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:

kefir-bb.sourceforge.net

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

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 is part of opensource project: JavaBB.

Descendible answered 15/9, 2010 at 22:9 Comment(2)
It looks like KefirBB has been moved to GitHub github.com/kefirfromperm/kefirbb and it is under active developmentUrge
Do you mean "it does NOT protect you" ?Waken
M
3

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.

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

Mortality answered 11/6, 2009 at 20:31 Comment(1)
Wiki markup is an good idea. My intention was to standardize text formatting mechanism in the application I work on. Right now we allow the user to enter anything and then do all the sanitization by whitelisting tags. Allowing BBCode/Wiki markup only seems to be a better idea to me.Markos
G
3

I believe a better option is to use wiki markup

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

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.

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

Gustafson answered 7/7, 2009 at 14:2 Comment(0)
C
3

Another Java option for BBCode parsing and transforming to HTML is 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.

Catlin answered 30/7, 2014 at 17:56 Comment(6)
Not sure why this response got down-voted because that library is open source and handles really complex BBCode.Selemas
@Selemas It wasn't me who downvoted, but it could happen because it doesn't work. The parser seems excellent, but the transformer crashes on instantiation. It's trying to use FreeMarker which crashes because there's no java.beans.introspector. So it's usefulness seems to be limited.Sexlimited
@Sexlimited that could be due to the version of Java you are using and the Java modules you are including in the runtime. You might also have used jlink to create a custom JDK and the modules in your custom JDK are missing the Java beans packages.Selemas
@Selemas Apparently, the beans thingy is not available on Android. It's trivial to implent a basic html Transformer, albeit very tedious.Sexlimited
It is important to remember that the Android VM (Dalvik, and now ART) is not the same as the Java VM. Just because this library is written in Java does not mean it is compatible with Android. There is a fork somewhere of this library for Android. If I can find it I’ll post it.Catlin
This Android library contains a modified version of prime-transformer. You may be able to use this, extract the code, or fork prime-transformer directly for use in Android. github.com/Ndersam/amebo. See amebo/core/src/main/java/com/amebo/core/Catlin

© 2022 - 2024 — McMap. All rights reserved.