jsbeauty 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ require 'mkmf'
2
+
3
+ base_dir = File.dirname(__FILE__)
4
+
5
+ have_library("v8")
6
+
7
+ makefile = %@
8
+ all: default
9
+
10
+ default:
11
+ \tg++ -I#{base_dir} #{base_dir}/jsbeautify.cpp -lv8 -o js-beautifier
12
+
13
+ install:
14
+
15
+ @
16
+
17
+ File.open("Makefile", "w") do |f|
18
+ f.write(makefile)
19
+ end
20
+
21
+
@@ -0,0 +1,215 @@
1
+ /*
2
+ Copyright (c) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
3
+ Copyright (c) 2009 Einar Lielmanis
4
+ Copyright (c) 2010 Nicolas Ferrero <ferrero.nicolas@gmail.com>
5
+
6
+ Permission is hereby granted, free of charge, to any person
7
+ obtaining a copy of this software and associated documentation
8
+ files (the "Software"), to deal in the Software without
9
+ restriction, including without limitation the rights to use,
10
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the
12
+ Software is furnished to do so, subject to the following
13
+ conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #include <v8.h>
29
+ #include <string.h>
30
+ #include <stdlib.h>
31
+ #include "beautify.h"
32
+
33
+ #define BUF_SIZE 1024
34
+
35
+ using namespace v8;
36
+
37
+ Handle<String> readFile(const char* name)
38
+ {
39
+ FILE *file = stdin;
40
+ int len = 0;
41
+ char *buf;
42
+ Handle<String> result;
43
+
44
+ if (name)
45
+ {
46
+ file = fopen(name, "rb");
47
+
48
+ if (file == NULL)
49
+ {
50
+ return Handle<String>();
51
+ }
52
+
53
+ fseek(file, 0, SEEK_END);
54
+ len = ftell(file);
55
+ rewind(file);
56
+ buf = new char[len + 1];
57
+ buf[len] = '\0';
58
+ fread(buf, 1, len, file);
59
+ fclose(file);
60
+ result = String::New(buf);
61
+ delete[] buf;
62
+ }
63
+ else
64
+ {
65
+ char c;
66
+ buf = (char*)malloc(BUF_SIZE + 1);
67
+ int buf_size = BUF_SIZE + 1;
68
+
69
+ while ((c = getchar()) != EOF)
70
+ {
71
+ buf[len++] = c;
72
+
73
+ if (len == buf_size)
74
+ {
75
+ buf_size <<= 1;
76
+ buf = (char*)realloc(buf, buf_size);
77
+ }
78
+ }
79
+
80
+ buf[len] = '\0';
81
+ result = String::New(buf);
82
+ free(buf);
83
+ }
84
+
85
+ return result;
86
+ }
87
+
88
+ void writeFile(Handle<Value> result, const char* name)
89
+ {
90
+ if (result.IsEmpty() || result->IsUndefined())
91
+ {
92
+ return;
93
+ }
94
+
95
+ FILE* file = stdout;
96
+
97
+ if (name)
98
+ {
99
+ file = fopen(name, "wt");
100
+
101
+ if (file == NULL)
102
+ {
103
+ return;
104
+ }
105
+ }
106
+
107
+ String::Utf8Value str(result);
108
+ fprintf(file, "%s\n", *str);
109
+
110
+ if (name)
111
+ {
112
+ fclose(file);
113
+ }
114
+ }
115
+
116
+ static void usage(char* progname)
117
+ {
118
+ printf("Usage: %s [options] source-file\n", progname);
119
+ printf("[options]\n");
120
+ printf(" --overwrite : Overwrite source-file (use with care!)\n");
121
+ printf(" --indent-size or -s: Indentation size. (default 4)\n");
122
+ printf(" --indent-char or -c: Character to indent with. (default space)\n");
123
+ printf(" --disable-preserve-newlines or -d: Do not preserve existing line breaks.\n");
124
+ printf(" --indent-level or -l: Initial indentation level, you probably won't need this ever. (default 0)\n");
125
+ printf(" --space-after-anon-function or -f: Space is added between \"function ()\", otherwise the common \"function()\" output is used.\n");
126
+ printf(" --braces-on-own-line or -b: ANSI / Allman brace style, each opening/closing brace gets its own line.\n");
127
+ printf(" --keep-array-indentation or -k: Keep array indentation.\n");
128
+ printf(" --help or -h: Prints this help statement.\n");
129
+ }
130
+
131
+ int main(int argc, char* argv[])
132
+ {
133
+ Handle<String> source;
134
+ HandleScope handle_scope;
135
+ Handle<ObjectTemplate> global = ObjectTemplate::New();
136
+ Handle<ObjectTemplate> options = ObjectTemplate::New();
137
+ bool overwrite = false;
138
+ const char* output = 0;
139
+
140
+ for (int argpos = 1; argpos < argc; ++argpos)
141
+ {
142
+ if (argv[argpos][0] != '-')
143
+ {
144
+ source = readFile(argv[argpos]);
145
+ output = argv[argpos];
146
+ }
147
+ else if (strcmp(argv[argpos], "--stdin") == 0)
148
+ {
149
+ source = readFile(0);
150
+ }
151
+ else if (strcmp(argv[argpos], "--overwrite") == 0)
152
+ {
153
+ overwrite = true;
154
+ }
155
+ else if (strcmp(argv[argpos], "--indent-size") == 0 ||
156
+ strcmp(argv[argpos], "-s") == 0)
157
+ {
158
+ options->Set("indent_size", String::New(argv[argpos+1]));
159
+ }
160
+ else if (strcmp(argv[argpos], "--indent-char") == 0 ||
161
+ strcmp(argv[argpos], "-c") == 0)
162
+ {
163
+ options->Set("indent_char", String::New(argv[argpos+1]));
164
+ }
165
+ else if (strcmp(argv[argpos], "--disable-preserve-newlines") == 0 ||
166
+ strcmp(argv[argpos], "-d") == 0)
167
+ {
168
+ options->Set("preserve_newlines", Boolean::New(false));
169
+ }
170
+ else if (strcmp(argv[argpos], "--indent-level") == 0 ||
171
+ strcmp(argv[argpos], "-l") == 0)
172
+ {
173
+ options->Set("indent_level", String::New(argv[argpos+1]));
174
+ }
175
+ else if (strcmp(argv[argpos], "--space-after-anon-function") == 0 ||
176
+ strcmp(argv[argpos], "-f") == 0)
177
+ {
178
+ options->Set("space_after_anon_function", Boolean::New(true));
179
+ }
180
+ else if (strcmp(argv[argpos], "--braces-on-own-line") == 0 ||
181
+ strcmp(argv[argpos], "-b") == 0)
182
+ {
183
+ options->Set("braces_on_own_line", Boolean::New(true));
184
+ }
185
+ else if (strcmp(argv[argpos], "--keep-array-indentation") == 0 ||
186
+ strcmp(argv[argpos], "-k") == 0)
187
+ {
188
+ options->Set("keep_array_indentation", Boolean::New(true));
189
+ }
190
+ else if (strcmp(argv[argpos], "--help") == 0 ||
191
+ strcmp(argv[argpos], "-h") == 0)
192
+ {
193
+ usage(argv[0]);
194
+ return -1;
195
+ }
196
+ }
197
+
198
+ if (source.IsEmpty())
199
+ {
200
+ usage(argv[0]);
201
+ return -1;
202
+ }
203
+
204
+ global->Set("source", source);
205
+ global->Set("options", options);
206
+ Handle<Context> context = Context::New(NULL, global);
207
+ Context::Scope context_scope(context);
208
+ Handle<Script> beautifyScript = Script::Compile(String::New(beautify_code));
209
+ beautifyScript->Run();
210
+ Handle<Script> runnerScript = Script::Compile(String::New("js_beautify(source, options);"));
211
+ Handle<Value> result = runnerScript->Run();
212
+ writeFile(result, overwrite ? output : 0);
213
+ return 0;
214
+ }
215
+
@@ -0,0 +1,73 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{jsbeauty}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["David A. Cuadrado"]
12
+ s.date = %q{2011-01-17}
13
+ s.default_executable = %q{jsbeauty}
14
+ s.description = %q{read more details on http://www.sencha.com/blog/2010/10/21/make-a-javascript-formatter-with-v8-and-jsbeautifier/}
15
+ s.email = %q{krawek@gmail.com}
16
+ s.executables = ["jsbeauty"]
17
+ s.extensions = ["ext/js-beautifier/extconf.rb"]
18
+ s.extra_rdoc_files = [
19
+ "LICENSE.txt",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ ".rspec",
25
+ "Gemfile",
26
+ "Gemfile.lock",
27
+ "LICENSE.txt",
28
+ "README.rdoc",
29
+ "Rakefile",
30
+ "VERSION",
31
+ "bin/jsbeauty",
32
+ "ext/js-beautifier/beautify.h",
33
+ "ext/js-beautifier/extconf.rb",
34
+ "ext/js-beautifier/jsbeautify.cpp",
35
+ "jsbeauty.gemspec",
36
+ "lib/jsbeauty.rb",
37
+ "lib/jsbeauty/command.rb",
38
+ "spec/jsbeauty_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+ s.homepage = %q{http://github.com/dcu/jsbeauty}
42
+ s.licenses = ["MIT"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.7}
45
+ s.summary = %q{JS beautifier based on V8 engine}
46
+ s.test_files = [
47
+ "spec/jsbeauty_spec.rb",
48
+ "spec/spec_helper.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
57
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
59
+ s.add_development_dependency(%q<rcov>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
62
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
64
+ s.add_dependency(%q<rcov>, [">= 0"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
70
+ s.add_dependency(%q<rcov>, [">= 0"])
71
+ end
72
+ end
73
+
@@ -0,0 +1,4 @@
1
+ require 'jsbeauty/command'
2
+
3
+ module JSBeauty
4
+ end
@@ -0,0 +1,40 @@
1
+ module JSBeauty
2
+ class Command
3
+ PATH = File.expand_path("../../../ext/js-beautifier/js-beautifier", __FILE__)
4
+
5
+ def initialize(files, options = {})
6
+ @files = files
7
+ @options = options
8
+
9
+ if @files.empty?
10
+ raise ArgumentError, "missing file"
11
+ end
12
+ end
13
+
14
+ def run
15
+ system("#{PATH} #{options_for_cmd} #{files_for_cmd}")
16
+ end
17
+
18
+ private
19
+ def options_for_cmd
20
+ str = ""
21
+ @options.each do |opt, value|
22
+ if value == true
23
+ str << " --#{opt.to_s.gsub("_", "-")}"
24
+ elsif value.nil? || value == false
25
+ str << " "
26
+ else
27
+ str << " --#{opt.to_s.gsub("_", "-")} '#{value}'"
28
+ end
29
+ end
30
+
31
+ str
32
+ end
33
+
34
+ def files_for_cmd
35
+ @files.map do |f|
36
+ "'#{f}'"
37
+ end.join(' ')
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Jsbeauty" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'jsbeauty'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsbeauty
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - David A. Cuadrado
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-17 00:00:00 -05:00
19
+ default_executable: jsbeauty
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 2
30
+ - 3
31
+ - 0
32
+ version: 2.3.0
33
+ requirement: *id001
34
+ prerelease: false
35
+ type: :development
36
+ name: rspec
37
+ - !ruby/object:Gem::Dependency
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ requirement: *id002
50
+ prerelease: false
51
+ type: :development
52
+ name: bundler
53
+ - !ruby/object:Gem::Dependency
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 1
62
+ - 5
63
+ - 2
64
+ version: 1.5.2
65
+ requirement: *id003
66
+ prerelease: false
67
+ type: :development
68
+ name: jeweler
69
+ - !ruby/object:Gem::Dependency
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirement: *id004
80
+ prerelease: false
81
+ type: :development
82
+ name: rcov
83
+ description: read more details on http://www.sencha.com/blog/2010/10/21/make-a-javascript-formatter-with-v8-and-jsbeautifier/
84
+ email: krawek@gmail.com
85
+ executables:
86
+ - jsbeauty
87
+ extensions:
88
+ - ext/js-beautifier/extconf.rb
89
+ extra_rdoc_files:
90
+ - LICENSE.txt
91
+ - README.rdoc
92
+ files:
93
+ - .document
94
+ - .rspec
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.rdoc
99
+ - Rakefile
100
+ - VERSION
101
+ - bin/jsbeauty
102
+ - ext/js-beautifier/beautify.h
103
+ - ext/js-beautifier/extconf.rb
104
+ - ext/js-beautifier/jsbeautify.cpp
105
+ - jsbeauty.gemspec
106
+ - lib/jsbeauty.rb
107
+ - lib/jsbeauty/command.rb
108
+ - spec/jsbeauty_spec.rb
109
+ - spec/spec_helper.rb
110
+ has_rdoc: true
111
+ homepage: http://github.com/dcu/jsbeauty
112
+ licenses:
113
+ - MIT
114
+ post_install_message:
115
+ rdoc_options: []
116
+
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ requirements: []
138
+
139
+ rubyforge_project:
140
+ rubygems_version: 1.3.7
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: JS beautifier based on V8 engine
144
+ test_files:
145
+ - spec/jsbeauty_spec.rb
146
+ - spec/spec_helper.rb