Hoshi

Summary

Hoshi is a library for creating real, first-class HTML/XML views. So, unlike template libraries, you can take advantage of mixins, inheritance, and all the other wonderful features of Ruby’s object system. There is also support for easy RSS feeds and CGI.

Hoshi is designed to: * Generate clean HTML/XHTML/XML with minimal effort * Be easy for a coder to use and understand * Take full advantage of Ruby’s object sytem * Be more readable and easier to write than bare HTML

It is semi-modeled after Markaby, but with a much more straightforward implementation and different semantics (e.g., no instance_eval, so scope inside a tag is as expected).

See doc/LICENSE for the license, doc/examples for examples.

Installation

You can install via rubygems,

gem install hoshi

or by downloading from github (github.com/pete/hoshi).

Usage

These examples and more featured in the fabulous doc/examples directory. Also, there is a program included called html2hoshi (and associated lib/html2hoshi.rb; see Hoshi.from_html) that takes HTML as input and converts it to Ruby code using Hoshi.

Class-based

These should be fairly straightforward:

require 'hoshi'

class Trivial < Hoshi::View :html5
  def show
    doctype
    html {
      head {
        title "Hello, world!"
        link :rel => 'stylesheet', :href => '/css/hoshi.css'
      }

      body {
        h1 "Hello, world!"
        p "This is a greeting to the world."
      }
    }
    render
  end
end

puts Trivial.new.show

You can get a little more complicated:

require 'hoshi'
require 'cgi'

module Layout
  def main_page(t)
    doctype
    html {
      head {
        title t
        script(:type => 'text/javascript') {
          raw "alert(\"Hi, I'm some javascript, I suppose.\");"
        }
      }

      body {
        h1 t, :class => 'page_title'

        yield
      }
    }
  end

  def list_page(t)
    main_page(t) {
      ul {
        yield
      }
    }
  end
end

class Fibonacci < Hoshi::View :xhtml1
  include Layout

  def list_page(n)
    super("Fibonacci: f(0)..f(#{n})") {
      fib_upto(n).map { |i| li i.to_s }
    }
    CGI.pretty(render)
  end

  private

  def fib_upto n
    a = Array.new(n) 

    0.upto(n) { |i|
      a[i] = 
        if i < 2
          1
        else
          a[i - 1] + a[i - 2]
        end
    }

    a
  end
end

puts Fibonacci.new.list_page(n)

Block-based

For simpler cases where you only intend to produce markup, perhaps for use as a templating engine.

require 'hoshi'

str = Hoshi::View::HTML5.build {
  doctype
  html {
    head {
      title "Hello, world!"
      link :rel => 'stylesheet', :href => '/css/hoshi.css'
    }

    body {
      h1 "Hello, world!"
      p "This is a greeting to the world."
    }
  }
}

puts str

Bugs

There needs to be some work done on correcting the tags; I suspect I’m missing or miscategorizing some of them.

I’d like to perhaps add a layer for serializing objects in the standard HTML5 method (i.e., the Schema.org/microdata stuff). That’s very speculative at the moment.

There are some requirements that were expedient at the time (requiring metaid and hpricot, embedding the gemspec in the Rakefile, etc.) that could use cleanup. Hash is monkey-patched, and using the new Ruby 2 refinements feature would be nicer.

Credits

Author: Pete Elmore – (pete(a)debu.gs)

Pretty heavily indebted to: _why the lucky stiff’s Markaby library

Initial design discussion: Dan Yoder

Simple block version: Nolan Darilek – (nolan(a)thewordnerd.info)

Homie that be lookin’ out for my broken deps: Lars Lethonen

The guys that paid me to do the initial version: AT&T Interactive.

Also, I guess I should credit Attractive Eighties Women (attractiveeightieswomen.com/), since I was blasting them the whole time I was developing this. Like, over and over. I couldn’t stop listening.

Home page

debu.gs/hoshi