https://github.com/jonathanwthom/ruby_html
Build HTML with Ruby
https://github.com/jonathanwthom/ruby_html
Last synced: 14 days ago
JSON representation
Build HTML with Ruby
- Host: GitHub
- URL: https://github.com/jonathanwthom/ruby_html
- Owner: JonathanWThom
- License: mit
- Created: 2018-05-04T06:37:08.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-11T04:46:19.000Z (about 8 years ago)
- Last Synced: 2025-09-17T08:45:30.640Z (10 months ago)
- Language: Ruby
- Homepage:
- Size: 103 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Ruby HTML
An HTML document builder in Ruby.
### Installation
Add this line to your application's Gemfile:
```ruby
gem "ruby_html"
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install ruby_html
### How to Use It
HTML elements have the names you think they would. For example, to create a div:
```
div = HTML::Div.new
```
Elements can receive arguments. These can be strings, or can be other elements themselves.
These elements will be nested under their parent.
```
div = HTML::Div.new("Hello", HTML::P.new("World"))
```
You can also add new elements to a previously created element with `#add_elements`, which accepts any number of arguments.
```
div.add_elements("Hello")
div.add_elements("Hello", "From", HTML::H3.new("Earth"))
```
You can pass the same element as an argument as many times as you want - just like a partial or component.
All elements have a method `#render`, which prints all the element's contents (calling `.render` on all non string elements
contained within.
```
div = HTML::Div.new(HTML::H6.new("Hello"), "World")
div.render
=> "
Hello
World"
```
To add attributes to an element, pass a hash to the object like so:
```
p_with_class = HTML::P.new("Hello World", attributes: { "class": "red" })
p_with_class.render
=>
Hello World
```
Most importantly, if you want to actually create an HTML page, you'll want to create a `Document` that holds all of your elements.
A document recieves elements, as well as header elements (such as `` or ``).
```
title = HTML::Title.new("Page Title")
Document.new(HTML::Div.new, header_elements: [title])
```
`Document` has a special method, `#render_to_file` that receives a file path, and creates your html document there.
```
HTML::Document.new.render_to_file("hello_world.html")
=> creates html document
```
### TODO
- Build out remaining elements. There are a few that conflict with ruby (such as `Object`). This should not be an issue anymore
now that the `HTML` namespace has been added.
- Code clean up (alphabetical order, some methods probably don't need to be public, etc).
- The single quotes for attributes are a bit weird, and you might not always want those.
- Example in the docs of how to use with Rails, and maybe a full document built with ruby_html.
### Contributing
1. Fork it
2. Clone it
3. Make a PR.
4. Be kind to others. Programming is fun.