honyomi 1.1.0 → 1.2.0

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: 4c387aa69369e0ac0b0c92d5ce0c56a0d6f9b782
4
- data.tar.gz: 36bd6672cc22367b2a368eb913122e96bd2ff322
3
+ metadata.gz: f785777fac05f71e8de95f2a030a5da69b791a4d
4
+ data.tar.gz: fbe61872b4dff870f4699de33df9a1278fbaa8e9
5
5
  SHA512:
6
- metadata.gz: 53df1091d9e29ba8d7fbabb7b0b4ba27d1080b74d29cc2f5af2afdb5f6947689f279094bffb0fb7c4e75d37c17b3c1eed0d324db88af83893eb6cd60890ebb99
7
- data.tar.gz: 00b79ec2b4122df5b55d7c869f102f1916dbf5406bb202f073806f8e2afb61ef5d3bb3e7a74955c8adc31a41df5e8805a35d533f9bf6686958bb1ca22e732fa1
6
+ metadata.gz: dfbec7c05b178b935ae27189c276382b9f7f67e1f46cf519609cb1b47a73f99dc08b45e1dffc68e734a59b6610fe511a0ef68fc8b7663d9c626f2a7481c121bc
7
+ data.tar.gz: 11d2d1af46f7515451e04c8bfd90983cca3c11245889423979ca13d9f055eb989bf4c3dad31be50686eb5881a953301738798b1077684cff16b483e241db07d3
data/HISTORY.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # HISTORY - Honyomi
2
2
 
3
+ ## 1.2 - 2015-07-03
4
+
5
+ * Add image command
6
+ * It can be inline display of page on browser
7
+ * Need 'pdftoppm'
8
+ * Remove duplicated :type => :boolean
9
+
10
+ * Add book on web
11
+ * Drag & drop OR Dialog
12
+ * If you want to disable this feature, set ENV["HONYOMI_DISABLE_WEB_ADD"]
13
+ * $ HONYOMI_DISABLE_WEB_ADD=1 honyomi web
14
+
15
+ * Fix encode error
16
+ * Specify file encoding in File.read
17
+ * Add String#scrub
18
+
3
19
  ## 1.1 - 2015-06-23
4
20
 
5
21
  * Add book on web
data/lib/honyomi/cli.rb CHANGED
@@ -120,7 +120,7 @@ module Honyomi
120
120
  end
121
121
 
122
122
  desc "web", "Web search interface"
123
- option :no_browser, :type => :boolean, :default => false, :aliases => '-n', :type => :boolean, :desc => 'Do not launch browser.'
123
+ option :no_browser, :type => :boolean, :default => false, :aliases => '-n', :desc => 'Do not launch browser.'
124
124
  option :host, :default => '127.0.0.1', :aliases => '-o', :desc => 'Listen on HOST.'
125
125
  option :port, :default => 9295, :aliases => '-p', :desc => 'Use PORT.'
126
126
  option :server, :default => 'thin', :aliases => '-s', :desc => 'Use SERVER.'
@@ -130,6 +130,16 @@ module Honyomi
130
130
  core.web(options)
131
131
  end
132
132
 
133
+ desc "image book_id1 [book_id2 ...]", "Generate page images (Need pdftoppm)"
134
+ def image(*args)
135
+ core = Core.new
136
+ core.load_database
137
+
138
+ args.each do |id|
139
+ core.image(id.to_i, { verbose: true })
140
+ end
141
+ end
142
+
133
143
  no_tasks do
134
144
  # Override method for support -h
135
145
  # defined in /lib/thor/invocation.rb
data/lib/honyomi/core.rb CHANGED
@@ -120,6 +120,13 @@ EOF
120
120
  end
121
121
  end
122
122
 
123
+ def image(id, options = {})
124
+ pdf = Pdf.new(@database.books[id].path)
125
+ output_dir = File.join(image_dir, id.to_s)
126
+ pdf.generate_images(output_dir)
127
+ puts "Generated images to '#{output_dir}'" if options[:verbose]
128
+ end
129
+
123
130
  def db_dir
124
131
  File.join(home_dir, 'db')
125
132
  end
@@ -138,5 +145,9 @@ EOF
138
145
 
139
146
  @home_dir
140
147
  end
148
+
149
+ def image_dir
150
+ File.join(home_dir, "image")
151
+ end
141
152
  end
142
153
  end
data/lib/honyomi/pdf.rb CHANGED
@@ -1,24 +1,31 @@
1
+ # coding: utf-8
1
2
  require 'honyomi'
2
3
  require 'tmpdir'
4
+ require 'fileutils'
3
5
 
4
6
  module Honyomi
5
7
  class Pdf
6
8
  def initialize(filename)
7
9
  @filename = filename
10
+ end
8
11
 
12
+ def pages
9
13
  Dir.mktmpdir do |dir|
10
14
  outfile = File.join(dir, "pdf.txt")
11
- system("pdftotext", filename, outfile) # Need pdftotext (poppler, xpdf)
15
+ system("pdftotext", @filename, outfile) # Need pdftotext (poppler, xpdf)
12
16
  @text = File.read(outfile, encoding: Encoding::UTF_8)
13
17
  if String.method_defined? :scrub
14
18
  @text = @text.scrub('?')
15
19
  end
16
20
  end
17
- end
18
21
 
19
- def pages
20
22
  @text.split("\f")
21
23
  end
24
+
25
+ def generate_images(output_dir)
26
+ FileUtils.mkdir_p output_dir
27
+ system("pdftoppm", "-jpeg", @filename, File.join(output_dir, "book"))
28
+ end
22
29
  end
23
30
  end
24
31
 
data/lib/honyomi/util.rb CHANGED
@@ -101,5 +101,14 @@ module Honyomi
101
101
  def default_home
102
102
  File.expand_path '~'
103
103
  end
104
+
105
+ def count_digit(num)
106
+ num.to_s.length
107
+ end
108
+
109
+ def image_path(page)
110
+ zerofill = format("%0#{count_digit(page.book.page_num)}d", page.page_no)
111
+ "#{home_dir}/image/#{page.book.id}/book-#{zerofill}.jpg"
112
+ end
104
113
  end
105
114
  end
@@ -1,3 +1,3 @@
1
1
  module Honyomi
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -93,12 +93,13 @@ get '/v/:id' do
93
93
 
94
94
  book = @database.books[params[:id].to_i]
95
95
 
96
- if params[:text] == '1'
97
- text_all(book)
98
- elsif params[:pdf] == '1'
96
+ if params[:pdf] == '1'
99
97
  send_file(book.path, :disposition => 'inline')
100
98
  elsif params[:dl] == '1'
101
99
  send_file(book.path, :disposition => 'download')
100
+ elsif params[:image] == '1'
101
+ page = @database.pages["#{params[:id].to_i}:#{params[:page].to_i}"]
102
+ send_file(Util.image_path(page))
102
103
  else
103
104
  if params[:page]
104
105
  text_page(book, params[:page].to_i)
@@ -293,6 +294,12 @@ EOF
293
294
  wrap_result_body_element(comment_hits) +
294
295
  wrap_result_body_element(text_hits)
295
296
 
297
+ image_path = Util.image_path(page)
298
+
299
+ if File.exist? image_path
300
+ main_contents += %|<div><img src="/v/#{page.book.id}?image=1&page=#{page.page_no}" width="100%"/></div>|
301
+ end
302
+
296
303
  <<EOF
297
304
  <div class="result">
298
305
  <div class="title">
@@ -339,9 +346,12 @@ EOF
339
346
  book = page.book
340
347
  title = book.title
341
348
 
349
+ image_path = Util.image_path(page)
350
+ has_image = File.exist? image_path
351
+
342
352
  content = []
343
353
  content << bookmark.comment if bookmark.comment
344
- content << page.text if page.text
354
+ content << page.text if !has_image && page.text
345
355
 
346
356
  r = []
347
357
  rest = BOOKMARK_COMMENT_LENGTH
@@ -357,6 +367,10 @@ EOF
357
367
 
358
368
  content = r.map { |e| "<p>#{escape_html(e)}</p>" }.join("\n")
359
369
 
370
+ if has_image
371
+ content += %|<p><img src="/v/#{page.book.id}?image=1&page=#{page.page_no}" width="100%"/></p>|
372
+ end
373
+
360
374
  <<EOF
361
375
  <div class="result">
362
376
  <div class="title">
@@ -418,21 +432,28 @@ EOF
418
432
  bm_text = ", <a href=\"/v/#{book.id}?b=1\"><span class=\"boomark-number\">#{bm.count}</span></a> bookmarks. "
419
433
  end
420
434
 
421
- %Q|#{pages} pages#{bm_text}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/v/#{book.id}?dl=1">Download</a> <span class="file-size">(#{file_mb}M)</span>&nbsp;&nbsp;&nbsp;<a href="/v/#{book.id}?pdf=1">Pdf</a>&nbsp;&nbsp;&nbsp;<a href="/v/#{book.id}?text=1#{query}">Text</a>|
435
+ %Q|#{pages} pages#{bm_text}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/v/#{book.id}?dl=1">Download</a> <span class="file-size">(#{file_mb}M)</span>|
422
436
  end
423
437
 
424
438
  def render_page(page, options = {})
425
439
  book = page.book
440
+
441
+ # with_number
426
442
  with_number = ""
427
443
  if options[:with_number]
428
444
  with_number = <<EOF
429
445
  <div class="no row">
430
- <div class="col-xs-3"><div class="ss-box">#{favstar(page)}</div> <a href="##{page.page_no}">P#{page.page_no}</a></div>
431
- <div class="col-xs-offset-8 col-xs-1"><a href="/v/#{book.id}?pdf=1#page=#{page.page_no}"><i class="fa fa-file-text-o"></i></a></div>
446
+ <div class="col-xs-8">
447
+ <div class="ss-box">#{favstar(page)}</div>
448
+ <a href="##{page.page_no}">P#{page.page_no}</a>
449
+ &nbsp;&nbsp;&nbsp;<a href="javascript:" class="page-text-toggle">Text</a>
450
+ &nbsp;&nbsp;&nbsp;<a href="/v/#{book.id}?pdf=1#page=#{page.page_no}">Pdf</a>
451
+ </div>
432
452
  </div>
433
453
  EOF
434
454
  end
435
455
 
456
+ # comment
436
457
  comment = ""
437
458
 
438
459
  bm = @database.bookmark_from_page(page)
@@ -445,17 +466,33 @@ EOF
445
466
  EOF
446
467
  end
447
468
 
448
- text = Util.highlight_keywords(page.text, options[:keywords], 'highlight')
449
- text = text.gsub("\n\n", "<br/><br/>")
450
-
451
469
  <<EOF
452
470
  <div class="page" id="#{page.page_no}">
453
471
  #{with_number}
454
472
  #{comment}
473
+ #{render_page_main(page, options)}
474
+ </div>
475
+ EOF
476
+ end
477
+
478
+ def render_page_main(page, options)
479
+ image_path = Util.image_path(page)
480
+
481
+ text = Util.highlight_keywords(page.text, options[:keywords], 'highlight').gsub("\n\n", "<br/><br/>")
482
+
483
+ if File.exist?(image_path)
484
+ body = <<EOF
485
+ <div class="page-text hidden">#{text}</div>
486
+ <div><img src="/v/#{page.book.id}?image=1&page=#{page.page_no}" width="100%"/></div>
487
+ EOF
488
+ else
489
+ body = text
490
+ end
491
+
492
+ <<EOF
455
493
  <div class="main">
456
- #{text}
494
+ #{body}
457
495
  </div>
458
- </div>
459
496
  EOF
460
497
  end
461
498
 
@@ -1,3 +1,8 @@
1
+ /* bootstrap custom */
2
+ .container {
3
+ width: auto;
4
+ }
5
+
1
6
  /* header */
2
7
  .header {
3
8
  }
@@ -3,6 +3,18 @@ function click_clear_button() {
3
3
  }
4
4
 
5
5
  $(document).ready(function() {
6
+ $(document).on('click', '.page-text-toggle', function(e) {
7
+ var node = $(this).parent().parent().parent().find(".page-text");
8
+
9
+ if (node.hasClass('hidden')) {
10
+ node.removeClass("hidden");
11
+ } else {
12
+ node.addClass("hidden");
13
+ }
14
+
15
+ e.preventDefault();
16
+ });
17
+
6
18
  $(document).on('click', '.star', function(e) {
7
19
  var id = $(this).attr('honyomi-id');
8
20
  var page_no = $(this).attr('honyomi-page-no');
data/test/test_pdf.rb CHANGED
@@ -24,4 +24,15 @@ class TestPdf < MiniTest::Test
24
24
  assert_equal 3, pdf.pages.size
25
25
  end
26
26
  end
27
+
28
+ def test_generate_images
29
+ Dir.mktmpdir do |dir|
30
+ pdf = Honyomi::Pdf.new(test_pdf)
31
+ pdf.generate_images(dir)
32
+
33
+ Dir.chdir(dir) do
34
+ assert_equal %w(book-1.jpg book-2.jpg book-3.jpg), Dir.glob("*")
35
+ end
36
+ end
37
+ end
27
38
  end
data/test/test_util.rb CHANGED
@@ -10,4 +10,11 @@ class TestUtil < MiniTest::Test
10
10
  assert_equal %w(aa), Util::extract_keywords("aa -bb a:c")
11
11
  end
12
12
 
13
+ def test_count_digit
14
+ assert_equal 1, Util::count_digit(0)
15
+ assert_equal 1, Util::count_digit(1)
16
+ assert_equal 2, Util::count_digit(10)
17
+ assert_equal 3, Util::count_digit(100)
18
+ assert_equal 4, Util::count_digit(9999)
19
+ end
13
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honyomi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ongaeshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-23 00:00:00.000000000 Z
11
+ date: 2015-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grn_mini