ruby_html 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/README.md +9 -8
- data/lib/ruby_html/elements/document.rb +21 -19
- data/lib/ruby_html/elements/element.rb +45 -43
- data/lib/ruby_html/elements/elements.rb +2 -2
- data/lib/ruby_html/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b034710a87722e625acd301794325c89c5ddabe186f6293b20a1529e2a0ef0f1
|
4
|
+
data.tar.gz: 6585a22ff7f15f3568062645259550247e9a1e8d0add3dd1b74a0c949ce142eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a08bebc39b11f328a313924e7596d37cab8f919248a200f4bccad852b4cd8975aed9a5f64a8f7d3b360030da1119ec0f25142087e168218a23dd8549d26a1806
|
7
|
+
data.tar.gz: 001d5efa900a8382c8348848e33733e2efdbdbed33852fbbd52b16c18b646bcc42522d5ac6f2be6395f66e64b46d08fe1e999e29dc1d5fce207f0102478ee462
|
data/README.md
CHANGED
@@ -23,14 +23,14 @@ Or install it yourself as:
|
|
23
23
|
HTML elements have the names you think they would. For example, to create a div:
|
24
24
|
|
25
25
|
```
|
26
|
-
div = Div.new
|
26
|
+
div = HTML::Div.new
|
27
27
|
```
|
28
28
|
|
29
29
|
Elements can receive arguments. These can be strings, or can be other elements themselves.
|
30
30
|
These elements will be nested under their parent.
|
31
31
|
|
32
32
|
```
|
33
|
-
div = Div.new("Hello", P.new("World"))
|
33
|
+
div = HTML::Div.new("Hello", P.new("World"))
|
34
34
|
```
|
35
35
|
|
36
36
|
You can also add new elements to a previously created element with `#add_elements`, which accepts any number of arguments.
|
@@ -46,7 +46,7 @@ All elements have a method `#render`, which prints all the element's contents (c
|
|
46
46
|
contained within.
|
47
47
|
|
48
48
|
```
|
49
|
-
div = Div.new(H6.new("Hello"), "World")
|
49
|
+
div = HTML::Div.new(H6.new("Hello"), "World")
|
50
50
|
div.render
|
51
51
|
=> "<div><h6>Hello</h6>World</div>"
|
52
52
|
```
|
@@ -54,7 +54,7 @@ div.render
|
|
54
54
|
To add attributes to an element, pass a hash to the object like so:
|
55
55
|
|
56
56
|
```
|
57
|
-
p_with_class = P.new("Hello World", attributes: { "class": "red" })
|
57
|
+
p_with_class = HTML::P.new("Hello World", attributes: { "class": "red" })
|
58
58
|
p_with_class.render
|
59
59
|
=> <p class='red'>Hello World</p>
|
60
60
|
```
|
@@ -63,21 +63,22 @@ Most importantly, if you want to actually create an HTML page, you'll want to cr
|
|
63
63
|
A document recieves elements, as well as header elements (such as `<title>` or `<style>`).
|
64
64
|
|
65
65
|
```
|
66
|
-
title = Title.new("Page Title")
|
67
|
-
Document.new(Div.new, header_elements: [title])
|
66
|
+
title = HTML::Title.new("Page Title")
|
67
|
+
Document.new(HTML::Div.new, header_elements: [title])
|
68
68
|
```
|
69
69
|
|
70
70
|
`Document` has a special method, `#render_to_file` that receives a file path, and creates your html document there.
|
71
71
|
|
72
72
|
```
|
73
|
-
Document.new.render_to_file("hello_world.html")
|
73
|
+
HTML::Document.new.render_to_file("hello_world.html")
|
74
74
|
=> creates html document
|
75
75
|
```
|
76
76
|
|
77
77
|
|
78
78
|
### TODO
|
79
79
|
|
80
|
-
- Build out remaining elements. There are a few that conflict with ruby (such as `Object`).
|
80
|
+
- Build out remaining elements. There are a few that conflict with ruby (such as `Object`). This should not be an issue anymore
|
81
|
+
now that the `HTML` namespace has been added.
|
81
82
|
- Code clean up (alphabetical order, some methods probably don't need to be public, etc).
|
82
83
|
- The single quotes for attributes are a bit weird, and you might not always want those.
|
83
84
|
- It desperately needs some tests!
|
@@ -1,26 +1,28 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module HTML
|
2
|
+
class Document < Element
|
3
|
+
attr_reader :header_elements
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
def initialize(*elements, header_elements: [])
|
6
|
+
@elements = elements
|
7
|
+
@header_elements = header_elements.flatten
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def opening_tag
|
11
|
+
"<!DOCTYPE><html><head>#{rendered_header_elements}</head><body>"
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def rendered_header_elements
|
15
|
+
header_elements.map do |element|
|
16
|
+
element.render
|
17
|
+
end.join("")
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def closing_tag
|
21
|
+
"</body></html>"
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
def render_to_file(path)
|
25
|
+
File.open(path, "w") { |file| file.write(render) }
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
@@ -1,55 +1,57 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module HTML
|
2
|
+
class Element
|
3
|
+
attr_reader :elements, :attributes
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
def initialize(*elements, attributes: {})
|
6
|
+
@elements = elements
|
7
|
+
@attributes = attributes
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def add_elements(*new_elements)
|
11
|
+
new_elements.each do |element|
|
12
|
+
@elements.push(element)
|
13
|
+
end
|
12
14
|
end
|
13
|
-
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def add_attribute(attribute_hash)
|
17
|
+
@attributes.merge!(attribute_hash)
|
18
|
+
## TODO: Account for adding elements that already exist.
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
def render
|
22
|
+
opening_tag + rendered_elements + closing_tag
|
23
|
+
end
|
24
|
+
|
25
|
+
def rendered_elements
|
26
|
+
elements.map do |element|
|
27
|
+
if element.is_a? String
|
28
|
+
element
|
29
|
+
else
|
30
|
+
element.render
|
31
|
+
end
|
32
|
+
end.join("")
|
33
|
+
end
|
34
|
+
|
35
|
+
def tag
|
36
|
+
self.class.name.downcase
|
37
|
+
end
|
38
|
+
|
39
|
+
def opening_tag
|
40
|
+
if rendered_attributes != ""
|
41
|
+
"<#{tag} #{rendered_attributes}>"
|
28
42
|
else
|
29
|
-
|
43
|
+
"<#{tag}>"
|
30
44
|
end
|
31
|
-
end.join("")
|
32
|
-
end
|
33
|
-
|
34
|
-
def tag
|
35
|
-
self.class.name.downcase
|
36
|
-
end
|
37
|
-
|
38
|
-
def opening_tag
|
39
|
-
if rendered_attributes != ""
|
40
|
-
"<#{tag} #{rendered_attributes}>"
|
41
|
-
else
|
42
|
-
"<#{tag}>"
|
43
45
|
end
|
44
|
-
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
def closing_tag
|
48
|
+
"</#{tag}>"
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
def rendered_attributes
|
52
|
+
attributes.map do |k, v|
|
53
|
+
"#{k}='#{v}'"
|
54
|
+
end.join(" ")
|
55
|
+
end
|
54
56
|
end
|
55
57
|
end
|
data/lib/ruby_html/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Thom
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: engtagger
|