muj 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/muj +13 -2
- data/lib/muj.rb +39 -10
- data/lib/mustache.js +5 -1
- metadata +14 -7
data/bin/muj
CHANGED
@@ -4,6 +4,8 @@ require 'optparse'
|
|
4
4
|
require 'muj'
|
5
5
|
|
6
6
|
$muj_cli_json = false
|
7
|
+
$muj_cli_views = nil
|
8
|
+
$muj_cli_layout = nil
|
7
9
|
|
8
10
|
module Muj
|
9
11
|
class CLI
|
@@ -22,6 +24,14 @@ module Muj
|
|
22
24
|
|
23
25
|
opts.on("-j","--json", "Skip YAML parser") { $muj_cli_json = true; ARGV.delete("-j"); ARGV.delete("--json") }
|
24
26
|
|
27
|
+
opts.on('-v', '--views DIR', 'views directory') do |dir|
|
28
|
+
$muj_cli_views = dir
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on('-l', '--layout NAME', 'layout name (should be in views dir use > instead of / for subdir)') do |name|
|
32
|
+
$muj_cli_layout = name
|
33
|
+
end
|
34
|
+
|
25
35
|
opts.on_tail("-h", "--help", "Show this message") do
|
26
36
|
puts opts
|
27
37
|
exit
|
@@ -43,10 +53,11 @@ module Muj
|
|
43
53
|
data = File.open(argv.first).read
|
44
54
|
end
|
45
55
|
template = File.open(argv[1]).read
|
56
|
+
opts = {'views' => $muj_cli_views, 'layout' => $muj_cli_layout}
|
46
57
|
if $muj_cli_json
|
47
|
-
puts Muj.render_with_json(template,data)
|
58
|
+
puts Muj.render_with_json(template,data,opts)
|
48
59
|
else
|
49
|
-
puts Muj.render_with_yaml(template,data)
|
60
|
+
puts Muj.render_with_yaml(template,data,opts)
|
50
61
|
end
|
51
62
|
end
|
52
63
|
end
|
data/lib/muj.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'yaml'
|
1
2
|
require 'tilt' unless defined? Tilt
|
2
3
|
require 'json' unless defined? JSON
|
3
4
|
|
@@ -17,25 +18,53 @@ module Tilt
|
|
17
18
|
end
|
18
19
|
|
19
20
|
module Muj
|
21
|
+
class Partials
|
22
|
+
def initialize(dir)
|
23
|
+
@dir = File.expand_path(dir)
|
24
|
+
end
|
25
|
+
|
26
|
+
def __exists(name)
|
27
|
+
fname = name.gsub('>','/')
|
28
|
+
File.exists?("#{@dir}/#{fname}.muj")
|
29
|
+
end
|
30
|
+
|
31
|
+
def __read(name)
|
32
|
+
fname = name.gsub('>','/')
|
33
|
+
f = File.open("#{@dir}/#{fname}.muj")
|
34
|
+
r = f.read.chomp
|
35
|
+
f.close
|
36
|
+
r
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.escape_template(v)
|
41
|
+
v.chomp.gsub("\n",'\n\\').gsub('"','\"')
|
42
|
+
end
|
20
43
|
|
21
|
-
def self.eval(
|
44
|
+
def self.eval(str,json,opts={})
|
22
45
|
require 'v8' unless defined? ::V8
|
23
46
|
cxt = ::V8::Context.new
|
24
|
-
cxt.eval('var locals='+json+'; for(var attrname in locals) {this[attrname] = locals[attrname]; }');
|
25
47
|
cxt.load(File.dirname(__FILE__)+"/mustache.js")
|
26
|
-
|
48
|
+
opts['views'] = '.' unless opts['views']
|
49
|
+
cxt['partials'] = Muj::Partials.new(opts['views'])
|
50
|
+
cxt.eval("var locals = #{json};")
|
51
|
+
r = cxt.eval(%Q{locals.__yield__ = Mustache.to_html("#{Muj.escape_template(str)}", locals, partials);})
|
52
|
+
if opts['layout']
|
53
|
+
layout = cxt['partials'].__read(opts['layout'])
|
54
|
+
r = cxt.eval(%Q{Mustache.to_html("#{Muj.escape_template(layout)}", locals, partials);})
|
55
|
+
end
|
56
|
+
r
|
27
57
|
end
|
28
58
|
|
29
|
-
def self.render(str,locals={})
|
30
|
-
self.eval(str,locals.to_json)
|
59
|
+
def self.render(str,locals={},opts={})
|
60
|
+
self.eval(str,locals.to_json,opts)
|
31
61
|
end
|
32
62
|
|
33
|
-
def self.render_with_json(str,json)
|
34
|
-
self.eval(str,json)
|
63
|
+
def self.render_with_json(str,json,opts={})
|
64
|
+
self.eval(str,json,opts)
|
35
65
|
end
|
36
66
|
|
37
|
-
def self.render_with_yaml(str,yml)
|
38
|
-
|
39
|
-
self.render(str,YAML.load(yml))
|
67
|
+
def self.render_with_yaml(str,yml,opts={})
|
68
|
+
self.render(str,YAML::load(yml),opts)
|
40
69
|
end
|
41
70
|
end
|
data/lib/mustache.js
CHANGED
@@ -107,7 +107,11 @@ var Mustache = function() {
|
|
107
107
|
render_partial: function(name, context, partials) {
|
108
108
|
name = this.trim(name);
|
109
109
|
if(!partials || partials[name] === undefined) {
|
110
|
-
|
110
|
+
if (partials && partials['__exists'] && partials.__exists(name)) {
|
111
|
+
partials[name] = partials.__read(name);
|
112
|
+
} else {
|
113
|
+
throw({message: "unknown_partial '" + name + "'"});
|
114
|
+
}
|
111
115
|
}
|
112
116
|
if(typeof(context[name]) != "object") {
|
113
117
|
return this.render(partials[name], context, partials, true);
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: muj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Foy Savas
|
@@ -14,16 +15,17 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2011-11-
|
18
|
-
default_executable:
|
18
|
+
date: 2011-11-28 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: tilt
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
24
25
|
requirements:
|
25
26
|
- - ">="
|
26
27
|
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
27
29
|
segments:
|
28
30
|
- 0
|
29
31
|
version: "0"
|
@@ -33,9 +35,11 @@ dependencies:
|
|
33
35
|
name: therubyracer
|
34
36
|
prerelease: false
|
35
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
36
39
|
requirements:
|
37
40
|
- - ">="
|
38
41
|
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
39
43
|
segments:
|
40
44
|
- 0
|
41
45
|
version: "0"
|
@@ -54,7 +58,6 @@ files:
|
|
54
58
|
- lib/muj.rb
|
55
59
|
- lib/mustache.js
|
56
60
|
- bin/muj
|
57
|
-
has_rdoc: true
|
58
61
|
homepage: http://github.com/foysavas/muj
|
59
62
|
licenses: []
|
60
63
|
|
@@ -64,23 +67,27 @@ rdoc_options: []
|
|
64
67
|
require_paths:
|
65
68
|
- lib
|
66
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
67
71
|
requirements:
|
68
72
|
- - ">="
|
69
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
70
75
|
segments:
|
71
76
|
- 0
|
72
77
|
version: "0"
|
73
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
74
80
|
requirements:
|
75
81
|
- - ">="
|
76
82
|
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
77
84
|
segments:
|
78
85
|
- 0
|
79
86
|
version: "0"
|
80
87
|
requirements: []
|
81
88
|
|
82
89
|
rubyforge_project:
|
83
|
-
rubygems_version: 1.
|
90
|
+
rubygems_version: 1.8.11
|
84
91
|
signing_key:
|
85
92
|
specification_version: 3
|
86
93
|
summary: Muj is Mustache.js for Ruby and your command line.
|