hamidashi 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b911bc9d39ccb9d0763cf351ea38704757d9c5d6
4
- data.tar.gz: 9df53507831095a16c00f323c1fc9d8374e3720f
3
+ metadata.gz: c5964cda6406fc9e8b3abe4d2a15759160b238d7
4
+ data.tar.gz: 031e1e7eebca473685d5157e2eaf645070554291
5
5
  SHA512:
6
- metadata.gz: 0fc143212b3e6b0a7e7598091817c591fea3c7a6097dcb1665dbae792ec79045639dab983da9743e78dfa9355dbb6ffcd8575f50f4f7c6671e9f13e597151af7
7
- data.tar.gz: b85faa5e17447be04bb1e182d2e9840b6ab40e06df95dddf3f42d842d300883abb7c8f9e370a40d55cc7fdaad16da067f8fb1e809e36ccc3ec755844a8432987
6
+ metadata.gz: 4ab484a6d6f5c38d03625724f6e9fc22b1e9e7de401c6eef42afa829fe5a2205d154ac2836e6b0c758a6e1bb420806d5fd2e76b5c63daf3d2be6d8fbbb177953
7
+ data.tar.gz: 6bf34812341e8488b994324780e48dc387204e938afbd969641fd001c1daf9853e74d22b95ba026ef5aed6b13229f0d944267f904975c9d4913e3b3dae96df57
data/README.md CHANGED
@@ -24,7 +24,22 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- $ hamidashi example.pdf
27
+ $ hamidashi -h
28
+ Hamidashi
29
+ =========
30
+ Detect PDF text overflow.
31
+
32
+ Usage:
33
+ exe/hamidashi <name> [--margin=<margin>]
34
+ exe/hamidashi preview <name> [--margin=<margin>] [--page=<page>]
35
+ exe/hamidashi -h | --help
36
+ exe/hamidashi --version
37
+
38
+ Options:
39
+ -h --help Show this screen.
40
+ --version Show version.
41
+ --page=<page> Show specified page [default: 10].
42
+ --margin=<margin> Right margin percentage [default: 4.375].
28
43
 
29
44
  ## Development
30
45
 
@@ -1,20 +1,56 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'docopt'
2
3
  require 'fileutils'
3
4
  require 'hamidashi'
4
5
  require 'pdf/reader'
5
6
  require 'tmpdir'
6
7
 
7
- if ARGV.empty?
8
- puts 'Usage: hamidashi example.pdf'
9
- exit 1
8
+ doc = <<DOCOPT
9
+ Hamidashi
10
+ =========
11
+ Detect PDF text overflow.
12
+
13
+ Usage:
14
+ #{__FILE__} <name> [--margin=<margin>]
15
+ #{__FILE__} preview <name> [--margin=<margin>] [--page=<page>]
16
+ #{__FILE__} -h | --help
17
+ #{__FILE__} --version
18
+
19
+ Options:
20
+ -h --help Show this screen.
21
+ --version Show version.
22
+ --page=<page> Show specified page [default: 10].
23
+ --margin=<margin> Right margin percentage [default: 4.375].
24
+ DOCOPT
25
+
26
+ begin
27
+ opt = Docopt::docopt(doc)
28
+ rescue Docopt::Exit => e
29
+ puts e.message
30
+ if Docopt::Exit.usage.empty?
31
+ exit
32
+ else
33
+ exit 1
34
+ end
35
+ end
36
+
37
+ if opt['--version']
38
+ puts "hamidashi version #{Hamidashi::VERSION}"
39
+ exit
40
+ end
41
+
42
+ pdf_path = File.expand_path(opt['<name>'])
43
+ hamidashi = Hamidashi.new(Rational(opt['--margin']))
44
+
45
+ if opt['preview']
46
+ preview_path = File.expand_path("hamidashi-preview-#{Time.now.strftime("%Y%m%d%H%M%S")}-" + File.basename(pdf_path, '.pdf') + "-#{opt['--page']}.png")
47
+ hamidashi.save_preview_page(pdf_path, opt['--page'], preview_path)
48
+ exit
10
49
  end
11
50
 
12
- pdf_path = File.expand_path(ARGV[0])
13
51
  output_dir = File.expand_path("hamidashi-#{Time.now.strftime("%Y%m%d%H%M%S")}")
14
52
 
15
53
  Dir.mktmpdir('hamidashi') do |temp_dir|
16
- hamidashi = Hamidashi.new
17
-
18
54
  (0...PDF::Reader.new(pdf_path).page_count).each do |page|
19
55
  if hamidashi.overflow?(pdf_path, page)
20
56
  puts page
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  spec.add_dependency "pdf-reader"
30
+ spec.add_dependency "docopt"
30
31
  spec.add_development_dependency "bundler", "~> 1.10"
31
32
  spec.add_development_dependency "pry"
32
33
  spec.add_development_dependency "rake", "~> 10.0"
@@ -1,13 +1,22 @@
1
1
  require "hamidashi/version"
2
+ require "shellwords"
2
3
 
3
4
  class Hamidashi
5
+ def initialize(percentage)
6
+ @crop_width = (width * percentage / 100).to_i
7
+ end
8
+
4
9
  def overflow?(pdf_path, page)
5
- colornum=`convert -density 92 -geometry #{width} -crop #{crop} "#{pdf_path}[#{page}]" -format %c histogram:info: | wc -l`.to_i
10
+ colornum=`convert -density 92 -geometry #{width} -crop #{crop} "#{pdf_path.shellescape}[#{page.shellescape}]" -format %c histogram:info: | wc -l`.to_i
6
11
  colornum != expected_colornum
7
12
  end
8
13
 
9
14
  def save_page(pdf_path, page, path)
10
- `convert -density 92 -geometry #{width} "#{pdf_path}[#{page}]" #{path}`
15
+ `convert -density 92 -geometry #{width} "#{pdf_path.shellescape}[#{page.shellescape}]" #{path.shellescape}`
16
+ end
17
+
18
+ def save_preview_page(pdf_path, page, path)
19
+ `convert -density 92 -geometry #{width} -fill "rgba(255, 0, 0, 50%)" -strokewidth 0 -draw "rectangle #{width - crop_width},#{height},#{width},0" "#{pdf_path.shellescape}[#{page.shellescape}]" #{path.shellescape}`
11
20
  end
12
21
 
13
22
  private
@@ -19,13 +28,18 @@ class Hamidashi
19
28
  end
20
29
 
21
30
  def crop_width
22
- 28
31
+ @crop_width
23
32
  end
24
33
 
25
34
  def expected_colornum
26
35
  1
27
36
  end
28
37
 
38
+ # FIXME
39
+ def height
40
+ 10000
41
+ end
42
+
29
43
  def width
30
44
  640
31
45
  end
@@ -1,3 +1,3 @@
1
1
  class Hamidashi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamidashi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiei Miyagi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-12 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pdf-reader
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: docopt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement