Class: HParser::Inline::Url

Inherits:
Object
  • Object
show all
Includes:
Html, Collectable
Defined in:
lib/hparser/html.rb,
lib/hparser/text.rb,
lib/hparser/latex.rb,
lib/hparser/hatena.rb,
lib/hparser/inline/url.rb

Constant Summary collapse

@@url_re =
%r!https?://[A-Za-z0-9~\/._\?\&=\-%#\+:;,\@\'\$\*\!]+!
@@bracket_url_with_title_re =
%r!\[(#{@@url_re}):title(?:=(.*?))?(:bookmark)?\]!
@@bracket_url_re =
%r!\[(#{@@url_re})\]!

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Html

#escape

Constructor Details

#initialize(url, title = nil, bookmark = false) ⇒ Url

Returns a new instance of Url.



28
29
30
31
32
# File 'lib/hparser/inline/url.rb', line 28

def initialize(url, title=nil, bookmark=false)
  @url = url
  @title = title.nil? ? url : title.empty? ? "(undefined)" : title
  @bookmark = bookmark
end

Instance Attribute Details

#bookmarkObject (readonly)

Returns the value of attribute bookmark.



14
15
16
# File 'lib/hparser/inline/url.rb', line 14

def bookmark
  @bookmark
end

#titleObject (readonly)

Returns the value of attribute title.



14
15
16
# File 'lib/hparser/inline/url.rb', line 14

def title
  @title
end

#urlObject (readonly) Also known as: to_text, to_hatena

Returns the value of attribute url.



14
15
16
# File 'lib/hparser/inline/url.rb', line 14

def url
  @url
end

Class Method Details

.parse(scanner, context = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hparser/inline/url.rb', line 15

def self.parse(scanner, context=nil)
  if scanner.scan(@@url_re) then
    Url.new scanner.matched, scanner.matched
  elsif scanner.scan(@@bracket_url_with_title_re) then
    title = ""
    title = scanner[2] if scanner[2] and scanner[2] != ":bookmark"
    bookmark = (scanner[2] == ":bookmark" || scanner[3] == ":bookmark")
    Url.new scanner[1], title, bookmark
  elsif scanner.scan(@@bracket_url_re)
    Url.new scanner[1]
  end
end

Instance Method Details

#==(o) ⇒ Object



34
35
36
37
# File 'lib/hparser/inline/url.rb', line 34

def ==(o)
  o and self.class and o.class and @url == o.url and @title == o.title and
    @bookmark == o.bookmark
end

#to_htmlObject



294
295
296
297
298
299
300
301
302
# File 'lib/hparser/html.rb', line 294

def to_html
  if @bookmark then
      require 'uri'
      enc_url = escape(URI.encode(url))
      bookmark = %( <a href="http://b.hatena.ne.jp/entry/#{enc_url}" class="http-bookmark">) + 
                 %(<img src="http://b.hatena.ne.jp/entry/image/#{enc_url}" alt="" class="http-bookmark"></a>)
  end
  %(<a href="#{escape(self.url)}">#{escape(self.title)}</a>#{bookmark})
end

#to_latexObject



236
237
238
# File 'lib/hparser/latex.rb', line 236

def to_latex
  "\\href{#{self.url}/}{#{self.url}}"
end