siuying-pdbook 0.1.2
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/LICENSE +22 -0
- data/README +22 -0
- data/Rakefile +59 -0
- data/bin/pdbook +14 -0
- data/lib/pdbook.rb +8 -0
- data/lib/pdbook/converter.rb +112 -0
- data/pdbook.gemspec +35 -0
- metadata +89 -0
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2009 siu [dot] ying [at] gmail [dot] com
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
|
4
|
+
obtaining a copy of this software and associated documentation
|
|
5
|
+
files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use,
|
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the
|
|
9
|
+
Software is furnished to do so, subject to the following
|
|
10
|
+
conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
PDBook
|
|
2
|
+
======
|
|
3
|
+
|
|
4
|
+
Convert PDB (Palm Database) Ebook to PDF format.
|
|
5
|
+
|
|
6
|
+
Specially handle document from haodoo.net.
|
|
7
|
+
|
|
8
|
+
Usage:
|
|
9
|
+
|
|
10
|
+
Convert 356a.pdb to a PDF file:
|
|
11
|
+
|
|
12
|
+
pdbook 356a.pdb 356a.pdf
|
|
13
|
+
|
|
14
|
+
Convert 356a.pdb to a PDF file, using custom fonts:
|
|
15
|
+
|
|
16
|
+
pdbook 356a.pdb 356a.pdf "Arial Unicode.ttf"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Enjoy!
|
|
20
|
+
|
|
21
|
+
For comments, please contact siu [dot] ying [at] gmail [dot] com
|
|
22
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require 'rake/clean'
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
|
|
5
|
+
require "rake/gempackagetask"
|
|
6
|
+
|
|
7
|
+
task :default => :package
|
|
8
|
+
|
|
9
|
+
# PACKAGING ============================================================
|
|
10
|
+
|
|
11
|
+
# Load the gemspec using the same limitations as github
|
|
12
|
+
def spec
|
|
13
|
+
@spec ||=
|
|
14
|
+
begin
|
|
15
|
+
require 'rubygems/specification'
|
|
16
|
+
data = File.read('pdbook.gemspec')
|
|
17
|
+
spec = nil
|
|
18
|
+
Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
|
|
19
|
+
spec
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
|
24
|
+
pkg.gem_spec = spec
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
desc "Install the Pdbook as a gem"
|
|
28
|
+
task :install => [:repackage] do
|
|
29
|
+
sh %{gem install pkg/#{spec.name}-#{spec.version}}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Gemspec Helpers ====================================================
|
|
33
|
+
|
|
34
|
+
def source_version
|
|
35
|
+
line = File.read('lib/pdbook.rb')[/^\s*VERSION = .*/]
|
|
36
|
+
line.match(/.*VERSION = '(.*)'/)[1]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
task 'pdbook.gemspec' => FileList['lib/**','bin/**','Rakefile','LICENSE','README'] do |f|
|
|
40
|
+
# read spec file and split out manifest section
|
|
41
|
+
spec = File.read(f.name)
|
|
42
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
|
43
|
+
# replace version and date
|
|
44
|
+
head.sub!(/\.version = '.*'/, ".version = '#{source_version}'")
|
|
45
|
+
head.sub!(/\.date = '.*'/, ".date = '#{Date.today.to_s}'")
|
|
46
|
+
# determine file list from git ls-files
|
|
47
|
+
files = `git ls-files`.
|
|
48
|
+
split("\n").
|
|
49
|
+
sort.
|
|
50
|
+
reject{ |file| file =~ /^\./ }.
|
|
51
|
+
reject { |file| file =~ /^doc/ }.
|
|
52
|
+
map{ |file| " #{file}" }.
|
|
53
|
+
join("\n")
|
|
54
|
+
# piece file back together and write...
|
|
55
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
|
56
|
+
spec = [head,manifest,tail].join(" # = MANIFEST =\n")
|
|
57
|
+
File.open(f.name, 'w') { |io| io.write(spec) }
|
|
58
|
+
puts "updated #{f.name}"
|
|
59
|
+
end
|
data/bin/pdbook
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
require "rubygems"
|
|
3
|
+
require "pdbook"
|
|
4
|
+
|
|
5
|
+
if ARGV.size != 2 && ARGV.size != 3
|
|
6
|
+
puts "pdbook - Convert PDB Ebook to PDF"
|
|
7
|
+
puts " pdbook <input-pdb-file> <output-pdf-file> [font-file]"
|
|
8
|
+
|
|
9
|
+
else
|
|
10
|
+
conv = Pdbook::Converter.new(ARGV[0], ARGV[1], ARGV[2])
|
|
11
|
+
conv.convert!
|
|
12
|
+
|
|
13
|
+
end
|
|
14
|
+
|
data/lib/pdbook.rb
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
require "rubygems"
|
|
2
|
+
require "prawn"
|
|
3
|
+
require "palm"
|
|
4
|
+
require 'iconv'
|
|
5
|
+
require 'UniversalDetector' # chardet gem
|
|
6
|
+
require 'logger'
|
|
7
|
+
|
|
8
|
+
require "prawn/measurement_extensions"
|
|
9
|
+
|
|
10
|
+
module Pdbook
|
|
11
|
+
class Converter
|
|
12
|
+
def initialize(input, output, font = "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf", options = {:page_size => "A4", :margin => 0.6.in, :compress => true})
|
|
13
|
+
@input = input
|
|
14
|
+
@output = output
|
|
15
|
+
@font = font
|
|
16
|
+
@options = options
|
|
17
|
+
@pdb = Palm::PDB.new(input)
|
|
18
|
+
@log = Logger.new($STDOUT)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def convert!
|
|
22
|
+
data = @pdb.data
|
|
23
|
+
Prawn::Document.generate @output, @options do |doc|
|
|
24
|
+
doc.font @font
|
|
25
|
+
toc = data.shift.data
|
|
26
|
+
content = data
|
|
27
|
+
|
|
28
|
+
# render cover & toc
|
|
29
|
+
render_toc(doc, toc)
|
|
30
|
+
|
|
31
|
+
# render content
|
|
32
|
+
doc.text_options.update(:wrap => :character, :size => 20, :spacing => 4)
|
|
33
|
+
content.each do |record|
|
|
34
|
+
render_content(doc, record.data)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
# find current document charset
|
|
41
|
+
def charset
|
|
42
|
+
text_check = @pdb.data.first.data.gsub("\e", "")
|
|
43
|
+
enc = UniversalDetector::chardet(text_check)["encoding"]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
# First \e\e\e is marker for first page
|
|
48
|
+
# Subsequent \e is link
|
|
49
|
+
def render_toc(doc, data)
|
|
50
|
+
current_charset = charset
|
|
51
|
+
data = to_utf8(current_charset, data)
|
|
52
|
+
|
|
53
|
+
cover, toc = data.split("\e\e\e")
|
|
54
|
+
if toc.nil?
|
|
55
|
+
toc = cover
|
|
56
|
+
cover = nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# print cover
|
|
60
|
+
unless cover.nil?
|
|
61
|
+
doc.text_options.update(:wrap => :character, :size => 40, :spacing => 4)
|
|
62
|
+
doc.bounding_box [doc.bounds.left, 2*doc.bounds.top/3], :width => doc.bounds.width do
|
|
63
|
+
doc.text cover
|
|
64
|
+
end
|
|
65
|
+
doc.start_new_page
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# print toc
|
|
69
|
+
unless toc.nil?
|
|
70
|
+
doc.text_options.update(:wrap => :character, :size => 20, :spacing => 4)
|
|
71
|
+
toc = toc.split("\e")
|
|
72
|
+
section_cnt = toc.shift.to_i
|
|
73
|
+
if section_cnt == toc.size
|
|
74
|
+
@log.debug "Print TOC"
|
|
75
|
+
# details
|
|
76
|
+
toc.each do |line|
|
|
77
|
+
doc.text line
|
|
78
|
+
end
|
|
79
|
+
doc.start_new_page
|
|
80
|
+
else
|
|
81
|
+
@log.error "TOC size not equals to number of section! #{toc.size} != #{section_cnt}"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def render_content(doc, content)
|
|
87
|
+
current_charset = self.charset
|
|
88
|
+
doc.bounding_box([doc.bounds.left, doc.bounds.top], :width => doc.bounds.width) do
|
|
89
|
+
# main text
|
|
90
|
+
content = to_utf8(current_charset, content)
|
|
91
|
+
section = cleanup_content_text(content).split("\r\n")
|
|
92
|
+
section.each do |line|
|
|
93
|
+
doc.text line
|
|
94
|
+
end
|
|
95
|
+
doc.start_new_page
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def cleanup_content_text(text)
|
|
100
|
+
text.gsub!('﹁', '「')
|
|
101
|
+
text.gsub!('﹂', '」')
|
|
102
|
+
text.gsub!('︽', '《')
|
|
103
|
+
text.gsub!('︾', '》')
|
|
104
|
+
text.gsub!('|', 'ー')
|
|
105
|
+
text
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def to_utf8(charset, text)
|
|
109
|
+
Iconv.conv("UTF-8//IGNORE", charset, text)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
data/pdbook.gemspec
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
|
3
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
4
|
+
s.name = 'pdbook'
|
|
5
|
+
s.version = '0.1.2'
|
|
6
|
+
s.date = '2009-06-09'
|
|
7
|
+
|
|
8
|
+
s.description = "Convert PDB (Palm Database) Ebook to PDF format. Specially handle document from haodoo.net."
|
|
9
|
+
s.summary = "Convert PDB (Palm Database) Ebook to PDF format. Specially handle document from haodoo.net."
|
|
10
|
+
|
|
11
|
+
s.author = "siuying"
|
|
12
|
+
s.email = "siu.ying@gmail.com"
|
|
13
|
+
|
|
14
|
+
# = MANIFEST =
|
|
15
|
+
s.files = %w[
|
|
16
|
+
LICENSE
|
|
17
|
+
README
|
|
18
|
+
Rakefile
|
|
19
|
+
bin/pdbook
|
|
20
|
+
lib/pdbook.rb
|
|
21
|
+
lib/pdbook/converter.rb
|
|
22
|
+
pdbook.gemspec
|
|
23
|
+
]
|
|
24
|
+
# = MANIFEST =
|
|
25
|
+
|
|
26
|
+
s.extra_rdoc_files = %w[README LICENSE]
|
|
27
|
+
s.add_dependency 'prawn'
|
|
28
|
+
s.add_dependency 'palm'
|
|
29
|
+
s.add_dependency 'chardet'
|
|
30
|
+
s.has_rdoc = false
|
|
31
|
+
s.executables = ["pdbook"]
|
|
32
|
+
s.require_paths = %w[lib]
|
|
33
|
+
s.rubygems_version = '1.1.1'
|
|
34
|
+
|
|
35
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: siuying-pdbook
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- siuying
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-06-09 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: prawn
|
|
17
|
+
type: :runtime
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: "0"
|
|
24
|
+
version:
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: palm
|
|
27
|
+
type: :runtime
|
|
28
|
+
version_requirement:
|
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: "0"
|
|
34
|
+
version:
|
|
35
|
+
- !ruby/object:Gem::Dependency
|
|
36
|
+
name: chardet
|
|
37
|
+
type: :runtime
|
|
38
|
+
version_requirement:
|
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: "0"
|
|
44
|
+
version:
|
|
45
|
+
description: Convert PDB (Palm Database) Ebook to PDF format. Specially handle document from haodoo.net.
|
|
46
|
+
email: siu.ying@gmail.com
|
|
47
|
+
executables:
|
|
48
|
+
- pdbook
|
|
49
|
+
extensions: []
|
|
50
|
+
|
|
51
|
+
extra_rdoc_files:
|
|
52
|
+
- README
|
|
53
|
+
- LICENSE
|
|
54
|
+
files:
|
|
55
|
+
- LICENSE
|
|
56
|
+
- README
|
|
57
|
+
- Rakefile
|
|
58
|
+
- bin/pdbook
|
|
59
|
+
- lib/pdbook.rb
|
|
60
|
+
- lib/pdbook/converter.rb
|
|
61
|
+
- pdbook.gemspec
|
|
62
|
+
has_rdoc: false
|
|
63
|
+
homepage:
|
|
64
|
+
post_install_message:
|
|
65
|
+
rdoc_options: []
|
|
66
|
+
|
|
67
|
+
require_paths:
|
|
68
|
+
- lib
|
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: "0"
|
|
74
|
+
version:
|
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: "0"
|
|
80
|
+
version:
|
|
81
|
+
requirements: []
|
|
82
|
+
|
|
83
|
+
rubyforge_project:
|
|
84
|
+
rubygems_version: 1.2.0
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 2
|
|
87
|
+
summary: Convert PDB (Palm Database) Ebook to PDF format. Specially handle document from haodoo.net.
|
|
88
|
+
test_files: []
|
|
89
|
+
|