A complete, single-pass transformer that converts common BBCode tags (bold, italic, underline, URL, and quote) to their Markdown equivalents.
ID: 1434
using uCalcSoftware;
var uc = new uCalc();
// 1. Setup the Transformer
using (var t = new uCalc.Transformer()) {
// Allow patterns to match across multiple lines
t.DefaultRuleSet.StatementSensitive = false;
// 2. Define the conversion rules
// Simple inline tags
t.FromTo("'['b']'{text}'['/b']'", "**{text}**");
t.FromTo("'['i']'{text}'['/i']'", "*{text}*");
t.FromTo("'['u']'{text}'['/u']'", "{text}");
// URL tag with attribute
t.FromTo("'['url={href}']'{text}'['/url']'", "[{text}]({href})");
// Quote block tag
t.FromTo("'['quote']'{content}'['/quote']'", "> {content}");
// 3. Define the input BBCode text
var bbCode = """
Hello, this is a test of the converter.
This text is [b]bold[/b] and this is [i]italic[/i].
You can also [u]underline[/u] text.
Here is a link to the uCalc website: [url=https://www.ucalc.com]uCalc[/url].
[quote]This is a block of quoted text.
It can span multiple lines.[/quote]
""";
// 4. Run the transformation and print the result
Console.WriteLine(t.Transform(bbCode));
}
Hello, this is a test of the converter.
This text is **bold** and this is *italic*.
You can also <u>underline</u> text.
Here is a link to the uCalc website: [uCalc](https://www.ucalc.com).
> This is a block of quoted text.
It can span multiple lines.
using uCalcSoftware; var uc = new uCalc(); // 1. Setup the Transformer using (var t = new uCalc.Transformer()) { // Allow patterns to match across multiple lines t.DefaultRuleSet.StatementSensitive = false; // 2. Define the conversion rules // Simple inline tags t.FromTo("'['b']'{text}'['/b']'", "**{text}**"); t.FromTo("'['i']'{text}'['/i']'", "*{text}*"); t.FromTo("'['u']'{text}'['/u']'", "<u>{text}</u>"); // URL tag with attribute t.FromTo("'['url={href}']'{text}'['/url']'", "[{text}]({href})"); // Quote block tag t.FromTo("'['quote']'{content}'['/quote']'", "> {content}"); // 3. Define the input BBCode text var bbCode = """ Hello, this is a test of the converter. This text is [b]bold[/b] and this is [i]italic[/i]. You can also [u]underline[/u] text. Here is a link to the uCalc website: [url=https://www.ucalc.com]uCalc[/url]. [quote]This is a block of quoted text. It can span multiple lines.[/quote] """; // 4. Run the transformation and print the result Console.WriteLine(t.Transform(bbCode)); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// 1. Setup the Transformer
{
uCalc::Transformer t;
t.Owned(); // Causes t to be released when it goes out of scope
// Allow patterns to match across multiple lines
t.DefaultRuleSet().StatementSensitive(false);
// 2. Define the conversion rules
// Simple inline tags
t.FromTo("'['b']'{text}'['/b']'", "**{text}**");
t.FromTo("'['i']'{text}'['/i']'", "*{text}*");
t.FromTo("'['u']'{text}'['/u']'", "{text}");
// URL tag with attribute
t.FromTo("'['url={href}']'{text}'['/url']'", "[{text}]({href})");
// Quote block tag
t.FromTo("'['quote']'{content}'['/quote']'", "> {content}");
// 3. Define the input BBCode text
auto bbCode = R"(
Hello, this is a test of the converter.
This text is [b]bold[/b] and this is [i]italic[/i].
You can also [u]underline[/u] text.
Here is a link to the uCalc website: [url=https://www.ucalc.com]uCalc[/url].
[quote]This is a block of quoted text.
It can span multiple lines.[/quote]
)";
// 4. Run the transformation and print the result
cout << t.Transform(bbCode) << endl;
}
}
Hello, this is a test of the converter.
This text is **bold** and this is *italic*.
You can also <u>underline</u> text.
Here is a link to the uCalc website: [uCalc](https://www.ucalc.com).
> This is a block of quoted text.
It can span multiple lines.
#include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // 1. Setup the Transformer { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope // Allow patterns to match across multiple lines t.DefaultRuleSet().StatementSensitive(false); // 2. Define the conversion rules // Simple inline tags t.FromTo("'['b']'{text}'['/b']'", "**{text}**"); t.FromTo("'['i']'{text}'['/i']'", "*{text}*"); t.FromTo("'['u']'{text}'['/u']'", "<u>{text}</u>"); // URL tag with attribute t.FromTo("'['url={href}']'{text}'['/url']'", "[{text}]({href})"); // Quote block tag t.FromTo("'['quote']'{content}'['/quote']'", "> {content}"); // 3. Define the input BBCode text auto bbCode = R"( Hello, this is a test of the converter. This text is [b]bold[/b] and this is [i]italic[/i]. You can also [u]underline[/u] text. Here is a link to the uCalc website: [url=https://www.ucalc.com]uCalc[/url]. [quote]This is a block of quoted text. It can span multiple lines.[/quote] )"; // 4. Run the transformation and print the result cout << t.Transform(bbCode) << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// 1. Setup the Transformer
Using t As New uCalc.Transformer()
'// Allow patterns to match across multiple lines
t.DefaultRuleSet.StatementSensitive = false
'// 2. Define the conversion rules
'// Simple inline tags
t.FromTo("'['b']'{text}'['/b']'", "**{text}**")
t.FromTo("'['i']'{text}'['/i']'", "*{text}*")
t.FromTo("'['u']'{text}'['/u']'", "{text}")
'// URL tag with attribute
t.FromTo("'['url={href}']'{text}'['/url']'", "[{text}]({href})")
'// Quote block tag
t.FromTo("'['quote']'{content}'['/quote']'", "> {content}")
'// 3. Define the input BBCode text
Dim bbCode = "
Hello, this is a test of the converter.
This text is [b]bold[/b] and this is [i]italic[/i].
You can also [u]underline[/u] text.
Here is a link to the uCalc website: [url=https://www.ucalc.com]uCalc[/url].
[quote]This is a block of quoted text.
It can span multiple lines.[/quote]
"
'// 4. Run the transformation and print the result
Console.WriteLine(t.Transform(bbCode))
End Using
End Sub
End Module
Hello, this is a test of the converter.
This text is **bold** and this is *italic*.
You can also <u>underline</u> text.
Here is a link to the uCalc website: [uCalc](https://www.ucalc.com).
> This is a block of quoted text.
It can span multiple lines.
Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// 1. Setup the Transformer Using t As New uCalc.Transformer() '// Allow patterns to match across multiple lines t.DefaultRuleSet.StatementSensitive = false '// 2. Define the conversion rules '// Simple inline tags t.FromTo("'['b']'{text}'['/b']'", "**{text}**") t.FromTo("'['i']'{text}'['/i']'", "*{text}*") t.FromTo("'['u']'{text}'['/u']'", "<u>{text}</u>") '// URL tag with attribute t.FromTo("'['url={href}']'{text}'['/url']'", "[{text}]({href})") '// Quote block tag t.FromTo("'['quote']'{content}'['/quote']'", "> {content}") '// 3. Define the input BBCode text Dim bbCode = " Hello, this is a test of the converter. This text is [b]bold[/b] and this is [i]italic[/i]. You can also [u]underline[/u] text. Here is a link to the uCalc website: [url=https://www.ucalc.com]uCalc[/url]. [quote]This is a block of quoted text. It can span multiple lines.[/quote] " '// 4. Run the transformation and print the result Console.WriteLine(t.Transform(bbCode)) End Using End Sub End Module