htmlbeautifier 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef0b7742f4aeee06c65142a6c5cfdd735b462f94
4
- data.tar.gz: 59a6d1b39bdc3ccf32ce4f10aa673e448802ec79
3
+ metadata.gz: 6f0dba839e3aa0da9272b5789530d578eb210cb4
4
+ data.tar.gz: 156a6912f4c8980d3ca06c5e43865c535af0238e
5
5
  SHA512:
6
- metadata.gz: 2133a2a77b572ca42386e4a3c3bc7d08c292962d4fe4869de237ca0a120b92fc6e2614bf927fde5ea3533864c68af756f60dcb5d3cd76067677e8593b8ad1702
7
- data.tar.gz: cb1d72451e92bf6bd2d0a7bb74e34956c35d5b83e9f9d2e4f39e1d1d9bb75ee483f97956d2d8b2c0c39c8f41e776e1d11fd3f22f6d4c5bad6f6fc090d269c28d
6
+ metadata.gz: 2a322f76309db244d0eb0d5b4860b3a6681efd0f8c6219b039baeb1e756c78df756995d6975574a4db7b9a559ff92fa6f87c5dcc6fef59c1dfbdcc5cbc4a1321
7
+ data.tar.gz: 8fe4f9811c1789f532a27a73862e0ad6b9495a96172e14ddc87cab6dcb47de7c722bd2a0252e49ae2ef540233e4c0281cf052340927831cd0c1480289af74be2
data/bin/htmlbeautifier CHANGED
@@ -1,23 +1,39 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'htmlbeautifier'
3
+ require 'optparse'
3
4
  require 'fileutils'
4
5
 
5
- def beautify(name, input, output)
6
- HtmlBeautifier::Beautifier.new(output).scan(input)
6
+ def beautify(name, input, output, options)
7
+ beautifier = HtmlBeautifier::Beautifier.new(output)
8
+ beautifier.tab_stops = options[:tab_stops]
9
+ beautifier.scan(input)
7
10
  output << "\n"
8
11
  rescue => e
9
12
  raise "Error parsing #{name}: #{e}"
10
13
  end
11
14
 
15
+ options = {:tab_stops => 2}
16
+ parser = OptionParser.new do |opts|
17
+ opts.banner = "Usage: #{__FILE__} [options] [file ...]"
18
+ opts.on("-t", "--tab-stops NUMBER", Integer, "Set number of tab stops") do |num|
19
+ options[:tab_stops] = num
20
+ end
21
+ opts.on("-h", "--help", "Display this help message and exit") do
22
+ puts opts
23
+ exit
24
+ end
25
+ end
26
+ parser.parse!
27
+
12
28
  if ARGV.any?
13
29
  ARGV.each do |path|
14
30
  input = File.read(path)
15
31
  temppath = path + ".tmp"
16
32
  File.open(temppath, "w") do |output|
17
- beautify path, input, output
33
+ beautify path, input, output, options
18
34
  end
19
35
  FileUtils.mv temppath, path
20
36
  end
21
37
  else
22
- beautify "standard input", $stdin.read, $stdout
38
+ beautify "standard input", $stdin.read, $stdout, options
23
39
  end
@@ -14,7 +14,7 @@ module HtmlBeautifier
14
14
 
15
15
  def initialize(output, tab_stops)
16
16
  @level = 0
17
- @new_line = true
17
+ @new_line = false
18
18
  @tab = ' ' * tab_stops
19
19
  @output = output
20
20
  @ie_cc_levels = []
@@ -30,15 +30,14 @@ module HtmlBeautifier
30
30
  end
31
31
 
32
32
  def emit(s)
33
- if (@new_line)
34
- @output << (@tab * @level)
33
+ if @new_line && !@output.empty?
34
+ @output << ("\n" + @tab * @level)
35
35
  end
36
36
  @output << s
37
37
  @new_line = false
38
38
  end
39
39
 
40
- def whitespace(*x)
41
- emit "\n"
40
+ def new_line(*_args)
42
41
  @new_line = true
43
42
  end
44
43
 
@@ -59,10 +58,10 @@ module HtmlBeautifier
59
58
  lines.pop while lines.last.strip.empty?
60
59
  indentation = lines.first[/^ +/]
61
60
 
62
- whitespace
61
+ new_line
63
62
  lines.each do |line|
64
63
  emit line.rstrip.sub(/^#{indentation}/, '')
65
- whitespace
64
+ new_line
66
65
  end
67
66
 
68
67
  outdent
@@ -70,10 +69,28 @@ module HtmlBeautifier
70
69
  emit closing
71
70
  end
72
71
 
72
+ def preformatted_block(opening, content, closing)
73
+ emit opening
74
+ emit content
75
+ emit closing
76
+ end
77
+
73
78
  def standalone_element(e)
74
79
  emit e
75
80
  end
76
81
 
82
+ def close_block_element(e)
83
+ outdent
84
+ emit e
85
+ new_line
86
+ end
87
+
88
+ def open_block_element(e)
89
+ new_line
90
+ emit e
91
+ indent
92
+ end
93
+
77
94
  def close_element(e)
78
95
  outdent
79
96
  emit e
@@ -97,8 +114,8 @@ module HtmlBeautifier
97
114
  end
98
115
 
99
116
  def text(t)
100
- emit(t.strip)
101
- whitespace if t =~ /\s$/
117
+ emit t.chomp
118
+ new_line if t.end_with? $/
102
119
  end
103
120
  end
104
121
  end
@@ -7,6 +7,12 @@ module HtmlBeautifier
7
7
  area | base | br | col | command | embed | hr | img | input | keygen |
8
8
  link | meta | param | source | track | wbr
9
9
  )}mix
10
+ HTML_BLOCK_ELEMENTS = %r{(?:
11
+ address | blockquote | center | dd | dir | div | dl | dt | fieldset |
12
+ form | h1 | h2 | h3 | h4 | h5 | h6 | hr | isindex | li | menu |
13
+ noframes | noscript | ol | p | pre | table | tbody | td | tfoot | th |
14
+ thead | tr | ul
15
+ )}mix
10
16
 
11
17
  def initialize
12
18
  super do |p|
@@ -20,20 +26,26 @@ module HtmlBeautifier
20
26
  :standalone_element
21
27
  p.map %r{<!.*?>}m,
22
28
  :standalone_element
23
- p.map %r{(<script#{ELEMENT_CONTENT}>)(.*?)(</script>)}m,
29
+ p.map %r{(<script#{ELEMENT_CONTENT}>)(.*?)(</script>)}mi,
24
30
  :foreign_block
25
- p.map %r{(<style#{ELEMENT_CONTENT}>)(.*?)(</style>)}m,
31
+ p.map %r{(<style#{ELEMENT_CONTENT}>)(.*?)(</style>)}mi,
26
32
  :foreign_block
33
+ p.map %r{(<pre#{ELEMENT_CONTENT}>)(.*?)(</pre>)}mi,
34
+ :preformatted_block
27
35
  p.map %r{<#{ELEMENT_CONTENT}/>}m,
28
36
  :standalone_element
29
37
  p.map %r{<#{HTML_VOID_ELEMENTS}(?: #{ELEMENT_CONTENT})?>}m,
30
38
  :standalone_element
39
+ p.map %r{</#{HTML_BLOCK_ELEMENTS}>}m,
40
+ :close_block_element
41
+ p.map %r{<#{HTML_BLOCK_ELEMENTS}(?: #{ELEMENT_CONTENT})?>}m,
42
+ :open_block_element
31
43
  p.map %r{</#{ELEMENT_CONTENT}>}m,
32
44
  :close_element
33
45
  p.map %r{<#{ELEMENT_CONTENT}>}m,
34
46
  :open_element
35
- p.map %r{\s+},
36
- :whitespace
47
+ p.map %r{\s*\r?\n\s*}m,
48
+ :new_line
37
49
  p.map %r{[^<]+},
38
50
  :text
39
51
  end
@@ -2,7 +2,7 @@ module HtmlBeautifier #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 10
5
+ TINY = 11
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -26,8 +26,7 @@ class TestHtmlBeautifierIntegration < Test::Unit::TestCase
26
26
  Heading 1
27
27
  </h1>
28
28
  </div>
29
- <div id="somethingElse">
30
- <p>Lorem Ipsum</p>
29
+ <div id="somethingElse"><p>Lorem Ipsum</p>
31
30
  <% if @x %>
32
31
  <% @ys.each do |y| %>
33
32
  <p>
@@ -44,12 +43,8 @@ class TestHtmlBeautifierIntegration < Test::Unit::TestCase
44
43
  <col style="width: 50%;">
45
44
  </colgroup>
46
45
  <tbody>
47
- <tr>
48
- <td>First column</td>
49
- </tr>
50
- <tr>
51
- <td>Second column</td>
52
- </tr>
46
+ <tr><td>First column</td></tr><tr>
47
+ <td>Second column</td></tr>
53
48
  </tbody>
54
49
  </table>
55
50
  </body>
@@ -83,7 +83,6 @@ class HtmlBeautifierRegressionTest < Test::Unit::TestCase
83
83
  assert_beautifies expected, source
84
84
  end
85
85
 
86
-
87
86
  def test_should_indent_styles
88
87
  source = code(%q(
89
88
  <style>
@@ -287,4 +286,39 @@ class HtmlBeautifierRegressionTest < Test::Unit::TestCase
287
286
  ))
288
287
  assert_beautifies source, source
289
288
  end
289
+
290
+ def test_should_not_modify_pre_content
291
+ source = code(%q(
292
+ <div>
293
+ <pre> Preformatted text
294
+
295
+ should <em>not be </em>
296
+ modified,
297
+ ever!
298
+
299
+ </pre>
300
+ </div>
301
+ ))
302
+ assert_beautifies source, source
303
+ end
304
+
305
+ def test_should_add_newline_after_block_elements
306
+ source = code(%q(
307
+ <section><h1>Title</h1><p>Lorem <em>ipsum</em></p>
308
+ <ol>
309
+ <li>First</li><li>Second</li></ol>
310
+ </section>
311
+ ))
312
+ expected = code(%(
313
+ <section>
314
+ <h1>Title</h1>
315
+ <p>Lorem <em>ipsum</em></p>
316
+ <ol>
317
+ <li>First</li>
318
+ <li>Second</li>
319
+ </ol>
320
+ </section>
321
+ ))
322
+ assert_beautifies expected, source
323
+ end
290
324
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmlbeautifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Battley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-28 00:00:00.000000000 Z
11
+ date: 2014-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description: A normaliser/beautifier for HTML that also understands embedded Ruby.