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(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.



24
25
26
27
28
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 24

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



22
23
24
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 22

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 34

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