Class: RDocSupport::HyperLinkHtml

Inherits:
SM::ToHtml
  • Object
show all
Defined in:
lib/rdocsupport.rb

Overview

Handle special hyperlinking requirments for RDoc formatted entries. Requires RDoc

Instance Method Summary collapse

Constructor Details

#initializeHyperLinkHtml

Initialize the HyperLinkHtml object.

path

location of the node

site

object representing the whole site (typically of class

+Site+)


49
50
51
52
# File 'lib/rdocsupport.rb', line 49

def initialize
  super()
  add_tag(:CENTER, "<center>", "</center>")
end

Instance Method Details

#handle_special_BR(special) ⇒ Object

handle <br/>



55
56
57
58
# File 'lib/rdocsupport.rb', line 55

def handle_special_BR(special)
  return "&lt;br/&gt" if special.text[0,1] == '\\'
  special.text
end

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.

img:

insert a <img> tag

link:

used to insert arbitrary <a> references

anchor:

used to create an anchor



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rdocsupport.rb', line 69

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

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

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

[[url label that may contain spaces]]


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rdocsupport.rb', line 104

def handle_special_TIDYLINK(special)
  text = special.text.strip
  return text[1..-1] if text[0,1] == '\\'
  unless text =~ /\[\[(\S+?)\s+(.+?)\]\]/
    return text
  end
  url   = $1
  label = $2
  label = RDocFormatter.new(label).to_html
  label = label.split.select{|x| x =~ /\S/}.
    map{|x| x.chomp}.join(' ')

  case url
  when /link:(\S+)/
    return %{<a href="#{$1}">#{label}</a>}
  when /img:(\S+)/
    return %{<img src="http://#{$1}" alt="#{label}" />}
  when /rubytalk:(\S+)/
    return %{<a href="http://ruby-talk.org/blade/#{$1}">#{label}</a>}
  when /rubygarden:(\S+)/
    return %{<a href="http://www.rubygarden.org/ruby?#{$1}">#{label}</a>}
  when /c2:(\S+)/
    return %{<a href="http://c2.com/cgi/wiki?#{$1}">#{label}</a>}
  when /isbn:(\S+)/
    return %{<a href="http://search.barnesandnoble.com/bookSearch/} +
      %{isbnInquiry.asp?isbn=#{$1}">#{label}</a>}
  end

  unless url =~ /\w+?:/
    url = "http://#{url}"
  end

  "<a href=\"#{url}\">#{label}</a>"
end