Class: RDocWithHyperlinkToHtml

Inherits:
SM::ToHtml
  • Object
show all
Defined in:
lib/acts_as_markup/exts/rdoc.rb

Instance Method Summary collapse

Instance Method Details

#gen_url(url, text) ⇒ Object

Generate a hyperlink for url, labeled with text. Handle the special cases for img: and link: described under handle_special_HYPEDLINK



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/acts_as_markup/exts/rdoc.rb', line 9

def gen_url(url, text)
  if url =~ /([A-Za-z]+):(.*)/
    type = $1
    path = $2
  else
    type = "http"
    path = url
    url  = "http://#{url}"
  end

  if type == "link"
    if path[0,1] == '#'  # is this meaningful?
      url = path
    else
      url = HTMLGenerator.gen_url(@from_path, path)
    end
  end

  if (type == "http" || type == "link") && url =~ /\.(gif|png|jpg|jpeg|bmp)$/
    "<img src=\"#{url}\" />"
  else
    "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
  end
end

And we’re invoked with a potential external hyperlink mailto: just gets inserted. http: links are checked to see if they reference an image. If so, that image gets inserted using an <img> tag. Otherwise a conventional <a href> is used. We also support a special type of hyperlink, link:, which is a reference to a local file whose path is relative to the –op directory.



41
42
43
44
# File 'lib/acts_as_markup/exts/rdoc.rb', line 41

def handle_special_HYPERLINK(special)
  url = special.text
  gen_url(url, url)
end

Here’s a hypedlink where the label is different to the URL

<label>[url]


49
50
51
52
53
54
55
56
57
# File 'lib/acts_as_markup/exts/rdoc.rb', line 49

def handle_special_TIDYLINK(special)
  text = special.text
  unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/ 
    return text
  end
  label = $1
  url   = $2
  gen_url(url, label)
end