Skip to content

Instantly share code, notes, and snippets.

@tka
Last active December 15, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tka/5302996 to your computer and use it in GitHub Desktop.
Save tka/5302996 to your computer and use it in GitHub Desktop.
Fire.app Jade http://jade-lang.com/ handler
require 'open3'
class JadeHandler
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] =~ /\/$/
env["PATH_INFO"] += "index.jade"
end
if env["PATH_INFO"] =~ /\.jade$/
path = env["PATH_INFO"][1..-1]
body = Open3.popen3('jade --path .') do |stdin, stdout, stderr|
template = open(path,'r'){|f| f.read}
stdin.write template
stdin.close
stdout.read + stderr.read.gsub(/\n/, '<br>')
end
[200, {"Content-Type" => "text/html"}, [body]]
else
status, headers, body = @app.call(env)
[status, headers, body]
end
end
end
use JadeHandler
https://gist.github.com/hlb/5581919
class JadeHandler
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] =~ /\/$/
env["PATH_INFO"] += "index.jade"
end
if env["PATH_INFO"] =~ /\.jade$/
path = env["PATH_INFO"][1..-1]
body = %x{jade --path . < #{path} }
[200, {"Content-Type" => "text/html"}, [body]]
else
status, headers, body = @app.call(env)
[status, headers, body]
end
end
end
use JadeHandler
@kenvunz
Copy link

kenvunz commented Apr 18, 2013

hi

how do you make it to convert the .jade file to .html?

@caasi
Copy link

caasi commented Jun 30, 2013

require 'open3'

class JadeHandler 
  def initialize(app)  
    @app = app  
  end  

  def call(env) 
    if env["PATH_INFO"] =~ /\/$/
      env["PATH_INFO"] += "index.jade"
    end

    if env["PATH_INFO"] =~ /\.jade$/
      path =  env["PATH_INFO"][1..-1]
      base = File.basename(path)
      dir = File.dirname(path)
      body = nil

      Dir.chdir(dir) do
        body = Open3.popen3('jade --path .') do |stdin, stdout, stderr|
          template = open(base,'r'){|f| f.read}

          stdin.write template
          stdin.close

          stdout.read + stderr.read.gsub(/\n/, '<br>')
        end
      end
      [200, {"Content-Type" => "text/html"}, [body]]
    else
      status, headers, body = @app.call(env)
      [status, headers, body]
    end 
  end  
end

use JadeHandler

added block and include support for linux version

@ETBlue
Copy link

ETBlue commented Oct 18, 2013

require 'open3'

class JadeHandler 
  def initialize(app)  
    @app = app  
  end  

  def call(env) 
    if env["PATH_INFO"] =~ /\/$/
      env["PATH_INFO"] += "index.jade"
    end

    if env["PATH_INFO"] =~ /\.jade$/
      path =  env["PATH_INFO"][1..-1]
      base = File.basename(path)
      dir = File.dirname(path) + "/src"
      body = nil

      Dir.chdir(dir) do
        body = Open3.popen3('node c:/users/etblue/appdata/roaming/npm/node_modules/jade/bin/jade --path .') do |stdin, stdout, stderr|
          template = open(base,'r'){|f| f.read}

          stdin.write template
          stdin.close

          stdout.read + stderr.read.gsub(/\n/, '<br>')
        end
      end
      [200, {"Content-Type" => "text/html"}, [body]]
    else
      status, headers, body = @app.call(env)
      [status, headers, body]
    end 
  end  
end

use JadeHandler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment