Class: Markbridge::Parsers::MediaWiki::InlineParser

Inherits:
Object
  • Object
show all
Defined in:
lib/markbridge/parsers/media_wiki/inline_parser.rb

Overview

Parses inline MediaWiki markup within a line of text. Handles bold ('''), italic (''), links ([[...]]), external links ([...]), and HTML inline tags via an InlineTagRegistry.

The parser works in byte offsets (byteindex/byteslice/getbyte): character indices are O(pos) on multibyte input in CRuby, and per-character probes allocate a String each. The main loop jumps between interesting bytes with a single regex search and copies the skipped span in one slice. Invariant: @pos always sits on a character boundary — jumps land on ASCII matches and advances step over ASCII bytes or whole matches.

Examples:

With custom registry

registry = InlineTagRegistry.build_from_default do |r|
  r.register("mark", :formatting, AST::Bold)
end
parser = InlineParser.new(handlers: registry)

Constant Summary collapse

MAX_INLINE_DEPTH =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handlers: nil, depth: 0, unknown_tags: nil) ⇒ InlineParser

Returns a new instance of InlineParser.



45
46
47
48
49
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 45

def initialize(handlers: nil, depth: 0, unknown_tags: nil)
  @registry = handlers || InlineTagRegistry.shared_default
  @depth = depth
  @unknown_tags = unknown_tags || Hash.new(0)
end

Instance Attribute Details

#unknown_tagsHash{String => Integer} (readonly)

Returns tag-name → occurrence count for HTML-like inline tags whose names are not registered. Shared with nested InlineParser instances so depth-recursive parses contribute to the same tally.

Returns:

  • (Hash{String => Integer})

    tag-name → occurrence count for HTML-like inline tags whose names are not registered. Shared with nested InlineParser instances so depth-recursive parses contribute to the same tally.



43
44
45
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 43

def unknown_tags
  @unknown_tags
end

Instance Method Details

#parse(text, parent:) ⇒ Object

Parse inline markup and append resulting AST nodes to the parent element.

Parameters:

  • text (String)

    the text to parse for inline markup

  • parent (AST::Element)

    the element to append children to



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 55

def parse(text, parent:)
  @input = text
  @pos = 0
  @length = text.bytesize
  @parent = parent
  @text_buffer = +""

  while (span_end = @input.byteindex(INTERESTING, @pos))
    # Unconditional on purpose: when the interesting byte sits at
    # @pos the slice is empty and both lines are no-ops.
    @text_buffer << @input.byteslice(@pos, span_end - @pos)
    @pos = span_end

    dispatch_interesting_byte
  end

  # Trailing text after the last interesting byte; appending the
  # empty slice at end-of-input is a no-op.
  @text_buffer << @input.byteslice(@pos, @length - @pos)
  flush_text
end