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

.maybe_escape(escapable, escape) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/hiccdown.rb', line 109

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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hiccdown.rb', line 60

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

    if value.is_a?(Hash)
      process_attributes(value, attribute_key, escape)
    elsif value.is_a?(Array)
      value_str = value
        .reject { |v| v.to_s == '' }
        .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



52
53
54
# File 'lib/hiccdown.rb', line 52

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

.standalone_tagsObject



56
57
58
# File 'lib/hiccdown.rb', line 56

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hiccdown.rb', line 80

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

    unless structure.first.is_a?(Symbol)
      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