Skip to main content

BBCodeRenderer

ClassSource

BBCodeRenderer(configs: BBCodeConfig | None = None)

The BBCode renderer.

Render the structured Document or an intemediate Node into the BBCode format.

Arguments​

Requires​

NameTypeRequired
Description
configsBBCodeConfig | None

The configurations used for customizing the BBCode tags.

If not specified, will use BBCodeConfig() by default.

Methods​

render​

v_1: str = self.render(node: Node | Document)

Render the BBCode.

Main dispatcher. Uses node.type directly so type checkers can narrow the union correctly.

Requires​

NameTypeRequired
Description
nodeNode | DocumentThe node or the document that would be rendered as BBCode.

Returns​

NameType
Description
v_1strThe rendered BBCode text.

render_alert​

v_1: str = self.render_alert(node: AlertNode)

Specific renderring. Render the alert block.

Requires​

NameTypeRequired
Description
nodeAlertNodeThe alert block to be rendered.

Returns​

NameType
Description
v_1strThe rendered alert block bbcode.

render_children​

v_1: str = self.render_children(children: list[Node])

Helper method. Render a list of children nodes into BBCode.

Requires​

NameTypeRequired
Description
childrenlist[Node]The children to be rendered.

Returns​

NameType
Description
v_1strThe rendered children bbcode.

render_code_block​

v_1: str = self.render_code_block(node: CodeBlockNode)

Specific renderring. Render the block code as

[code]
...
[/code]

Requires​

NameTypeRequired
Description
nodeCodeBlockNodeThe code block to be rendered.

Returns​

NameType
Description
v_1strThe rendered code block.

render_document​

v_1: str = self.render_document(doc: Document)

Specific renderring. Render the whole document.

Requires​

NameTypeRequired
Description
docDocumentThe document to be rendered.

Returns​

NameType
Description
v_1strThe rendered document.

render_heading​

v_1: str = self.render_heading(node: HeadingNode)

Specific renderring. Render the heading (title).

Requires​

NameTypeRequired
Description
nodeHeadingNodeThe heading/title to be rendered.

Returns​

NameType
Description
v_1strThe rendered title bbcode.

render_inline_code​

v_1: str = self.render_inline_code(node: InlineCodeNode)

Specific renderring. Render the inline code.

Requires​

NameTypeRequired
Description
nodeInlineCodeNodeThe inline code to be rendered.

Returns​

NameType
Description
v_1strThe rendered inline code bbcode.

v_1: str = self.render_link(node: LinkNode)

Specific renderring. Render the url (link).

Note that there is an exception. If href is the same as text, will render the plain text directly.

Requires​

NameTypeRequired
Description
nodeLinkNodeThe link/url to be rendered.

Returns​

NameType
Description
v_1strThe rendered url bbcode.

render_list​

v_1: str = self.render_list(node: ListNode)

Specific rendering. Render the ordered or unordered list.

Requires​

NameTypeRequired
Description
nodeListNodeThe list to be rendered.

Returns​

NameType
Description
v_1strThe rendered list bbcode.

render_list_item​

v_1: str = self.render_list_item(
node: ListItemNode, idx: int, parent_node: ListNode | None = None
)

Specific renderring. Render the list item.

Requires​

NameTypeRequired
Description
nodeListItemNodeThe list item to be rendered.
idxintThe optional argument idx is the current index of the item in the list.
parent_nodeListNode | NoneThe optional parent_node is the nearest unordered/ordered list wrapper of this item.

Returns​

NameType
Description
v_1strThe rendered list item bbcode.

render_paragraph​

v_1: str = self.render_paragraph(node: ParagraphNode)

Specific renderring. Render the paragraph.

Requires​

NameTypeRequired
Description
nodeParagraphNodeThe paragraph to be rendered.

Returns​

NameType
Description
v_1strThe rendered paragraph bbcode.

render_quote​

v_1: str = self.render_quote(node: QuoteNode)

Specific renderring. Render the quote block.

Requires​

NameTypeRequired
Description
nodeQuoteNodeThe quote block to be rendered.

Returns​

NameType
Description
v_1strThe rendered quote block.

render_table​

v_1: str = self.render_table(node: TableNode)

Specific renderring. Render the table.

BBCode table syntax varies by forum. We adopt the Steam's format.

[table]
[tr][th]Header[/th][th]Header[/th][/tr]
[tr][td]Cell[/td][td]Cell[/td][/tr]
[/table]

Requires​

NameTypeRequired
Description
nodeTableNodeThe table to be rendered.

Returns​

NameType
Description
v_1strThe rendered table bbcode.

render_table_cell​

v_1: str = self.render_table_cell(node: TableCellNode)

Specific renderring. Render the table cell (head or data cells).

Requires​

NameTypeRequired
Description
nodeTableCellNodeThe table cell to be rendered.

Returns​

NameType
Description
v_1strThe rendered table cell.

render_table_row​

v_1: str = self.render_table_row(node: TableRowNode)

Specific renderring. Render the table row.

Requires​

NameTypeRequired
Description
nodeTableRowNodeThe table row to be rendered.

Returns​

NameType
Description
v_1strThe rendered table row.

render_text​

v_1: str = self.render_text(node: TextNode)

Specific renderring. Render the plain text into BBCode.

Requires​

NameTypeRequired
Description
nodeTextNodeThe pure text to be rendered.

Returns​

NameType
Description
v_1strThe rendered text bbcode.

wrap_children​

v_1: str = self.wrap_children(children: list[Node], start: str, end: str | None = None)

Helper method. Render children and surround the results by:

[start]children[/end]

Requires​

NameTypeRequired
Description
childrenlist[Node]The children to be rendered with the wrapper.
startstrThe "start" tag prepended to the children.
endstr | NoneThe "end" tag appended to the children.

Returns​

NameType
Description
v_1strThe rendered and wrapped children bbcode.