Class: HtmlCompressor::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/core_ext/htmlcompressor.rb

Instance Method Summary collapse

Instance Method Details

#profileObject



3
4
5
# File 'lib/alula/core_ext/htmlcompressor.rb', line 3

def profile
  @profile
end

#profile=(profile) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/alula/core_ext/htmlcompressor.rb', line 7

def profile=(profile)
  @profile = profile
  case profile
  when :none
    @options[:enabled] = false
  when :normal
    @options[:enabled] = true
    @options[:remove_surrounding_spaces] = HtmlCompressor::Compressor::BLOCK_TAGS_MAX + ",source,title,meta,header,footer,div,section,article,time,img,video,script"
    @options[:remove_intertag_spaces] = true
    @options[:remove_quotes] = false
    @options[:remove_script_attributes] = true
    @options[:remove_style_attributes] = true
    @options[:remove_link_attributes] = true
    @options[:simple_boolean_attributes] = true
    @options[:remove_http_protocol] = false
    @options[:remove_https_protocol] = false
  when :high
    @options[:enabled] = true
    @options[:remove_surrounding_spaces] = HtmlCompressor::Compressor::BLOCK_TAGS_MAX + ",source,title,meta,header,footer,div,section,article,time,img,video,script"
    @options[:remove_intertag_spaces] = true
    @options[:remove_quotes] = true
    @options[:remove_script_attributes] = true
    @options[:remove_style_attributes] = true
    @options[:remove_link_attributes] = true
    @options[:simple_boolean_attributes] = true
    @options[:remove_http_protocol] = "href,src,cite,action,data-original,data-hires"
    @options[:remove_https_protocol] = "href,src,cite,action,data-original,data-hires"
  end
end

#remove_http_protocol(html) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/alula/core_ext/htmlcompressor.rb', line 37

def remove_http_protocol(html)
  # remove http protocol from tag attributes
  if @options[:remove_http_protocol]
    pattern = case @options[:remove_http_protocol]
      when true
        HTTP_PROTOCOL_PATTERN
      else
        Regexp.new("(<[^>]+?(?:#{@options[:remove_http_protocol].gsub(",", "|")})\\s*=\\s*['\"])http:(//[^>]+?>)", Regexp::MULTILINE | Regexp::IGNORECASE)
      end
    html = html.gsub(pattern) do |match|
      group_1 = $1
      group_2 = $2

      if match =~ REL_EXTERNAL_PATTERN
        match
      else
        "#{group_1}#{group_2}"
      end
    end
  end

  html
end

#remove_https_protocol(html) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/alula/core_ext/htmlcompressor.rb', line 61

def remove_https_protocol(html)
  # remove https protocol from tag attributes
  if @options[:remove_https_protocol]
    pattern = case @options[:remove_https_protocol]
      when true
        HTTPS_PROTOCOL_PATTERN
      else
        Regexp.new("(<[^>]+?(?:#{@options[:remove_https_protocol].gsub(",", "|")})\\s*=\\s*['\"])http:(//[^>]+?>)", Regexp::MULTILINE | Regexp::IGNORECASE)
      end

    html = html.gsub(pattern) do |match|
      group_1 = $1
      group_2 = $2

      if match =~ REL_EXTERNAL_PATTERN
        match
      else
        "#{group_1}#{group_2}"
      end
    end
  end

  html
end