ruby_html 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8cf10da42b2a32aff7c67a58f3a8d0483d1b754c0df61ed073069006a4817478
4
+ data.tar.gz: 2fdc6c6b4168d6afba39009d17010604645d3dfe66af2caac8b36f7ec06b0ca1
5
+ SHA512:
6
+ metadata.gz: 5c7be02af205f74dca18f915dcf984e986bda08013bca8ba3a3f4365c02c5cadf66e295eb5c5c00bd8ebb3fa32d81279f824116991ecb4382a3fe9d984b52763
7
+ data.tar.gz: 415fccbcc1c9ddd93962209db002c48bc81f1a80bf7156c03ddbf3bcc5d10163908e29612bee9fd664c147cb6fa99a75fb302be9a5208871720c391425e6766c
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle/
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "rspec"
4
+ gem "rake"
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.3)
5
+ rake (12.3.1)
6
+ rspec (3.7.0)
7
+ rspec-core (~> 3.7.0)
8
+ rspec-expectations (~> 3.7.0)
9
+ rspec-mocks (~> 3.7.0)
10
+ rspec-core (3.7.1)
11
+ rspec-support (~> 3.7.0)
12
+ rspec-expectations (3.7.0)
13
+ diff-lcs (>= 1.2.0, < 2.0)
14
+ rspec-support (~> 3.7.0)
15
+ rspec-mocks (3.7.0)
16
+ diff-lcs (>= 1.2.0, < 2.0)
17
+ rspec-support (~> 3.7.0)
18
+ rspec-support (3.7.1)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ rake
25
+ rspec
26
+
27
+ BUNDLED WITH
28
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2018 Jonathan Thom
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Ruby HTML
2
+
3
+ A (totally unnecessary) HTML document builder in Ruby.
4
+
5
+ ### Installation
6
+
7
+ Eventually, this will be a gem, but for now you can do:
8
+
9
+ ```
10
+ git clone https://github.com/jonathanwthom/ruby_html.git
11
+ cd rhtml
12
+ irb
13
+ require_relative "ruby_html"
14
+ ```
15
+
16
+ Or you can include the rhtml directory in a project, and
17
+ require it there.
18
+
19
+ ### How to Use It
20
+
21
+ HTML element classes have the names you think they would. For example, to create a div:
22
+
23
+ ```
24
+ div = Div.new
25
+ ```
26
+
27
+ Elements can receive arguments. These can be strings, or can be other elements themselves.
28
+ These elements will be nested under their parent.
29
+
30
+ ```
31
+ div = Div.new("Hello", P.new("World"))
32
+ ```
33
+
34
+ You can also add new elements to a previously created element with `#add_elements`, which accepts any number of arguments.
35
+
36
+ ```
37
+ div.add_elements("Hello")
38
+ div.add_elements("Hello", "From", H3.new("Earth"))
39
+ ```
40
+
41
+ You can pass the same element as an argument as many times as you want - just like a partial or component.
42
+
43
+ All elements have a method `#render`, which prints all the element's contents (calling `.render` on all non string elements
44
+ contained within.
45
+
46
+ ```
47
+ div = Div.new(H6.new("Hello"), "World")
48
+ div.render
49
+ => "<div><h6>Hello</h6>World</div>"
50
+ ```
51
+
52
+ To add attributes to an element, pass a hash to the object like so:
53
+
54
+ ```
55
+ p_with_class = P.new("Hello World", attributes: { "class": "red" })
56
+ p_with_class.render
57
+ => <p class='red'>Hello World</p>
58
+ ```
59
+
60
+ Most importantly, if you want to actually create an HTML page, you'll want to create a `Document` that holds all of your elements.
61
+ A document recieves elements, as well as header elements (such as `<title>` or `<style>`).
62
+
63
+ ```
64
+ title = Title.new("Page Title")
65
+ Document.new(Div.new, header_elements: [title])
66
+ ```
67
+
68
+ `Document` has a special method, `#render_to_file` that recieves a file path, and creates your html document there.
69
+
70
+ ```
71
+ Document.new.render_to_file("hello_world.html")
72
+ => creates html document
73
+ ```
74
+
75
+
76
+ ### TODO
77
+
78
+ So much!
79
+
80
+ - Build out all other elements. See the current list of supported elements [here](https://github.com/JonathanWThom/rhtml/blob/master/elements/elements.rb).
81
+ - Tests!
82
+ - Gemify it.
83
+
84
+ ### Contributing
85
+
86
+ 1. Fork it
87
+ 2. Clone it
88
+ 3. Make a PR.
89
+ 4. Be kind to others. Programming is fun.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ class Document < Element
2
+ attr_reader :header_elements
3
+
4
+ def initialize(*elements, header_elements: [])
5
+ @elements = elements
6
+ @header_elements = header_elements.flatten
7
+ end
8
+
9
+ def opening_tag
10
+ "<!DOCTYPE><html><head>#{rendered_header_elements}</head><body>"
11
+ end
12
+
13
+ def rendered_header_elements
14
+ header_elements.map do |element|
15
+ element.render
16
+ end.join("")
17
+ end
18
+
19
+ def closing_tag
20
+ "</body></html>"
21
+ end
22
+
23
+ def render_to_file(path)
24
+ File.open(path, "w") { |file| file.write(render) }
25
+ end
26
+ end
@@ -0,0 +1,55 @@
1
+ class Element
2
+ attr_reader :elements, :attributes
3
+
4
+ def initialize(*elements, attributes: {})
5
+ @elements = elements
6
+ @attributes = attributes
7
+ end
8
+
9
+ def add_elements(*new_elements)
10
+ new_elements.each do |element|
11
+ @elements.push(element)
12
+ end
13
+ end
14
+
15
+ def add_attribute(attribute_hash)
16
+ @attributes.merge!(attribute_hash)
17
+ ## TODO: Account for adding elements that already exist.
18
+ end
19
+
20
+ def render
21
+ opening_tag + rendered_elements + closing_tag
22
+ end
23
+
24
+ def rendered_elements
25
+ elements.map do |element|
26
+ if element.is_a? String
27
+ element
28
+ else
29
+ element.render
30
+ 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
+ end
44
+ end
45
+
46
+ def closing_tag
47
+ "</#{tag}>"
48
+ end
49
+
50
+ def rendered_attributes
51
+ attributes.map do |k, v|
52
+ "#{k}='#{v}'"
53
+ end.join(" ")
54
+ end
55
+ end
@@ -0,0 +1,119 @@
1
+ elements = %w(
2
+ Div
3
+ P
4
+ H1
5
+ H2
6
+ H3
7
+ H4
8
+ H5
9
+ H6
10
+ Img
11
+ Script
12
+ Style
13
+ Title
14
+ Link
15
+ Meta
16
+ Address
17
+ Article
18
+ Aside
19
+ Footer
20
+ Header
21
+ HGroup
22
+ Nav
23
+ Section
24
+ BlockQuote
25
+ Dd
26
+ Dl
27
+ Dt
28
+ FigCaption
29
+ Figure
30
+ Hr
31
+ Li
32
+ Main
33
+ Ol
34
+ Pre
35
+ Ul
36
+ A
37
+ Abbr
38
+ B
39
+ Bdi
40
+ Bdo
41
+ Br
42
+ Cite
43
+ Code
44
+ Dfn
45
+ Em
46
+ I
47
+ Kbd
48
+ Nobr
49
+ Q
50
+ Rp
51
+ Rt
52
+ Rtc
53
+ Ruby
54
+ S
55
+ Samp
56
+ Small
57
+ Span
58
+ Strong
59
+ Sub
60
+ Sup
61
+ Tt
62
+ U
63
+ Var
64
+ Wbr
65
+ Area
66
+ Audio
67
+ Map
68
+ Track
69
+ Video
70
+ Applet
71
+ Embed
72
+ IFrame
73
+ NoEmbed
74
+ Param
75
+ Picture
76
+ Source
77
+ Canvas
78
+ NoScript
79
+ Del
80
+ Ins
81
+ Caption
82
+ Col
83
+ ColGroup
84
+ Table
85
+ TBody
86
+ Td
87
+ TFoot
88
+ Th
89
+ THead
90
+ Tr
91
+ Button
92
+ DataList
93
+ FieldSet
94
+ Form
95
+ Input
96
+ Label
97
+ Legend
98
+ Meter
99
+ OptGroup
100
+ Option
101
+ Output
102
+ Progress
103
+ Select
104
+ TextArea
105
+ Details
106
+ Dialog
107
+ Menu
108
+ MenuItem
109
+ Summary
110
+ Content
111
+ Shadow
112
+ Slot
113
+ Template
114
+ )
115
+
116
+ elements.each do |klass_name|
117
+ klass = Class.new(Element)
118
+ Object.const_set(klass_name, klass)
119
+ end
@@ -0,0 +1,3 @@
1
+ module RubyHtml
2
+ VERSION = "0.0.2"
3
+ end
data/lib/ruby_html.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "ruby_html/version"
2
+ require "ruby_html/elements/element"
3
+ require "ruby_html/elements/document"
4
+ require "ruby_html/elements/elements"
data/ruby_html.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ruby_html/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_html"
8
+ spec.version = RubyHtml::VERSION
9
+ spec.authors = ["Jonathan Thom"]
10
+ spec.email = ["jonathan.thom1990@gmail"]
11
+ spec.description = %{Build HTML with Ruby}
12
+ spec.summary = ""
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'engtagger'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ end
File without changes
File without changes
File without changes
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Thom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: engtagger
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Build HTML with Ruby
70
+ email:
71
+ - jonathan.thom1990@gmail
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/ruby_html.rb
83
+ - lib/ruby_html/elements/document.rb
84
+ - lib/ruby_html/elements/element.rb
85
+ - lib/ruby_html/elements/elements.rb
86
+ - lib/ruby_html/version.rb
87
+ - ruby_html.gemspec
88
+ - spec/document_spec.rb
89
+ - spec/element_spec.rb
90
+ - spec/elements_spec.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.6
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: ''
115
+ test_files:
116
+ - spec/document_spec.rb
117
+ - spec/element_spec.rb
118
+ - spec/elements_spec.rb