Sha256: 14954b0db2cc35870393b4d11e35fda6e2800e6b170ed091499c493c85bfcc93

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

require 'rubygems'
require 'treetop'

require "#{File.dirname(__FILE__)}/indenting"
Treetop.load("#{File.dirname(__FILE__)}/rhtml.treetop")

module Erector
  class Erected
    
    def initialize(in_file, superklass = 'Erector::Widget', method_name = 'content')
      @in_file = in_file
      @superklass = superklass
      @method_name = method_name
    end

    def filename
      dir + basename + ".rb"
    end

    def classnames
      base = classize(basename)
      parent = File.dirname(@in_file)
      grandparent = File.dirname(parent)
      if File.basename(grandparent) == "views"
        ["Views::" + classize(File.basename(parent)) + "::" + base, @superklass]
      else
        [base, @superklass]
      end
    end

    def classname
      classnames[0]
    end
    
    def parent_class
      classnames[1]
    end

    def text
      File.read(@in_file)
    end

    def convert
      parser = RhtmlParser.new
      parsed = parser.parse(File.read(@in_file))
      if parsed.nil?
        raise "Could not parse #{@in_file}\n" +
          parser.failure_reason
      else
        File.open(filename, "w") do |f|
          f.puts("class #{classname} < #{parent_class}")
          f.puts("  def #{@method_name}")
          f.puts(parsed.set_indent(2).convert)
          f.puts("  end")
          f.puts("end")
        end
      end
    end

    protected

    def basename
      @in_file.split("/").last.gsub(/\..*$/, '')
    end

    def dir
      x = File.dirname(@in_file)
      return (x == ".") ? "" : "#{x}/"
    end

    def classize(filename)
      filename.split("_").map{|part| part.capitalize}.join
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
erector-0.10.0 lib/erector/erect/erected.rb
erector-0.9.0 lib/erector/erect/erected.rb
erector-0.9.0.pre1 lib/erector/erect/erected.rb
erector-0.8.3 lib/erector/erect/erected.rb
erector-0.8.2 lib/erector/erect/erected.rb
honkster-erector-0.8.1 lib/erector/erect/erected.rb