module Shaf::JsonHtml
Constants
- STRUCTURAL_PATTERN
Public Instance Methods
as_html(obj)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 11 def as_html(obj) "<pre><code>#{to_html(obj)}</code></pre>" end
json2html(json)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 7 def json2html(json) as_html JSON.parse(json) end
Private Instance Methods
html_array(a, indent, pre_indent)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 28 def html_array(a, indent, pre_indent) array_of_strings = a.map do |e| to_html(e, indent: indent + 1, pre_indent: indentation(indent + 1)) end <<~EOS.chomp #{pre_indent}#{span '['} #{array_of_strings.join(item_separator)} #{indentation(indent)}#{span ']'} EOS end
html_hash(h, indent, pre_indent)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 40 def html_hash(h, indent, pre_indent) <<~EOS.chomp #{pre_indent}#{span '{'} #{h.map { |k,v| sub_hash(k,v, indent + 1) }.join(item_separator)}#{item_separator} #{indentation(indent)}#{span '}'} EOS end
html_scalar(s, pre_indent)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 48 def html_scalar(s, pre_indent) format '%s%s', pre_indent, span(s) end
indentation(i)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 62 def indentation(i) " " * i end
item_separator()
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 104 def item_separator "#{span ','}\n" end
link(href)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 79 def link(href) format '<a href="%s">%s</a>', href, quoted(href) end
quoted(obj)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 66 def quoted(obj) case obj when STRUCTURAL_PATTERN obj when String, Symbol format '"%s"', obj when NilClass 'null' else obj end end
span(value)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 83 def span(value) clazz = span_class(value) value = quoted(value) format '<span class="%s">%s</span>', clazz, value end
span_class(obj)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 89 def span_class(obj) case obj when TrueClass, FalseClass 'boolean' when NilClass 'null' when Numeric 'number' when STRUCTURAL_PATTERN 'structural' else 'string' end end
sub_hash(key, value, indent)
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 52 def sub_hash(key, value, indent) left_side = format '%s%s%s ', indentation(indent), quoted(key), span(':') left_side + if key.to_s == 'href' link(value) else to_html(value, indent: indent) end end
to_html(obj, indent: 0, pre_indent: "")
click to toggle source
# File lib/shaf/helpers/json_html.rb, line 17 def to_html(obj, indent: 0, pre_indent: "") case obj when Array html_array(obj, indent, pre_indent) when Hash html_hash(obj, indent, pre_indent) else html_scalar(obj, pre_indent) end end