Class: HtmlPress::Entities

Inherits:
Object
  • Object
show all
Defined in:
lib/html_press/html_entities.rb

Instance Method Summary collapse

Constructor Details

#initializeEntities

Returns a new instance of Entities.



5
6
7
8
# File 'lib/html_press/html_entities.rb', line 5

def initialize
  @replacement_hash = 'MINIFYENTITY' + Time.now.to_i.to_s
  @placeholders = []
end

Instance Method Details

#minify(text) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/html_press/html_entities.rb', line 15

def minify text
  out = text.dup

  out.gsub! /<|<|>|>|&|&/ do |m|
    reserve m
  end

  out = HTMLEntities.new.decode(out)

  re = Regexp.new('%' + @replacement_hash + '%(\d+)%')
  out.gsub! re do |m|
    m.gsub!(re, "\\1")
    @placeholders[m.to_i]
  end

  out
end

#reserve(content) ⇒ Object



10
11
12
13
# File 'lib/html_press/html_entities.rb', line 10

def reserve(content)
  @placeholders.push content
  '%' + @replacement_hash + '%' + (@placeholders.size - 1).to_s + '%'
end