baren 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +61 -0
- data/lib/baren.rb +66 -0
- data/opt/Rakefile +62 -0
- data/opt/phantomjs/pjs2png.js.erb +9 -0
- metadata +99 -0
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
Baren
|
2
|
+
=====
|
3
|
+
Baren generate .png images form .pjs ([Processing.js](http://processingjs.org/) script).
|
4
|
+
|
5
|
+
- A template engine implemented in [tilt](http://github.com/rtomayko/tilt) API
|
6
|
+
- Can be a part of the asset pipeline of Rails 3.1
|
7
|
+
|
8
|
+
|
9
|
+
Usages
|
10
|
+
------
|
11
|
+
### With Rails 3.1 Asset pipeline
|
12
|
+
Add the line below in Gemfile of your Rails 3.1 app.
|
13
|
+
|
14
|
+
gem "baren"
|
15
|
+
|
16
|
+
Place a .png.pjs file or two in {your app root}/app/assets/images.
|
17
|
+
This is an example of processing.js script:
|
18
|
+
|
19
|
+
/* reddisc.png.pjs */
|
20
|
+
/* @pjs transparent=true; */
|
21
|
+
size(30, 30);
|
22
|
+
background(0, 0, 0, 0);
|
23
|
+
stroke(255, 0, 0);
|
24
|
+
fill(255, 0, 0);
|
25
|
+
ellipse(15, 15, 28, 28);
|
26
|
+
|
27
|
+
Access http://localhost:3000/assets/reddisc.png (tweak URL for your environment).
|
28
|
+
You will see a red circle, and please enjoy with [processing.js API reference](http://processingjs.org/reference).
|
29
|
+
|
30
|
+
|
31
|
+
Caveats
|
32
|
+
-------
|
33
|
+
Baren requires PhantomJS as the Javascript and Canvas engine,
|
34
|
+
but current version of baren gem only install mac version of PhantomJS.
|
35
|
+
If you use other platform, place a binary of phantomjs on {path to baren gem}/opt/phantomjs/bin/phantomjs.
|
36
|
+
I'll support Linux and Windows soon after.
|
37
|
+
|
38
|
+
|
39
|
+
Developer Notes
|
40
|
+
---------------
|
41
|
+
If you didn't install baren as a gem, you will need to install some non-gem prerequisites.
|
42
|
+
|
43
|
+
(cd baren/opt && rake)
|
44
|
+
|
45
|
+
|
46
|
+
Misc
|
47
|
+
----
|
48
|
+
If you notice a bug, or possible enhancement, join us
|
49
|
+
[Issues - hiroshi/baren - GitHub](https://github.com/hiroshi/baren/issues).
|
50
|
+
|
51
|
+
|
52
|
+
License
|
53
|
+
-------
|
54
|
+
|
55
|
+
Copyright © 2011 Hiroshi Saito.
|
56
|
+
|
57
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
58
|
+
|
59
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
60
|
+
|
61
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/baren.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "tilt"
|
2
|
+
require "sprockets"
|
3
|
+
module Baren
|
4
|
+
class ProcessingJs
|
5
|
+
def self.path
|
6
|
+
File.join(File.dirname(__FILE__), '..', 'vendor', 'assets', 'javascripts', 'processing.min.js')
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class PhantomJs
|
11
|
+
def self.path
|
12
|
+
File.join(File.dirname(__FILE__), '..', 'opt', 'phantomjs', 'bin', 'phantomjs')
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.pjs2png_path
|
16
|
+
File.join(File.dirname(__FILE__), '..', 'opt', 'phantomjs', 'pjs2png.js.erb')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class PjsTemplate < Tilt::Template
|
21
|
+
#self.default_mime_type = 'image/png'
|
22
|
+
def prepare
|
23
|
+
end
|
24
|
+
|
25
|
+
def evaluate(scope, locals, &block)
|
26
|
+
require "tempfile"
|
27
|
+
dataURL = Tempfile.open("pjs2png") do |f|
|
28
|
+
f.print Tilt.new(PhantomJs.pjs2png_path).render(nil, :pjs => data.inspect, :processingjs => File.read(ProcessingJs.path).inspect)
|
29
|
+
f.flush
|
30
|
+
cmd = "#{PhantomJs.path} #{f.path}"
|
31
|
+
`#{cmd}`
|
32
|
+
end
|
33
|
+
require "base64"
|
34
|
+
@output ||= Base64.decode64(dataURL[%r"data:image/png;base64,(.*)",1])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
#Tilt.register Baren::PjsTemplate, "pjs"
|
38
|
+
|
39
|
+
# workaround for the problem about Sprockets::DirectiveProcessor parsing binary (png)
|
40
|
+
class PossibleBinaryDirectiveProcessor < Sprockets::DirectiveProcessor
|
41
|
+
def prepare
|
42
|
+
if !data.respond_to?(:valid_encoding?) || data.valid_encoding?
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def evaluate(context, locals, &block)
|
48
|
+
if @directive_parser
|
49
|
+
super
|
50
|
+
else
|
51
|
+
data
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Railtie < Rails::Railtie
|
57
|
+
config.after_initialize do |app|
|
58
|
+
app.assets.instance_eval do
|
59
|
+
register_mime_type 'image/png', '.png'
|
60
|
+
register_engine '.pjs', Baren::PjsTemplate
|
61
|
+
register_processor 'image/png', Baren::PossibleBinaryDirectiveProcessor #Sprockets::DirectiveProcessor
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
data/opt/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# PhantomJS
|
2
|
+
case RUBY_PLATFORM
|
3
|
+
when /darwin/
|
4
|
+
phantomjs = {
|
5
|
+
:bindir => "phantomjs/bin",
|
6
|
+
:target => "phantomjs/bin/phantomjs",
|
7
|
+
:url => "http://phantomjs.googlecode.com/files/phantomjs-1.1.0-macosx-universal.dmg",
|
8
|
+
:archive => "phantomjs/phantomjs-1.1.0-macosx-universal.dmg",
|
9
|
+
:app => "phantomjs/phantomjs.app",
|
10
|
+
:volume => "/Volumes/phantomjs",
|
11
|
+
}
|
12
|
+
|
13
|
+
file phantomjs[:target] => [phantomjs[:app], File.dirname(phantomjs[:target])] do |t|
|
14
|
+
sh "(cd #{t.prerequisites[1]} && ln -s ../#{File.basename(t.prerequisites[0])}/Contents/MacOS/phantomjs phantomjs)"
|
15
|
+
end
|
16
|
+
|
17
|
+
directory File.dirname(phantomjs[:target])
|
18
|
+
|
19
|
+
file phantomjs[:app] => [phantomjs[:archive]] do |t|
|
20
|
+
sh "hdiutil attach #{t.prerequisites[0]}"
|
21
|
+
begin
|
22
|
+
sh "cp -R #{phantomjs[:volume]}/#{File.basename(t.name)} phantomjs"
|
23
|
+
ensure
|
24
|
+
sh "hdiutil detach #{phantomjs[:volume]}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
file phantomjs[:archive] do |t|
|
29
|
+
sh "(cd phantomjs && curl -O #{phantomjs[:url]})"
|
30
|
+
end
|
31
|
+
else
|
32
|
+
# http://code.google.com/p/phantomjs/downloads/list
|
33
|
+
# http://code.google.com/p/phantomjs/wiki/Installation
|
34
|
+
abort "Your RUBY_PLATFORM (#{RUBY_PLATFORM}) is not supported yet. See http://code.google.com/p/phantomjs/wiki/Installation and help us."
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
# Processing.js
|
39
|
+
processingjs = {
|
40
|
+
:url => "http://processingjs.org/content/download/processing-js-1.1.0/processing-1.1.0.min.js",
|
41
|
+
:target => "../vendor/assets/javascripts/processing.min.js"
|
42
|
+
}
|
43
|
+
|
44
|
+
file processingjs[:target] => [File.dirname(processingjs[:target])] do |t|
|
45
|
+
sh "curl -o #{t.name} #{processingjs[:url]}"
|
46
|
+
end
|
47
|
+
|
48
|
+
directory File.dirname(processingjs[:target])
|
49
|
+
|
50
|
+
|
51
|
+
# Install
|
52
|
+
desc "install PhantomJS"
|
53
|
+
task :install => [phantomjs[:target], processingjs[:target]]
|
54
|
+
|
55
|
+
desc "remove all destination files"
|
56
|
+
task :clean do |t|
|
57
|
+
victims = phantomjs.reject{|k,v| [:url, :volume].include?(k) }.values
|
58
|
+
sh "rm -Rf #{victims.join(" ")}"
|
59
|
+
end
|
60
|
+
|
61
|
+
task :default => :install
|
62
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
if (phantom.state.length === 0) {
|
2
|
+
phantom.state = 1;
|
3
|
+
phantom.content = '<html><body><script>' + <%= processingjs %> + '</script><canvas id="surface"></canvas></body></html>';
|
4
|
+
} else {
|
5
|
+
var canvas = document.getElementById("surface");
|
6
|
+
new Processing(canvas, <%= pjs %>);
|
7
|
+
console.log(canvas.toDataURL());
|
8
|
+
phantom.exit();
|
9
|
+
}
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baren
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Hiroshi Saito
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-29 00:00:00 +09:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: railties
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 1
|
31
|
+
- 0
|
32
|
+
- rc1
|
33
|
+
version: 3.1.0.rc1
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sprockets
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
- beta
|
49
|
+
- 8
|
50
|
+
version: 2.0.0.beta.8
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
description: A asset pipeline processor, and a template engine for images.
|
54
|
+
email: hiroshi3110@gmail.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions:
|
58
|
+
- opt/Rakefile
|
59
|
+
extra_rdoc_files: []
|
60
|
+
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- lib/baren.rb
|
64
|
+
- opt/Rakefile
|
65
|
+
- opt/phantomjs/pjs2png.js.erb
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/hiroshi/baren
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.3.7
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Generate .png form .pjs, processing.js script.
|
98
|
+
test_files: []
|
99
|
+
|