TextFormatter
s9e\TextFormatter is the PHP library that phpBB 3.2+ and Flarum format posts with. These forums do not store what the user typed. They store XML that the library made of it, with the BBCode or Markdown markup still inside. If the post column of your database starts with <r> or <t>, this is your format.
Read the XML, not the markup inside it. The XML says what the forum made of every tag: which [b] was really bold, where a list item ends, what a bare URL links to. A BBCode parser would have to guess all of that again.
Requirements
Section titled “Requirements”Add nokogiri to your Gemfile. Markbridge does not depend on it, because the BBCode and MediaWiki parsers work without it:
gem "nokogiri"Quick start
Section titled “Quick start”require "markbridge/textformatter"
xml = "<r><B><s>[b]</s>Hello<e>[/b]</e></B> world!</r>"result = Markbridge.text_formatter_xml_to_markdown(xml)result.markdown# => "**Hello** world!"The format in short
Section titled “The format in short”The root element says whether the post has markup:
<t>is plain text.<r>is text with markup (“rich”).
Inside <r>, the elements with a meaning have uppercase names: <B>, <URL>, <CODE>. The markup the user typed is kept next to them in three lowercase elements, which the parser skips:
<s>and<e>hold the start and the end markup of an element, like[b]and[/b].<i>holds text that s9e ignored, like the newline after[list]or a[/b]that closes nothing. Do not mix it up with<I>, which is italic.
A line break is stored as <br/> with a newline after it: a<br/>\nb. The forum shows the XML as HTML, where that newline and spaces at the start of a line are not visible. The parser removes them, so a<br/>\nb is a\nb in Markdown and an indented line does not become a code block. Two <br/> are a blank line. Text inside <CODE> stays as it is.
Supported elements
Section titled “Supported elements”Litedown
Section titled “Litedown”Litedown is the Markdown plugin of s9e\TextFormatter. Flarum uses it, so a Flarum post is Markdown that was stored as XML. Litedown has elements of its own next to the BBCode ones:
| Element | Markdown source | Renders as | AST node |
|---|---|---|---|
<STRONG> |
**bold** |
**bold** |
AST::Bold |
<EM> |
*italic* |
*italic* |
AST::Italic |
<DEL> |
~~strike~~ |
~~strike~~ |
AST::Strikethrough |
<SUP>, <SUB> |
x^2^, H~2~O |
<sup>…</sup> / <sub>…</sub> |
AST::Superscript, AST::Subscript |
<C> |
`code` |
Code span | AST::Code |
<CODE> |
Fenced or indented code | Fenced code block | AST::Code with block: true |
<H1>–<H6> |
# Heading or an underlined one |
# through ###### headings |
AST::Heading |
<ISPOILER> |
||spoiler|| |
[spoiler]…[/spoiler] inside the line |
AST::Spoiler |
<SPOILER> |
>! spoiler |
Block spoiler | AST::Spoiler with block: true |
<p> |
A paragraph | Paragraph spacing | AST::Paragraph |
<QUOTE>, <LIST>, <LI>, <URL>, <EMAIL>, <IMG> and <HR> are the same elements as for BBCode. The Markdown markers (**, #, >) are in <s>, <e> and <i> elements, like the BBCode tags.
Three more elements have no handler on purpose: <ESC> (a character the user escaped with a backslash), <E> (an emoticon) and <EMOJI>. Their text goes into the output and unknown_tags does not count them. They are the transparent_tags of the HandlerRegistry. Add your own to the set like in the HTML parser.
Flarum’s own elements have no handler, for example <USERMENTION> and <POSTMENTION>. Their text is kept and unknown_tags counts them. Register handlers for the ones your forum uses, see Extending Markbridge.
Notes on the elements
Section titled “Notes on the elements”phpBB shows a [spoiler] as a block, so <SPOILER> is a block spoiler and the text after it starts a new line. <ISPOILER> is the inline spoiler of the Litedown plugin. <HR> comes from the [hr] BBCode of the s9e repository and from Litedown.
The parser reads these attributes: lang of <CODE>, url of <URL>, email of <EMAIL>, src of <IMG>, type of <LIST> (bullet or decimal), the author of <QUOTE>, and colspan and rowspan of <TD> and <TH>.
Relative links and images are written as they are, and Discourse drops an address without a scheme and without a leading slash. Give the renderer the address of the old forum as base_url:.
HandlerRegistry.default is the exact list.
Colors and sizes
Section titled “Colors and sizes”Discourse shows colors and sizes only with the discourse-bbcode plugin. Markbridge cannot know whether your site has it, so you have to say:
features: { bbcode: false }writes the text without color and size.features: { bbcode: true }writes[color=…]and[size=…]BBCode for the plugin.
Without the decision, the first color or size raises Markbridge::UndecidedFeatureError.
phpBB 3.x stores [size] as a percentage and the plugin reads it as one, so the size passes as it is:
renderer = Markbridge.discourse_renderer(features: { bbcode: true }, sizes: ->(size) { size })
xml = '<r><SIZE size="85"><s>[size=85]</s>small<e>[/size]</e></SIZE></r>'Markbridge.text_formatter_xml_to_markdown(xml, renderer:).markdown# => "[size=85]small[/size]"See Customizing the renderer for features:.
Using the parser directly
Section titled “Using the parser directly”parser = Markbridge::Parsers::TextFormatter::Parser.newast = parser.parse('<r><B><s>[b]</s>bold<e>[/b]</e></B></r>')
renderer = Markbridge::Renderers::Discourse::Renderer.newrenderer.render(ast)# => "**bold**"What else to know
Section titled “What else to know”- Input that is not valid XML is read as plain text. Nothing is raised.
- An element without a handler is dropped and its content is processed.
unknown_tagscounts it. - The content of
<IMG>and<ATTACHMENT>is skipped. It is only the URL or the filename again. - A handler is an object with
#process(element:, parent:, processor:), or a lambda with the same keywords.processoris the parser. Callprocessor.process_children(xml_element, ast_node)when the handler has to process the children itself.