Class: Markbridge::Parsers::TextFormatter::Parser
- Inherits:
-
Object
- Object
- Markbridge::Parsers::TextFormatter::Parser
- Defined in:
- lib/markbridge/parsers/text_formatter/parser.rb
Overview
Parses s9e/TextFormatter XML format into an AST
The s9e/TextFormatter library (github.com/s9e/TextFormatter) stores BBCode as XML:
-
Plain text: <t>text content</t>
-
Rich text: <r>bold <URL url=“…”>link</URL></r>
-
Markup preservation: <s> and <e> elements (ignored during parsing)
This format is used by phpBB 3.2+ and other forum software.
Requires Nokogiri gem to be installed. Add to your Gemfile:
gem "nokogiri"
Instance Attribute Summary collapse
-
#unknown_tags ⇒ Object
readonly
Returns the value of attribute unknown_tags.
Instance Method Summary collapse
-
#initialize(handlers: nil) {|HandlerRegistry| ... } ⇒ Parser
constructor
Create a new parser with optional custom handler registry.
-
#parse(input) ⇒ AST::Document
Parse s9e/TextFormatter XML into an AST.
-
#process_children(element, ast_parent) ⇒ Object
Process children of an XML element (public for handler access).
Constructor Details
#initialize(handlers: nil) {|HandlerRegistry| ... } ⇒ Parser
Create a new parser with optional custom handler registry
31 32 33 34 35 36 37 38 39 |
# File 'lib/markbridge/parsers/text_formatter/parser.rb', line 31 def initialize(handlers: nil, &block) @handlers = if block_given? HandlerRegistry.build_from_default(&block) else handlers || HandlerRegistry.default end @unknown_tags = Hash.new(0) end |
Instance Attribute Details
#unknown_tags ⇒ Object (readonly)
Returns the value of attribute unknown_tags.
18 19 20 |
# File 'lib/markbridge/parsers/text_formatter/parser.rb', line 18 def @unknown_tags end |
Instance Method Details
#parse(input) ⇒ AST::Document
Parse s9e/TextFormatter XML into an AST
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/markbridge/parsers/text_formatter/parser.rb', line 44 def parse(input) @unknown_tags.clear xml_doc = Nokogiri.XML(input) root = xml_doc.root unless root # Invalid or non-XML - treat as plain text document = AST::Document.new document << AST::Text.new(input) unless input.empty? return document end document = AST::Document.new process_node(root, document) document rescue Nokogiri::XML::SyntaxError => e # Invalid XML - treat as plain text document = AST::Document.new document << AST::Text.new(input) document end |
#process_children(element, ast_parent) ⇒ Object
Process children of an XML element (public for handler access)
70 71 72 |
# File 'lib/markbridge/parsers/text_formatter/parser.rb', line 70 def process_children(element, ast_parent) element.children.each { |child| process_node(child, ast_parent) } end |