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.



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

def initialize(parent)
  @parent = parent
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



42
43
44
# File 'lib/wikitxt/parser.rb', line 42

def parent
  @parent
end

Instance Method Details

#to_htmlObject



48
49
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
# File 'lib/wikitxt/parser.rb', line 48

def to_html
  pending = nil

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

  until scanner.eos? do
    if result = scanner.scan(/ ?#<.+>/)
      parent.children << pending if pending
      pending = nil
      match = result.match(/#<(?<title>.*) *(?<url>https?:\/\/.+\.\S+) *(?<title>.*)>/)
      if match[:url].match(/\.(png|jpg|jpeg|svg)/)
        parent.children << ImageNode.new(url: match[:url], title: match[:title])
        next
      end
      parent.children << LinkNode.new(url: match[:url], title: match[:title])
      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