Module: HtmlPretty

Defined in:
lib/html_pretty.rb,
lib/html_pretty/version.rb

Constant Summary collapse

SPECIAL =
/(<!DOCTYPE.*?>|<!--.*?-->|<script.*?\/script>|<style.*?\/style>)/m
VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.run(html, out = '') ⇒ Object

Really bad assumption heavy tidy… Self rolled because other versions have weird errors



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/html_pretty.rb', line 9

def self.run(html, out='')
  indent = 0
  html.split(SPECIAL).each do |blob|
    if blob.start_with?('<!--!!!-->')
      raise
    elsif blob =~ SPECIAL
      out << "  " * indent << blob << "\n"
    else
      # Remove all newlines within a tag definition
      # Example: '<a\nb=1\nc=2>' => '<a b=1 c=2>'
      blob = blob.split(/(<.*?>)/m).map do |blub|
               if blub.start_with?('<')
                 blub.gsub(/\n/, ' ')
               else
                 blub
               end
             end.join

      blob = blob.gsub(/^[ \t]*\n/, '')   # kill blank lines
      blob = blob.gsub(/^[ \t]+/, '')     # kill opening whitespace
      blob = blob.gsub(/[ \t]+$/, '')     # kill closing whitespace
      blob = blob.gsub(/[ \t]+/, ' ')     # collapse all whitespace
      blob = blob.gsub(/<[ \t]+/, '<')    # remove whitespace between brackets
      blob = blob.gsub(/[ \t]+>/, '>')    # remove whitespace between brackets
      blob.each_line do |line|
        open_tags = line.scan(/<[^\/][^>]*>/).select{|d| d !~ /\/>$/}.size
        close_tags = line.scan(/<\/[^>]*>/).size
        if open_tags > close_tags
          out << "  " * indent + line
          indent += open_tags - close_tags
          indent = 0 if indent < 0
        else
          indent += open_tags - close_tags
          indent = 0 if indent < 0
          out << "  " * indent + line
        end
      end
    end
  end

  out
end