From 2b4d2b105705d80930a78bb7ffcb06d557bcf9f2 Mon Sep 17 00:00:00 2001 From: Kendall Buchanan Date: Mon, 17 Sep 2012 03:08:22 -0600 Subject: [PATCH 1/4] Accepting non-ascii characters in the PDF header --- lib/docsplit/info_extractor.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/docsplit/info_extractor.rb b/lib/docsplit/info_extractor.rb index 3d50d53..d878496 100644 --- a/lib/docsplit/info_extractor.rb +++ b/lib/docsplit/info_extractor.rb @@ -5,14 +5,14 @@ class InfoExtractor # Regex matchers for different bits of information. MATCHERS = { - :author => /^Author:\s+([^\n]+)/, - :date => /^CreationDate:\s+([^\n]+)/, - :creator => /^Creator:\s+([^\n]+)/, - :keywords => /^Keywords:\s+([^\n]+)/, - :producer => /^Producer:\s+([^\n]+)/, - :subject => /^Subject:\s+([^\n]+)/, - :title => /^Title:\s+([^\n]+)/, - :length => /^Pages:\s+([^\n]+)/, + :author => Regexp.new("^Author:\s+([^\n]+)".encode('UTF-8')), + :date => Regexp.new("^CreationDate:\s+([^\n]+)".encode('UTF-8')), + :creator => Regexp.new("^Creator:\s+([^\n]+)".encode('UTF-8')), + :keywords => Regexp.new("^Keywords:\s+([^\n]+)".encode('UTF-8')), + :producer => Regexp.new("^Producer:\s+([^\n]+)".encode('UTF-8')), + :subject => Regexp.new("^Subject:\s+([^\n]+)".encode('UTF-8')), + :title => Regexp.new("^Title:\s+([^\n]+)".encode('UTF-8')), + :length => Regexp.new("^Pages:\s+([^\n]+)".encode('UTF-8')), } # Pull out a single datum from a pdf. @@ -29,4 +29,4 @@ def extract(key, pdfs, opts) end -end \ No newline at end of file +end From 17837933532e6d2d81def44efb0f47145acd1fed Mon Sep 17 00:00:00 2001 From: Kendall Buchanan Date: Mon, 17 Sep 2012 14:07:04 -0600 Subject: [PATCH 2/4] Remove non-ascii characters --- lib/docsplit/info_extractor.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/docsplit/info_extractor.rb b/lib/docsplit/info_extractor.rb index d878496..72acb0f 100644 --- a/lib/docsplit/info_extractor.rb +++ b/lib/docsplit/info_extractor.rb @@ -1,3 +1,5 @@ +require 'iconv' + module Docsplit # Delegates to **pdfinfo** in order to extract information about a PDF file. @@ -5,21 +7,21 @@ class InfoExtractor # Regex matchers for different bits of information. MATCHERS = { - :author => Regexp.new("^Author:\s+([^\n]+)".encode('UTF-8')), - :date => Regexp.new("^CreationDate:\s+([^\n]+)".encode('UTF-8')), - :creator => Regexp.new("^Creator:\s+([^\n]+)".encode('UTF-8')), - :keywords => Regexp.new("^Keywords:\s+([^\n]+)".encode('UTF-8')), - :producer => Regexp.new("^Producer:\s+([^\n]+)".encode('UTF-8')), - :subject => Regexp.new("^Subject:\s+([^\n]+)".encode('UTF-8')), - :title => Regexp.new("^Title:\s+([^\n]+)".encode('UTF-8')), - :length => Regexp.new("^Pages:\s+([^\n]+)".encode('UTF-8')), + :author => /^Author:\s+([^\n]+)/, + :date => /^CreationDate:\s+([^\n]+)/, + :creator => /^Creator:\s+([^\n]+)/, + :keywords => /^Keywords:\s+([^\n]+)/, + :producer => /^Producer:\s+([^\n]+)/, + :subject => /^Subject:\s+([^\n]+)/, + :title => /^Title:\s+([^\n]+)/, + :length => /^Pages:\s+([^\n]+)/, } # Pull out a single datum from a pdf. def extract(key, pdfs, opts) pdf = [pdfs].flatten.first cmd = "pdfinfo #{ESCAPE[pdf]} 2>&1" - result = `#{cmd}`.chomp + result = Iconv.conv('ASCII//IGNORE', 'UTF8', `#{cmd}`.chomp) raise ExtractionFailed, result if $? != 0 match = result.match(MATCHERS[key]) answer = match && match[1] From 290a0b6c49cb2ecd0551fba22f5653da8dfdbeef Mon Sep 17 00:00:00 2001 From: Kendall Buchanan Date: Fri, 21 Sep 2012 08:50:06 -0600 Subject: [PATCH 3/4] Moved to String#encode instead of Iconv --- .gitignore | 1 + lib/docsplit/info_extractor.rb | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f1d7f5e..b052394 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.gem .DS_Store +*.swp diff --git a/lib/docsplit/info_extractor.rb b/lib/docsplit/info_extractor.rb index 72acb0f..7284c59 100644 --- a/lib/docsplit/info_extractor.rb +++ b/lib/docsplit/info_extractor.rb @@ -1,5 +1,3 @@ -require 'iconv' - module Docsplit # Delegates to **pdfinfo** in order to extract information about a PDF file. @@ -21,7 +19,7 @@ class InfoExtractor def extract(key, pdfs, opts) pdf = [pdfs].flatten.first cmd = "pdfinfo #{ESCAPE[pdf]} 2>&1" - result = Iconv.conv('ASCII//IGNORE', 'UTF8', `#{cmd}`.chomp) + result = `#{cmd}`.encode(Encoding::ISO_8859_1, :undef => :replace) raise ExtractionFailed, result if $? != 0 match = result.match(MATCHERS[key]) answer = match && match[1] From e89ab4dbbc17377617f4df989832988fc830a783 Mon Sep 17 00:00:00 2001 From: Kendall Buchanan Date: Mon, 1 Oct 2012 06:42:52 -0600 Subject: [PATCH 4/4] Moving back to iconv --- lib/docsplit/info_extractor.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/docsplit/info_extractor.rb b/lib/docsplit/info_extractor.rb index 7284c59..72acb0f 100644 --- a/lib/docsplit/info_extractor.rb +++ b/lib/docsplit/info_extractor.rb @@ -1,3 +1,5 @@ +require 'iconv' + module Docsplit # Delegates to **pdfinfo** in order to extract information about a PDF file. @@ -19,7 +21,7 @@ class InfoExtractor def extract(key, pdfs, opts) pdf = [pdfs].flatten.first cmd = "pdfinfo #{ESCAPE[pdf]} 2>&1" - result = `#{cmd}`.encode(Encoding::ISO_8859_1, :undef => :replace) + result = Iconv.conv('ASCII//IGNORE', 'UTF8', `#{cmd}`.chomp) raise ExtractionFailed, result if $? != 0 match = result.match(MATCHERS[key]) answer = match && match[1]