Skip to content
This repository
Browse code

Added rake task to remove CRLF characters added by git

  • Loading branch information...
commit bef6cdf3da86f8a55f8f7ad05bd240eb64802b07 1 parent 3bcbfec
Lucas Hills authored December 19, 2011 yob committed December 19, 2011
2  .gitignore
... ...
@@ -1,3 +1,5 @@
1 1
 doc
2 2
 pkg
3 3
 Gemfile.lock
  4
+.idea
  5
+
10  README.rdoc
Source Rendered
@@ -124,6 +124,16 @@ don't, 'rescue MalformedPDFError' will catch all the subclassed errors as well.
124 124
 Any other exceptions should be considered bugs in either PDF::Reader (please
125 125
 report it!).
126 126
 
  127
+= PDF Integrity
  128
+
  129
+*Windows developers* may run into problems when running specs due to MalformedPDFError's
  130
+This is usually because CRLF characters are automatically added to some of the PDF's in
  131
+the spec folder when you checkout a branch from Git.
  132
+
  133
+To remove any invalid CRLF characters added while checking out a branch from Git, run:
  134
+
  135
+    rake fix_integrity
  136
+
127 137
 = Maintainers
128 138
 
129 139
 - James Healy <mailto:jimmy@deefa.com>
35  Rakefile
@@ -18,7 +18,7 @@ RSpec::Core::RakeTask.new("spec") do |t|
18 18
   t.ruby_opts = "-w"
19 19
 end
20 20
 
21  
-# Genereate the RDoc documentation
  21
+# Generate the RDoc documentation
22 22
 desc "Create documentation"
23 23
 Rake::RDocTask.new("doc") do |rdoc|
24 24
   rdoc.title = "pdf-reader"
@@ -33,7 +33,7 @@ end
33 33
 
34 34
 RoodiTask.new 'roodi', ['lib/**/*.rb']
35 35
 
36  
-desc "create a YAML file of integrity info for PDFs in the spec suite"
  36
+desc "Create a YAML file of integrity info for PDFs in the spec suite"
37 37
 task :integrity_yaml do
38 38
   data = {}
39 39
   Dir.glob("spec/data/**/*.*").each do |path|
@@ -45,3 +45,34 @@ task :integrity_yaml do
45 45
   end
46 46
   File.open("spec/integrity.yml","wb") { |f| f.write YAML.dump(data)}
47 47
 end
  48
+
  49
+desc "Remove any CRLF characters added by Git"
  50
+task :fix_integrity do
  51
+  yaml_path = File.expand_path("spec/integrity.yml",File.dirname(__FILE__))
  52
+  integrity = YAML.load_file(yaml_path)
  53
+
  54
+  Dir.glob("spec/data/**/*.pdf").each do |path|
  55
+    path_relative_to_spec_folder = path[/.+(data\/.+)/,1]
  56
+    item = integrity[path_relative_to_spec_folder]
  57
+
  58
+    if File.file?(path)
  59
+      file_contents = File.open(path, "rb") { |f| f.read }
  60
+      md5 = Digest::MD5.hexdigest(file_contents)
  61
+
  62
+      unless md5 == item[:md5]
  63
+        #file md5 does not match what was checked into Git
  64
+
  65
+        if Digest::MD5.hexdigest(file_contents.gsub(/\r\n/, "\n")) == item[:md5]
  66
+          #pdf file is fixable by swapping CRLF characters
  67
+
  68
+          File.open(path, "wb") do |f|
  69
+            f.write(file_contents.gsub(/\r\n/, "\n"))
  70
+          end
  71
+          puts "Replaced CRLF characters in: #{path}"
  72
+        else
  73
+          puts "Failed to fix: #{path}"
  74
+        end
  75
+      end
  76
+    end
  77
+  end
  78
+end

0 notes on commit bef6cdf

Please sign in to comment.
Something went wrong with that request. Please try again.