jadeite 0.0.2 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -25,3 +25,4 @@ spec/reports
25
25
  test/tmp
26
26
  test/version_tmp
27
27
  tmp
28
+ .jadeite-cache
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,68 @@
1
+ require 'benchmark'
2
+ require '../lib/jadeite'
3
+ require '../lib/jadeite/environment'
4
+
5
+ TEMPLATES = {
6
+ :form => {
7
+ :user => {}
8
+ },
9
+ :"include-mixin" => {
10
+
11
+ },
12
+ :includes => {
13
+
14
+ }
15
+ }
16
+ OPTIONS = [
17
+ {
18
+ cache: true
19
+ },
20
+ {
21
+ cache: false
22
+ }
23
+ ]
24
+
25
+ def measure(times, title, &blk)
26
+ puts
27
+ puts "=== #{title} - #{times} times each"
28
+ OPTIONS.each do |opts|
29
+ puts "--- Options: #{opts}"
30
+ TEMPLATES.each do |name, data|
31
+ file = "./templates/#{name}.jade"
32
+ puts "=> #{name} (#{file})"
33
+ blk.call(times, file, data, opts)
34
+ puts
35
+ end
36
+ end
37
+ end
38
+
39
+ measure 10000, "Render precompiled templates" do |times, file, data, options|
40
+ puts Benchmark.measure {
41
+ env = Jadeite::Environment.new(options)
42
+ compiled = env.compile_file(file)
43
+ times.times do
44
+ compiled.render data
45
+ end
46
+ }
47
+ end
48
+
49
+ measure 10000, "Compile and render" do |times, file, data|
50
+ puts Benchmark.measure {
51
+ env = Jadeite::Environment.new
52
+ times.times do
53
+ compiled = env.compile_file(file)
54
+ compiled.render data
55
+ end
56
+ }
57
+ end
58
+
59
+ measure 10, "Initialize environment, compile and render" do |times, file, data|
60
+ puts Benchmark.measure {
61
+ times.times do
62
+ env = Jadeite::Environment.new
63
+
64
+ compiled = env.compile_file(file)
65
+ compiled.render data
66
+ end
67
+ }
68
+ end
@@ -0,0 +1,3 @@
1
+ <script>
2
+ console.log("foo\nbar")
3
+ </script>
@@ -0,0 +1,6 @@
1
+ html
2
+ head
3
+ title My Application
4
+ block head
5
+ body
6
+ block content
@@ -0,0 +1,3 @@
1
+
2
+ mixin foo()
3
+ p bar
@@ -0,0 +1 @@
1
+ <p>:)</p>
@@ -0,0 +1,29 @@
1
+ form(method="post")
2
+ fieldset
3
+ legend General
4
+ p
5
+ label(for="user[name]") Username:
6
+ input(type="text", name="user[name]", value=user.name)
7
+ p
8
+ label(for="user[email]") Email:
9
+ input(type="text", name="user[email]", value=user.email)
10
+ .tip.
11
+ Enter a valid
12
+ email address
13
+ such as <em>tj@vision-media.ca</em>.
14
+ fieldset
15
+ legend Location
16
+ p
17
+ label(for="user[city]") City:
18
+ input(type="text", name="user[city]", value=user.city)
19
+ p
20
+ select(name="user[province]")
21
+ option(value="") -- Select Province --
22
+ option(value="AB") Alberta
23
+ option(value="BC") British Columbia
24
+ option(value="SK") Saskatchewan
25
+ option(value="MB") Manitoba
26
+ option(value="ON") Ontario
27
+ option(value="QC") Quebec
28
+ p.buttons
29
+ input(type="submit", value="Save")
@@ -0,0 +1,11 @@
1
+
2
+ extend auxiliary/layout
3
+
4
+ mixin article(title)
5
+ if title
6
+ h1= title
7
+ block
8
+
9
+ block content
10
+ +article("The meaning of life")
11
+ p Foo bar baz!
@@ -0,0 +1,7 @@
1
+ include auxiliary/mixins
2
+
3
+ +foo
4
+
5
+ body
6
+ include auxiliary/smile.html
7
+ include auxiliary/escapes.html
@@ -1,4 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
2
3
  require File.expand_path('../lib/jadeite/version', __FILE__)
3
4
 
4
5
  Gem::Specification.new do |gem|
@@ -1,6 +1,5 @@
1
- require "jadeite/version"
2
- require "jadeite/environment"
3
-
4
1
  module Jadeite
5
-
6
2
  end
3
+
4
+ require "jadeite/version"
5
+ require "jadeite/environment"
@@ -1,33 +1,85 @@
1
1
  require 'v8'
2
2
  require "nodejs"
3
+ require "fileutils"
4
+ require "digest"
3
5
 
4
6
  module Jadeite
5
7
  class Environment
6
- def initialize
7
- context = V8::Context.new
8
- context.eval("var process = {env: {}}")
9
- context.load(File.expand_path(File.join('../../../', 'node_modules/jade/runtime.js'), __FILE__))
10
8
 
11
- node_env = NodeJS::Environment.new(context, File.expand_path('../../', __FILE__))
9
+ DEFAULT_OPTIONS = {
10
+ cache: true,
11
+ cache_dir: ".jadeite-cache",
12
+ compile_options: {
13
+ client: false,
14
+ compileDebug: true
15
+ }
16
+ }.freeze
17
+
18
+ def initialize(options={})
19
+ @options = DEFAULT_OPTIONS.merge(options)
20
+
21
+ FileUtils.mkdir_p(cache_dir) if cache? and !File.directory?(cache_dir)
22
+
23
+ # Setup V8 context
24
+ @context = V8::Context.new
25
+
26
+ # Load jade-runtime
27
+ node_env = NodeJS::Environment.new(@context, File.expand_path('../../', __FILE__))
28
+ @context['jade'] = node_env.require('jade-runtime').runtime
12
29
  @jade = node_env.require('jade')
13
30
  end
14
31
 
15
32
  def compile(template_str, opts={})
16
- Template.new(@jade.compile(template_str.strip, opts))
33
+ opts = compile_options.merge(opts)
34
+ compiled = if cache?
35
+ cached = cached_read(template_str, opts) do
36
+ @jade.compile(template_str, opts.merge(client: true))
37
+ end
38
+ @context.eval(cached)
39
+ else
40
+ @jade.compile(template_str.strip, compile_options.merge(opts))
41
+ end
42
+ Template.new(compiled)
17
43
  end
18
44
 
19
45
  def render(template_str, data={}, opts={})
20
- compile(template_str, opts).render(data)
46
+ compile(template_str, compile_options.merge(opts)).render(data)
21
47
  end
22
-
48
+
23
49
  def compile_file(file, opts = {})
24
- opts.merge!(:filename => File.expand_path(file))
25
- compile(File.read(file), opts)
50
+ compile(File.read(file), opts.merge(filename: File.expand_path(file)))
26
51
  end
27
52
 
28
53
  def render_file(file, data={}, opts = {})
29
54
  compile_file(file, opts).render(data)
30
55
  end
56
+
57
+ def compile_options
58
+ @options[:compile_options]
59
+ end
60
+
61
+ def cache?
62
+ !!@options[:cache]
63
+ end
64
+
65
+ def cache_dir
66
+ @options[:cache_dir]
67
+ end
68
+
69
+ private
70
+
71
+ def cached_read(template_str, opts, &blk)
72
+ cache_file = File.join(cache_dir, "#{Digest::MD5.hexdigest("#{template_str}#{opts.inspect}")}.jade.js")
73
+ if File.exists?(cache_file)
74
+ File.read(cache_file)
75
+ else
76
+ File.open(cache_file, "w") do |f|
77
+ cached = "(#{blk.call});"
78
+ f.write(cached)
79
+ cached
80
+ end
81
+ end
82
+ end
31
83
  end
32
84
 
33
85
  private
@@ -35,9 +87,11 @@ module Jadeite
35
87
  def initialize(compiled)
36
88
  @compiled = compiled
37
89
  end
90
+
38
91
  def render(data={})
39
92
  @compiled.call data
40
93
  end
94
+
41
95
  def to_s
42
96
  @compiled.to_s
43
97
  end
@@ -1,3 +1,3 @@
1
1
  module Jadeite
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -2,14 +2,15 @@ require 'json'
2
2
  require 'pathname'
3
3
  module NodeJS
4
4
  class Environment
5
- def initialize(runtime, base_path)
6
- @runtime = runtime
5
+ def initialize(context, base_path)
6
+ @context = context
7
7
  @base_path = Pathname(base_path)
8
8
  @cache = {}
9
+ @context.eval("var process = {env: {}}")
9
10
  end
10
11
 
11
12
  def new_object
12
- @runtime['Object'].new
13
+ @context['Object'].new
13
14
  end
14
15
 
15
16
  def require(module_or_path)
@@ -22,7 +23,7 @@ module NodeJS
22
23
 
23
24
  mod = @cache[file] = Module.new(self, file)
24
25
 
25
- loader = @runtime.eval("(function(module, require, exports) {#{File.read(file)}});", file.to_s)
26
+ loader = @context.eval("(function(module, require, exports) {#{File.read(file)}});", file.to_s)
26
27
  loader.call(mod, mod.require_function, mod.exports)
27
28
  mod.exports
28
29
  end
@@ -2,6 +2,7 @@
2
2
  "name": "jadeite",
3
3
  "version": "0.0.0",
4
4
  "dependencies": {
5
+ "jade-runtime": "git://github.com/bjoerge/jade-runtime",
5
6
  "jade": "latest"
6
7
  },
7
8
  "engines": {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jadeite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-17 00:00:00.000000000 Z
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: therubyracer
@@ -72,6 +72,15 @@ files:
72
72
  - Gemfile
73
73
  - LICENSE
74
74
  - README.md
75
+ - Rakefile
76
+ - benchmarks/benchmarks.rb
77
+ - benchmarks/templates/auxiliary/escapes.html
78
+ - benchmarks/templates/auxiliary/layout.jade
79
+ - benchmarks/templates/auxiliary/mixins.jade
80
+ - benchmarks/templates/auxiliary/smile.html
81
+ - benchmarks/templates/form.jade
82
+ - benchmarks/templates/include-mixin.jade
83
+ - benchmarks/templates/includes.jade
75
84
  - extconf.rb
76
85
  - jadeite.gemspec
77
86
  - lib/jadeite.rb