Class: Markbridge::Parsers::BBCode::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/markbridge/parsers/bbcode/parser.rb

Overview

Parses BBCode into an AST using handlers from HandlerRegistry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handlers: nil) {|HandlerRegistry| ... } ⇒ Parser

Create a new parser with optional custom handlers

Examples:

Using default handlers

parser = Parser.new

Using custom handlers

parser = Parser.new(handlers: my_registry)

Customizing default handlers

parser = Parser.new do |registry|
  registry.register("quote", QuoteHandler.new)
end

Parameters:

  • handlers (HandlerRegistry, nil) (defaults to: nil)

    custom handler registry, defaults to HandlerRegistry.default

Yields:



24
25
26
27
28
29
30
31
32
# File 'lib/markbridge/parsers/bbcode/parser.rb', line 24

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

#auto_closed_tags_countObject (readonly)

Returns the value of attribute auto_closed_tags_count.



8
9
10
# File 'lib/markbridge/parsers/bbcode/parser.rb', line 8

def auto_closed_tags_count
  @auto_closed_tags_count
end

#depth_exceeded_countObject (readonly)

Returns the value of attribute depth_exceeded_count.



8
9
10
# File 'lib/markbridge/parsers/bbcode/parser.rb', line 8

def depth_exceeded_count
  @depth_exceeded_count
end

#unclosed_raw_tagsObject (readonly)

Returns the value of attribute unclosed_raw_tags.



8
9
10
# File 'lib/markbridge/parsers/bbcode/parser.rb', line 8

def unclosed_raw_tags
  @unclosed_raw_tags
end

#unknown_tagsObject (readonly)

Returns the value of attribute unknown_tags.



8
9
10
# File 'lib/markbridge/parsers/bbcode/parser.rb', line 8

def unknown_tags
  @unknown_tags
end

Instance Method Details

#parse(input) ⇒ AST::Document

Parse BBCode string into an AST

Parameters:

  • input (String)

    BBCode source

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/markbridge/parsers/bbcode/parser.rb', line 37

def parse(input)
  @unknown_tags.clear

  normalized = normalize_line_endings(input)

  document = AST::Document.new
  context = ParserState.new(document)

  scanner = Scanner.new(normalized)
  parse_tokens(scanner, context)

  @auto_closed_tags_count = context.auto_closed_count
  @depth_exceeded_count = context.depth_exceeded_count
  @unclosed_raw_tags = context.unclosed_raw_tags
  document
end