Add PDF::Reader::Page#find_text method

main
Guillaume Dott 2022-02-23 17:12:31 +01:00
parent e8260726e1
commit 64576a1c7e
6 changed files with 19 additions and 12 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
/pkg/ /pkg/
/spec/reports/ /spec/reports/
/tmp/ /tmp/
/Gemfile.lock

View File

@ -1,4 +1,4 @@
# Pdf::Reader::FindText # PDF::Reader::FindText
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pdf/reader/find_text`. To experiment with that code, run `bin/console` for an interactive prompt. Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pdf/reader/find_text`. To experiment with that code, run `bin/console` for an interactive prompt.

View File

@ -1,12 +1,18 @@
# frozen_string_literal: true # frozen_string_literal: true
require "pdf-reader"
require_relative "find_text/version" require_relative "find_text/version"
module Pdf module PDF::Reader::FindText
module Reader def find_text(text)
module FindText runs(merge: false).each_cons(text.size).select do |r|
class Error < StandardError; end r.map(&:text).join == text
# Your code goes here... end.map do |r|
PDF::Reader::TextRun.new r.first.x, r.first.y,
r.sum(&:width), r.map(&:font_size).max, r.map(&:text).join
end end
end end
end end
PDF::Reader::Page.include PDF::Reader::FindText

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
module Pdf module PDF
module Reader class Reader
module FindText module FindText
VERSION = "0.1.0" VERSION = "0.1.0"
end end

View File

@ -4,7 +4,7 @@ require_relative "lib/pdf/reader/find_text/version"
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "pdf-reader-find_text" spec.name = "pdf-reader-find_text"
spec.version = Pdf::Reader::FindText::VERSION spec.version = PDF::Reader::FindText::VERSION
spec.authors = ["Guillaume Dott"] spec.authors = ["Guillaume Dott"]
spec.email = ["guillaume+github@dott.fr"] spec.email = ["guillaume+github@dott.fr"]
@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"] spec.require_paths = ["lib"]
# Uncomment to register a new dependency of your gem # Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0" spec.add_dependency "pdf-reader", "~> 2.9.2"
# For more information and examples about making a new gem, checkout our # For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html # guide at: https://bundler.io/guides/creating_gem.html

View File

@ -2,9 +2,9 @@
require "test_helper" require "test_helper"
class Pdf::Reader::FindTextTest < Minitest::Test class PDF::Reader::FindTextTest < Minitest::Test
def test_that_it_has_a_version_number def test_that_it_has_a_version_number
refute_nil ::Pdf::Reader::FindText::VERSION refute_nil ::PDF::Reader::FindText::VERSION
end end
def test_it_does_something_useful def test_it_does_something_useful