ruht 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +13 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +3 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +20 -0
  8. data/Gemfile.lock +114 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +82 -0
  11. data/Rakefile +11 -0
  12. data/Steepfile +15 -0
  13. data/lib/ruht/attributes/boolean_attribute.rb +18 -0
  14. data/lib/ruht/attributes/class_attribute.rb +19 -0
  15. data/lib/ruht/attributes/content_attribute.rb +21 -0
  16. data/lib/ruht/attributes/data_attributes.rb +20 -0
  17. data/lib/ruht/attributes/style_attribute.rb +19 -0
  18. data/lib/ruht/attributes.rb +44 -0
  19. data/lib/ruht/document.rb +42 -0
  20. data/lib/ruht/element.rb +29 -0
  21. data/lib/ruht/file.rb +11 -0
  22. data/lib/ruht/fragment.rb +52 -0
  23. data/lib/ruht/html.rb +18 -0
  24. data/lib/ruht/tags.rb +74 -0
  25. data/lib/ruht/version.rb +5 -0
  26. data/lib/ruht/void_element.rb +17 -0
  27. data/lib/ruht.rb +12 -0
  28. data/ruht.gemspec +41 -0
  29. data/sig/ruht/attributes/boolean_attribute.rbs +10 -0
  30. data/sig/ruht/attributes/class_attribute.rbs +7 -0
  31. data/sig/ruht/attributes/content_attribute.rbs +14 -0
  32. data/sig/ruht/attributes/data_attributes.rbs +16 -0
  33. data/sig/ruht/attributes/style_attribute.rbs +7 -0
  34. data/sig/ruht/attributes.rbs +13 -0
  35. data/sig/ruht/document.rbs +12 -0
  36. data/sig/ruht/element.rbs +14 -0
  37. data/sig/ruht/file.rbs +3 -0
  38. data/sig/ruht/fragment.rbs +19 -0
  39. data/sig/ruht/html.rbs +3 -0
  40. data/sig/ruht/tags.rbs +11 -0
  41. data/sig/ruht/version.rbs +3 -0
  42. data/sig/ruht/void_element.rbs +9 -0
  43. data/sig/ruht.rbs +2 -0
  44. metadata +90 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 778e8a28e69f33686442343e199b4aa9fbe3f295008a9d3bcc16c743c03d9358
4
+ data.tar.gz: 7fbb6762ef0e55c160b17542b4d872895c35f673488d3dd398ef69adf090a185
5
+ SHA512:
6
+ metadata.gz: 12283a4a5a1a3ef88c7037bd143cd8842b46bdce0ca53ebadb5e55d1b7bf7a86879ff289c33e6469ee095651f7e6d16fd78cb8058818858b6db30fffe4bdc011
7
+ data.tar.gz: 3b9143c7d17fe9ff91fb5f3e4e7e02fdb366c03687d190ee436a7aa132f04059da092975919b5a6b94a6e97c58c18ff7da6653160e3cd2a37b35bacf3e237e91
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.7
7
+ NewCops: enable
8
+ Exclude:
9
+ - 'bin/*'
10
+ - 'vendor/bundle/**/*'
11
+
12
+ Layout/LineLength:
13
+ Max: 80
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.0.1] - 2023-02-05
2
+
3
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'rake', '~> 13.0'
8
+
9
+ group :development do
10
+ gem 'rubocop', '~> 1.44.1', require: false
11
+ gem 'rubocop-rake', require: false
12
+ gem 'rubocop-rspec', require: false
13
+
14
+ gem 'steep', require: false
15
+ gem 'typeprof', require: false
16
+ end
17
+
18
+ group :test do
19
+ gem 'rspec', '~> 3.0'
20
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,114 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruht (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (7.0.4.2)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ ast (2.4.2)
15
+ concurrent-ruby (1.2.0)
16
+ csv (3.2.6)
17
+ diff-lcs (1.5.0)
18
+ ffi (1.15.5)
19
+ fileutils (1.7.0)
20
+ i18n (1.12.0)
21
+ concurrent-ruby (~> 1.0)
22
+ json (2.6.3)
23
+ language_server-protocol (3.17.0.3)
24
+ listen (3.8.0)
25
+ rb-fsevent (~> 0.10, >= 0.10.3)
26
+ rb-inotify (~> 0.9, >= 0.9.10)
27
+ logger (1.5.3)
28
+ minitest (5.17.0)
29
+ parallel (1.22.1)
30
+ parser (3.2.0.0)
31
+ ast (~> 2.4.1)
32
+ rainbow (3.1.1)
33
+ rake (13.0.6)
34
+ rb-fsevent (0.11.2)
35
+ rb-inotify (0.10.1)
36
+ ffi (~> 1.0)
37
+ rbs (2.8.4)
38
+ regexp_parser (2.6.2)
39
+ rexml (3.2.5)
40
+ rspec (3.12.0)
41
+ rspec-core (~> 3.12.0)
42
+ rspec-expectations (~> 3.12.0)
43
+ rspec-mocks (~> 3.12.0)
44
+ rspec-core (3.12.1)
45
+ rspec-support (~> 3.12.0)
46
+ rspec-expectations (3.12.2)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.12.0)
49
+ rspec-mocks (3.12.3)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.12.0)
52
+ rspec-support (3.12.0)
53
+ rubocop (1.44.1)
54
+ json (~> 2.3)
55
+ parallel (~> 1.10)
56
+ parser (>= 3.2.0.0)
57
+ rainbow (>= 2.2.2, < 4.0)
58
+ regexp_parser (>= 1.8, < 3.0)
59
+ rexml (>= 3.2.5, < 4.0)
60
+ rubocop-ast (>= 1.24.1, < 2.0)
61
+ ruby-progressbar (~> 1.7)
62
+ unicode-display_width (>= 2.4.0, < 3.0)
63
+ rubocop-ast (1.24.1)
64
+ parser (>= 3.1.1.0)
65
+ rubocop-capybara (2.17.0)
66
+ rubocop (~> 1.41)
67
+ rubocop-rake (0.6.0)
68
+ rubocop (~> 1.0)
69
+ rubocop-rspec (2.18.1)
70
+ rubocop (~> 1.33)
71
+ rubocop-capybara (~> 2.17)
72
+ ruby-progressbar (1.11.0)
73
+ securerandom (0.2.2)
74
+ steep (1.3.0)
75
+ activesupport (>= 5.1)
76
+ csv (>= 3.0.9)
77
+ fileutils (>= 1.1.0)
78
+ json (>= 2.1.0)
79
+ language_server-protocol (>= 3.15, < 4.0)
80
+ listen (~> 3.0)
81
+ logger (>= 1.3.0)
82
+ parallel (>= 1.0.0)
83
+ parser (>= 3.1)
84
+ rainbow (>= 2.2.2, < 4.0)
85
+ rbs (>= 2.8.0)
86
+ securerandom (>= 0.1)
87
+ strscan (>= 1.0.0)
88
+ terminal-table (>= 2, < 4)
89
+ strscan (3.0.5)
90
+ terminal-table (3.0.2)
91
+ unicode-display_width (>= 1.1.1, < 3)
92
+ typeprof (0.21.4)
93
+ rbs (>= 1.8.1)
94
+ tzinfo (2.0.6)
95
+ concurrent-ruby (~> 1.0)
96
+ unicode-display_width (2.4.2)
97
+
98
+ PLATFORMS
99
+ arm64-darwin-21
100
+ ruby
101
+ x86_64-linux
102
+
103
+ DEPENDENCIES
104
+ rake (~> 13.0)
105
+ rspec (~> 3.0)
106
+ rubocop (~> 1.44.1)
107
+ rubocop-rake
108
+ rubocop-rspec
109
+ ruht!
110
+ steep
111
+ typeprof
112
+
113
+ BUNDLED WITH
114
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Ruby Hyper Text https://github.com/ruby-hyper-text
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Ruht
2
+
3
+ Ruht generates HTML using Ruby language.
4
+ `ruht` stands for RUby Hyper Text or RUby HTml.
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ ```bash
11
+ bundle add ruht
12
+ ```
13
+
14
+ If bundler is not being used to manage dependencies, install the gem by executing:
15
+
16
+ ```bash
17
+ gem install ruht
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ To generate HTML code right away you can use `Ruht.html` method.
23
+
24
+ ```ruby
25
+ require 'ruht'
26
+
27
+ Ruht.html do
28
+ doctype :html
29
+ html do
30
+ head lang: :en do
31
+ # ...
32
+ end
33
+
34
+ body do
35
+ # ...
36
+ end
37
+ end
38
+ end
39
+ #=>
40
+ # <!DOCTYPE html>
41
+ # <html lang="en">
42
+ # <head>
43
+ # ...
44
+ # </head>
45
+ # <body>
46
+ # ...
47
+ # </body>
48
+ # </html>
49
+ ```
50
+
51
+ More examples can be found in [ruht-example repository][1].
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org][2].
58
+
59
+ ### Running type checks in GitHub Actions
60
+
61
+ If you want to run Steep and RBS in CI add `[steep]` and `[rbs]` respectfully
62
+ to a commit message. You can run them locally using `bin/steep check` and
63
+ `bin/rbs`.
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome at [our GitHub repository][3].
68
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct][4].
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License][5].
73
+
74
+ ## Code of Conduct
75
+
76
+ Everyone interacting in the Ruht project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct][4].
77
+
78
+ [1]: https://github.com/ruby-hyper-text/ruht-example
79
+ [2]: https://rubygems.org
80
+ [3]: https://github.com/ruby-hyper-text/ruht
81
+ [4]: https://github.com/ruby-hyper-text/ruht/blob/main/CODE_OF_CONDUCT.md
82
+ [5]: https://opensource.org/licenses/MIT
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:rspec)
7
+
8
+ require 'rubocop/rake_task'
9
+ RuboCop::RakeTask.new
10
+
11
+ task default: %i[rspec]
data/Steepfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ D = Steep::Diagnostic
4
+
5
+ target :lib do
6
+ signature 'sig', 'sig-private'
7
+
8
+ check 'lib'
9
+
10
+ configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
11
+ # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
12
+ configure_code_diagnostics do |hash| # You can setup everything yourself
13
+ hash[D::Ruby::NoMethod] = :information
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruht
4
+ class Attributes
5
+ # Formats HTML boolean attributes.
6
+ # Boolean attributes are the ones without values
7
+ # (e. g. <input disabled required checked readonly ...> )
8
+ class BooleanAttribute
9
+ def initialize(name)
10
+ @name = name
11
+ end
12
+
13
+ def to_s
14
+ @name.to_s
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/attributes/content_attribute'
4
+
5
+ module Ruht
6
+ class Attributes
7
+ # Formats HTML class attribute value
8
+ class ClassAttribute < ContentAttribute
9
+ protected
10
+
11
+ def value
12
+ raw = super()
13
+ return raw if raw.is_a?(String)
14
+
15
+ raw.join(' ')
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruht
4
+ class Attributes
5
+ # Generic attributes formatter
6
+ class ContentAttribute
7
+ def initialize(name, value)
8
+ @name = name
9
+ @value = value
10
+ end
11
+
12
+ def to_s
13
+ format('%<name>s="%<value>s"', name: name, value: value)
14
+ end
15
+
16
+ protected
17
+
18
+ attr_reader :name, :value
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/attributes/content_attribute'
4
+
5
+ module Ruht
6
+ class Attributes
7
+ # Formats HTML data attributes
8
+ class DataAttributes
9
+ def initialize(_name, values)
10
+ @values = values
11
+ end
12
+
13
+ def to_s
14
+ @values.map do |attribute, value|
15
+ ContentAttribute.new("data-#{attribute}", value)
16
+ end.join(' ')
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/attributes/content_attribute'
4
+
5
+ module Ruht
6
+ class Attributes
7
+ # Formats HTML style attribute value
8
+ class StyleAttribute < ContentAttribute
9
+ protected
10
+
11
+ def value
12
+ raw = super()
13
+ return raw.to_s if raw.is_a?(String)
14
+
15
+ raw.map { |property, value| "#{property}: #{value};" }.join(' ')
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/attributes/boolean_attribute'
4
+ require 'ruht/attributes/class_attribute'
5
+ require 'ruht/attributes/content_attribute'
6
+ require 'ruht/attributes/data_attributes'
7
+ require 'ruht/attributes/style_attribute'
8
+
9
+ module Ruht
10
+ # Represents HTML attributes.
11
+ # Does not validate them for existance.
12
+ class Attributes
13
+ def initialize(*boolean_attributes, **content_attributes)
14
+ @boolean_attributes = boolean_attributes
15
+ @content_attributes = content_attributes
16
+ end
17
+
18
+ def to_s
19
+ [
20
+ *boolean_attributes,
21
+ *content_attributes
22
+ ].compact.join(' ').strip
23
+ end
24
+
25
+ private
26
+
27
+ def boolean_attributes
28
+ @boolean_attributes.map do |attr_name|
29
+ BooleanAttribute.new(attr_name)
30
+ end
31
+ end
32
+
33
+ def content_attributes
34
+ @content_attributes.map do |attr_name, attr_value|
35
+ case attr_name.to_sym
36
+ when :class then ClassAttribute.new(attr_name, attr_value)
37
+ when :data then DataAttributes.new(attr_name, attr_value)
38
+ when :style then StyleAttribute.new(attr_name, attr_value)
39
+ else ContentAttribute.new(attr_name, attr_value)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/attributes'
4
+ require 'ruht/element'
5
+ require 'ruht/void_element'
6
+
7
+ module Ruht
8
+ # Represents an HTML document
9
+ # Document.new do
10
+ # doctype :html
11
+ # html do
12
+ # ...
13
+ # end
14
+ # end.to_s
15
+ #=>
16
+ # <!DOCTYPE html>
17
+ # <html>
18
+ # ...
19
+ # </html>
20
+ class Document
21
+ def initialize(&child_block)
22
+ @doctype = nil
23
+ @html = nil
24
+ @child_block = child_block
25
+ end
26
+
27
+ def doctype(*args)
28
+ @doctype = Ruht::VoidElement.new('!DOCTYPE', Ruht::Attributes.new(*args))
29
+ end
30
+
31
+ def html(*args, **kwargs, &block)
32
+ @html = Ruht::Element.new(
33
+ :html, Ruht::Attributes.new(*args, **kwargs), &block
34
+ )
35
+ end
36
+
37
+ def to_s
38
+ instance_eval(&@child_block)
39
+ [@doctype, @html].compact.join("\n")
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/fragment'
4
+
5
+ module Ruht
6
+ # Represents an HTML element / node
7
+ class Element < Fragment
8
+ def initialize(tag_name, attributes, &child_block)
9
+ @tag_name = tag_name
10
+ @attributes = attributes
11
+ super(&child_block)
12
+ end
13
+
14
+ # There is no such thing as self closing tags. E.g. <img /> or <script />.
15
+ # https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
16
+ def to_s
17
+ children = super() # calls Fragment#to_s
18
+ opening_tag = [@tag_name, @attributes].join(' ').strip
19
+ closing_tag = "/#{@tag_name}"
20
+ return "<#{opening_tag}><#{closing_tag}>" if children.empty?
21
+
22
+ [
23
+ "<#{opening_tag}>",
24
+ children,
25
+ "<#{closing_tag}>"
26
+ ].join("\n").strip
27
+ end
28
+ end
29
+ end
data/lib/ruht/file.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Allows to render Ruby files
4
+ module Ruht
5
+ def self.file(path = nil, &block)
6
+ full_path = File.expand_path(path || block.call)
7
+ html do
8
+ instance_eval(File.read(full_path))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/tags'
4
+
5
+ module Ruht
6
+ # Represents a piece of HTML (a bunch of siblings-tags).
7
+ # It has no name nor attributes. Not being rendered itself,
8
+ # only renders its children.
9
+ # Fragment.new do
10
+ # input
11
+ # p
12
+ # a
13
+ #=>
14
+ # <input>
15
+ # <p />
16
+ # <a />
17
+ class Fragment
18
+ include Tags
19
+
20
+ def initialize(&child_block)
21
+ @children = []
22
+ @evaluated = false
23
+ @child_block = child_block
24
+ end
25
+
26
+ # Allows to render other fragments / elements inside
27
+ def render!(child_node)
28
+ @children << child_node
29
+ end
30
+
31
+ def to_s
32
+ eval_children!
33
+ @children.join("\n").strip
34
+ end
35
+
36
+ private
37
+
38
+ def eval_children!
39
+ return if @evaluated || @child_block.nil?
40
+
41
+ result = instance_eval(&@child_block)
42
+ render!(result) if can_render?(result)
43
+ @evaluated = true
44
+ end
45
+
46
+ def can_render?(object)
47
+ [String, Numeric, FalseClass, TrueClass, Symbol].any? do |simple_class|
48
+ object.is_a?(simple_class)
49
+ end
50
+ end
51
+ end
52
+ end
data/lib/ruht/html.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/document'
4
+
5
+ # Entry point DSL for Ruht:
6
+ # Ruht.html do
7
+ # doctype :html
8
+ # head { ... }
9
+ # end
10
+ #=>
11
+ # <!DOCTYPE html>
12
+ # ... etc
13
+ # See Ruht::Document for details.
14
+ module Ruht
15
+ def self.html(&block)
16
+ Ruht::Document.new(&block).to_s
17
+ end
18
+ end
data/lib/ruht/tags.rb ADDED
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruht
4
+ # DSL magic here. Including this module will define methods for HTML tags.
5
+ module Tags
6
+ # https://developer.mozilla.org/en-US/docs/Web/HTML/Element
7
+ TAG_NAMES = %i[
8
+ head style title body
9
+ address article aside footer header h1 h2 h3 h4 h5 h6 main nav section
10
+ blockquote dd div dl dt figcaption figure li menu ol p pre ul
11
+ a abbr b bdi bdo cite code data dfn em i kbd mark q rp rt ruby
12
+ s samp small span strong sub sup time u var
13
+ audio map video
14
+ iframe object picture portal
15
+ svg math
16
+ canvas noscript script
17
+ del ins
18
+ caption colgroup table tbody td tfoot th thead tr
19
+ button datalist fieldset form label legend meter optgroup option
20
+ output progress select textarea
21
+ details dialog summary
22
+ slot template
23
+ ].freeze
24
+
25
+ # https://developer.mozilla.org/en-US/docs/Glossary/Void_element
26
+ VOID_TAG_NAMES = %i[
27
+ area base br col embed hr img input link meta source track wbr
28
+ ].freeze
29
+
30
+ # Must be implemented
31
+ def render!(_element)
32
+ raise NotImplementedError
33
+ end
34
+
35
+ # Allows to define new tags
36
+ module ClassMethods
37
+ def def_tag(tag_name)
38
+ define_method(tag_name) do |*args, **kwargs, &block|
39
+ require 'ruht/attributes'
40
+ attributes = Ruht::Attributes.new(*args, **kwargs)
41
+
42
+ require 'ruht/element'
43
+ element = Ruht::Element.new(tag_name, attributes, &block)
44
+ render!(element)
45
+ end
46
+ end
47
+
48
+ def def_void_tag(tag_name)
49
+ define_method(tag_name) do |*args, **kwargs|
50
+ require 'ruht/attributes'
51
+ attributes = Ruht::Attributes.new(*args, **kwargs)
52
+
53
+ require 'ruht/void_element'
54
+ element = Ruht::VoidElement.new(tag_name, attributes)
55
+ render!(element)
56
+ end
57
+ end
58
+ end
59
+
60
+ def self.included(receiver)
61
+ receiver.extend ClassMethods
62
+
63
+ receiver.instance_eval do
64
+ TAG_NAMES.each do |tag_name|
65
+ def_tag tag_name
66
+ end
67
+
68
+ VOID_TAG_NAMES.each do |tag_name|
69
+ def_void_tag tag_name
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruht
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/element'
4
+
5
+ module Ruht
6
+ # Represents elements with no closing tag and no children
7
+ class VoidElement
8
+ def initialize(tag_name, attributes)
9
+ @tag_name = tag_name
10
+ @attributes = attributes
11
+ end
12
+
13
+ def to_s
14
+ "<#{[@tag_name, @attributes].join(' ').strip}>"
15
+ end
16
+ end
17
+ end
data/lib/ruht.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruht/version'
4
+ require 'ruht/html'
5
+ require 'ruht/document'
6
+ require 'ruht/fragment'
7
+ require 'ruht/file'
8
+
9
+ # Ruht allows generating HTML in Ruby with fancy DSL.
10
+ # Refer to Ruht::Document for more info.
11
+ module Ruht
12
+ end
data/ruht.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ruht/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ruht'
7
+ spec.version = Ruht::VERSION
8
+ spec.authors = ['Dmitry Barskov']
9
+ spec.email = ['dmitry.barskov64@gmail.com']
10
+
11
+ spec.summary = 'DSL for building HTML written in Ruby'
12
+ spec.description = 'Ruht lets you generate HTML using Ruby syntax'
13
+ spec.homepage = 'https://github.com/DmitryBarskov/ruht'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.7.0'
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem
25
+ # that have been added into git.
26
+ spec.files = Dir.chdir(__dir__) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|spec)/|\.(?:git|github))})
29
+ end
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ spec.metadata['rubygems_mfa_required'] = 'true'
41
+ end
@@ -0,0 +1,10 @@
1
+ module Ruht
2
+ class Attributes
3
+ class BooleanAttribute
4
+ @name: String | Symbol
5
+
6
+ def initialize: (String | Symbol name) -> void
7
+ def to_s: -> String
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Ruht
2
+ class Attributes
3
+ class ClassAttribute < ContentAttribute
4
+ def value: -> String
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ module Ruht
2
+ class Attributes
3
+ class ContentAttribute
4
+ @name: String | Symbol
5
+ @value: untyped
6
+
7
+ def initialize: (String | Symbol name, untyped value) -> void
8
+ def to_s: -> String
9
+
10
+ attr_reader name: String | Symbol
11
+ attr_reader value: untyped
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ # TypeProf 0.21.4
2
+
3
+ # Classes
4
+ module Ruht
5
+ class Attributes
6
+ class DataAttributes
7
+ @values: Hash[String | Symbol, String | Symbol | bool | Numeric]
8
+
9
+ def initialize: (
10
+ void _name,
11
+ Hash[String | Symbol, String | Symbol | bool | Numeric] values
12
+ ) -> void
13
+ def to_s: -> String
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Ruht
2
+ class Attributes
3
+ class StyleAttribute < ContentAttribute
4
+ def value: -> untyped
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module Ruht
2
+ class Attributes
3
+ @boolean_attributes: untyped
4
+ @content_attributes: untyped
5
+
6
+ def initialize: (*untyped boolean_attributes, **untyped) -> void
7
+ def to_s: -> String
8
+
9
+ private
10
+ def boolean_attributes: -> Array[Ruht::Attributes::BooleanAttribute]
11
+ def content_attributes: -> Array[ContentAttribute | DataAttributes]
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Ruht
2
+ class Document
3
+ @doctype: VoidElement?
4
+ @html: Element?
5
+ @child_block: (^(self) -> untyped)
6
+
7
+ def initialize: { (self) -> untyped } -> void
8
+ def doctype: (*Symbol | String args) ?{ () -> untyped } -> VoidElement
9
+ def html: (*Symbol args, **untyped kwargs) -> Element
10
+ def to_s: -> String
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Ruht
2
+ class Element < Fragment
3
+ @tag_name: Symbol
4
+ @attributes: Ruht::Attributes | nil
5
+ @children: Array[untyped]
6
+
7
+ def initialize: (
8
+ Symbol tag_name,
9
+ Ruht::Attributes | nil attributes
10
+ ) ?{ (self) -> untyped } -> void
11
+
12
+ def to_s: -> String
13
+ end
14
+ end
data/sig/ruht/file.rbs ADDED
@@ -0,0 +1,3 @@
1
+ module Ruht
2
+ def self.file: (?nil path) -> String
3
+ end
@@ -0,0 +1,19 @@
1
+ module Ruht
2
+ class Fragment
3
+ extend Tags::ClassMethods
4
+ include Tags
5
+
6
+ @children: Array[untyped]
7
+ @evaluated: bool
8
+ @child_block: (^(self) -> untyped | nil)
9
+
10
+ def initialize: () ?{ (self) -> untyped } -> void
11
+ def render!: (String | Fragment | Element | Symbol child_node) -> void
12
+ def to_s: -> String
13
+
14
+ private
15
+
16
+ def eval_children!: -> void
17
+ def can_render?: (untyped object) -> bool
18
+ end
19
+ end
data/sig/ruht/html.rbs ADDED
@@ -0,0 +1,3 @@
1
+ module Ruht
2
+ def self.html: -> String
3
+ end
data/sig/ruht/tags.rbs ADDED
@@ -0,0 +1,11 @@
1
+ module Ruht
2
+ module Tags
3
+ def render!: (untyped _element) -> void
4
+ def self.included: (Module receiver) -> void
5
+
6
+ module ClassMethods
7
+ def def_tag: (Symbol tag_name) -> void
8
+ def def_void_tag: (Symbol tag_name) -> void
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Ruht
2
+ VERSION: String
3
+ end
@@ -0,0 +1,9 @@
1
+ module Ruht
2
+ class VoidElement
3
+ @tag_name: Symbol | String
4
+ @attributes: Ruht::Attributes
5
+
6
+ def initialize: (Symbol | String tag_name, Ruht::Attributes attributes) -> void
7
+ def to_s: -> String
8
+ end
9
+ end
data/sig/ruht.rbs ADDED
@@ -0,0 +1,2 @@
1
+ module Ruht
2
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruht
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Barskov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruht lets you generate HTML using Ruby syntax
14
+ email:
15
+ - dmitry.barskov64@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - ".ruby-version"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - Steepfile
31
+ - lib/ruht.rb
32
+ - lib/ruht/attributes.rb
33
+ - lib/ruht/attributes/boolean_attribute.rb
34
+ - lib/ruht/attributes/class_attribute.rb
35
+ - lib/ruht/attributes/content_attribute.rb
36
+ - lib/ruht/attributes/data_attributes.rb
37
+ - lib/ruht/attributes/style_attribute.rb
38
+ - lib/ruht/document.rb
39
+ - lib/ruht/element.rb
40
+ - lib/ruht/file.rb
41
+ - lib/ruht/fragment.rb
42
+ - lib/ruht/html.rb
43
+ - lib/ruht/tags.rb
44
+ - lib/ruht/version.rb
45
+ - lib/ruht/void_element.rb
46
+ - ruht.gemspec
47
+ - sig/ruht.rbs
48
+ - sig/ruht/attributes.rbs
49
+ - sig/ruht/attributes/boolean_attribute.rbs
50
+ - sig/ruht/attributes/class_attribute.rbs
51
+ - sig/ruht/attributes/content_attribute.rbs
52
+ - sig/ruht/attributes/data_attributes.rbs
53
+ - sig/ruht/attributes/style_attribute.rbs
54
+ - sig/ruht/document.rbs
55
+ - sig/ruht/element.rbs
56
+ - sig/ruht/file.rbs
57
+ - sig/ruht/fragment.rbs
58
+ - sig/ruht/html.rbs
59
+ - sig/ruht/tags.rbs
60
+ - sig/ruht/version.rbs
61
+ - sig/ruht/void_element.rbs
62
+ homepage: https://github.com/DmitryBarskov/ruht
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ allowed_push_host: https://rubygems.org
67
+ homepage_uri: https://github.com/DmitryBarskov/ruht
68
+ source_code_uri: https://github.com/DmitryBarskov/ruht
69
+ changelog_uri: https://github.com/DmitryBarskov/ruht/blob/main/CHANGELOG.md
70
+ rubygems_mfa_required: 'true'
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.7.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.4.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: DSL for building HTML written in Ruby
90
+ test_files: []