For the longest time I've been talking about using an Abstract Syntax Tree. That involves creating a lexer/parser that creates an array of tokens. Those tokens are then read at runtime. I think what we want is a little different but I'm no expert on lexers.
Here's an example post:
Hello Emanuele,
I am writing you to tell you about my new site: [url=https://www.elkarte.net]ElkArte[/url]. I added a lot of features:
[*]create new posts
[*]two-factor authentication
[*]bbc parsing [i]we're going to call this ForumML or [b]FML[/b] :D[/i]
ttyl,
Josh
https://www.github.com/joshuaadickerson
This will create an array (JS object for brevity):
[
{node: 'text', value: 'Hello Emanuele,'},
{node: 'new line'},
{node: 'empty line'},
{node: 'text', value: 'I am writing you to tell you about my new site: '},
{node: 'tag', value: 'url',
attr: [{node: 'url', value: 'https://www.elkarte.net'}],
children: [{node: 'text', value: 'ElkArte'}]
},
{node: 'text', value: '. I added a lot of features: '},
{node: 'new line'},
// Maybe the parser should make this a new list?
{node: 'itemcode', value: '*',
children: [{node: 'text', value: 'create new posts'}]
},
{node: 'new line'},
{node: 'itemcode', value: '*',
children: [{node: 'text', value: 'two-factor authentication'}]
},
{node: 'new line'},
{node: 'itemcode', value: '*',
children: [
{node: 'text', value: 'bbc parsing '},
{node: 'tag', value: 'i', children: [
{node: 'text', 'value': 'we\'re going to call this ForumML or '},
{node: 'tag', value: 'b', children: [{node: 'text', 'value': 'FML'}]},
{node: 'emoji', 'value': ':D'},
]}
]
},
{node: 'new line'},
{node: 'empty line'},
{node: 'text', value: 'ttyl,'},
{node: 'new line'},
{node: 'text', value: 'Josh'},
{node: 'new line'},
{node: 'tag', value: 'url',
attr: [{node: 'url', value: 'https://www.github.com/joshuaadickerson'}],
children: [{node: 'text', value: 'https://www.github.com/joshuaadickerson'}]
}
]
I'm sure there's a lot that can be done to make that smaller and maybe make it faster, but I did this in this reply window and I'm not an expert. Baby steps.
I'll leave that there for now and work on a formatter for ForumML next.