Class: Rackful::XHTML

Inherits:
Serializer show all
Defined in:
gems/rackful-0.1.1/lib/rackful_serializer.rb,
gems/rackful-0.1.1.orig/lib/rackful_serializer.rb

Overview

Since:

Direct Known Subclasses

EPIC::Handle::XHTML, EPIC::XHTML, HTTPStatus::XHTML

Constant Summary

CONTENT_TYPES =

The content types served by this serializer.

See Also:

  • Serializer::CONTENT_TYPES

Since:

  • 0.1.0

[
  'application/xhtml+xml; charset=UTF-8',
  'text/html; charset=UTF-8',
  'text/xml; charset=UTF-8',
  'application/xml; charset=UTF-8'
]

Instance Attribute Summary

Attributes inherited from Serializer

#content_type, #resource

Instance Method Summary (collapse)

Methods inherited from Serializer

#initialize

Constructor Details

This class inherits a constructor from Rackful::Serializer

Instance Method Details

- (void) each {|<<EOS <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <head> EOS| ... }

Yields:

  • (<<EOS <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <head> EOS)

Since:

  • 0.1.0



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 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 97

def each &block
  request = Request.current
  if /xml/ === self.content_type
    yield <<EOS
<?xml version="1.0" encoding="UTF-8"?>
EOS
  end
  yield <<EOS
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<head>
EOS
  unless request.path == request.content_path
    yield <<EOS
<base href="#{request.base_path}"/>
EOS
  end
  unless '/' == request.path
    yield <<EOS
<link rel="contents" href="#{File::dirname(request.path).to_path.slashify}"/>
EOS
  end
  yield header + '<div id="rackful_content">'
  each_nested &block
  yield '</div>' + footer
end

- (void) each_nested(p = self.resource.to_rackful, &block)

Serialize almost any kind of Ruby object to XHTML.

Since:

  • 0.1.0



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 135

def each_nested p = self.resource.to_rackful, &block
  # p = (args.size > 0) ? args[0] : self.resource.to_rackful
  if p.kind_of?( Path )
    yield "<a href=\"#{self.htmlify(p)}\">" +
      Rack::Utils.escape_html( File::basename(p.unslashify).to_path.unescape ) +
      '</a>'
  elsif p.kind_of?( Resource ) && ! p.equal?( self.resource )
    p.serializer( self.content_type ).each_nested &block
  # elsif p.kind_of?( Hash )
    # yield '<dl class="rackful_object">'
    # p.each_pair do
      # |key, value|
      # yield '<dt>' + key.to_s.split('_').join(' ').escape_html +
        # "</dt><dd class=\"rackful_object_#{key.to_s.escape_html}\"#{self.xsd_type(value)}>"
      # self.each_nested value, &block
      # yield "</dd>\n"
    # end
    # yield '</dl>'
  elsif p.kind_of?( Enumerable ) and p.respond_to?( :each_pair ) and
        p.all? { |r, s| r.kind_of?( Path ) }
    yield '<dl class="rackful-resources">'
    p.each_pair do
      |path, child|
      yield '<dt>'
      self.each_nested path, &block
      yield '</dt><dd>'
      self.each_nested child, &block
      yield "</dd>\n"
    end
    yield '</dl>'
  elsif p.respond_to?( :each_pair )
    yield '<dl class="rackful-object">'
    p.each_pair do
      |path, child|
      yield '<dt>'
      self.each_nested path, &block
      yield '</dt><dd>'
      self.each_nested child, &block
      yield "</dd>\n"
    end
    yield '</dl>'
  elsif p.kind_of?( Enumerable ) and ( q = p.first ) and (
          q.respond_to?(:keys) && ( keys = q.keys ) &&
          p.all? { |r| r.respond_to?(:keys) && r.keys == keys }
        )
    yield '<table class="rackful-objects"><thead><tr>' +
      keys.collect {
        |column|
        '<th>' +
        Rack::Utils.escape_html( column.to_s.split('_').join(' ') ) +
        "</th>\n"
      }.join + '</tr></thead><tbody>'
    p.each do
      |h|
      yield '<tr>'
      h.each_pair do
        |key, value|
        yield "<td class=\"rackful-objects-#{Rack::Utils.escape_html( key.to_s )}\"#{self.xsd_type(value)}>"
        self.each_nested value, &block
        yield "</td>\n"
      end
      yield '</tr>'
    end
    yield "</tbody></table>"
  elsif p.kind_of?( Enumerable )
    yield '<ul class="rackful-array">'
    p.each do
      |value|
      yield "<li#{self.xsd_type(value)}>"
      self.each_nested value, &block
      yield "</li>\n"
    end
    yield '</ul>'
  elsif p.kind_of?( Time )
    yield p.utc.xmlschema
  elsif p.kind_of?( String ) && p.encoding == Encoding::BINARY
    yield Base64.encode64(p).chomp
  else
    yield Rack::Utils.escape_html( p.to_s )
  end
end

Look at the source code!

Since:

  • 0.1.0



130
131
132
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 130

def footer
  '<div class="rackful_powered">Powered by <a href="http://github.com/pieterb/Rackful">Rackful</a></div></body></html>'
end

- (void) header

Look at the source code!

Since:

  • 0.1.0



125
126
127
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 125

def header
  "<title>#{ Rack::Utils.escape_html(resource.title) }</title></head><body>"
end

- (String) htmlify(path)

Turns a relative URI (starting with `/`) into a relative path (starting with `./`)

Parameters:

Returns:

Since:

  • 0.1.0



86
87
88
89
90
91
92
93
94
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 86

def htmlify path
  @rackful_bp ||= Request.current.base_path # caching
  length = @rackful_bp.length
  if @rackful_bp == path[0, length]
    './' + path[length .. -1]
  else
    path.dup
  end
end

- (void) xsd_type(v)

Since:

  • 0.1.0



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'gems/rackful-0.1.1/lib/rackful_serializer.rb', line 219

def xsd_type v
  if v.respond_to? :to_rackful
    v = v.to_rackful
  end
  if [nil, true, false].include? v
    ' xs:type="xs:boolean" xs:nil="true"'
  elsif v.kind_of? Integer
    ' xs:type="xs:integer"'
  elsif v.kind_of? Numeric
    ' xs:type="xs:decimal"'
  elsif v.kind_of? Time
    ' xs:type="xs:dateTime"'
  elsif v.kind_of?( String ) && v.encoding == Encoding::BINARY
    ' xs:type="xs:base64Binary"'
  elsif v.kind_of?( String ) && !v.kind_of?( Path )
    ' xs:type="xs:string"'
  else
    ''
  end
end