forked from yomurb/yomu
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional encoding
argument to set output character encoding
#47
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,8 +47,8 @@ def self.mimetype(content_type) | |
# text = Henkei.read :text, data | ||
# metadata = Henkei.read :metadata, data | ||
# | ||
def self.read(type, data, include_ocr: false) | ||
result = client_read(type, data, include_ocr: include_ocr) | ||
def self.read(type, data, include_ocr: false, encoding: nil) | ||
result = client_read(type, data, include_ocr: include_ocr, encoding: encoding) | ||
|
||
case type | ||
when :text, :html then result | ||
|
@@ -96,10 +96,14 @@ def initialize(input) | |
# | ||
# henkei.text(include_ocr: true) | ||
# | ||
def text(include_ocr: false) | ||
# Set the output character encoding (e.g. 'UTF-8') | ||
# | ||
# henkei.text(encoding: 'UTF-8') | ||
# | ||
def text(include_ocr: false, encoding: nil) | ||
return @text if defined? @text | ||
|
||
@text = Henkei.read :text, data, include_ocr: include_ocr | ||
@text = Henkei.read :text, data, include_ocr: include_ocr, encoding: encoding | ||
end | ||
|
||
# Returns the text content of the Henkei document in HTML. | ||
|
@@ -111,10 +115,14 @@ def text(include_ocr: false) | |
# | ||
# henkei.html(include_ocr: true) | ||
# | ||
def html(include_ocr: false) | ||
# Set the output character encoding (e.g. 'UTF-8') | ||
# | ||
# henkei.text(encoding: 'UTF-8') | ||
# | ||
def html(include_ocr: false, encoding: nil) | ||
return @html if defined? @html | ||
|
||
@html = Henkei.read :html, data, include_ocr: include_ocr | ||
@html = Henkei.read :html, data, include_ocr: include_ocr, encoding: encoding | ||
end | ||
|
||
# Returns the metadata hash of the Henkei document. | ||
|
@@ -211,20 +219,34 @@ def self.java_path | |
|
||
# Internal helper for calling to Tika library directly | ||
# | ||
def self.client_read(type, data, include_ocr: false) | ||
Open3.capture2(*tika_command(type, include_ocr: include_ocr), stdin_data: data, binmode: true).first | ||
def self.client_read(type, data, include_ocr: false, encoding: nil) | ||
unless encoding.nil? || Encoding.name_list.include?(encoding) | ||
raise ArgumentError, "unsupported encoding - #{encoding}" | ||
end | ||
|
||
Open3.popen2(*tika_command(type, include_ocr: include_ocr, encoding: encoding)) do |stdin, stdout| | ||
stdin.binmode | ||
stdout.binmode | ||
stdout.set_encoding encoding unless encoding.nil? | ||
|
||
stdin.puts data | ||
out_reader = Thread.new { stdout.read } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and it also sets up the read thread before writing to the input pipe |
||
stdin.close | ||
out_reader.value | ||
end | ||
end | ||
private_class_method :client_read | ||
|
||
# Internal helper for building the Java command to call Tika | ||
# | ||
def self.tika_command(type, include_ocr: false) | ||
def self.tika_command(type, include_ocr: false, encoding: nil) | ||
[ | ||
java_path, | ||
'-Djava.awt.headless=true', | ||
'-jar', | ||
Henkei::JAR_PATH, | ||
"--config=#{include_ocr ? Henkei::CONFIG_PATH : Henkei::CONFIG_WITHOUT_OCR_PATH}" | ||
"--config=#{include_ocr ? Henkei::CONFIG_PATH : Henkei::CONFIG_WITHOUT_OCR_PATH}", | ||
*("--encoding=#{encoding}" unless encoding.nil?) | ||
] + switch_for_type(type) | ||
end | ||
private_class_method :tika_command | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class Henkei | ||
VERSION = '2.9.2.1' | ||
VERSION = '2.9.2.2' | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
capture2
useswrite
at this stage. It is also wrapped in a begin/rescue block catching theErrno::EPIPE
error.Looking at the documentation/source they do slightly different things.. including
puts
writing an extra newline