/bbcode-converter

Provides a class to effectively convert custom BBCode to HTML

Primary LanguageJavaGNU Lesser General Public License v3.0LGPL-3.0

BBCode Converter

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.

Intro

First of all, you've got 3 types of so-called "tags":

  1. 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.
  2. Complex tags. These tags can contain 2 or 3 parts. Check addComplexTag() for more information.
  3. 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:

  1. 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.
  2. BBCodeConverter will escape all necessary HTML and URL entities. You don't need to add it by yourself.
  3. 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:

"	=>	&quot;
'	=>	&apos;
<	=>	&lt;
>	=>	&gt;

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>

How to use

  1. Import the project :D
import io.github.matafokka.bbcode_converter.BBCodeConverter
  1. 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);
  1. Add your custom tags:
conv.addSimpleTag(...);
conv.addComplexTag(...);
conv.addUrlEntity(...);
  1. Use your converter:
String output = conv.toHtml(input);
  1. Put the output to your page or whatever. DO NOT escape the output!