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.
Accepts either a String of XML or a pre-parsed Nokogiri node. A Nokogiri::XML::Document is unwrapped via #root; any other node is treated as the root itself.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/markbridge/parsers/text_formatter/parser.rb', line 50 def parse(input) @unknown_tags.clear if input.is_a?(Nokogiri::XML::Node) root = input.is_a?(Nokogiri::XML::Document) ? input.root : input document = AST::Document.new process_node(root, document) if root return document end input = input.to_s 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 AST::Document.new << AST::Text.new(input) end |
#process_children(element, ast_parent) ⇒ Object
Process children of an XML element (public for handler access)
81 82 83 |
# File 'lib/markbridge/parsers/text_formatter/parser.rb', line 81 def process_children(element, ast_parent) element.children.each { |child| process_node(child, ast_parent) } end |