code2pdf 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/bin/code2pdf +91 -0
- data/code2pdf.gemspec +23 -0
- data/lib/code2pdf/convert_to_pdf.rb +27 -0
- data/lib/code2pdf/version.rb +3 -0
- metadata +85 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/bin/code2pdf
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#
|
3
|
+
# code2pdf
|
4
|
+
# Created by Lucas Caton at 2011, may 31
|
5
|
+
# Updated at 2011, may 31
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'optparse'
|
9
|
+
require 'rubygems'
|
10
|
+
require 'prawn'
|
11
|
+
require 'convert_to_pdf'
|
12
|
+
|
13
|
+
options = {}
|
14
|
+
|
15
|
+
optparse = OptionParser.new do |opts|
|
16
|
+
opts.banner = "Usage: code2pdf <project path> <blacklist file>\n\nYou can use flags as such:"
|
17
|
+
|
18
|
+
opts.on('-h', '--help', 'Display this screen') do
|
19
|
+
puts opts
|
20
|
+
exit
|
21
|
+
end
|
22
|
+
|
23
|
+
if ARGV.size < 2
|
24
|
+
puts opts
|
25
|
+
exit
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
optparse.parse!
|
31
|
+
rescue OptionParser::InvalidOption => e
|
32
|
+
puts e
|
33
|
+
exit
|
34
|
+
end
|
35
|
+
|
36
|
+
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 != [:directories, :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(/</, '<').gsub(/^/, "<color rgb='AAAAAA'>#{line_number}</color> "); line_number += 1
|
78
|
+
end
|
79
|
+
file.close
|
80
|
+
@code_files << ["File: <strong>#{path_and_name}</strong>", file_content]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
read_code_files_from_path PATH
|
86
|
+
|
87
|
+
# Convert to PDF
|
88
|
+
puts "\n\nConverting to PDF..."
|
89
|
+
filename = PATH.gsub(/(\.|\/)/, '_')
|
90
|
+
pdf = ConvertToPDF.new "#{filename}.pdf", @code_files
|
91
|
+
pdf.save
|
data/code2pdf.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require "code2pdf/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'code2pdf'
|
7
|
+
s.version = Code2pdf::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Lucas Caton']
|
10
|
+
s.email = ['lucascaton@gmail.com']
|
11
|
+
s.homepage = 'http://blog.lucascaton.com.br/'
|
12
|
+
s.summary = %q{Convert your source code to PDF}
|
13
|
+
s.description = %q{Convert your source code to PDF}
|
14
|
+
|
15
|
+
s.add_development_dependency 'prawn'
|
16
|
+
|
17
|
+
s.rubyforge_project = 'code2pdf'
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ['lib']
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class ConvertToPDF
|
2
|
+
|
3
|
+
PDF_OPTIONS = {
|
4
|
+
:page_size => 'A4'
|
5
|
+
}
|
6
|
+
|
7
|
+
def initialize(filename, code_files)
|
8
|
+
@filename, @code_files = filename, code_files
|
9
|
+
end
|
10
|
+
|
11
|
+
def pdf
|
12
|
+
Prawn::Document.new PDF_OPTIONS do |pdf|
|
13
|
+
@code_files.each do |file|
|
14
|
+
pdf.font 'Courier' do
|
15
|
+
pdf.text file.first, :size => 12, :inline_format => true
|
16
|
+
pdf.move_down 20
|
17
|
+
pdf.text file.last, :size => 12, :inline_format => true
|
18
|
+
pdf.move_down 40
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def save
|
25
|
+
pdf.render_file @filename
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: code2pdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Lucas Caton
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-02 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: prawn
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Convert your source code to PDF
|
35
|
+
email:
|
36
|
+
- lucascaton@gmail.com
|
37
|
+
executables:
|
38
|
+
- code2pdf
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- Rakefile
|
47
|
+
- bin/code2pdf
|
48
|
+
- code2pdf.gemspec
|
49
|
+
- lib/code2pdf/convert_to_pdf.rb
|
50
|
+
- lib/code2pdf/version.rb
|
51
|
+
homepage: http://blog.lucascaton.com.br/
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project: code2pdf
|
80
|
+
rubygems_version: 1.7.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Convert your source code to PDF
|
84
|
+
test_files: []
|
85
|
+
|