class EZML::Buffer

Attributes

active[W]
buffer[RW]
capture_position[RW]
options[RW]
upper[RW]

Public Class Methods

new(upper = nil, options = {}) click to toggle source
# File lib/ezml/buffer.rb, line 46
def initialize(upper = nil, options = {})
  @active     = true
  @upper      = upper
  @options    = Options.buffer_defaults
  @options    = @options.merge(options) unless options.empty?
  @buffer     = new_encoded_string
  @tabulation = 0

  # The number of tabs that Engine thinks we should have
  # @real_tabs + @tabulation is the number of tabs actually output
  @real_tabs = 0
end

Public Instance Methods

active?() click to toggle source
# File lib/ezml/buffer.rb, line 33
def active?
  @active
end
adjust_tabs(tab_change) click to toggle source
# File lib/ezml/buffer.rb, line 72
def adjust_tabs(tab_change)
  @real_tabs += tab_change
end
attributes(class_id, obj_ref, *attributes_hashes) click to toggle source
# File lib/ezml/buffer.rb, line 76
def attributes(class_id, obj_ref, *attributes_hashes)
  attributes = class_id
  attributes_hashes.each do |old|
    result = {}
    old.each { |k, v| result[k.to_s] = v }
    AttributeBuilder.merge_attributes!(attributes, result)
  end
  AttributeBuilder.merge_attributes!(attributes, parse_object_ref(obj_ref)) if obj_ref
  AttributeBuilder.build_attributes(
    html?, @options[:attr_wrapper], @options[:escape_attrs], @options[:hyphenate_data_attrs], attributes)
end
fix_textareas!(input) click to toggle source
# File lib/ezml/buffer.rb, line 97
def fix_textareas!(input)
  return input unless input.include?('<textarea'.freeze)

  pattern = /<(textarea)([^>]*)>(\n|&#x000A;)(.*?)<\/textarea>/im
  input.gsub!(pattern) do |s|
    match = pattern.match(s)
    content = match[4]
    if match[3] == '&#x000A;'
      content.sub!(/\A /, '&#x0020;')
    else
      content.sub!(/\A[ ]*/, '')
    end
    "<#{match[1]}#{match[2]}>\n#{content}</#{match[1]}>"
  end
  input
end
html4?() click to toggle source
# File lib/ezml/buffer.rb, line 21
def html4?
  @options[:format] == :html4
end
html5?() click to toggle source
# File lib/ezml/buffer.rb, line 25
def html5?
  @options[:format] == :html5
end
html?() click to toggle source
# File lib/ezml/buffer.rb, line 17
def html?
  html4? or html5?
end
push_text(text, tab_change, dont_tab_up) click to toggle source
# File lib/ezml/buffer.rb, line 59
def push_text(text, tab_change, dont_tab_up)
  if @tabulation > 0
    # Have to push every line in by the extra user set tabulation.
    # Don't push lines with just whitespace, though,
    # because that screws up precompiled indentation.
    text.gsub!(/^(?!\s+$)/m, tabs)
    text.sub!(tabs, '') if dont_tab_up
  end

  @real_tabs += tab_change
  @buffer << text
end
rstrip!() click to toggle source
# File lib/ezml/buffer.rb, line 88
def rstrip!
  if capture_position.nil?
    buffer.rstrip!
    return
  end

  buffer << buffer.slice!(capture_position..-1).rstrip
end
tabulation() click to toggle source
# File lib/ezml/buffer.rb, line 37
def tabulation
  @real_tabs + @tabulation
end
tabulation=(val) click to toggle source
# File lib/ezml/buffer.rb, line 41
def tabulation=(val)
  val = val - @real_tabs
  @tabulation = val > -1 ? val : 0
end
toplevel?() click to toggle source
# File lib/ezml/buffer.rb, line 29
def toplevel?
  upper.nil?
end
xhtml?() click to toggle source
# File lib/ezml/buffer.rb, line 13
def xhtml?
  not html?
end

Private Instance Methods

new_encoded_string() click to toggle source
# File lib/ezml/buffer.rb, line 116
def new_encoded_string
  "".encode(options[:encoding])
end
parse_object_ref(ref) click to toggle source
# File lib/ezml/buffer.rb, line 127
def parse_object_ref(ref)
  prefix = ref[1]
  ref = ref[0]
  # Let's make sure the value isn't nil. If it is, return the default Hash.
  return {} if ref.nil?
  class_name =
    if ref.respond_to?(:ezml_object_ref)
      ref.ezml_object_ref
    else
      underscore(ref.class)
    end
  ref_id =
    if ref.respond_to?(:to_key)
      key = ref.to_key
      key.join('_') unless key.nil?
    else
      ref.id
    end
  id = "#{class_name}_#{ref_id || 'new'}"
  if prefix
    class_name = "#{ prefix }_#{ class_name}"
    id = "#{ prefix }_#{ id }"
  end

  { 'id'.freeze => id, 'class'.freeze => class_name }
end
tabs(count = 0) click to toggle source

Gets `count` tabs. Mostly for internal use.

# File lib/ezml/buffer.rb, line 122
def tabs(count = 0)
  tabs = [count + @tabulation, 0].max
  @@tab_cache[tabs] ||= '  ' * tabs
end
underscore(camel_cased_word) click to toggle source
# File lib/ezml/buffer.rb, line 154
def underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '_')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end