Module: Hiccdown

Defined in:
lib/hiccdown.rb,
lib/hiccdown/railtie.rb

Defined Under Namespace

Modules: CustomViewRendering, ViewHelpers Classes: Railtie, Renderable

Class Method Summary collapse

Class Method Details

.hash_to_html_attributes(hash, escape) ⇒ Object



58
59
60
# File 'lib/hiccdown.rb', line 58

def self.hash_to_html_attributes(hash, escape)
  process_hash(hash, nil, escape).join(' ')
end

.maybe_escape(escapable, escape) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/hiccdown.rb', line 91

def self.maybe_escape escapable, escape
  if escape && !escapable.html_safe?
    CGI::escapeHTML(escapable)
  else
    escapable
  end
end

.process_hash(hash, prefix = nil, escape) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hiccdown.rb', line 42

def self.process_hash(hash, prefix = nil, escape)
  hash.flat_map do |key, value|
    attribute_key = prefix ? "#{prefix}-#{key}" : key.to_s

    if value.is_a?(Hash)
      process_hash(value, attribute_key, escape)
    elsif value.is_a?(Array)
      value_str = value.map { |v| maybe_escape(v.to_s, escape) }.join(' ')
      ["#{attribute_key}=\"#{value_str}\""]
    else
      value_str = maybe_escape(value.to_s, escape)
      ["#{attribute_key}=\"#{value_str}\""]
    end
  end
end

.scope(*args, &block) ⇒ Object



34
35
36
# File 'lib/hiccdown.rb', line 34

def self.scope *args, &block
  block.call(*args)
end

.standalone_tagsObject



38
39
40
# File 'lib/hiccdown.rb', line 38

def self.standalone_tags
  Set.new([:area, :base, :br, :col, :command, :embed, :hr, :img, :input, :keygen, :link, :menuitem, :meta, :param, :source, :track, :wbr])
end

.to_html(structure, escape = true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hiccdown.rb', line 62

def self.to_html structure, escape = true
  if structure.is_a? Hash
    self.hash_to_html_attributes(structure, escape)
  elsif structure.is_a? Array
    if structure.empty?
      return nil
    end

    if structure.first.is_a?(Array)
      return structure.map { |s| to_html(s, escape) }.join
    end

    if structure[1].is_a? Hash
      tag, attrs, *children = structure.map { |s| to_html(s, escape) }
      tag_and_attrs = structure[1].any? ? [tag, ' ', attrs].join : tag
    else
      tag, *children = structure.map { |s| to_html(s, escape) }
    end

    if standalone_tags.include? tag.to_sym
      ['<', tag_and_attrs || tag, '/>'].join
    else
      ['<', tag_and_attrs || tag, '>', children.join, '</', tag, '>'].join
    end
  else
    self.maybe_escape(structure.to_s, escape)
  end
end