Class: Markbridge::Parsers::MediaWiki::Parser

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

Overview

Parses MediaWiki wikitext into an AST.

Supports:

  • Bold (”‘), italic (”), bold italic (””’)

  • Headings (= through ======)

  • Unordered lists (* / ** / ***)

  • Ordered lists (# / ## / ###)

  • Horizontal rules (—-)

  • Internal links ([[target]] / [[target|display]])

  • External links ([url text])

  • Preformatted text (lines starting with a space)

  • Tables ({| … |})

  • HTML tags: <nowiki>, <code>, <pre>,
    , <s>, <del>, <u>, <ins>, <sup>, <sub>

Examples:

Basic usage

parser = Markbridge::Parsers::MediaWiki::Parser.new
ast = parser.parse("'''bold''' and ''italic''")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.

Parameters:

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

    custom registry or use default. Named handlers: for consistency with sibling parsers; the value is still an InlineTagRegistry instance.

Yields:



33
34
35
36
37
38
# File 'lib/markbridge/parsers/media_wiki/parser.rb', line 33

def initialize(handlers: nil, &block)
  # InlineParser falls back to InlineTagRegistry.default when this is
  # nil, so we don't need to materialise it here.
  @handlers = block_given? ? InlineTagRegistry.build_from_default(&block) : handlers
  @unknown_tags = Hash.new(0)
end

Instance Attribute Details

#unknown_tagsHash{String => Integer} (readonly)

Returns tag-name → occurrence count for inline HTML-like tags whose names are not registered. Reset at the start of every #parse call.

Returns:

  • (Hash{String => Integer})

    tag-name → occurrence count for inline HTML-like tags whose names are not registered. Reset at the start of every #parse call.



27
28
29
# File 'lib/markbridge/parsers/media_wiki/parser.rb', line 27

def unknown_tags
  @unknown_tags
end

Instance Method Details

#parse(input) ⇒ AST::Document

Parse MediaWiki wikitext into an AST Document.

Parameters:

  • input (String)

    MediaWiki source

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/markbridge/parsers/media_wiki/parser.rb', line 44

def parse(input)
  normalized = normalize_line_endings(input)
  lines = normalized.split("\n")

  @unknown_tags.clear
  @document = AST::Document.new
  @inline_parser = InlineParser.new(handlers: @handlers, unknown_tags: @unknown_tags)
  @list_stack = []

  process_lines(lines)
  @document
end