pdbook 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.1.2 / 2010-02-02
2
+
3
+ * Init release
4
+
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ Rakefile
4
+ lib/pdbook.rb
5
+ lib/pdbook/converter.rb
6
+ bin/pdbook
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+
4
+ Hoe.plugin :gemcutter
5
+ Hoe.spec 'pdbook' do
6
+ developer('Francis Chong', 'francis@ignition.hk')
7
+ end
8
+
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,8 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'pdbook/converter'
5
+
6
+ module Pdbook
7
+ VERSION = '0.1.2'
8
+ end
@@ -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
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdbook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Francis Chong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-02 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.4.0
24
+ version:
25
+ description: |-
26
+ Convert PDB (Palm Database) Ebook to PDF format.
27
+
28
+ Specially handle document from haodoo.net.
29
+ email:
30
+ - francis@ignition.hk
31
+ executables:
32
+ - pdbook
33
+ extensions: []
34
+
35
+ extra_rdoc_files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ files:
39
+ - History.txt
40
+ - Manifest.txt
41
+ - Rakefile
42
+ - lib/pdbook.rb
43
+ - lib/pdbook/converter.rb
44
+ - bin/pdbook
45
+ has_rdoc: true
46
+ homepage:
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --main
52
+ - README.txt
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: pdbook
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Convert PDB (Palm Database) Ebook to PDF format
74
+ test_files: []
75
+