Class: ExtendedHtml

Inherits:
SM::ToHtml
  • Object
show all
Defined in:
lib/wee-pm/slideshtml.rb

Direct Known Subclasses

SlidesHtml

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#block_processorObject

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.



12
13
14
# File 'lib/wee-pm/slideshtml.rb', line 12

def block_processor
  @block_processor
end

Instance Method Details

#accept_verbatim(am, fragment) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/wee-pm/slideshtml.rb', line 95

def accept_verbatim(am, fragment)
  lines = fragment.txt.split("\n")

  # remove leading whitespace
  margin = if lines.first =~ /^(\s+)/ then $1.size else 0 end
  if lines.all? {|l| l[0, margin].strip.empty? }
    lines.map!{|l| l[margin..-1]}
  end

  first_line = lines.first.strip

  case first_line
  when  /^\!\!([^:]*)(:(.*))?$/
    processor, option = $1, $3
    @res << @block_processor.call(processor, option, lines[1..-1].join("\n"))
  else
    @res << annotate("<pre>")
    output = CGI.escapeHTML(lines.join("\n"))

    # strip whitespaces on the right
    if pos = (output =~ /\s*\z/)
      output = output[0,pos] 
    end

    @res << output
    @res << annotate("</pre>") << "\n"
  end
end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wee-pm/slideshtml.rb', line 14

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

  img_attrs = {}
  if type == "http" &&  url =~ /\.(gif|png|jpg|jpeg|bmp)((;.*)?)$/   #((;\w+[=][^;]*)*)
    if $2.nil? or $2.empty?
      attr_size = 0
    else
      attr_size = $~.offset(2)[1] - $~.offset(2)[0]
      m = $2.to_s[1..-1]
      m.split(";").each {|a| 
        k, v = a.split("=", 2)
        img_attrs[k] = v
      }
    end

    s = img_attrs.keys.grep(/^\d+x\d+$/)
    width, height = s.first.split("x", 2) if s.size == 1
    width = img_attrs['w'] || img_attrs['width'] || width
    height = img_attrs['h'] || img_attrs['height'] || height

    attrs = ""
    attrs << 'width="' + width + '" ' if width 
    attrs << 'height="' + height + '" ' if height 
    attrs << 'style="float: left;padding-right: 40px;" ' if img_attrs['floating'] == 'left'

    src = 
    if path[0,2] == "./"
      path[2..(-1-attr_size)]
    elsif path[0,2] == "//"
      "http:" + path[0..(-1-attr_size)]
    else
      "http://" + path[0..(-1-attr_size)]
    end
    %{<a href="#{src}"><img src="#{src}" #{ attrs } border="0"></a>}
  else
    "<a href=\"#{url}\">#{url.sub(%r{^\w+:/*}, '')}</a>"
  end
end

#handle_special_RUBYTALK(special) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/wee-pm/slideshtml.rb', line 63

def handle_special_RUBYTALK(special)
  if special.text =~ /^ruby-talk:(\d+)$/
    num = $1.to_i
    %(<a href="http://rubytalk.com/cgi-bin/scat.rb/ruby/ruby-talk/#{ num }">ruby-talk:#{ num }</a>)
  else
    raise
  end
end

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

<label>[url]


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wee-pm/slideshtml.rb', line 76

def handle_special_TIDYLINK(special)
  text = special.text
  unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/ 
    return text
  end
  label = $1
  url   = $2
  
  if url !~ /\w+?:/ 
    if url =~ /\./
      url = "http://#{url}"
      #else
      #return find_wiki_word(url, label)
    end
  end
  
  "<a href=\"#{url}\">#{label}</a>"
end