Class: Wikitxt::Parser::Inline

Inherits:
Object
  • Object
show all
Defined in:
lib/wikitxt/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Inline

Returns a new instance of Inline.



46
47
48
# File 'lib/wikitxt/parser.rb', line 46

def initialize(parent)
  @parent = parent
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



44
45
46
# File 'lib/wikitxt/parser.rb', line 44

def parent
  @parent
end

Instance Method Details

#to_htmlObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/wikitxt/parser.rb', line 50

def to_html
  pending = nil

  scanner = StringScanner.new(parent.attrs[:text])

  until scanner.eos? do
    if result = scanner.scan(URI.regexp(["http", "https"]))
      parent.children << pending if pending
      pending = nil
      if result.match(/\.(png|jpg|jpeg|svg)/)
        parent.children << ImageNode.new(url: result)
        next
      end
      parent.children << LinkNode.new(url: result)
      next
    end

    if result = scanner.scan(/(^| )#\S+( |$)/)
      parent.children << pending if pending
      pending = nil
      match = result.match(/(^| )#(?<url>\S+)( |$)/)
      if match[:url].match(/\.(png|jpg|jpeg|svg)/)
        parent.children << ImageNode.new(url: "/#{match[:url]}")
        next
      end
      parent.children << LinkNode.new(url: "/#{match[:url]}.html", title: match[:url])
      next
    end

    if pending
      pending.attrs[:text] += scanner.scan(/./)
      next
    end

    pending = TextNode.new(text: scanner.scan(/./))
  end

  parent.children << pending if pending

  parent.children.map(&:to_html).join
end