class KnjEruby

Public Class Methods

connect(signal, &block) click to toggle source
# File lib/knj/erb/include.rb, line 148
def self.connect(signal, &block)
  @connects[signal] = [] if !@connects[signal]
  @connects[signal] << block
end
connects() click to toggle source
# File lib/knj/erb/include.rb, line 105
def self.connects; return @connects; end
fcgi() click to toggle source
# File lib/knj/erb/include.rb, line 104
def self.fcgi; return @fcgi; end
fcgi=(newvalue) click to toggle source
# File lib/knj/erb/include.rb, line 103
def self.fcgi=(newvalue); @fcgi = newvalue; end
filepath() click to toggle source
# File lib/knj/erb/include.rb, line 144
def self.filepath
  return @filepath
end
has_status_header?() click to toggle source
# File lib/knj/erb/include.rb, line 120
def self.has_status_header?
  @headers.each do |header|
    if header[0] == "Status"
      return true
    end
  end
  
  return false
end
header(key, value) click to toggle source
# File lib/knj/erb/include.rb, line 140
def self.header(key, value)
  @headers << [key, value]
end
headers() click to toggle source
# File lib/knj/erb/include.rb, line 106
def self.headers; return @headers; end
load(filename, args = {}) click to toggle source
# File lib/knj/erb/include.rb, line 189
def self.load(filename, args = {})
  begin
    if !args[:custom_io]
      tmp_out = StringIO.new
      $stdout = tmp_out
    end
    
    ERuby.import(filename)
    
    if KnjEruby.connects["exit"]
      KnjEruby.connects["exit"].each do |block|
        block.call
      end
    end
    
    KnjEruby.printcont(tmp_out, args)
  rescue SystemExit => e
    KnjEruby.printcont(tmp_out, args)
  rescue Exception => e
    begin
      if KnjEruby.connects["error"]
        KnjEruby.connects["error"].each do |block|
          block.call(e)
        end
      end
    rescue SystemExit => e
      exit
    rescue Exception => e
      #An error occurred while trying to run the on-error-block - show this as an normal error.
      print "\n\n<pre>\n\n"
      print "<b>#{Knj::Web.html(e.class.name)}: #{Knj::Web.html(e.message)}</b>\n\n"
      
      #Lets hide all the stuff in what is not the users files to make it easier to debug.
      bt = e.backtrace
      #to = bt.length - 9
      #bt = bt[0..to]
      
      bt.each do |line|
        print Knj::Web.html(line) + "\n"
      end
      
      print "</pre>"
    end
    
    print "\n\n<pre>\n\n"
    print "<b>#{Knj::Web.html(e.class.name)}: #{Knj::Web.html(e.message)}</b>\n\n"
    
    #Lets hide all the stuff in what is not the users files to make it easier to debug.
    bt = e.backtrace
    to = bt.length - 9
    bt = bt[0..to]
    
    bt.each do |line|
      print Knj::Web.html(line) + "\n"
    end
    
    KnjEruby.printcont(tmp_out, args)
  end
end
load_return(filename, args = {}) click to toggle source
# File lib/knj/erb/include.rb, line 174
def self.load_return(filename, args = {})
  if !args[:io]
    retio = StringIO.new
    args[:io] = retio
  end
  
  @args = args
  KnjEruby.load(filename, args)
  
  if !args[:custom_io]
    retio.rewind
    return retio.read
  end
end
print_headers(args = {}) click to toggle source
printcont(tmp_out, args = {}) click to toggle source
# File lib/knj/erb/include.rb, line 153
def self.printcont(tmp_out, args = {})
  if @fcgi
    @fcgi.print self.print_headers
    tmp_out.rewind
    @fcgi.print tmp_out.read.to_s
  else
    if args[:io] and !args[:custom_io]
      old_out = $stdout
      $stdout = args[:io]
    elsif !args[:custom_io]
      $stdout = STDOUT
    end
    
    if !args[:custom_io]
      print self.print_headers if !args.key?(:with_headers) or args[:with_headers]
      tmp_out.rewind
      print tmp_out.read
    end
  end
end
reset_connects() click to toggle source
# File lib/knj/erb/include.rb, line 130
def self.reset_connects
  @connects = {}
end
reset_headers() click to toggle source
# File lib/knj/erb/include.rb, line 134
def self.reset_headers
  @headers = [
    ["Content-Type", "text/html; charset=utf-8"]
  ]
end