Method: Wikitxt::Parser::Inline#to_html

Defined in:
lib/wikitxt/parser.rb

#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