A Java library that provides a class to effectively convert custom BBCode to HTML. Also prevents XSS attacks.
Grab it from maven:
<dependency>
<groupId>io.github.matafokka</groupId>
<artifactId>bbcode-converter</artifactId>
<version>1.0</version>
</dependency>
Or for your building system from code here.
You can find the documentation here.
First of all, you've got 3 types of so-called "tags":
- Simple tags. These tags contains only one so-called part (string), i.e. "[B]", that will be replaced with another string, i.e. "<b>". These tags shouldn't be closed. Also, this tags contains HTML entities.
- Complex tags. These tags can contain 2 or 3 parts. Check addComplexTag() for more information.
- URL entites. Same as simple tags, but these will be used when parsing text between 1st and 2nd parts of a complex tag.
Next, you need to know these rules:
- All tags are case-dependent. Don't add all the cases by yourself! Apply a code-style to your project, i.e. all tags should be either in a lower case or in a upper case.
- BBCodeConverter will escape all necessary HTML and URL entities. You don't need to add it by yourself.
- Put the output of toHtml() into
<object></object>
block. This is needed to deal with bad markup.
List of included tags:
Click to expand
Tags added anyways
Simple tags:
" => "
' => '
< => <
> => >
URL entities:
" => %22
' => %27
; => %3B
< => %3C
> => %3E
Tags added when addDefaultTags = true
Simple tags:
[B] => <b>
[/B] => </b>
[I] => <i>
[/I] => </i>
[U] => <u>
[/U] => </u>
[S] => <s>
[/S] => </s>
Complex tags:
[URL="http:// "] [/URL]
V V V
<a href="http:// ">" </a>
[URL="https:// "] [/URL]
V V V
<a href="https:// ">" </a>
[COLOR=" "] [/COLOR]
V V V
<span style="color: ;"> </span>
- Import the project :D
import io.github.matafokka.bbcode_converter.BBCodeConverter
- Make an instance of this class:
// With all default tags included
BBCodeConverter conv = new BBCodeConverter();
BBCodeConverter conv = new BBCodeConverter(true);
// Only with necessary tags (HTML and URL-entities):
BBCodeConverter conv = new BBCodeConverter(false);
- Add your custom tags:
conv.addSimpleTag(...);
conv.addComplexTag(...);
conv.addUrlEntity(...);
- Use your converter:
String output = conv.toHtml(input);
- Put the output to your page or whatever. DO NOT escape the output!