Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Converting all ERb views to Haml

A little script to convert all your .erb views to .haml using html2haml, which is included with Haml installation.
Just drop this in your rails root folder and run it.

class ToHaml
  def initialize(path)
    @path = path
  end
  
  def convert!
    Dir["#{@path}/**/*.erb"].each do |file|
      `html2haml -rx #{file} #{file.gsub(/\.erb$/, '.haml')}`
    end
  end
end

path = File.join(File.dirname(__FILE__), 'app', 'views')
ToHaml.new(path).convert!

abbr formatting

haml (easily transformed to css) snippet to format abbrs in a proper manner - you can still type them in ALL CAPS, but they will be displayed in Titlecase and small-caps. If you're using this, you're probably also using a custom face via @font-face, so you might want to explicitly declare the small-caps and lowercase variants of the typeface - the browser often fucks up auto-small-caps.

also, not tested in internet explorer. nothing I write is.

abbr
  :display inline-block
  :text-transform lowercase
  :font-variant small-caps
  
  &:first-letter
    :text-transform uppercase

Convert XML to Haml

This Ruby code converts a simple XML document into HAML.

require 'rexml/document'
include REXML

fruit = "<fruit><apple country='france' month='august'>tasty<gdelicious/></apple><pear></pear></fruit>"

doc_fruit = Document.new(fruit)

def xml2haml(nodex, indent ='')

  nodex.elements.each do |node|

    attributex = ' '
    buffer = indent + '%' + node.name

    if node.attributes.size > 0 then
      alist = Array.new(node.attributes.size)
      i = 0
      node.attributes.each { |x, y|
        alist[i] = " :#{x} => '#{y}'"  
        i += 1
      }
      attributex = '{' + alist.join(",")  + '} ' 
    end
 
    buffer +=  attributex + node.text if not node.text.nil?  
    puts buffer
    xml2haml(node, indent + '  ')
  end
end

xml2haml(doc_fruit)


output:
%fruit
  %apple{ :month => 'august', :country => 'france'} tasty
    %gdelicious
  %pear


Running Haml stand-alone

This Ruby code converts HAML to XHTML. code based on the example given at 'A HAML Server for Web Designers' http://urltea.com/23j8 [wiseheartdesign.com].

require 'rubygems'
require 'haml'

def parse_haml(string)
  engine = Haml::Engine.new(string)
  engine.render
end

parse_haml("#hello")


Note: The above code worked on Ubuntu 7.10, and on Gentoo I declared require 'haml/engine' as an alternative to requiring 'rubygems' and 'haml'.
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS