code2pdf 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4941f0a5f601ad6095ff1974786411e65b1a77cc
4
- data.tar.gz: 61cde353d6a96c7181475fc1ad065660b0ade4e8
3
+ metadata.gz: cbd789fcae0b1e9a87bcbaf2a4cfd857e3f9530f
4
+ data.tar.gz: 995d1ba8d9cd25dc87f2bac42a5a46a0122f9430
5
5
  SHA512:
6
- metadata.gz: ebb4244b65d10af3ae6f7e9ed142f66f8fa7df8922a41a673c5252c27656e3ad9bb63c3e1a235a95f9b0ea81f3dd6a4848e2721cead6d7e2e55c905c19d4c297
7
- data.tar.gz: fd7fa00878664bc894745801f962ca868cefa9f6c7a718aaac0d2c15f669fec3c17d7ee02fe674617146ffb7a128cf4c90c70a5d1a2da08a7610caef0b5c3693
6
+ metadata.gz: 5e2226893a3293c0de832211e1694a133103286d8f949eb17b93423107ee4f461b614c89d428f8d165132edc51cbeeea6f41deb41ba707e359411f56890ebbd0
7
+ data.tar.gz: 4ec7dc0b1fdbe3b5013f79ed566fcd3b62b6bbe8f5e19e6a439d61d7cfa3428149df56d8db8ee22f93c3fc444ab80e5101b42b4e15b5835413ac0251db7d792b
data/Gemfile.lock CHANGED
@@ -1,39 +1,50 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code2pdf (0.3.1)
5
- prawn
4
+ code2pdf (0.4.1)
5
+ pdfkit
6
+ rouge
7
+ wkhtmltopdf-binary
6
8
 
7
9
  GEM
8
10
  remote: http://rubygems.org/
9
11
  specs:
10
- coderay (1.1.1)
11
- diff-lcs (1.2.5)
12
- method_source (0.8.2)
13
- pdf-core (0.6.1)
14
- prawn (2.1.0)
15
- pdf-core (~> 0.6.1)
16
- ttfunk (~> 1.4.0)
17
- pry (0.10.4)
12
+ Ascii85 (1.0.2)
13
+ afm (0.2.2)
14
+ coderay (1.1.2)
15
+ diff-lcs (1.3)
16
+ hashery (2.1.2)
17
+ method_source (0.9.0)
18
+ pdf-inspector (1.3.0)
19
+ pdf-reader (>= 1.0, < 3.0.a)
20
+ pdf-reader (2.0.0)
21
+ Ascii85 (~> 1.0.0)
22
+ afm (~> 0.2.1)
23
+ hashery (~> 2.0)
24
+ ruby-rc4
25
+ ttfunk
26
+ pdfkit (0.8.2)
27
+ pry (0.11.1)
18
28
  coderay (~> 1.1.0)
19
- method_source (~> 0.8.1)
20
- slop (~> 3.4)
21
- rake (11.2.2)
22
- rspec (3.5.0)
23
- rspec-core (~> 3.5.0)
24
- rspec-expectations (~> 3.5.0)
25
- rspec-mocks (~> 3.5.0)
26
- rspec-core (3.5.3)
27
- rspec-support (~> 3.5.0)
28
- rspec-expectations (3.5.0)
29
+ method_source (~> 0.9.0)
30
+ rake (12.1.0)
31
+ rouge (3.0.0)
32
+ rspec (3.6.0)
33
+ rspec-core (~> 3.6.0)
34
+ rspec-expectations (~> 3.6.0)
35
+ rspec-mocks (~> 3.6.0)
36
+ rspec-core (3.6.0)
37
+ rspec-support (~> 3.6.0)
38
+ rspec-expectations (3.6.0)
29
39
  diff-lcs (>= 1.2.0, < 2.0)
30
- rspec-support (~> 3.5.0)
31
- rspec-mocks (3.5.0)
40
+ rspec-support (~> 3.6.0)
41
+ rspec-mocks (3.6.0)
32
42
  diff-lcs (>= 1.2.0, < 2.0)
33
- rspec-support (~> 3.5.0)
34
- rspec-support (3.5.0)
35
- slop (3.6.0)
36
- ttfunk (1.4.0)
43
+ rspec-support (~> 3.6.0)
44
+ rspec-support (3.6.0)
45
+ ruby-rc4 (0.1.5)
46
+ ttfunk (1.5.1)
47
+ wkhtmltopdf-binary (0.12.3.1)
37
48
 
38
49
  PLATFORMS
39
50
  ruby
@@ -41,9 +52,10 @@ PLATFORMS
41
52
  DEPENDENCIES
42
53
  bundler
43
54
  code2pdf!
55
+ pdf-inspector
44
56
  pry
45
57
  rake
46
58
  rspec
47
59
 
48
60
  BUNDLED WITH
49
- 1.12.5
61
+ 1.15.4
@@ -1,4 +1,8 @@
1
- Copyright (c) 2011 Lucas Caton
1
+ LICENSE
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2011-2017 Lucas Caton
2
6
 
3
7
  Permission is hereby granted, free of charge, to any person obtaining
4
8
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # code2pdf
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/code2pdf.svg)](https://rubygems.org/gems/code2pdf)
4
+
3
5
  **code2pdf** is a simple tool for convert your source code to a PDF file.
4
6
 
5
7
  It can be useful if you need to licence you software.
@@ -47,4 +49,4 @@ The blacklist file must be a yaml file named `.code2pdf`, placed in project root
47
49
 
48
50
  ## Copyright
49
51
 
50
- Copyright © 2011 Lucas Caton. See LICENSE.txt for further details.
52
+ Copyright © 2011-2017 Lucas Caton. See LICENSE for further details.
data/code2pdf.gemspec CHANGED
@@ -4,7 +4,7 @@ Gem::Specification.new do |gem|
4
4
  gem.name = 'code2pdf'
5
5
  gem.summary = 'Convert your source code to PDF'
6
6
  gem.description = 'Convert your source code to PDF'
7
- gem.authors = %w(Lucas Caton)
7
+ gem.authors = ['Lucas Caton']
8
8
  gem.platform = Gem::Platform::RUBY
9
9
  gem.version = Code2pdf::VERSION
10
10
  gem.required_ruby_version = '>= 2.0.0'
@@ -13,9 +13,12 @@ Gem::Specification.new do |gem|
13
13
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
14
  gem.require_paths = %w(lib)
15
15
 
16
- gem.add_dependency 'prawn'
16
+ gem.add_dependency 'pdfkit'
17
+ gem.add_dependency 'wkhtmltopdf-binary'
18
+ gem.add_dependency 'rouge'
17
19
  gem.add_development_dependency 'bundler'
18
20
  gem.add_development_dependency 'pry'
19
21
  gem.add_development_dependency 'rake'
20
22
  gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'pdf-inspector'
21
24
  end
data/lib/code2pdf.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'yaml'
2
- require 'prawn'
2
+ require 'pdfkit'
3
3
  require 'code2pdf/convert_to_pdf'
4
+ require 'rouge'
@@ -3,7 +3,7 @@ class ConvertToPDF
3
3
  page_size: 'A4'
4
4
  }
5
5
 
6
- def initialize(params={})
6
+ def initialize(params = {})
7
7
  if !params.has_key?(:from) || params[:from].nil?
8
8
  raise ArgumentError.new 'where is the codebase you want to convert to PDF?'
9
9
  elsif !valid_directory?(params[:from])
@@ -11,11 +11,10 @@ class ConvertToPDF
11
11
  elsif !params.has_key?(:to) || params[:to].nil?
12
12
  raise ArgumentError.new 'where should I save the generated pdf file?'
13
13
  else
14
- @from, @to = params[:from], params[:to]
14
+ @from, @to, @except = params[:from], params[:to], params[:except].to_s
15
15
 
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?
16
+ if File.exists?(@except) && invalid_blacklist?
17
+ raise LoadError.new "#{@except} is not a valid blacklist YAML file"
19
18
  end
20
19
 
21
20
  save
@@ -24,32 +23,42 @@ class ConvertToPDF
24
23
 
25
24
  private
26
25
 
26
+ def save
27
+ pdf.to_file(@to)
28
+ end
29
+
27
30
  def pdf
28
- Prawn::Document.new PDF_OPTIONS do |pdf|
29
- read_files.each do |file|
30
- puts "Converting to PDF: #{file.first}"
31
-
32
- pdf.font 'Courier' do
33
- pdf.text "<strong>File: #{file.first}</strong>", size: 12, inline_format: true
34
- pdf.move_down 20
35
- pdf.text file.last, size: 12, inline_format: true
36
- pdf.move_down 40
37
- end
38
- end
31
+ html ||= ''
32
+
33
+ style = 'size: 12px; font-family: Helvetica, sans-serif;'
34
+
35
+ read_files.each do |file|
36
+ html += "<strong style='#{style}'>File: #{file.first}</strong></br></br>"
37
+ html += prepare_line_breaks(syntax_highlight(file)).to_s
38
+ html += add_space(30)
39
39
  end
40
+
41
+ @kit = PDFKit.new(html, page_size: 'A4')
42
+ @kit
40
43
  end
41
44
 
42
- def save
43
- pdf.render_file @to
45
+ def syntax_highlight(file)
46
+ file_type = File.extname(file.first)[1..-1]
47
+ file_lexer = Rouge::Lexer.find(file_type)
48
+ return file.last unless file_lexer
49
+
50
+ theme = Rouge::Themes::Base16.mode(:light)
51
+ formatter = Rouge::Formatters::HTMLInline.new(theme)
52
+ formatter = Rouge::Formatters::HTMLTable.new(formatter, start_line: 1)
53
+ formatter.format(file_lexer.lex(file.last))
44
54
  end
45
55
 
46
- def valid_blacklist?
47
- return false if FileTest.directory?(@except)
56
+ def invalid_blacklist?
57
+ return true if FileTest.directory?(@except)
48
58
 
49
- return true unless File.exists?(@except)
59
+ @blacklist = YAML.load_file(@except)
50
60
 
51
- @blacklist = YAML.load(File.read(@except))
52
- @blacklist.has_key?(:directories) && @blacklist.has_key?(:files)
61
+ !@blacklist.has_key?(:directories) || !@blacklist.has_key?(:files)
53
62
  end
54
63
 
55
64
  def in_directory_blacklist?(item_path)
@@ -77,13 +86,10 @@ class ConvertToPDF
77
86
  Dir.foreach(path) do |item|
78
87
  item_path = "#{path}/#{item}"
79
88
 
80
- if valid_directory?(item_path) && !['.', '..'].include?(item)
81
- read_files(item_path) unless in_directory_blacklist?(item_path)
82
- elsif valid_file?(item_path)
83
- unless in_file_blacklist?(item_path)
84
- content = process_file(item_path)
85
- @files << [item_path, content]
86
- end
89
+ if valid_directory?(item_path) && !%w[. ..].include?(item) && !in_directory_blacklist?(item_path)
90
+ read_files(item_path)
91
+ elsif valid_file?(item_path) && !in_file_blacklist?(item_path)
92
+ @files << [item_path, process_file(item_path)]
87
93
  end
88
94
  end
89
95
 
@@ -99,10 +105,18 @@ class ConvertToPDF
99
105
  content << "<color rgb='777777'>[binary]</color>"
100
106
  else
101
107
  f.each_line.with_index do |line_content, line_number|
102
- content << line_content.gsub(/</,'&lt;').gsub(/^/, "<color rgb='AAAAAA'>#{line_number + 1}</color> ")
108
+ content << line_content
103
109
  end
104
110
  end
105
111
  end
106
112
  content
107
113
  end
114
+
115
+ def prepare_line_breaks(content)
116
+ content.gsub(/\n/, '<br>')
117
+ end
118
+
119
+ def add_space(height)
120
+ "<div style='margin-bottom: #{height}px'>&nbsp;</div>"
121
+ end
108
122
  end
@@ -1,3 +1,3 @@
1
1
  module Code2pdf
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.1'.freeze
3
3
  end
@@ -1,15 +1,24 @@
1
1
  require 'spec_helper'
2
2
  require 'digest/md5'
3
+ require 'pdf/inspector'
3
4
 
4
5
  describe ConvertToPDF do
6
+ after do
7
+ pdf = 'spec/fixtures/hello_world.pdf'
8
+ File.delete(pdf) if File.exist?(pdf)
9
+ end
10
+
5
11
  describe '#pdf' do
6
12
  it 'creates a PDF file containing all desired source code' do
7
- path = 'spec/fixtures/hello_world'
8
- pdf = 'spec/fixtures/hello_world.pdf'
13
+ path = 'spec/fixtures/hello_world'
14
+ pdf = 'spec/fixtures/hello_world_output.pdf'
15
+ original = 'spec/fixtures/hello_world_original.pdf'
9
16
  blacklist = 'spec/fixtures/hello_world/.code2pdf'
10
17
 
11
18
  ConvertToPDF.new from: path, to: pdf, except: blacklist
12
- expect(Digest::MD5.hexdigest(File.read(pdf))).to eq('e8c7066d59eb2097fc8900d1a8bc386b')
19
+ text_analysis = read_strings("#{Dir.pwd}/#{pdf}")
20
+ text_analysis_original = read_strings("#{Dir.pwd}/#{original}")
21
+ expect(text_analysis).to eq(text_analysis_original)
13
22
  File.delete(pdf)
14
23
  end
15
24
 
@@ -32,4 +41,33 @@ describe ConvertToPDF do
32
41
  expect { ConvertToPDF.new from: path, to: pdf, except: blacklist }.to raise_error(LoadError)
33
42
  end
34
43
  end
44
+
45
+ describe '#prepare_line_breaks' do
46
+ let :pdf do
47
+ ConvertToPDF.new from: 'spec/fixtures/hello_world', to: 'spec/fixtures/hello_world.pdf'
48
+ end
49
+
50
+ it 'converts strings with \n to <br> for PDF generation' do
51
+ test_text = "test\ntest"
52
+ expect(pdf.send(:prepare_line_breaks, test_text)).to eq('test<br>test')
53
+ end
54
+ end
55
+
56
+ describe '#syntax_highlight' do
57
+ let :pdf do
58
+ ConvertToPDF.new from: 'spec/fixtures/hello_world', to: 'spec/fixtures/hello_world.pdf'
59
+ end
60
+
61
+ it 'returns file with syntax_highlight html clases' do
62
+ path = 'spec/fixtures/hello_world/lib/hello.rb'
63
+ output = File.read('spec/fixtures/syntax_highlight.html')
64
+ content = pdf.send(:process_file, path)
65
+ file = [path, content]
66
+ expect(pdf.send(:syntax_highlight, file)).to eq(output)
67
+ end
68
+ end
69
+ end
70
+
71
+ def read_strings(file)
72
+ PDF::Inspector::Text.analyze(File.read(file)).strings
35
73
  end
@@ -0,0 +1,15 @@
1
+ <table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
+ 2
3
+ 3
4
+ 4
5
+ 5
6
+ 6
7
+ 7
8
+ </pre></td><td class="rouge-code"><pre><span style="color: #aa759f">module</span> <span style="color: #f4bf75">HelloWorld</span>
9
+ <span style="color: #aa759f">class</span> <span style="color: #f4bf75">Helo</span>
10
+ <span style="color: #aa759f">def</span> <span style="color: #303030">say</span><span style="color: #d0d0d0">(</span><span style="color: #303030">name</span><span style="color: #d0d0d0">)</span>
11
+ <span style="color: #90a959">"Hi, </span><span style="color: #8f5536">#{</span><span style="color: #303030">name</span><span style="color: #8f5536">}</span><span style="color: #90a959">!"</span>
12
+ <span style="color: #aa759f">end</span>
13
+ <span style="color: #aa759f">end</span>
14
+ <span style="color: #aa759f">end</span>
15
+ </pre></td></tr></tbody></table>
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,10 @@
1
- $:.push File.expand_path('../../lib', __FILE__)
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
2
3
  require 'code2pdf'
4
+
5
+ Dir['./spec/support/**/*.rb'].each { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.filter_run :focus
9
+ config.run_all_when_everything_filtered = true
10
+ end
metadata CHANGED
@@ -1,18 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code2pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
- - Lucas
8
- - Caton
7
+ - Lucas Caton
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2016-09-24 00:00:00.000000000 Z
11
+ date: 2017-10-17 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: prawn
14
+ name: pdfkit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wkhtmltopdf-binary
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rouge
16
43
  requirement: !ruby/object:Gem::Requirement
17
44
  requirements:
18
45
  - - ">="
@@ -81,6 +108,20 @@ dependencies:
81
108
  - - ">="
82
109
  - !ruby/object:Gem::Version
83
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pdf-inspector
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
84
125
  description: Convert your source code to PDF
85
126
  email:
86
127
  executables:
@@ -92,7 +133,7 @@ files:
92
133
  - ".rspec"
93
134
  - Gemfile
94
135
  - Gemfile.lock
95
- - LICENSE.txt
136
+ - LICENSE
96
137
  - README.md
97
138
  - Rakefile
98
139
  - bin/code2pdf
@@ -118,7 +159,9 @@ files:
118
159
  - spec/fixtures/hello_world/lib/goodbye.rb
119
160
  - spec/fixtures/hello_world/lib/hello.rb
120
161
  - spec/fixtures/hello_world/tmp.txt
162
+ - spec/fixtures/hello_world_original.pdf
121
163
  - spec/fixtures/purplelist.yml
164
+ - spec/fixtures/syntax_highlight.html
122
165
  - spec/spec_helper.rb
123
166
  homepage:
124
167
  licenses: []
@@ -139,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
182
  version: '0'
140
183
  requirements: []
141
184
  rubyforge_project:
142
- rubygems_version: 2.6.6
185
+ rubygems_version: 2.6.13
143
186
  signing_key:
144
187
  specification_version: 4
145
188
  summary: Convert your source code to PDF