djvu-tools 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Evan Boyd Sosenko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,83 @@
1
+ = DjVu-Tools
2
+
3
+ == What is DjVu-Tools?
4
+
5
+ DjVu-Tools is a Ruby library for manipulating DjVu files. The interface is built on the DjVuLibre djvused[http://djvu.sourceforge.net/doc/man/djvused.html] command line tool provided by DjVuLibre[http://djvu.sourceforge.net/index.html]. Current tools include:
6
+
7
+ * Easy page title management for page number generation.
8
+ The only way to achieve consistent page numbering for most books is to set the page title for each page.
9
+ The DjVuNumberer class provides a way to title individual pages and number ranges of pages in various styles.
10
+
11
+ == Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'djvu-tools'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install djvu-tools
24
+
25
+ == Using DjVu-Tools
26
+
27
+ === Numberer
28
+
29
+ Fist pick a file to work on:
30
+
31
+ djvu = DjVuNumberer.new 'book.djvu'
32
+
33
+ Then create sections to title or number:
34
+
35
+ * A single page
36
+
37
+ djvu.add_section( { title: 'Cover', range: (1..1) } )
38
+
39
+ * A range of pages with uppercase Roman numerals starting with I:
40
+
41
+ djvu.add_section( { start: 1, range: (2..10), type: :upper_roman } )
42
+
43
+ * A range of pages with lowercase Roman numerals starting with x:
44
+
45
+ djvu.add_section( { start: 10, range: (11..20), type: :lower_roman } )
46
+
47
+ * A range of pages with Arabic numerals starting with 1:
48
+
49
+ djvu.add_section( { start: 1, range: (21..500), type: :arabic } )
50
+
51
+ Finally, you must run djvused and write to the file with
52
+
53
+ djvu.run
54
+
55
+ If you get an error such as
56
+
57
+ File with TITLE 'foo' already exists in the DJVM directory.
58
+
59
+ then a page with the same title already exists (this may be case intensive). You may wish to first relabel all of the pages with a range of numbers you will not use before you attempt again.
60
+
61
+ == Development
62
+
63
+ === Source Repository
64
+
65
+ DjVu-Tools is currently hosted at github. The github web page is
66
+ https://github.com/razor-x/DjVu-Tools. To clone the project run
67
+
68
+ $ git clone git://github.com/razor-x/DjVu-Tools.git
69
+
70
+ After cloning, you should run yard to generate documentation for the source.
71
+
72
+ == License
73
+
74
+ DjVu-Tools is released under the MIT license:
75
+
76
+ * http://www.opensource.org/licenses/MIT
77
+
78
+ == Warranty
79
+
80
+ This software is provided "as is" and without any express or
81
+ implied warranties, including, without limitation, the implied
82
+ warranties of merchantibility and fitness for a particular
83
+ purpose.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path( '../lib/djvu-tools/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Evan Boyd Sosenko']
6
+ gem.email = ['razorx@evansosenko.com']
7
+ gem.description = %q{Ruby toolbox for manipulating DjVu files.}
8
+ gem.summary = %q{Ruby library for djvused. Currenly tools include: easy page title management for page number generation.}
9
+ gem.homepage = "http://evansosenko.com"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = 'djvu-tools'
15
+ gem.require_paths = ['lib']
16
+ gem.platform = Gem::Platform::RUBY
17
+ gem.version = DjVuTools::VERSION
18
+
19
+ gem.add_dependency 'roman-numerals'
20
+ gem.add_dependency 'which'
21
+
22
+ gem.add_development_dependency 'rspec'
23
+
24
+ gem.requirements << 'djvused'
25
+ end
@@ -0,0 +1,34 @@
1
+ # Page title management for page number generation
2
+ class DjVuNumberer < DjVuTools
3
+
4
+ # @see ObjectName#method
5
+ def initialize file
6
+ super
7
+ @sections = []
8
+ end
9
+
10
+ # Add a labeled section.
11
+ # @param [Hash] section
12
+ # @see file:README.rdoc The Numberer section in the README
13
+ def add_section section
14
+
15
+ if section[:start].nil?
16
+ raise ArgumentError, 'Cannot label a range of pages with the same title' if ( section[:range].max - section[:range].min > 0 )
17
+ section[:range].each { |n| @djvu.title_page n, section[:title] }
18
+ else
19
+ section[:range].each_with_index do |n, i|
20
+ number = section[:start] + i
21
+ number =
22
+ case section[:type]
23
+ when :arabic
24
+ number.to_s
25
+ when :upper_roman
26
+ RomanNumerals::to_roman(number).upcase
27
+ when :lower_roman
28
+ RomanNumerals::to_roman(number).downcase
29
+ end
30
+ @djvu.title_page n, "#{number}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ # Interface to djvused.
2
+ class DjVused
3
+
4
+ attr_reader :file
5
+ attr_accessor :command
6
+
7
+ # @see DjVused#new
8
+ def initialize file
9
+ raise SystemCallError, 'djvused command not found' if ( Which::which 'djvused' ).empty?
10
+
11
+ @file = File.expand_path file
12
+ raise RuntimeError, 'File not found' unless ( File.exists? @file )
13
+ @command = ""
14
+ end
15
+
16
+ # Appends command to set page title.
17
+ # @param [Integer] page number
18
+ # @param [String] title cannot contain single or double quotes
19
+ # @return [String] command string
20
+ def title_page page, title
21
+ raise ArgumentError, 'Argument is not an integer' unless page.is_a? Integer
22
+ raise ArgumentError, 'Argument is cannot contain a single or double quote' if title =~ /['"]/
23
+
24
+ @command << %Q{select #{page}; set-page-title "#{title}";}
25
+ end
26
+
27
+ # Runs djvused with command and saves to file.
28
+ def save
29
+ system *['djvused', '-s','-e', @command, @file]
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ class DjVuTools
2
+ # Version number.
3
+ VERSION = "0.0.1"
4
+ end
data/lib/djvu-tools.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'roman-numerals'
2
+ require 'which'
3
+
4
+ require 'djvu-tools/version'
5
+ require 'djvu-tools/djvused'
6
+ require 'djvu-tools/djvu-numberer'
7
+
8
+ # Ruby toolbox for manipulating DjVu files.
9
+ class DjVuTools
10
+
11
+ # @see DjVused#new
12
+ def initialize file
13
+ @djvu = DjVused.new file
14
+ end
15
+
16
+ # Run djvused with the current command string.
17
+ def run
18
+ @djvu.save
19
+ end
20
+ end
@@ -0,0 +1,46 @@
1
+ require 'djvu-tools/djvu-numberer'
2
+
3
+ describe DjVuNumberer do
4
+
5
+ before :each do
6
+ File.stub(:exists?).and_return(true)
7
+ Which.stub(:which).and_return( ['djvused'] )
8
+ @numberer = DjVuNumberer.new 'input file.djvu'
9
+ end
10
+
11
+ describe ".add_section" do
12
+
13
+ it "titles a single page" do
14
+ section = { title: 'the title', range: (10..10) }
15
+ @numberer.instance_eval{ @djvu }.should_receive(:title_page).with(10, section[:title])
16
+ @numberer.add_section section
17
+ end
18
+
19
+ it "will not title a range of pages with a single title" do
20
+ section = { title: 'the title', range: (10..12) }
21
+ expect { @numberer.add_section section }.to raise_error ArgumentError
22
+ end
23
+
24
+ it "titles a range of pages with arabic numerals" do
25
+ section = { start: 42, range: (10..12), type: :arabic }
26
+ section[:range].each_with_index do |n, i|
27
+ @numberer.instance_eval{ @djvu }.should_receive(:title_page).with(n, "#{section[:start] + i}")
28
+ end
29
+ @numberer.add_section section
30
+ end
31
+
32
+ it "titles a range of pages with uppercase roman numerals" do
33
+ section = { start: 3, range: (10..11), type: :upper_roman }
34
+ @numberer.instance_eval{ @djvu }.should_receive(:title_page).with(10, "III")
35
+ @numberer.instance_eval{ @djvu }.should_receive(:title_page).with(11, "IV")
36
+ @numberer.add_section section
37
+ end
38
+
39
+ it "titles a range of pages with lowercaseroman numerals" do
40
+ section = { start: 9, range: (20..21), type: :lower_roman }
41
+ @numberer.instance_eval{ @djvu }.should_receive(:title_page).with(20, "ix")
42
+ @numberer.instance_eval{ @djvu }.should_receive(:title_page).with(21, "x")
43
+ @numberer.add_section section
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,81 @@
1
+ require 'djvu-tools/djvused'
2
+
3
+ describe DjVused do
4
+
5
+ describe ".new" do
6
+
7
+ it "fails when djvused command is not installed" do
8
+ Which.stub(:which).and_return( [] )
9
+ expect { DjVused.new 'input.djvu' }.to raise_error SystemCallError
10
+ end
11
+
12
+ it "fails when file does not exist" do
13
+ File.stub(:exists?).and_return(false)
14
+ expect { DjVused.new 'input.djvu' }.to raise_error RuntimeError
15
+ end
16
+
17
+ it "creates a new object with path to input file" do
18
+ File.stub(:exists?).and_return(true)
19
+ djvu = DjVused.new 'input.djvu'
20
+ djvu.file.should eql ( File.expand_path 'input.djvu' )
21
+ end
22
+ end
23
+
24
+ context "when a new DjVused object can be created" do
25
+
26
+ before :each do
27
+ File.stub(:exists?).and_return(true)
28
+ Which.stub(:which).and_return( ['djvused'] )
29
+ @djvu = DjVused.new 'input file.djvu'
30
+ end
31
+
32
+ describe ".title_page" do
33
+
34
+ context "when given bad arguments" do
35
+
36
+ it "fails if page number is not an integer" do
37
+ expect { @djvu.title_page '1', 'the title' }.to raise_error ArgumentError
38
+ end
39
+
40
+ it "fails if title contains single or double quotes" do
41
+ %W{ 'the title' "the title" 'the title" }.each do |w|
42
+ expect { @djvu.title_page 1, w }.to raise_error ArgumentError
43
+ end
44
+ end
45
+ end
46
+
47
+ context "when given valid arguments" do
48
+
49
+ before :each do
50
+ @args_1 = [ 1, 'the title' ]
51
+ @args_2 = [ 42, 'the other title' ]
52
+
53
+ def cmd args
54
+ %Q{select #{args[0]}; set-page-title "#{args[1]}";}
55
+ end
56
+ end
57
+
58
+ it "generates the command section to set a page title" do
59
+ ( @djvu.title_page *@args_1 ).should eql ( cmd @args_1 )
60
+ end
61
+
62
+ it "adds the command section to the full command" do
63
+ @djvu.title_page *@args_1
64
+ @djvu.title_page *@args_2
65
+
66
+ @djvu.command.should match /^#{cmd @args_1}/
67
+ @djvu.command.should match /#{cmd @args_2}$/
68
+ end
69
+ end
70
+ end
71
+
72
+ describe ".save" do
73
+
74
+ it "runs the command and saves to file" do
75
+ @djvu.command = 'full; command;'
76
+ @djvu.should_receive(:system).with('djvused', '-s', '-e', @djvu.command, an_instance_of(String) )
77
+ @djvu.save
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ require 'djvu-tools'
2
+
3
+ describe DjVuTools do
4
+
5
+ before :each do
6
+ File.stub(:exists?).and_return(true)
7
+ Which.stub(:which).and_return( ['djvused'] )
8
+ @tool = DjVuTools.new 'input file.djvu'
9
+ end
10
+
11
+ describe ".run" do
12
+ it "calls save on the DjVused object" do
13
+ @tool.instance_eval{ @djvu }.should_receive(:save)
14
+ @tool.run
15
+ end
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: djvu-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Evan Boyd Sosenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: roman-numerals
16
+ requirement: &5110660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *5110660
25
+ - !ruby/object:Gem::Dependency
26
+ name: which
27
+ requirement: &5110100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *5110100
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &6112020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *6112020
47
+ description: Ruby toolbox for manipulating DjVu files.
48
+ email:
49
+ - razorx@evansosenko.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.rdoc
58
+ - Rakefile
59
+ - djvu-tools.gemspec
60
+ - lib/djvu-tools.rb
61
+ - lib/djvu-tools/djvu-numberer.rb
62
+ - lib/djvu-tools/djvused.rb
63
+ - lib/djvu-tools/version.rb
64
+ - spec/djvu_numberer_spec.rb
65
+ - spec/djvused_spec.rb
66
+ - spec/djvutools_spec.rb
67
+ homepage: http://evansosenko.com
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements:
86
+ - djvused
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.11
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: ! 'Ruby library for djvused. Currenly tools include: easy page title management
92
+ for page number generation.'
93
+ test_files:
94
+ - spec/djvu_numberer_spec.rb
95
+ - spec/djvused_spec.rb
96
+ - spec/djvutools_spec.rb
97
+ has_rdoc: