lib/yaks/format/html.rb in yaks-html-0.8.1 vs lib/yaks/format/html.rb in yaks-html-0.8.2
- old
+ new
@@ -1,26 +1,29 @@
# -*- coding: utf-8 -*-
module Yaks
class Format
class HTML < self
- include Adamantium, Util
+ include Util
register :html, :html, 'text/html'
def template
- Hexp.parse(File.read(File.expand_path('../template.html', __FILE__)))
+ @template ||= Hexp.parse(File.read(File.expand_path('../template.html', __FILE__)))
end
- memoize :template
def section(name)
template.select(".#{name}").first
end
def serialize_resource(resource)
- template.replace('body') do |body|
- body.content(render_resource(resource))
+ template.replace('.resource') do |_|
+ render_resource(resource)
+ end.replace('.yaks-version') do |ver|
+ ver.content(Yaks::VERSION)
+ end.replace('.request-info') do |req|
+ req.content(env['REQUEST_METHOD'], ' ', env['PATH_INFO'])
end
end
def render_resource(resource, templ = section('resource'))
templ
@@ -39,15 +42,23 @@
.replace('.value') {|x| x.content(value.inspect) }
end
end
end
+ def rel_href(rel)
+ if rel.is_a?(Symbol)
+ "http://www.iana.org/assignments/link-relations/link-relations.xhtml"
+ else
+ rel.to_s
+ end
+ end
+
def render_links(links)
->(templ) do
links.map do |link|
templ
- .replace('.rel a') {|a| a.attr('href', link.rel.to_s).content(link.rel.to_s) }
+ .replace('.rel a') {|a| a.attr('href', rel_href(link.rel)).content(link.rel.to_s) }
.replace('.uri a') {|a| a.attr('href', link.uri).content(link.uri) }
.replace('.title') {|x| x.content(link.title.to_s) }
.replace('.templated') {|x| x.content(link.templated?.inspect) }
end
end
@@ -59,13 +70,13 @@
resource.seq.map do |r|
render_resource(r, templ)
end
else
resource.subresources.map do |resources|
- rel = resources.rels.first.to_s
+ rel = resources.rels.first
sub_templ
- .replace('.rel a') {|a| a.attr('href', rel).content(rel) }
+ .replace('.rel a') {|a| a.attr('href', rel_href(rel)).content(rel.to_s) }
.replace('.value') {|x| x.content(resources.seq.map { |resource| render_resource(resource, templ) })}
end
end
end
@@ -85,26 +96,30 @@
form = form.attr('action', form_control.action) if form_control.action
form = form.attr('enctype', form_control.media_type) if form_control.media_type
rows = form_control.fields.map(&method(:render_field))
- form.content(H[:table, form_control.title || '', *rows, H[:tr, H[:td, H[:input, {type: 'submit'}]]]])
+ form.content(H[:table, H[:h4, form_control.title || form_control.name.to_s], *rows, H[:tr, H[:td], H[:td, H[:input, {type: 'submit'}]]]])
end
def render_field(field)
return render_fieldset(field) if field.type == :fieldset
extra_info = reject_keys(field.to_h_compact, :type, :name, :value, :label, :options)
H[:tr,
H[:td,
- H[:label, {for: field.name}, [field.label, field.required ? '*' : ''].join]],
+ H[:label, {for: field.name}, [field.label || field.name.to_s, field.required ? '*' : ''].join]],
H[:td,
case field.type
when /select/
H[:select, reject_keys(field.to_h_compact, :options), render_select_options(field.options)]
when /textarea/
H[:textarea, reject_keys(field.to_h_compact, :value), field.value || '']
when /legend/
H[:legend, field.to_h_compact]
+ when /hidden/
+ [ field.value.inspect,
+ H[:input, field.to_h_compact]
+ ]
else
H[:input, field.to_h_compact]
end],
H[:td, extra_info.empty? ? '' : extra_info.inspect]
]