module Eggshell::BlockHandler::HtmlUtils

Useful methods for generating HTML tags

Constants

HASH_HTML_ESCAPE

Public Instance Methods

attrib_string(map, keys = nil) click to toggle source
# File lib/eggshell/block-handler.rb, line 177
def attrib_string(map, keys = nil)
        keys = map.keys if !keys
        buff = []
        keys.each do |key|
                val = map[key]
                if val.is_a?(Hash)
                        buff << attrib_string(val)
                elsif val && key[0] != '@'
                        buff << " #{key}='#{html_escape(val)}'"
                end
        end
        buff.join()
end
create_tag(tag, attribs, open = true, body = nil) click to toggle source
# File lib/eggshell/block-handler.rb, line 154
def create_tag(tag, attribs, open = true, body = nil)
        str_attribs = ''
        if attribs.is_a?(String)
                str_attribs = attribs
        else
                nattribs = attribs.is_a?(Hash) ? attribs.clone : {}
                if nattribs[BlockParams::STYLE].is_a?(Hash)
                        nattribs[BlockParams::STYLE] = css_string(nattribs[BlockParams::STYLE])
                end
                str_attribs = attrib_string(nattribs, BlockParams::KEYS)
        end

        if !open
                return "<#{tag}#{str_attribs}/>"
        else
                if body
                        return "<#{tag}#{str_attribs}>#{body}</#{tag}>"
                else
                        return "<#{tag}#{str_attribs}>"
                end
        end
end
css_string(map) click to toggle source
# File lib/eggshell/block-handler.rb, line 211
def css_string(map)
        css = []
        map.each do |s,v|
                css << "#{s}: #{html_escape(v)};"
        end
        css.join(' ')
end
html_escape(str) click to toggle source

@todo more chars

# File lib/eggshell/block-handler.rb, line 228
def html_escape(str)
        return str.gsub(/("|'|<|>|&)/, HASH_HTML_ESCAPE)
end
inject_attribs(attribs, map = {}, escape = true) click to toggle source

Given an attribute string in the form `key1=“value” key2=“value”` and a map containing new attribute keys, either append to attribute string or inject values into an existing attribute. @param String attribs @param Hash map @param Boolean escape If true (default), calls {@see html_escape()} on each value.

# File lib/eggshell/block-handler.rb, line 197
def inject_attribs(attribs, map = {}, escape = true)
        map.each do |key, val|
                key = key.to_s if key.is_a?(Symbol)
                olen = attribs.length
                match = attribs.match(/#{key}=(['"])([^'"]*)(['"])/)
                if !match
                        attribs += " #{key}='#{escape ? html_escape(val) : val}'"
                else
                        attribs = attribs.gsub(match[0], "#{key}='#{match[2]} #{escape ? html_escape(val) : val}'")
                end
        end
        attribs
end