module EZML::Helpers

Constants

HTML_ESCAPE
HTML_ESCAPE_ONCE_REGEX
HTML_ESCAPE_REGEX

Public Class Methods

action_view?() click to toggle source
# File lib/ezml/helpers.rb, line 36
def self.action_view?
  @@action_view_defined
end

Public Instance Methods

block_is_ezml?(block) click to toggle source
# File lib/ezml/helpers.rb, line 258
def block_is_ezml?(block)
  eval('!!defined?(_ezmlout)', block.binding)
end
capture_ezml(*args, &block) click to toggle source
# 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) click to toggle source
# 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 = "") click to toggle source
# File lib/ezml/helpers.rb, line 146
def ezml_concat(text = "")
  ezml_internal_concat text
  ErrorReturn.new("ezml_concat")
end
ezml_indent() click to toggle source
# File lib/ezml/helpers.rb, line 162
def ezml_indent
  '  ' * ezml_buffer.tabulation
end
ezml_internal_concat_raw(text = "", newline = true, indent = true)
ezml_tag(name, *rest, &block) click to toggle source
# 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.options[:attr_wrapper],
    ezml_buffer.options[:escape_attrs],
    ezml_buffer.options[:hyphenate_data_attrs],
    attrs)

  if text.nil? && block.nil? && (ezml_buffer.options[:autoclose].include?(name) || flags.include?(:/))
    ezml_internal_concat_raw "<#{name}#{attributes}#{' /' if ezml_buffer.options[:format] == :xhtml}>"
    return ret
  end

  if flags.include?(:/)
    raise Error.new(Error.message(:self_closing_content)) if text
    raise Error.new(Error.message(: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.message(: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) { || ... } click to toggle source
# 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) click to toggle source
# File lib/ezml/helpers.rb, line 53
def find_and_preserve(input = nil, tags = ezml_buffer.options[:preserve], &block)
  return find_and_preserve(capture_ezml(&block), input || tags) if block
  tags = tags.map { |tag| Regexp.escape(tag) }.join('|')
  re = /<(#{tags})([^>]*)>(.*?)(<\/\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
flatten(input = nil, &block)
Alias for: preserve
html_attrs(lang = 'en-US') click to toggle source
# File lib/ezml/helpers.rb, line 88
def html_attrs(lang = 'en-US')
  if ezml_buffer.options[:format] == :xhtml
    {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
  else
    {:lang => lang}
  end
end
html_escape(text) click to toggle source
# File lib/ezml/helpers.rb, line 243
def html_escape(text)
  ERB::Util.html_escape(text)
end
init_ezml_helpers() click to toggle source
# 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?() click to toggle source
# File lib/ezml/helpers.rb, line 254
def is_ezml?
  !@ezml_buffer.nil? && @ezml_buffer.active?
end
list_of(enum, opts={}, &block) click to toggle source
# 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() { || ... } click to toggle source
# 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) click to toggle source
# File lib/ezml/helpers.rb, line 118
def precede(str, &block)
  "#{str}#{capture_ezml(&block).chomp}\n"
end
preserve(input = nil, &block) click to toggle source
# 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/, '&#x000A;')
  s.delete!("\r")
  s
end
Also aliased as: flatten
succeed(str, &block) click to toggle source
# File lib/ezml/helpers.rb, line 122
def succeed(str, &block)
  "#{capture_ezml(&block).chomp}#{str}\n"
end
surround(front, back = front, &block) click to toggle source
# 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) click to toggle source
# File lib/ezml/helpers.rb, line 100
def tab_down(i = 1)
  ezml_buffer.tabulation -= i
end
tab_up(i = 1) click to toggle source
# File lib/ezml/helpers.rb, line 96
def tab_up(i = 1)
  ezml_buffer.tabulation += i
end
with_tabs(i) { || ... } click to toggle source
# 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

Private Instance Methods

ezml_bind_proc(&proc) click to toggle source
# File lib/ezml/helpers.rb, line 286
def ezml_bind_proc(&proc)
  _ezmlout = ezml_buffer
  #double assignment is to avoid warnings
  _erbout = _erbout = _ezmlout.buffer
  proc { |*args| proc.call(*args) }
end
ezml_buffer() click to toggle source
# File lib/ezml/helpers.rb, line 282
def ezml_buffer
  @ezml_buffer if defined? @ezml_buffer
end
ezml_internal_concat(text = "", newline = true, indent = true) click to toggle source
# File lib/ezml/helpers.rb, line 151
def ezml_internal_concat(text = "", newline = true, indent = true)
  if ezml_buffer.tabulation == 0
    ezml_buffer.buffer << "#{text}#{"\n" if newline}"
  else
    ezml_buffer.buffer << %[#{ezml_indent if indent}#{text.to_s.gsub("\n", "\n#{ezml_indent}")}#{"\n" if newline}]
  end
end
Also aliased as: ezml_internal_concat_raw
merge_name_and_attributes(name, attributes_hash = {}) click to toggle source
# File lib/ezml/helpers.rb, line 264
def merge_name_and_attributes(name, attributes_hash = {})
  return name, attributes_hash unless name =~ /^(.+?)?([\.#].*)$/

  return $1 || "div", AttributeBuilder.merge_attributes!(
    EZML::Parser.parse_class_and_id($2), attributes_hash)
end
with_ezml_buffer(buffer) { || ... } click to toggle source
# File lib/ezml/helpers.rb, line 271
def with_ezml_buffer(buffer)
  @ezml_buffer, old_buffer = buffer, @ezml_buffer
  old_buffer.active, old_was_active = false, old_buffer.active? if old_buffer
  @ezml_buffer.active, was_active = true, @ezml_buffer.active?
  yield
ensure
  @ezml_buffer.active = was_active
  old_buffer.active = old_was_active if old_buffer
  @ezml_buffer = old_buffer
end