comment-ripper 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Max Aller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = comment-ripper
2
+
3
+ Strip comments from your source files. Useful removing potentially sensitive files before transferring to, say, clients, while preserving some readability.
4
+ Currently only supports Javascript, and partly at that. More to come later.
5
+
6
+ == Note on Patches/Pull Requests
7
+
8
+ * Fork the project.
9
+ * Make your feature addition or bug fix.
10
+ * Add tests for it. This is important so I don't break it in a
11
+ future version unintentionally.
12
+ * Commit, do not mess with rakefile, version, or history.
13
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 Max Aller. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "comment-ripper"
8
+ gem.summary = %Q{Strip comments from your source files.}
9
+ gem.description = %Q{Strip comments from your source files. Useful removing potentially sensitive files before transferring to, say, clients, while preserving some readability.}
10
+ gem.email = "nanodeath@gmail.com"
11
+ gem.homepage = "http://github.com/nanodeath/comment-ripper"
12
+ gem.authors = ["Max Aller"]
13
+ gem.add_dependency "treetop", ">= 1.4"
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+
16
+ # This doesn't actually build a native extension, but eh, it works.
17
+ gem.extensions << "Rakefile"
18
+ gem.files = Dir['lib/grammars/**/*.tt']
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default do
40
+ # Build grammars
41
+ generated_directory = "lib/grammars/generated_parsers"
42
+ mkdir_p File.join(File.dirname(__FILE__), generated_directory)
43
+
44
+ grammars = Dir[File.expand_path(File.dirname(__FILE__) + "/lib/grammars/*.tt")]
45
+ puts "Building #{grammars.length} grammar parsers..."
46
+ grammars.each do |g|
47
+ outfile = File.join(File.dirname(__FILE__), generated_directory, File.basename(g, ".tt") + ".rb")
48
+ print "Processing grammar #{File.basename(g)}"
49
+ `tt #{g} -o #{outfile}`
50
+ puts "...Success!"
51
+ end
52
+ end
53
+
54
+ require 'rake/rdoctask'
55
+ Rake::RDocTask.new do |rdoc|
56
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
57
+
58
+ rdoc.rdoc_dir = 'rdoc'
59
+ rdoc.title = "comment-ripper #{version}"
60
+ rdoc.rdoc_files.include('README*')
61
+ rdoc.rdoc_files.include('lib/**/*.rb')
62
+ end
63
+
data/bin/rip-comments ADDED
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/ruby
2
+
3
+ begin
4
+ # Try to require the preresolved locked set of gems.
5
+ require File.expand_path('../.bundle/environment', __FILE__)
6
+ rescue LoadError
7
+ # Fall back on doing an unlocked resolve at runtime.
8
+ require "rubygems"
9
+ require "bundler"
10
+ Bundler.setup
11
+ end
12
+
13
+ Bundler.require
14
+
15
+ require 'optparse'
16
+
17
+ options = {}
18
+
19
+ $:.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib/grammars/generated_parsers"))
20
+
21
+ class Loader
22
+ def self.load(filename, options={})
23
+ raise "self.load on #{self.class} is abstract"
24
+ end
25
+ end
26
+
27
+ class UnsupportedLoader < Loader
28
+ def self.load(filename, options={})
29
+ raise "File extension #{File.extname(filename)} is not supported"
30
+ end
31
+ end
32
+
33
+ require 'singleton'
34
+ class JavascriptLoader < Loader
35
+ include Singleton
36
+
37
+ def initialize
38
+ require 'javascript'
39
+ @parser = JavascriptParser.new
40
+ end
41
+
42
+ def load(filename, options={})
43
+ require 'javascript'
44
+ File.open(filename) do |file|
45
+ input = file.read
46
+ output = @parser.parse(input)
47
+ if(output.nil?)
48
+ $stderr.puts output.failure_reason
49
+ elsif(!options.nil? && options.has_key?(:outfile))
50
+ outfile = options[:outfile]
51
+ if(outfile != filename)
52
+ File.open(outfile, 'w') {|f| f.write(output.value) }
53
+ else
54
+ $stderr.puts("Infile and outfile are the same (#{outfile}), and won't overwrite without force flag.")
55
+ end
56
+ else
57
+ puts output.value
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ loaders = Hash.new(UnsupportedLoader)
64
+ loaders[:'.js'] = JavascriptLoader
65
+
66
+ optparse = OptionParser.new do |opts|
67
+ opts.banner = "Usage: #{__FILE__} [options] file"
68
+
69
+ opts.on("-i", "--input infile1,infile2,infile3", Array, "List of files to process") do |l|
70
+ options[:input_files] = l
71
+ end
72
+
73
+ opts.on("-o", "--output outfile1,outfile2,outfile3", Array, "List of files to process") do |l|
74
+ options[:output_files] = l
75
+ end
76
+ end
77
+ optparse.parse!
78
+
79
+ if(options[:input_files].nil? ^ options[:output_files].nil?)
80
+ raise "Can't specify input files or output files and not the other"
81
+ end
82
+
83
+ if(!options[:input_files].nil?)
84
+ raise "Output list must have the same count as input list (#{options[:input_files].length} inputs, #{options[:output_files].length} outputs)" if options[:input_files].length != options[:output_files].length
85
+ until options[:input_files].empty?
86
+ infile = options[:input_files].shift
87
+ outfile = options[:output_files].shift
88
+ ext_symbol = File.extname(infile).to_sym
89
+ loaders[ext_symbol].instance.load(infile, :outfile => outfile)
90
+ end
91
+ elsif(ARGV.length == 1)
92
+ ext_symbol = File.extname(ARGV[0]).to_sym
93
+ loaders[ext_symbol].load(ARGV[0])
94
+ else
95
+ $stderr.puts "No file given and no input/output lists given."
96
+ raise optparse.banner
97
+ end
98
+
data/bin/rip-comments~ ADDED
@@ -0,0 +1,100 @@
1
+ #!/usr/bin/ruby
2
+
3
+ begin
4
+ # Try to require the preresolved locked set of gems.
5
+ require File.expand_path('../.bundle/environment', __FILE__)
6
+ rescue LoadError
7
+ # Fall back on doing an unlocked resolve at runtime.
8
+ require "rubygems"
9
+ require "bundler"
10
+ Bundler.setup
11
+ end
12
+
13
+ Bundler.require
14
+
15
+ require 'optparse'
16
+
17
+ options = {}
18
+
19
+ $:.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib/grammars/generated_parsers"))
20
+
21
+ class Loader
22
+ def self.load(filename, options={})
23
+ raise "self.load on #{self.class} is abstract"
24
+ end
25
+ end
26
+
27
+ class UnsupportedLoader < Loader
28
+ def self.load(filename, options={})
29
+ raise "File extension #{File.extname(filename)} is not supported"
30
+ end
31
+ end
32
+
33
+ require 'singleton'
34
+ class JavascriptLoader < Loader
35
+ include Singleton
36
+
37
+ def initialize
38
+ require 'javascript'
39
+ @parser = JavascriptParser.new
40
+ end
41
+
42
+ def load(filename, options={})
43
+ require 'javascript'
44
+ File.open(filename) do |file|
45
+ #p = JavascriptParser.new
46
+ input = file.read
47
+ #output = p.parse(input)
48
+ output = @@parser.parse(input)
49
+ if(output.nil?)
50
+ $stderr.puts output.failure_reason
51
+ elsif(!options.nil? && options.has_key?(:outfile))
52
+ outfile = options[:outfile]
53
+ if(outfile != filename)
54
+ File.open(outfile, 'w') {|f| f.write(output.value) }
55
+ else
56
+ $stderr.puts("Infile and outfile are the same (#{outfile}), and won't overwrite without force flag.")
57
+ end
58
+ else
59
+ puts output.value
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ loaders = Hash.new(UnsupportedLoader)
66
+ loaders[:'.js'] = JavascriptLoader
67
+
68
+ optparse = OptionParser.new do |opts|
69
+ opts.banner = "Usage: #{__FILE__} [options] file"
70
+
71
+ opts.on("-i", "--input infile1,infile2,infile3", Array, "List of files to process") do |l|
72
+ options[:input_files] = l
73
+ end
74
+
75
+ opts.on("-o", "--output outfile1,outfile2,outfile3", Array, "List of files to process") do |l|
76
+ options[:output_files] = l
77
+ end
78
+ end
79
+ optparse.parse!
80
+
81
+ if(options[:input_files].nil? ^ options[:output_files].nil?)
82
+ raise "Can't specify input files or output files and not the other"
83
+ end
84
+
85
+ if(!options[:input_files].nil?)
86
+ raise "Output list must have the same count as input list (#{options[:input_files].length} inputs, #{options[:output_files].length} outputs)" if options[:input_files].length != options[:output_files].length
87
+ until options[:input_files].empty?
88
+ infile = options[:input_files].shift
89
+ outfile = options[:output_files].shift
90
+ ext_symbol = File.extname(infile).to_sym
91
+ loaders[ext_symbol].instance.load(infile, :outfile => outfile)
92
+ end
93
+ elsif(ARGV.length == 1)
94
+ ext_symbol = File.extname(ARGV[0]).to_sym
95
+ loaders[ext_symbol].load(ARGV[0])
96
+ else
97
+ $stderr.puts "No file given and no input/output lists given."
98
+ raise optparse.banner
99
+ end
100
+
data/bin/test.js~ ADDED
@@ -0,0 +1,5 @@
1
+ //Hello!
2
+ function(){
3
+ /* Foo */
4
+ bar();
5
+ }
@@ -0,0 +1,35 @@
1
+ grammar Javascript
2
+ rule monkey
3
+ (one_line_comment / multi_line_comment / code)* {
4
+ def value
5
+ elements.map do |e|
6
+ e.value if e.respond_to? :value
7
+ end.join
8
+ end
9
+ }
10
+ end
11
+
12
+ rule one_line_comment
13
+ newline? ' '* '//' (!newline . )+ newline?
14
+ end
15
+
16
+ rule multi_line_comment
17
+ '/*' (!'*/' . )* '*/'
18
+ end
19
+
20
+ rule code
21
+ (!(one_line_comment / multi_line_comment) . )+ {
22
+ def value
23
+ text_value
24
+ end
25
+ }
26
+ end
27
+
28
+ rule newline
29
+ "\n" {
30
+ def value
31
+ text_value
32
+ end
33
+ }
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'javascript'
3
+
4
+ describe JavascriptParser do
5
+ before do
6
+ @p = JavascriptParser.new
7
+ end
8
+
9
+ it "shouldn't touch non-comment code" do
10
+ input = <<-JS
11
+ alert("cat");
12
+ JS
13
+ output = @p.parse(input).text_value
14
+ expected_output = input
15
+
16
+ output.should == expected_output
17
+ end
18
+
19
+ it "should handle single-line comments on their own line" do
20
+ input = %Q{// monkey!\nalert("cat");\n// tree!}
21
+
22
+ expected_output = %q{alert("cat");}
23
+ output = @p.parse(input)
24
+
25
+ output.value.should == expected_output
26
+ end
27
+
28
+ it "should handle single-line comments at the end of regular lines" do
29
+ input = %Q{alert("cat"); // monkey! // banana!}
30
+
31
+ expected_output = %Q{alert("cat");}
32
+ output = @p.parse(input)
33
+
34
+ output.value.should == expected_output
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ begin
2
+ # Try to require the preresolved locked set of gems.
3
+ require File.expand_path('../.bundle/environment', __FILE__)
4
+ rescue LoadError
5
+ # Fall back on doing an unlocked resolve at runtime.
6
+ require "rubygems"
7
+ require "bundler"
8
+ Bundler.setup
9
+ end
10
+
11
+ # Your application's requires come here, e.g.
12
+ # require 'date' # a ruby standard library
13
+ # require 'rack' # a bundled gem
14
+
15
+ # Alternatively, you can require all the bundled libs at once
16
+ Bundler.require(:default, :test)
17
+
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib', 'grammars', 'generated_parsers'))
21
+ require 'comment-ripper'
22
+ require 'spec'
23
+ require 'spec/autorun'
24
+
25
+ Spec::Runner.configure do |config|
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comment-ripper
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Max Aller
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-28 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: treetop
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 4
30
+ version: "1.4"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 1
42
+ - 2
43
+ - 9
44
+ version: 1.2.9
45
+ type: :development
46
+ version_requirements: *id002
47
+ description: Strip comments from your source files. Useful removing potentially sensitive files before transferring to, say, clients, while preserving some readability.
48
+ email: nanodeath@gmail.com
49
+ executables:
50
+ - rip-comments
51
+ - rip-comments~
52
+ - test.js~
53
+ extensions:
54
+ - Rakefile
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ files:
59
+ - lib/grammars/javascript.tt
60
+ - LICENSE
61
+ - README.rdoc
62
+ has_rdoc: true
63
+ homepage: http://github.com/nanodeath/comment-ripper
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.6
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Strip comments from your source files.
92
+ test_files:
93
+ - spec/comment-ripper_spec.rb
94
+ - spec/spec_helper.rb