Module: EZML::Helpers
Defined Under Namespace
Classes: ErrorReturn
Constant Summary collapse
- HTML_ESCAPE =
{ '&' => '&', '<' => '<', '>' => '>', '"' => '"', "'" => ''' }
- HTML_ESCAPE_REGEX =
/['"><&]/- HTML_ESCAPE_ONCE_REGEX =
/['"><]|&(?!(?:[a-zA-Z]+|#(?:\d+|[xX][0-9a-fA-F]+));)/- @@action_view_defined =
false
Class Method Summary collapse
Instance Method Summary collapse
- #block_is_ezml?(block) ⇒ Boolean
- #capture_ezml(*args, &block) ⇒ Object
- #escape_once(text) ⇒ Object
- #ezml_concat(text = "") ⇒ Object
- #ezml_indent ⇒ Object
- #ezml_tag(name, *rest, &block) ⇒ Object
- #ezml_tag_if(condition, *tag) ⇒ Object
- #find_and_preserve(input = nil, tags = ezml_buffer.options[:preserve], &block) ⇒ Object
- #html_attrs(lang = 'en-US') ⇒ Object
- #html_escape(text) ⇒ Object
- #init_ezml_helpers ⇒ Object
- #is_ezml? ⇒ Boolean
- #list_of(enum, opts = {}, &block) ⇒ Object
- #non_ezml ⇒ Object
- #precede(str, &block) ⇒ Object
- #preserve(input = nil, &block) ⇒ Object (also: #flatten)
- #succeed(str, &block) ⇒ Object
- #surround(front, back = front, &block) ⇒ Object
- #tab_down(i = 1) ⇒ Object
- #tab_up(i = 1) ⇒ Object
- #with_tabs(i) ⇒ Object
Class Method Details
.action_view? ⇒ Boolean
36 37 38 |
# File 'lib/ezml/helpers.rb', line 36 def self.action_view? @@action_view_defined end |
Instance Method Details
#block_is_ezml?(block) ⇒ Boolean
258 259 260 |
# File 'lib/ezml/helpers.rb', line 258 def block_is_ezml?(block) eval('!!defined?(_ezmlout)', block.binding) end |
#capture_ezml(*args, &block) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ezml/helpers.rb', line 126 def capture_ezml(*args, &block) buffer = eval('if defined? _ezmlout then _ezmlout else nil end', block.binding) || ezml_buffer with_ezml_buffer(buffer) do position = ezml_buffer.buffer.length ezml_buffer.capture_position = position value = block.call(*args) captured = ezml_buffer.buffer.slice!(position..-1) if captured == '' and value != ezml_buffer.buffer captured = (value.is_a?(String) ? value : nil) end captured end ensure ezml_buffer.capture_position = nil end |
#escape_once(text) ⇒ Object
249 250 251 252 |
# File 'lib/ezml/helpers.rb', line 249 def escape_once(text) text = text.to_s text.gsub(HTML_ESCAPE_ONCE_REGEX, HTML_ESCAPE) end |
#ezml_concat(text = "") ⇒ Object
146 147 148 149 |
# File 'lib/ezml/helpers.rb', line 146 def ezml_concat(text = "") ezml_internal_concat text ErrorReturn.new("ezml_concat") end |
#ezml_indent ⇒ Object
162 163 164 |
# File 'lib/ezml/helpers.rb', line 162 def ezml_indent ' ' * ezml_buffer.tabulation end |
#ezml_tag(name, *rest, &block) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/ezml/helpers.rb', line 166 def ezml_tag(name, *rest, &block) ret = ErrorReturn.new("ezml_tag") text = rest.shift.to_s unless [Symbol, Hash, NilClass].any? {|t| rest.first.is_a? t} flags = [] flags << rest.shift while rest.first.is_a? Symbol attrs = (rest.shift || {}) attrs.keys.each {|key| attrs[key.to_s] = attrs.delete(key)} unless attrs.empty? name, attrs = merge_name_and_attributes(name.to_s, attrs) attributes = EZML::AttributeBuilder.build_attributes(ezml_buffer.html?, ezml_buffer.[:attr_wrapper], ezml_buffer.[:escape_attrs], ezml_buffer.[:hyphenate_data_attrs], attrs) if text.nil? && block.nil? && (ezml_buffer.[:autoclose].include?(name) || flags.include?(:/)) ezml_internal_concat_raw "<#{name}#{attributes}#{' /' if ezml_buffer.[:format] == :xhtml}>" return ret end if flags.include?(:/) raise Error.new(Error.(:self_closing_content)) if text raise Error.new(Error.(:illegal_nesting_self_closing)) if block end tag = "<#{name}#{attributes}>" end_tag = "</#{name}>" if block.nil? text = text.to_s if text.include?("\n") ezml_internal_concat_raw tag tab_up ezml_internal_concat text tab_down ezml_internal_concat_raw end_tag else ezml_internal_concat_raw tag, false ezml_internal_concat text, false, false ezml_internal_concat_raw end_tag, true, false end return ret end if text raise Error.new(Error.(:illegal_nesting_line, name)) end if flags.include?(:<) ezml_internal_concat_raw tag, false ezml_internal_concat "#{capture_ezml(&block).strip}", false, false ezml_internal_concat_raw end_tag, true, false return ret end ezml_internal_concat_raw tag tab_up block.call tab_down ezml_internal_concat_raw end_tag ret end |
#ezml_tag_if(condition, *tag) ⇒ Object
230 231 232 233 234 235 236 237 |
# File 'lib/ezml/helpers.rb', line 230 def ezml_tag_if(condition, *tag) if condition ezml_tag(*tag){ yield } else yield end ErrorReturn.new("ezml_tag_if") end |
#find_and_preserve(input = nil, tags = ezml_buffer.options[:preserve], &block) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/ezml/helpers.rb', line 53 def find_and_preserve(input = nil, = ezml_buffer.[:preserve], &block) return find_and_preserve(capture_ezml(&block), input || ) if block = .map { |tag| Regexp.escape(tag) }.join('|') re = /<(#{})([^>]*)>(.*?)(<\/\1>)/im input.to_s.gsub(re) do |s| s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible "<#{$1}#{$2}>#{preserve($3)}</#{$1}>" end end |
#html_attrs(lang = 'en-US') ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/ezml/helpers.rb', line 88 def html_attrs(lang = 'en-US') if ezml_buffer.[:format] == :xhtml {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang} else {:lang => lang} end end |
#html_escape(text) ⇒ Object
243 244 245 |
# File 'lib/ezml/helpers.rb', line 243 def html_escape(text) ERB::Util.html_escape(text) end |
#init_ezml_helpers ⇒ Object
40 41 42 43 |
# File 'lib/ezml/helpers.rb', line 40 def init_ezml_helpers @ezml_buffer = EZML::Buffer.new(ezml_buffer, Options.new.for_buffer) nil end |
#is_ezml? ⇒ Boolean
254 255 256 |
# File 'lib/ezml/helpers.rb', line 254 def is_ezml? !@ezml_buffer.nil? && @ezml_buffer.active? end |
#list_of(enum, opts = {}, &block) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ezml/helpers.rb', line 72 def list_of(enum, opts={}, &block) opts_attributes = opts.map { |k, v| " #{k}='#{v}'" }.join enum.map do |i| result = capture_ezml(i, &block) if result.count("\n") > 1 result.gsub!("\n", "\n ") result = "\n #{result.strip}\n" else result.strip! end %Q!<li#{opts_attributes}>#{result}</li>! end.join("\n") end |
#non_ezml ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/ezml/helpers.rb', line 45 def non_ezml was_active = @ezml_buffer.active? @ezml_buffer.active = false yield ensure @ezml_buffer.active = was_active end |
#precede(str, &block) ⇒ Object
118 119 120 |
# File 'lib/ezml/helpers.rb', line 118 def precede(str, &block) "#{str}#{capture_ezml(&block).chomp}\n" end |
#preserve(input = nil, &block) ⇒ Object Also known as: flatten
63 64 65 66 67 68 69 |
# File 'lib/ezml/helpers.rb', line 63 def preserve(input = nil, &block) return preserve(capture_ezml(&block)) if block s = input.to_s.chomp("\n") s.gsub!(/\n/, '
') s.delete!("\r") s end |
#succeed(str, &block) ⇒ Object
122 123 124 |
# File 'lib/ezml/helpers.rb', line 122 def succeed(str, &block) "#{capture_ezml(&block).chomp}#{str}\n" end |
#surround(front, back = front, &block) ⇒ Object
112 113 114 115 116 |
# File 'lib/ezml/helpers.rb', line 112 def surround(front, back = front, &block) output = capture_ezml(&block) "#{front}#{output.chomp}#{back}\n" end |
#tab_down(i = 1) ⇒ Object
100 101 102 |
# File 'lib/ezml/helpers.rb', line 100 def tab_down(i = 1) ezml_buffer.tabulation -= i end |
#tab_up(i = 1) ⇒ Object
96 97 98 |
# File 'lib/ezml/helpers.rb', line 96 def tab_up(i = 1) ezml_buffer.tabulation += i end |
#with_tabs(i) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/ezml/helpers.rb', line 104 def with_tabs(i) old_tabs = ezml_buffer.tabulation ezml_buffer.tabulation = i yield ensure ezml_buffer.tabulation = old_tabs end |