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.

Examples:

With custom registry

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

Constant Summary collapse

MAX_INLINE_DEPTH =
20

Instance Method Summary collapse

Constructor Details

#initialize(inline_tag_registry: nil, depth: 0) ⇒ InlineParser

Returns a new instance of InlineParser.



18
19
20
21
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 18

def initialize(inline_tag_registry: nil, depth: 0)
  @registry = inline_tag_registry || InlineTagRegistry.default
  @depth = depth
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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 27

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

  while @pos < @length
    char = @input[@pos]

    case char
    when "'"
      consecutive_apostrophes_at(@pos) >= 2 ? parse_bold_italic : append_literal(char)
    when "["
      flush_text
      @input[@pos + 1] == "[" ? parse_internal_link : parse_external_link
    when "<"
      flush_text
      parse_html_tag
    else
      append_literal(char)
    end
  end

  flush_text
end