-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ifad/features/txt_conversion
Add support for conversion to text
- Loading branch information
Showing
14 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'nokogiri' | ||
|
||
module Heathen | ||
class Processor | ||
def htmltotext | ||
expect_mime_type 'text/html' | ||
|
||
begin | ||
doc = Nokogiri::HTML(File.open(job.content_file)) | ||
|
||
# Strip JS / CSS from the file so it doesn't appear in the output | ||
doc.css('script, link').each { |node| node.remove } | ||
|
||
text = doc.css('body').text | ||
rescue Nokogiri::SyntaxError => e | ||
raise ConversionFailed.new(e) | ||
end | ||
|
||
job.content = text | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Heathen | ||
class Processor | ||
def pdftotext | ||
expect_mime_type 'application/pdf' | ||
|
||
target_file = temp_file_name | ||
executioner.execute( | ||
'pdftotext', | ||
job.content_file, | ||
target_file | ||
) | ||
raise ConversionFailed.new(executioner.last_messages) if executioner.last_exit_status != 0 | ||
job.content = File.read(target_file) | ||
File.unlink(target_file) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'spec_helper' | ||
|
||
describe Heathen::Processor do | ||
let(:content) { File.read(fixture('heathen/quickfox.html')) } | ||
let(:job) { Heathen::Job.new 'foo', content, 'en' } | ||
let(:processor) { described_class.new job: job, logger: Logger.new($stderr) } | ||
|
||
after do | ||
processor.clean_up | ||
end | ||
|
||
context '#htmltotext' do | ||
it 'converts HTML to TXT' do | ||
processor.htmltotext | ||
expect(job.content.mime_type).to eq 'text/plain; charset=us-ascii' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'spec_helper' | ||
|
||
describe Heathen::Processor do | ||
let(:content) { File.read(fixture('heathen/quickfox.pdf')) } | ||
let(:job) { Heathen::Job.new 'foo', content, 'en' } | ||
let(:processor) { described_class.new job: job, logger: Logger.new($stderr) } | ||
|
||
after do | ||
processor.clean_up | ||
end | ||
|
||
context '#pdftotext' do | ||
it 'converts PDF to TXT' do | ||
processor.pdftotext | ||
expect(job.content.mime_type).to eq 'text/plain; charset=us-ascii' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters