glam 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +1 -0
- data/bin/glam +5 -0
- data/glam.gemspec +20 -0
- data/lib/glam.rb +15 -0
- data/lib/glam/cli.rb +38 -0
- data/lib/glam/glamorizer.rb +74 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ebfb766d24926bb7ae2327e74fe4aa4c28cba6ed
|
4
|
+
data.tar.gz: 3f90158c080e4ff95ac0d365443455d2fdf377a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e72d8918cb66f7f1d014dc027505b066ee53150e5f09b6421600baadbcec5e43aeb6cbf9881cf97ede2dcdf371bfb2ea80b8c78b49175504f000e2184a72e7a
|
7
|
+
data.tar.gz: db70c41eba8cd202ba60eadb6e2a72028ab2af932f4a4dc3ef0947820fa24af1fc942645b27fc18e70fec683113d78ce564f5cf636b51dcffbc006e386eeb258
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Paul Barry
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Glam
|
2
|
+
|
3
|
+
Make your HTML look glamorous! Glam takes well-formed but possibly dubiously formatted HTML and outputs it as pretty printed HTML 5.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'glam'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install glam
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Glam can be used as a command-line tool. For example, if you have an HTML file like this:
|
22
|
+
|
23
|
+
**example.html**
|
24
|
+
|
25
|
+
``` html
|
26
|
+
<html><head><title>Example</title></head><body><h1>Example</h1></body></html>
|
27
|
+
```
|
28
|
+
|
29
|
+
You can glamorize it like this:
|
30
|
+
|
31
|
+
$ glam example.html
|
32
|
+
|
33
|
+
Which outputs:
|
34
|
+
|
35
|
+
``` html
|
36
|
+
<!doctype html>
|
37
|
+
<html>
|
38
|
+
<head>
|
39
|
+
<title>
|
40
|
+
Example
|
41
|
+
</title>
|
42
|
+
</head>
|
43
|
+
<body>
|
44
|
+
<h1>
|
45
|
+
Example
|
46
|
+
</h1>
|
47
|
+
</body>
|
48
|
+
</html>
|
49
|
+
```
|
50
|
+
|
51
|
+
You can also produce the same output by piping HTML into `glam`:
|
52
|
+
|
53
|
+
$ cat example.html | glam
|
54
|
+
|
55
|
+
You can also use Glam from ruby code:
|
56
|
+
|
57
|
+
``` ruby
|
58
|
+
require 'glam'
|
59
|
+
|
60
|
+
puts Glam(File.read('example.html'))
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/glam
ADDED
data/glam.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "glam"
|
3
|
+
spec.version = "0.0.1"
|
4
|
+
spec.authors = ["Paul Barry"]
|
5
|
+
spec.email = ["mail@paulbarry.com"]
|
6
|
+
spec.description = %q{An HTML pretty printer}
|
7
|
+
spec.summary = %q{Make your HTML look glamorous!}
|
8
|
+
spec.homepage = "http://github.com/pjb3/glam"
|
9
|
+
spec.license = "MIT"
|
10
|
+
|
11
|
+
spec.files = `git ls-files`.split($/)
|
12
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
13
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
14
|
+
spec.require_paths = ["lib"]
|
15
|
+
|
16
|
+
spec.add_runtime_dependency "nokogiri"
|
17
|
+
|
18
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
19
|
+
spec.add_development_dependency "rake"
|
20
|
+
end
|
data/lib/glam.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'glam/glamorizer'
|
2
|
+
|
3
|
+
module Glam
|
4
|
+
VERSION = '0.0.1'
|
5
|
+
end
|
6
|
+
|
7
|
+
# Pretty-print the HTML
|
8
|
+
# @param html [String] The HTML to be pretty-printed
|
9
|
+
# @param opts [Hash] The options
|
10
|
+
# @option opts [Integer] :indent_size
|
11
|
+
# How many spaces should be used for each level of indentation
|
12
|
+
# @return [String] The pretty-printed HTML
|
13
|
+
def Glam(html, opts={})
|
14
|
+
Glam.glamorize(html, opts={})
|
15
|
+
end
|
data/lib/glam/cli.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'glam'
|
3
|
+
|
4
|
+
module Glam
|
5
|
+
class CLI
|
6
|
+
|
7
|
+
def self.run
|
8
|
+
new.run
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@options = {
|
13
|
+
indent_size: 2,
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
@parser = OptionParser.new do |o|
|
19
|
+
o.on "-i", "--indent [INDENT]", Integer do |indent_size|
|
20
|
+
@options[:indent_size] = indent_size
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@parser.banner = "glam [options] file"
|
25
|
+
|
26
|
+
@parser.on_tail "-h", "--help", "Show help" do
|
27
|
+
$stderr.puts @parser
|
28
|
+
exit 1
|
29
|
+
end
|
30
|
+
|
31
|
+
@parser.parse!
|
32
|
+
|
33
|
+
glam = Glam::Glamorizer.new(@options)
|
34
|
+
|
35
|
+
puts glam.glamorize(ARGV[0] ? File.read(ARGV[0]) : STDIN.read)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
require "set"
|
3
|
+
|
4
|
+
module Glam
|
5
|
+
class Glamorizer
|
6
|
+
VOID_ELEMENTS = Set.new(%w[area base br col embed hr img input keygen link meta param source track wbr])
|
7
|
+
|
8
|
+
attr_accessor :indent_size, :indent_char, :current_indent
|
9
|
+
|
10
|
+
# @param opts [Hash] The options
|
11
|
+
# @option opts [Integer] :indent_size
|
12
|
+
# How many spaces should be used for each level of indentation
|
13
|
+
def initialize(opts={})
|
14
|
+
self.indent_size = 2
|
15
|
+
self.indent_char = ' '
|
16
|
+
self.current_indent = 0
|
17
|
+
|
18
|
+
if opts
|
19
|
+
opts.each do |k,v|
|
20
|
+
send("#{k}=", v)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Pretty-print the HTML
|
26
|
+
# @param html [String] The HTML to be pretty-printed
|
27
|
+
# @return [String] The pretty-printed HTML
|
28
|
+
def glamorize(html)
|
29
|
+
node = if html.is_a?(Nokogiri::XML::Node)
|
30
|
+
result = ''
|
31
|
+
html
|
32
|
+
else
|
33
|
+
result = "<!doctype html>\n"
|
34
|
+
Nokogiri::HTML(html).root
|
35
|
+
end
|
36
|
+
|
37
|
+
result << indented_node_with_attributes(node)
|
38
|
+
self.current_indent += 1
|
39
|
+
node.children.each do |child_node|
|
40
|
+
if child_node.text?
|
41
|
+
unless child_node.blank?
|
42
|
+
result << "#{indent}#{child_node.content.strip}\n"
|
43
|
+
end
|
44
|
+
elsif VOID_ELEMENTS.include?(child_node.name)
|
45
|
+
result << indented_node_with_attributes(child_node)
|
46
|
+
else
|
47
|
+
result << glamorize(child_node)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
self.current_indent -= 1
|
51
|
+
result << "#{indent}</#{node.name}>\n"
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def indent
|
56
|
+
indent_char * (indent_size * current_indent)
|
57
|
+
end
|
58
|
+
|
59
|
+
def indented_node_with_attributes(node)
|
60
|
+
"#{indent}<#{node.name}#{node.attributes.map{|n,v| %{ #{n}="#{v}"}}.join}>\n"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Pretty-print the HTML
|
65
|
+
# @param html [String] The HTML to be pretty-printed
|
66
|
+
# @param opts [Hash] The options
|
67
|
+
# @option opts [Integer] :indent_size
|
68
|
+
# How many spaces should be used for each level of indentation
|
69
|
+
# @return [String] The pretty-printed HTML
|
70
|
+
def self.glamorize(html, opts={})
|
71
|
+
Glamorizer.new(opts).glamorize(html)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glam
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Barry
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: An HTML pretty printer
|
56
|
+
email:
|
57
|
+
- mail@paulbarry.com
|
58
|
+
executables:
|
59
|
+
- glam
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/glam
|
69
|
+
- glam.gemspec
|
70
|
+
- lib/glam.rb
|
71
|
+
- lib/glam/cli.rb
|
72
|
+
- lib/glam/glamorizer.rb
|
73
|
+
homepage: http://github.com/pjb3/glam
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.14
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Make your HTML look glamorous!
|
97
|
+
test_files: []
|
98
|
+
has_rdoc:
|