html_pretty 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +2 -0
- data/Rakefile +25 -0
- data/bin/html_pretty +10 -0
- data/lib/html_pretty/version.rb +3 -0
- data/lib/html_pretty.rb +41 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2012 Benjamin Feng
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
|
3
|
+
|
4
|
+
task :compare do
|
5
|
+
dstdir = 'compare'
|
6
|
+
srcfile = "#{dstdir}/_orig.html"
|
7
|
+
srcstr = IO.read(srcfile)
|
8
|
+
|
9
|
+
`ruby -Ilib bin/html_pretty #{srcfile} >#{dstdir}/html_pretty.html`
|
10
|
+
`htmlbeautifier <#{srcfile} >#{dstdir}/htmlbeautifier.html`
|
11
|
+
`tidy -i -q #{srcfile} >#{dstdir}/tidy.html 2>&-`
|
12
|
+
|
13
|
+
begin
|
14
|
+
require 'nokogiri'
|
15
|
+
doc = Nokogiri::XML(srcstr, &:noblanks)
|
16
|
+
File.open("#{dstdir}/nokogiri.html", 'w'){|f| f.write doc.to_xhtml(:indent => 2)}
|
17
|
+
rescue LoadError => e
|
18
|
+
$stderr.puts '-- nokogiri not found -- skipping'
|
19
|
+
end
|
20
|
+
|
21
|
+
doc = REXML::Document.new(srcstr)
|
22
|
+
File.open("#{dstdir}/rexml.html", 'w'){|f| doc.write f, 2}
|
23
|
+
end
|
24
|
+
|
25
|
+
task :default => :compare
|
data/bin/html_pretty
ADDED
data/lib/html_pretty.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'html_pretty/version'
|
2
|
+
|
3
|
+
|
4
|
+
module HtmlPretty
|
5
|
+
SPECIAL = /(<!--.*?-->|<script.*?\/script>|<style.*?\/style>)/m
|
6
|
+
|
7
|
+
# Really bad assumption heavy tidy...
|
8
|
+
# Self rolled because other versions have weird errors
|
9
|
+
def self.run(html, out='')
|
10
|
+
indent = 0
|
11
|
+
html.split(SPECIAL).each do |blob|
|
12
|
+
if blob.start_with?('<!--!!!-->')
|
13
|
+
raise
|
14
|
+
elsif blob =~ SPECIAL
|
15
|
+
out << " " * indent << blob << "\n"
|
16
|
+
else
|
17
|
+
blob = blob.gsub(/^[ \t]*\n/, "") # kill blank lines
|
18
|
+
blob = blob.gsub(/^[ \t]+/, '') # kill opening whitespace
|
19
|
+
blob = blob.gsub(/[ \t]+$/, '') # kill closing whitespace
|
20
|
+
blob = blob.gsub(/[ \t]+/, ' ') # collapse all whitespace
|
21
|
+
blob = blob.gsub(/<[ \t]+/, '<') # remove whitespace between brackets
|
22
|
+
blob = blob.gsub(/[ \t]+>/, '>') # remove whitespace between brackets
|
23
|
+
blob.each_line do |line|
|
24
|
+
open_tags = line.scan(/<[^\/][^>]*>/).select{|d| d !~ /\/>$/}.size
|
25
|
+
close_tags = line.scan(/<\/[^>]*>/).size
|
26
|
+
if open_tags > close_tags
|
27
|
+
out << " " * indent + line
|
28
|
+
indent += open_tags - close_tags
|
29
|
+
indent = 0 if indent < 0
|
30
|
+
else
|
31
|
+
indent += open_tags - close_tags
|
32
|
+
indent = 0 if indent < 0
|
33
|
+
out << " " * indent + line
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
out
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html_pretty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Benjamin Feng
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.0.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.0.0
|
30
|
+
description: html_pretty is yet another HTML prettifier. It purposefully does not
|
31
|
+
do parsing or validation.
|
32
|
+
email: benjamin.feng@gmail.com
|
33
|
+
executables:
|
34
|
+
- html_pretty
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- bin/html_pretty
|
39
|
+
- lib/html_pretty/version.rb
|
40
|
+
- lib/html_pretty.rb
|
41
|
+
- LICENSE
|
42
|
+
- Rakefile
|
43
|
+
- README.md
|
44
|
+
homepage: http://github.com/fengb/html_pretty
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.23
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Make HTML pretty.
|
68
|
+
test_files: []
|
69
|
+
has_rdoc:
|