code2pdf 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format d
data/Gemfile.lock CHANGED
@@ -1,17 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code2pdf (0.0.1)
4
+ code2pdf (0.2.0)
5
+ prawn (~> 0.11.1)
5
6
 
6
7
  GEM
7
8
  remote: http://rubygems.org/
8
9
  specs:
9
10
  Ascii85 (1.0.1)
11
+ diff-lcs (1.1.2)
10
12
  pdf-reader (0.9.2)
11
13
  Ascii85 (>= 0.9)
12
14
  prawn (0.11.1)
13
15
  pdf-reader (>= 0.9.0)
14
16
  ttfunk (~> 1.0.0)
17
+ rspec (2.6.0)
18
+ rspec-core (~> 2.6.0)
19
+ rspec-expectations (~> 2.6.0)
20
+ rspec-mocks (~> 2.6.0)
21
+ rspec-core (2.6.4)
22
+ rspec-expectations (2.6.0)
23
+ diff-lcs (~> 1.1.2)
24
+ rspec-mocks (2.6.0)
15
25
  ttfunk (1.0.1)
16
26
 
17
27
  PLATFORMS
@@ -19,4 +29,4 @@ PLATFORMS
19
29
 
20
30
  DEPENDENCIES
21
31
  code2pdf!
22
- prawn
32
+ rspec
data/README.textile CHANGED
@@ -12,11 +12,11 @@ h1. Usage
12
12
 
13
13
  Open a terminal and run:
14
14
 
15
- <pre><code>code2pdf <project path> <blacklist file></code></pre>
15
+ <pre><code>code2pdf <project path></code></pre>
16
16
 
17
17
  h2. BlackList file example:
18
18
 
19
- The blacklist file must be a yaml file, as such:
19
+ The blacklist file must be a yaml file, named .code2pdf and it should be located in <project path> root, containing a list of ignored files and/or directories, such as:
20
20
  <pre><code>:directories:
21
21
  - .git
22
22
  - db/migrate
data/bin/code2pdf CHANGED
@@ -4,23 +4,22 @@
4
4
  # Created by Lucas Caton at 2011, may 31
5
5
  #
6
6
 
7
- require 'optparse'
8
- require 'yaml'
9
7
  require 'rubygems'
10
- require 'prawn'
11
- require 'code2pdf/convert_to_pdf'
8
+ require 'optparse'
9
+ $:.push File.expand_path('../../lib', __FILE__)
10
+ require 'code2pdf'
12
11
 
13
12
  options = {}
14
13
 
15
14
  optparse = OptionParser.new do |opts|
16
- opts.banner = "Usage: code2pdf <project path> <blacklist file>\n\nYou can use flags as such:"
15
+ opts.banner = "Usage: code2pdf <project path>\n\nYou can use flags as such:"
17
16
 
18
17
  opts.on('-h', '--help', 'Display this screen') do
19
18
  puts opts
20
19
  exit
21
20
  end
22
21
 
23
- if ARGV.size < 2
22
+ if ARGV.size < 1
24
23
  puts opts
25
24
  exit
26
25
  end
@@ -34,58 +33,7 @@ rescue OptionParser::InvalidOption => e
34
33
  end
35
34
 
36
35
  PATH = ARGV[0].gsub(/\/$/, '')
37
-
38
- # Check if path exists
39
- if !File.exists?(PATH) || FileTest.file?(PATH)
40
- puts "'#{PATH}' path does not exist or it is not a directory."
41
- exit
42
- end
43
-
44
- BLACK_LIST_YML_FILE = ARGV[1]
45
-
46
- # Check if blacklist file exists
47
- if !File.exists?(BLACK_LIST_YML_FILE) || FileTest.directory?(BLACK_LIST_YML_FILE)
48
- puts "'#{BLACK_LIST_YML_FILE}' file does not exist or it is not a regular file."
49
- exit
50
- end
51
-
52
- BLACK_LIST = YAML.load File.read(BLACK_LIST_YML_FILE)
53
-
54
- # Check if blacklist file has directories and files keys
55
- if !BLACK_LIST.keys.include?(:directories) || !BLACK_LIST.keys.include?(:files)
56
- puts "'#{BLACK_LIST_YML_FILE}' file has no directories and files keys."
57
- exit
58
- end
59
-
60
- BLACK_LIST_DIRECTORIES = BLACK_LIST[:directories].map {|directory| "#{PATH}/#{directory}"}
61
- BLACK_LIST_FILES = BLACK_LIST[:files]
62
-
63
- @code_files = []
64
-
65
- # Read code files from path
66
- def read_code_files_from_path(path)
67
- Dir.foreach path do |it|
68
- path_and_name = "#{path}/#{it}"
69
- if FileTest.directory?(path_and_name) && it != '.' && it != '..'
70
- read_code_files_from_path path_and_name unless BLACK_LIST_DIRECTORIES.include? path_and_name
71
- elsif FileTest.file?(path_and_name) && !BLACK_LIST_FILES.include?(it)
72
- puts "Processing => #{path_and_name}"
73
- file = File.open(path_and_name, 'r')
74
- file_content = ''
75
- line_number = 1
76
- file.each_line do |line|
77
- file_content << line.gsub(/</, '&lt;').gsub(/^/, "<color rgb='AAAAAA'>#{line_number}</color> "); line_number += 1
78
- end
79
- file.close
80
- @code_files << [path_and_name, file_content]
81
- end
82
- end
83
- end
84
-
85
- read_code_files_from_path PATH
86
-
36
+ BLACK_LIST_YML_FILE = "#{PATH}/.code2pdf"
87
37
  # Convert to PDF
88
- puts "\n\nConverting to PDF..."
89
- filename = PATH.gsub(/(\.|\/)/, '_')
90
- pdf = ConvertToPDF.new "#{filename}.pdf", @code_files
91
- pdf.save
38
+ filename = "#{PATH.gsub(/(\.|\/)/, '_')}.pdf"
39
+ ConvertToPDF.new :from => PATH, :to => filename, :except => BLACK_LIST_YML_FILE
data/code2pdf.gemspec CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12
12
  s.summary = %q{Convert your source code to PDF}
13
13
  s.description = %q{Convert your source code to PDF}
14
14
 
15
- s.add_development_dependency 'prawn'
16
- s.add_development_dependency 'prawn', '~> 0.11.1'
15
+ s.add_dependency 'prawn', '~> 0.11.1'
16
+ s.add_development_dependency 'rspec'
17
17
 
18
18
  s.rubyforge_project = 'code2pdf'
19
19
 
data/lib/code2pdf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'yaml'
2
+ require 'prawn'
3
+ require 'code2pdf/convert_to_pdf'
@@ -4,13 +4,27 @@ class ConvertToPDF
4
4
  :page_size => 'A4'
5
5
  }
6
6
 
7
- def initialize(filename, code_files)
8
- @filename, @code_files = filename, code_files
7
+ def initialize(params={})
8
+ if not params.has_key?(:from) or params[:from].nil?
9
+ raise ArgumentError.new 'where are the codes you want to convert to pdf?'
10
+ elsif not valid_directory?(params[:from])
11
+ raise LoadError.new "#{params[:from]} not found"
12
+ elsif not params.has_key?(:to) or params[:to].nil?
13
+ raise ArgumentError.new 'where should I save the generated pdf file?'
14
+ else
15
+ @from, @to = params[:from], params[:to]
16
+ if params.has_key?(:except)
17
+ @except = params[:except]
18
+ raise LoadError.new "#{@except} is not a valid blacklist yaml file" unless valid_blacklist?
19
+ end
20
+ save
21
+ end
9
22
  end
10
23
 
24
+ private
11
25
  def pdf
12
26
  Prawn::Document.new PDF_OPTIONS do |pdf|
13
- @code_files.each do |file|
27
+ read_files.each do |file|
14
28
  puts "Converting to PDF => #{file.first}"
15
29
  pdf.font 'Courier' do
16
30
  pdf.text "File: <strong>#{file.first}</strong>", :size => 12, :inline_format => true
@@ -23,6 +37,53 @@ class ConvertToPDF
23
37
  end
24
38
 
25
39
  def save
26
- pdf.render_file @filename
40
+ pdf.render_file @to
41
+ end
42
+
43
+ def valid_blacklist?
44
+ return false if not File.exists?(@except) or FileTest.directory?(@except)
45
+ @blacklist = YAML.load(File.read(@except))
46
+ @blacklist.has_key?(:directories) and @blacklist.has_key?(:files)
47
+ end
48
+
49
+ def in_blacklist?(item)
50
+ if @blacklist
51
+ @blacklist[:directories].include?(item) or @blacklist[:files].include?(item)
52
+ end
53
+ end
54
+
55
+ def valid_directory?(dir)
56
+ File.exists?(dir) and FileTest.directory?(dir)
57
+ end
58
+
59
+ def valid_file?(file)
60
+ File.exists?(file) and FileTest.file?(file)
61
+ end
62
+
63
+ def read_files(path=nil)
64
+ @files ||= [] ; path ||= @from
65
+ Dir.foreach(path) do |item|
66
+ item_path = "#{path}/#{item}"
67
+ unless in_blacklist?(item)
68
+ if valid_directory?(item_path) and not ['.','..'].include?(item)
69
+ read_files(item_path)
70
+ elsif valid_file?(item_path)
71
+ content = processing_file(item_path)
72
+ @files << ["File: <strong>#{item_path}</strong>", content]
73
+ end
74
+ end
75
+ end
76
+ @files
77
+ end
78
+
79
+ def processing_file(file)
80
+ content = ''
81
+ File.open(file,'r') do |f|
82
+ f.each_line.with_index do |line_content,line_number|
83
+ # TODO: this line is ugly, need find a better way ;D
84
+ content << line_content.gsub(/</,'&lt;').gsub(/^/, "<color rgb='AAAAAA'>#{line_number+1}</color> ")
85
+ end
86
+ end
87
+ content
27
88
  end
28
89
  end
@@ -1,3 +1,3 @@
1
1
  module Code2pdf
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'digest/md5'
3
+
4
+ describe ConvertToPDF do
5
+ describe '#pdf' do
6
+ it 'should create a PDF file containing all desired source code' do
7
+ path = 'spec/fixtures/hello_world'
8
+ pdf = 'spec/fixtures/hello_world.pdf'
9
+ blacklist = 'spec/fixtures/hello_world/.code2pdf'
10
+ ConvertToPDF.new :from => path, :to => pdf, :except => blacklist
11
+ Digest::MD5.hexdigest(File.read(pdf)).should eq 'f0270dcebd0abc942739d3c83ad4b978'
12
+ File.delete pdf
13
+ end
14
+
15
+ it 'should verify if essential params are present' do
16
+ expect{ConvertToPDF.new(:foo => 'bar')}.to raise_error ArgumentError
17
+ end
18
+
19
+ it 'should verify if specified path exists' do
20
+ path = 'spec/fixtures/isto_non_existe_quevedo'
21
+ pdf = 'spec/fixtures/isto_non_existe_quevedo.pdf'
22
+ expect{ConvertToPDF.new(:from => path, :to => pdf)}.to raise_error LoadError
23
+ end
24
+
25
+ it 'should verify if specified blacklist file is valid' do
26
+ path = 'spec/fixtures/hello_world'
27
+ pdf = 'spec/fixtures/hello_world.pdf'
28
+ blacklist = 'spec/fixtures/purplelist.yml'
29
+ expect{ConvertToPDF.new :from => path, :to => pdf, :except => blacklist}.to raise_error LoadError
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ :directories:
2
+ - doc
3
+ :files:
4
+ - .DS_Store
5
+ - .code2pdf
6
+ - tmp.txt
7
+ - gambi.rb
@@ -0,0 +1,162 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
7
+
8
+ <title>Module: HelloWorld</title>
9
+
10
+ <link rel="stylesheet" href="./rdoc.css" type="text/css" media="screen" />
11
+
12
+ <script src="./js/jquery.js" type="text/javascript"
13
+ charset="utf-8"></script>
14
+ <script src="./js/thickbox-compressed.js" type="text/javascript"
15
+ charset="utf-8"></script>
16
+ <script src="./js/quicksearch.js" type="text/javascript"
17
+ charset="utf-8"></script>
18
+ <script src="./js/darkfish.js" type="text/javascript"
19
+ charset="utf-8"></script>
20
+
21
+ </head>
22
+ <body class="module">
23
+
24
+ <div id="metadata">
25
+ <div id="home-metadata">
26
+ <div id="home-section" class="section">
27
+ <h3 class="section-header">
28
+ <a href="./index.html">Home</a>
29
+ <a href="./index.html#classes">Classes</a>
30
+ <a href="./index.html#methods">Methods</a>
31
+ </h3>
32
+ </div>
33
+ </div>
34
+
35
+ <div id="file-metadata">
36
+ <div id="file-list-section" class="section">
37
+ <h3 class="section-header">In Files</h3>
38
+ <div class="section-body">
39
+ <ul>
40
+
41
+ <li><a href="./lib/gambi_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
42
+ class="thickbox" title="lib/gambi.rb">lib/gambi.rb</a></li>
43
+
44
+ <li><a href="./lib/goodbye_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
45
+ class="thickbox" title="lib/goodbye.rb">lib/goodbye.rb</a></li>
46
+
47
+ <li><a href="./lib/hello_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
48
+ class="thickbox" title="lib/hello.rb">lib/hello.rb</a></li>
49
+
50
+ </ul>
51
+ </div>
52
+ </div>
53
+
54
+
55
+ </div>
56
+
57
+ <div id="class-metadata">
58
+
59
+ <!-- Parent Class -->
60
+
61
+
62
+ <!-- Namespace Contents -->
63
+
64
+ <div id="namespace-list-section" class="section">
65
+ <h3 class="section-header">Namespace</h3>
66
+ <ul class="link-list">
67
+
68
+ <li><span class="type">CLASS</span> <a href="HelloWorld/Gambi.html">HelloWorld::Gambi</a></li>
69
+
70
+ <li><span class="type">CLASS</span> <a href="HelloWorld/Goodbye.html">HelloWorld::Goodbye</a></li>
71
+
72
+ <li><span class="type">CLASS</span> <a href="HelloWorld/Helo.html">HelloWorld::Helo</a></li>
73
+
74
+ </ul>
75
+ </div>
76
+
77
+
78
+ <!-- Method Quickref -->
79
+
80
+
81
+ <!-- Included Modules -->
82
+
83
+ </div>
84
+
85
+ <div id="project-metadata">
86
+
87
+
88
+ <div id="fileindex-section" class="section project-section">
89
+ <h3 class="section-header">Files</h3>
90
+ <ul>
91
+
92
+ <li class="file"><a href="./tmp_txt.html">tmp.txt</a></li>
93
+
94
+ </ul>
95
+ </div>
96
+
97
+
98
+ <div id="classindex-section" class="section project-section">
99
+ <h3 class="section-header">Class Index
100
+ <span class="search-toggle"><img src="./images/find.png"
101
+ height="16" width="16" alt="[+]"
102
+ title="show/hide quicksearch" /></span></h3>
103
+ <form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
104
+ <fieldset>
105
+ <legend>Quicksearch</legend>
106
+ <input type="text" name="quicksearch" value=""
107
+ class="quicksearch-field" />
108
+ </fieldset>
109
+ </form>
110
+
111
+ <ul class="link-list">
112
+
113
+ <li><a href="./HelloWorld.html">HelloWorld</a></li>
114
+
115
+ <li><a href="./HelloWorld/Gambi.html">HelloWorld::Gambi</a></li>
116
+
117
+ <li><a href="./HelloWorld/Goodbye.html">HelloWorld::Goodbye</a></li>
118
+
119
+ <li><a href="./HelloWorld/Helo.html">HelloWorld::Helo</a></li>
120
+
121
+ </ul>
122
+ <div id="no-class-search-results" style="display: none;">No matching classes.</div>
123
+ </div>
124
+
125
+
126
+ </div>
127
+ </div>
128
+
129
+ <div id="documentation">
130
+ <h1 class="module">HelloWorld</h1>
131
+
132
+ <div id="description">
133
+
134
+ </div>
135
+
136
+ <!-- Constants -->
137
+
138
+
139
+ <!-- Attributes -->
140
+
141
+
142
+ <!-- Methods -->
143
+
144
+
145
+ </div>
146
+
147
+
148
+ <div id="rdoc-debugging-section-dump" class="debugging-section">
149
+
150
+ <p>Disabled; run with --debug to generate this.</p>
151
+
152
+ </div>
153
+
154
+ <div id="validator-badges">
155
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
156
+ <p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
157
+ Rdoc Generator</a> 1.1.6</small>.</p>
158
+ </div>
159
+
160
+ </body>
161
+ </html>
162
+