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 (<code>, <nowiki>, <s>, <del>, <u>, <ins>, <sup>, <sub>,
).

Instance Method Summary collapse

Constructor Details

#initializeInlineParser

Returns a new instance of InlineParser.



10
11
12
13
14
15
16
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 10

def initialize
  @input = nil
  @pos = 0
  @length = 0
  @parent = nil
  @text_buffer = +""
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



22
23
24
25
26
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
53
54
55
56
57
58
# File 'lib/markbridge/parsers/media_wiki/inline_parser.rb', line 22

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

  while @pos < @length
    char = @input[@pos]
    next_char = @pos + 1 < @length ? @input[@pos + 1] : nil

    case char
    when "'"
      if next_char == "'"
        parse_bold_italic
      else
        @text_buffer << char
        @pos += 1
      end
    when "["
      flush_text
      if next_char == "["
        parse_internal_link
      else
        parse_external_link
      end
    when "<"
      flush_text
      parse_html_tag
    else
      @text_buffer << char
      @pos += 1
    end
  end

  flush_text
end