GithubHelp home page GithubHelp logo

ruby_html's Introduction

Ruby HTML

An HTML document builder in Ruby.

Installation

Add this line to your application's Gemfile:

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
=> "<div><h6>Hello</h6>World</div>"

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
=> <p class='red'>Hello World</p>

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 <title> or <style>).

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.

ruby_html's People

Contributors

jonathanwthom avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.