to_html 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +4 -0
- data/README +83 -0
- data/lib/to_html.rb +105 -0
- data/test/pics/lastnode.png +0 -0
- data/test/pics/node.png +0 -0
- data/test/pics/vline.png +0 -0
- data/test/test_to_html.rb +39 -0
- data/test/to_html.css +44 -0
- metadata +66 -0
data/History.txt
ADDED
data/README
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
== to_html
|
2
|
+
|
3
|
+
by F.Febles
|
4
|
+
ilo.rubyforge.org
|
5
|
+
|
6
|
+
== DESCRIPTION:
|
7
|
+
|
8
|
+
Converts an object to an Html structured list.
|
9
|
+
|
10
|
+
It's used basically with Arrays, Hashes, and Enumerable objects.
|
11
|
+
|
12
|
+
== FEATURES:
|
13
|
+
|
14
|
+
You can convert a 'complex' object like this one:
|
15
|
+
|
16
|
+
h={ :array => [1,2,3,4],
|
17
|
+
:string => 'test',
|
18
|
+
:fixnum => 42}
|
19
|
+
|
20
|
+
to an Html structured list, that you can beautify using CSS (look for the example)
|
21
|
+
|
22
|
+
The easy way to use it is including enumerable in object class:
|
23
|
+
|
24
|
+
class Object
|
25
|
+
include ToHtml
|
26
|
+
end
|
27
|
+
|
28
|
+
Or perhaps better, including only the class or object you are going to use:
|
29
|
+
|
30
|
+
class Hash
|
31
|
+
include ToHtml
|
32
|
+
end
|
33
|
+
|
34
|
+
Then, you can call to_html in your program:
|
35
|
+
|
36
|
+
require 'to_html'
|
37
|
+
h={ :array => [1,2,3,4],
|
38
|
+
:string => 'test',
|
39
|
+
:fixnum => 42}
|
40
|
+
class Hash
|
41
|
+
include ToHtml
|
42
|
+
end
|
43
|
+
|
44
|
+
puts ToHtml::TO_HTML_HEAD + h.to_html + ToHtml::TO_HTML_END
|
45
|
+
|
46
|
+
You can see a better example in the test folder of the gem, using CSS.
|
47
|
+
|
48
|
+
== PROBLEMS:
|
49
|
+
|
50
|
+
Probably truncates the output of an object that you can't traverse its structure using 'each0.
|
51
|
+
|
52
|
+
== REQUIREMENTS:
|
53
|
+
|
54
|
+
We only need parse_tree for tests.
|
55
|
+
|
56
|
+
== INSTALL:
|
57
|
+
|
58
|
+
sudo gem install to_html
|
59
|
+
|
60
|
+
== LICENSE:
|
61
|
+
|
62
|
+
(The MIT License)
|
63
|
+
|
64
|
+
Copyright (c) 2008 FIXME (different license?)
|
65
|
+
|
66
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
67
|
+
a copy of this software and associated documentation files (the
|
68
|
+
'Software'), to deal in the Software without restriction, including
|
69
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
70
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
71
|
+
permit persons to whom the Software is furnished to do so, subject to
|
72
|
+
the following conditions:
|
73
|
+
|
74
|
+
The above copyright notice and this permission notice shall be
|
75
|
+
included in all copies or substantial portions of the Software.
|
76
|
+
|
77
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
78
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
79
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
80
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
81
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
82
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
83
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/to_html.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
module ToHtml
|
4
|
+
|
5
|
+
VERSION='0.1.0'
|
6
|
+
|
7
|
+
TO_HTML_HEAD=<<-HTMLTEXT
|
8
|
+
<html>
|
9
|
+
<head>
|
10
|
+
<title>ToHtml Test</title>
|
11
|
+
<link rel="stylesheet" type="text/css" href="to_html.css" />
|
12
|
+
</head>
|
13
|
+
<body>
|
14
|
+
<div class="title">
|
15
|
+
<a href="http://ilo.rubyforge.org"><span class="title">ToHtml Test</span></a>
|
16
|
+
</div>
|
17
|
+
HTMLTEXT
|
18
|
+
|
19
|
+
TO_HTML_END='</body></html>'
|
20
|
+
|
21
|
+
|
22
|
+
##
|
23
|
+
# Converts the "object structure" to an HTML tree list,
|
24
|
+
# that you can beautify using css (take a look to our gem test)
|
25
|
+
#
|
26
|
+
def to_html
|
27
|
+
html='<div class="treebox"><ul class="tree">'+ptree(self)+'</ul></div>'
|
28
|
+
return html
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
##
|
33
|
+
# Tells if the object is a "branch structure" that contains another objects,
|
34
|
+
# basically, "long" arrays, hashes, and enumerable objects.
|
35
|
+
#
|
36
|
+
# String and Range return false, in spite of their lengths, because
|
37
|
+
# they don't contain "complex" objects.
|
38
|
+
#
|
39
|
+
# Hashes of only one string value are considered "short".
|
40
|
+
#
|
41
|
+
def branch?(obj, min_length=120)
|
42
|
+
case
|
43
|
+
when (obj.kind_of?(String) or obj.kind_of?(Range)) then false
|
44
|
+
when (obj.kind_of?(Hash) and obj.length==1 and
|
45
|
+
obj.to_a[0][1].kind_of?(String)) then false
|
46
|
+
else (obj.kind_of?(Enumerable) and (min_length< obj.inspect.length))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Formats an 'simple' object as Html
|
52
|
+
#
|
53
|
+
# It uses the convention of adding ":" in front of a symbol name,
|
54
|
+
# and enclosing {hashes} and [arrays].
|
55
|
+
#
|
56
|
+
def var2html(var)
|
57
|
+
case
|
58
|
+
when var.kind_of?(Hash)
|
59
|
+
'<span class="'+var.class.to_s+'">{'+var.to_a.map{|a| var2html(a[0])+' => '+var2html(a[1])}.join(", ")+"}</span>"
|
60
|
+
when var.kind_of?(Array)
|
61
|
+
'<span class="'+var.class.to_s+'">['+var.map{|a| var2html(a)}.join(", ")+"]</span>"
|
62
|
+
else
|
63
|
+
'<span class="'+var.class.to_s+'">'+CGI.escapeHTML(var.inspect)+"</span>"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
##
|
68
|
+
# Travels the object tree, and calls var2html for each 'simple' object
|
69
|
+
def ptree(obj)
|
70
|
+
ret=""
|
71
|
+
if branch?(obj)
|
72
|
+
# It's a "complex" object
|
73
|
+
# creating default indexes for travelling the object structure
|
74
|
+
indexes=0..(obj.length-1)
|
75
|
+
|
76
|
+
# Sorting object
|
77
|
+
case
|
78
|
+
when obj.respond_to?(:keys) then indexes=obj.keys.sort_by{|k| k.to_s.downcase}
|
79
|
+
# when hash, indexes are ordered keys
|
80
|
+
when (obj.respond_to?('sort_by') and obj.respond_to?('to_s')) then obj.sort_by{|k| k.to_s.downcase}
|
81
|
+
end
|
82
|
+
|
83
|
+
#travelling object
|
84
|
+
indexes.each{|index|
|
85
|
+
value=obj[index]
|
86
|
+
if branch?(value,0)
|
87
|
+
ret<< '<li>'+var2html(index)+'<ul>' unless indexes==(0..0)
|
88
|
+
ret<< ptree(value)
|
89
|
+
ret<< '</ul></li>' unless indexes==(0..0)
|
90
|
+
else
|
91
|
+
if obj.respond_to?(:keys) then ret<< "<li>#{var2html({index =>value})}</li>"
|
92
|
+
else ret<< "<li>#{var2html(value)}</li>"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
}
|
96
|
+
else
|
97
|
+
# It's a simple object: converting to a simple
|
98
|
+
# html list item
|
99
|
+
ret<< "<li>#{var2html(obj)}</li>"
|
100
|
+
end
|
101
|
+
return ret
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
Binary file
|
data/test/pics/node.png
ADDED
Binary file
|
data/test/pics/vline.png
ADDED
Binary file
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib to_html]))
|
2
|
+
require 'parse_tree'
|
3
|
+
|
4
|
+
HASH_TEST= {:ruby_version => VERSION,
|
5
|
+
:processor => ENV["PROCESSOR_IDENTIFIER"],
|
6
|
+
:array_test => Fixnum.ancestors,
|
7
|
+
:another_hash => {'string_key' => (1..8),
|
8
|
+
:symbol_key => "lorem ipsum",
|
9
|
+
:lambda_Value => lambda{puts "hello!"}
|
10
|
+
},
|
11
|
+
:an_array => [(0..100), 'a', 7, :sym],
|
12
|
+
[0,1,2,3] => nil,
|
13
|
+
"pi" => 3.1415,
|
14
|
+
42 => "the answer",
|
15
|
+
:long_array => [{1,2,3,4,5,6,7,8,9,0},
|
16
|
+
{:one => 'one',
|
17
|
+
:two => 2,
|
18
|
+
:three => "III"
|
19
|
+
},
|
20
|
+
['ab','cd','de','abcde',{:a,1,'b',:two}],
|
21
|
+
[1,3,5,7,11,13,17],
|
22
|
+
ENV,
|
23
|
+
{1,1.class,'f','f'.class,:g,:g.class}],
|
24
|
+
:parse_tree => ParseTree.translate(ToHtml, :to_html)
|
25
|
+
}
|
26
|
+
|
27
|
+
##
|
28
|
+
# We are extending ALL objects with the ToHtml module.
|
29
|
+
# Probably you should only extend the Classes or even the
|
30
|
+
# objects that you'll need.
|
31
|
+
#
|
32
|
+
class Object
|
33
|
+
include ToHtml
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Try with 'ruby test_to_html.rb > test.html'
|
38
|
+
#
|
39
|
+
puts ToHtml::TO_HTML_HEAD+HASH_TEST.to_html+ToHtml::TO_HTML_END
|
data/test/to_html.css
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
a {text-decoration: none}
|
2
|
+
|
3
|
+
ul.tree, ul.tree ul{
|
4
|
+
font-family: 'Lucida Console', Monaco, 'DejaVu Sans Mono', monospace;
|
5
|
+
font-size: 13px;
|
6
|
+
color: DimGray;
|
7
|
+
font-weight: normal;
|
8
|
+
list-style-type: none;
|
9
|
+
background: url(pics/vline.png) repeat-y;
|
10
|
+
margin: 0;
|
11
|
+
padding: 0}
|
12
|
+
|
13
|
+
ul.tree ul{
|
14
|
+
margin-left: 10px}
|
15
|
+
|
16
|
+
ul.tree li{
|
17
|
+
margin: 0;
|
18
|
+
padding: 0 12px;
|
19
|
+
line-height: 20px;
|
20
|
+
background: url(pics/node.png) no-repeat}
|
21
|
+
|
22
|
+
ul.tree li:last-child{
|
23
|
+
background: #fff url(pics/lastnode.png) no-repeat}
|
24
|
+
|
25
|
+
div.treebox{
|
26
|
+
background-color: #fff;
|
27
|
+
border: dashed 2px #ccc;
|
28
|
+
margin: auto 4em;
|
29
|
+
padding: 1em}
|
30
|
+
|
31
|
+
div.title {
|
32
|
+
margin: 2em 3em 1em}
|
33
|
+
|
34
|
+
span.title {
|
35
|
+
color: #369;
|
36
|
+
font-family: 'Lucida Console', Monaco, monospace;
|
37
|
+
font-size: 17px;
|
38
|
+
font-weight: bold}
|
39
|
+
|
40
|
+
span.String {color: IndianRed}
|
41
|
+
span.Fixnum, span.Float, span.Range {color:CadetBlue}
|
42
|
+
span.Proc {color:#008000}
|
43
|
+
span.NilClass {color:orange}
|
44
|
+
span.Symbol {color:#369}
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_html
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- F.Febles
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-26 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Converts an object to a Html structured list.
|
17
|
+
email: ferfebles@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- History.txt
|
27
|
+
- lib/to_html.rb
|
28
|
+
- test/pics/lastnode.png
|
29
|
+
- test/pics/node.png
|
30
|
+
- test/pics/vline.png
|
31
|
+
- test/test_to_html.rb
|
32
|
+
- test/to_html.css
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://ilo.rubyforge.org/
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --title
|
40
|
+
- to_html
|
41
|
+
- --main
|
42
|
+
- README
|
43
|
+
- --line-numbers
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: ilo
|
61
|
+
rubygems_version: 1.3.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Converts an object to an Html structured list.
|
65
|
+
test_files: []
|
66
|
+
|