Module: Eggshell::BlockHandler::HtmlUtils
- Included in:
- Eggshell::Bundles::Basic::BasicFormatHandlers, Eggshell::Bundles::Basic::ListBlocks, Eggshell::Bundles::Basic::SectionBlocks, Eggshell::Bundles::Basic::TableBlock, Eggshell::Bundles::Basic::TextBlocks
- Defined in:
- lib/eggshell/block-handler.rb
Overview
Useful methods for generating HTML tags
Constant Summary collapse
- HASH_HTML_ESCAPE =
{ "'" => ''', '"' => '"', '<' => '<', '>' => '>', '&' => '&' }.freeze
Instance Method Summary collapse
- #attrib_string(map, keys = nil) ⇒ Object
- #create_tag(tag, attribs, open = true, body = nil) ⇒ Object
- #css_string(map) ⇒ Object
- #html_escape(str) ⇒ Object
-
#inject_attribs(attribs, map = {}, escape = true) ⇒ Object
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.
Instance Method Details
#attrib_string(map, keys = nil) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# 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) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# 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) ⇒ Object
211 212 213 214 215 216 217 |
# 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) ⇒ Object
228 229 230 |
# 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) ⇒ Object
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.
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# 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 |