review-peg 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +36 -0
  3. data/.rubocop.yml +47 -0
  4. data/.rubocop_todo.yml +605 -0
  5. data/.travis.yml +18 -0
  6. data/COPYING +515 -0
  7. data/ChangeLog +2449 -0
  8. data/Dockerfile +22 -0
  9. data/Gemfile +6 -0
  10. data/README.rdoc +81 -0
  11. data/Rakefile +51 -0
  12. data/bin/review-catalog-converter-peg +129 -0
  13. data/bin/review-check-peg +169 -0
  14. data/bin/review-checkdep-peg +63 -0
  15. data/bin/review-compile-peg +202 -0
  16. data/bin/review-epubmaker-legacy-peg +1024 -0
  17. data/bin/review-epubmaker-peg +44 -0
  18. data/bin/review-index-peg +110 -0
  19. data/bin/review-init-peg +151 -0
  20. data/bin/review-pdfmaker-peg +18 -0
  21. data/bin/review-preproc-peg +131 -0
  22. data/bin/review-validate-peg +51 -0
  23. data/bin/review-vol-peg +100 -0
  24. data/debian/README.Debian +12 -0
  25. data/debian/README.source +5 -0
  26. data/debian/changelog +5 -0
  27. data/debian/compat +1 -0
  28. data/debian/control +22 -0
  29. data/debian/copyright +62 -0
  30. data/debian/docs +6 -0
  31. data/debian/manpage.1.ex +59 -0
  32. data/debian/patches/path.diff +91 -0
  33. data/debian/patches/series +1 -0
  34. data/debian/review.install +13 -0
  35. data/debian/review.links +4 -0
  36. data/debian/rules +13 -0
  37. data/debian/source/format +1 -0
  38. data/doc/NEWS.ja.md +350 -0
  39. data/doc/NEWS.md +354 -0
  40. data/doc/catalog.ja.md +53 -0
  41. data/doc/catalog.md +52 -0
  42. data/doc/format.ja.md +734 -0
  43. data/doc/format.md +746 -0
  44. data/doc/format_idg.ja.md +203 -0
  45. data/doc/quickstart.ja.md +222 -0
  46. data/doc/quickstart.md +252 -0
  47. data/doc/ruby-uuid/README +11 -0
  48. data/doc/ruby-uuid/README.ja +34 -0
  49. data/doc/sample.css +108 -0
  50. data/doc/sample.yml +238 -0
  51. data/lib/epubmaker.rb +24 -0
  52. data/lib/epubmaker/content.rb +93 -0
  53. data/lib/epubmaker/epubcommon.rb +424 -0
  54. data/lib/epubmaker/epubv2.rb +139 -0
  55. data/lib/epubmaker/epubv3.rb +222 -0
  56. data/lib/epubmaker/producer.rb +330 -0
  57. data/lib/lineinput.rb +107 -0
  58. data/lib/review.rb +3 -0
  59. data/lib/review/book.rb +43 -0
  60. data/lib/review/book/base.rb +401 -0
  61. data/lib/review/book/chapter.rb +100 -0
  62. data/lib/review/book/compilable.rb +184 -0
  63. data/lib/review/book/image_finder.rb +71 -0
  64. data/lib/review/book/index.rb +413 -0
  65. data/lib/review/book/page_metric.rb +47 -0
  66. data/lib/review/book/part.rb +54 -0
  67. data/lib/review/book/volume.rb +67 -0
  68. data/lib/review/builder.rb +452 -0
  69. data/lib/review/catalog.rb +52 -0
  70. data/lib/review/compiler.rb +5183 -0
  71. data/lib/review/compiler/literals_1_9.kpeg +22 -0
  72. data/lib/review/compiler/literals_1_9.rb +435 -0
  73. data/lib/review/configure.rb +64 -0
  74. data/lib/review/epubbuilder.rb +18 -0
  75. data/lib/review/epubmaker.rb +480 -0
  76. data/lib/review/ewbbuilder.rb +381 -0
  77. data/lib/review/exception.rb +21 -0
  78. data/lib/review/extentions.rb +4 -0
  79. data/lib/review/extentions/array.rb +25 -0
  80. data/lib/review/extentions/object.rb +9 -0
  81. data/lib/review/extentions/string.rb +33 -0
  82. data/lib/review/htmlbuilder.rb +1166 -0
  83. data/lib/review/htmllayout.rb +41 -0
  84. data/lib/review/htmltoc.rb +45 -0
  85. data/lib/review/htmlutils.rb +90 -0
  86. data/lib/review/i18n.rb +96 -0
  87. data/lib/review/i18n.yml +169 -0
  88. data/lib/review/idgxmlbuilder.rb +1233 -0
  89. data/lib/review/inaobuilder.rb +357 -0
  90. data/lib/review/latexbuilder.rb +941 -0
  91. data/lib/review/latexindex.rb +35 -0
  92. data/lib/review/latexutils.rb +95 -0
  93. data/lib/review/layout.tex.erb +340 -0
  94. data/lib/review/lineinput.rb +17 -0
  95. data/lib/review/location.rb +24 -0
  96. data/lib/review/makerhelper.rb +67 -0
  97. data/lib/review/markdownbuilder.rb +339 -0
  98. data/lib/review/node.rb +288 -0
  99. data/lib/review/pdfmaker.rb +332 -0
  100. data/lib/review/preprocessor.rb +530 -0
  101. data/lib/review/review.kpeg +745 -0
  102. data/lib/review/sec_counter.rb +69 -0
  103. data/lib/review/template.rb +21 -0
  104. data/lib/review/textbuilder.rb +17 -0
  105. data/lib/review/textutils.rb +16 -0
  106. data/lib/review/tocparser.rb +348 -0
  107. data/lib/review/tocprinter.rb +205 -0
  108. data/lib/review/topbuilder.rb +796 -0
  109. data/lib/review/unfold.rb +138 -0
  110. data/lib/review/version.rb +3 -0
  111. data/lib/uuid.rb +312 -0
  112. data/review.gemspec +32 -0
  113. data/templates/html/layout-html5.html.erb +17 -0
  114. data/templates/html/layout-xhtml1.html.erb +20 -0
  115. data/templates/ncx/epubv2.ncx.erb +11 -0
  116. data/templates/opf/epubv2.opf.erb +21 -0
  117. data/templates/opf/epubv3.opf.erb +18 -0
  118. data/templates/xml/container.xml.erb +6 -0
  119. data/test/CHAPS +2 -0
  120. data/test/assets/test.xml.erb +3 -0
  121. data/test/assets/test_template.tex +255 -0
  122. data/test/assets/test_template_backmatter.tex +32 -0
  123. data/test/bib.re +13 -0
  124. data/test/book_test_helper.rb +35 -0
  125. data/test/sample-book/README.md +7 -0
  126. data/test/sample-book/src/Rakefile +58 -0
  127. data/test/sample-book/src/_cover.html +3 -0
  128. data/test/sample-book/src/catalog.yml +10 -0
  129. data/test/sample-book/src/ch01.re +71 -0
  130. data/test/sample-book/src/ch02.re +3 -0
  131. data/test/sample-book/src/config.yml +186 -0
  132. data/test/sample-book/src/images/ch01-imgsample.jpg +0 -0
  133. data/test/sample-book/src/images/cover.jpg +0 -0
  134. data/test/sample-book/src/preface.re +15 -0
  135. data/test/sample-book/src/sty/jumoline.sty +310 -0
  136. data/test/sample-book/src/sty/reviewmacro.sty +39 -0
  137. data/test/sample-book/src/style.css +251 -0
  138. data/test/sample-book/src/vendor/jumoline/README +29 -0
  139. data/test/sample-book/src/vendor/jumoline/jumoline.dtx +2988 -0
  140. data/test/sample-book/src/vendor/jumoline/jumoline.ins +6 -0
  141. data/test/test.re +43 -0
  142. data/test/test_book.rb +556 -0
  143. data/test/test_book_chapter.rb +280 -0
  144. data/test/test_book_part.rb +54 -0
  145. data/test/test_builder.rb +80 -0
  146. data/test/test_catalog.rb +119 -0
  147. data/test/test_catalog_converter_cmd.rb +73 -0
  148. data/test/test_compiler.rb +92 -0
  149. data/test/test_configure.rb +50 -0
  150. data/test/test_epub3maker.rb +529 -0
  151. data/test/test_epubmaker.rb +569 -0
  152. data/test/test_epubmaker_cmd.rb +40 -0
  153. data/test/test_helper.rb +92 -0
  154. data/test/test_htmlbuilder.rb +1114 -0
  155. data/test/test_htmltoc.rb +32 -0
  156. data/test/test_htmlutils.rb +50 -0
  157. data/test/test_i18n.rb +180 -0
  158. data/test/test_idgxmlbuilder.rb +608 -0
  159. data/test/test_image_finder.rb +82 -0
  160. data/test/test_inaobuilder.rb +245 -0
  161. data/test/test_index.rb +174 -0
  162. data/test/test_latexbuilder.rb +732 -0
  163. data/test/test_lineinput.rb +182 -0
  164. data/test/test_makerhelper.rb +66 -0
  165. data/test/test_markdownbuilder.rb +125 -0
  166. data/test/test_pdfmaker.rb +171 -0
  167. data/test/test_pdfmaker_cmd.rb +40 -0
  168. data/test/test_preprocessor.rb +23 -0
  169. data/test/test_review_ext.rb +31 -0
  170. data/test/test_template.rb +26 -0
  171. data/test/test_textutils.rb +32 -0
  172. data/test/test_topbuilder.rb +291 -0
  173. data/test/test_uuid.rb +157 -0
  174. metadata +357 -0
@@ -0,0 +1,222 @@
1
+ # encoding: utf-8
2
+ # = epubv3.rb -- EPUB version 3 producer.
3
+ #
4
+ # Copyright (c) 2010-2015 Kenshi Muto
5
+ #
6
+ # This program is free software.
7
+ # You can distribute or modify this program under the terms of
8
+ # the GNU LGPL, Lesser General Public License version 2.1.
9
+ # For details of the GNU LGPL, see the file "COPYING".
10
+ #
11
+
12
+ require 'epubmaker/epubcommon'
13
+
14
+ module EPUBMaker
15
+
16
+ # EPUBv3 is EPUB version 3 producer.
17
+ class EPUBv3 < EPUBCommon
18
+ # Construct object with parameter hash +params+ and message resource hash +res+.
19
+ def initialize(producer)
20
+ super
21
+ end
22
+
23
+ # Return opf file content.
24
+ def opf
25
+ @opf_metainfo = opf_metainfo
26
+ @opf_manifest = opf_manifest
27
+ @opf_toc = opf_tocx
28
+
29
+ tmplfile = File.expand_path('./opf/epubv3.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
30
+ tmpl = ReVIEW::Template.load(tmplfile)
31
+ return tmpl.result(binding)
32
+ end
33
+
34
+ def opf_metainfo
35
+ s = ""
36
+ %w[title language date type format source description relation coverage subject rights].each do |item|
37
+ next if @producer.params[item].nil?
38
+ if @producer.params[item].instance_of?(Array)
39
+ @producer.params[item].each_with_index {|v, i|
40
+ s << %Q[ <dc:#{item} id="#{item}-#{i}">#{CGI.escapeHTML(v.to_s)}</dc:#{item}>\n]
41
+ }
42
+ else
43
+ s << %Q[ <dc:#{item} id="#{item}">#{CGI.escapeHTML(@producer.params[item].to_s)}</dc:#{item}>\n]
44
+ end
45
+ end
46
+
47
+ s << %Q[ <meta property="dcterms:modified">#{@producer.params["modified"]}</meta>\n]
48
+
49
+ # ID
50
+ if @producer.params["isbn"].nil?
51
+ s << %Q[ <dc:identifier id="BookId">#{@producer.params["urnid"]}</dc:identifier>\n]
52
+ else
53
+ s << %Q[ <dc:identifier id="BookId">#{@producer.params["isbn"]}</dc:identifier>\n]
54
+ end
55
+
56
+ # creator (should be array)
57
+ %w[a-adp a-ann a-arr a-art a-asn a-aqt a-aft a-aui a-ant a-bkp a-clb a-cmm a-csl a-dsr a-edt a-ill a-lyr a-mdc a-mus a-nrt a-oth a-pht a-prt a-red a-rev a-spn a-ths a-trc a-trl aut].each do |role|
58
+ next if @producer.params[role].nil?
59
+ @producer.params[role].each_with_index do |v, i|
60
+ if v.instance_of?(Hash)
61
+ s << %Q[ <dc:creator id="#{role}-#{i}">#{CGI.escapeHTML(v["name"])}</dc:creator>\n]
62
+ s << %Q[ <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role.sub('a-', '')}</meta>\n]
63
+ v.each_pair do |name, val|
64
+ next if name == "name"
65
+ s << %Q[ <meta refines="##{role.sub('a-', '')}-#{i}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n]
66
+ end
67
+ else
68
+ s << %Q[ <dc:creator id="#{role}-#{i}">#{CGI.escapeHTML(v)}</dc:creator>\n]
69
+ s << %Q[ <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role.sub('a-', '')}</meta>\n]
70
+ end
71
+ end
72
+ end
73
+
74
+ # contributor (should be array)
75
+ %w[adp ann arr art asn aqt aft aui ant bkp clb cmm csl dsr edt ill lyr mdc mus nrt oth pbd pbl pht prt red rev spn ths trc trl].each do |role|
76
+ next if @producer.params[role].nil?
77
+ @producer.params[role].each_with_index do |v, i|
78
+ if v.instance_of?(Hash)
79
+ s << %Q[ <dc:contributor id="#{role}-#{i}">#{CGI.escapeHTML(v["name"])}</dc:contributor>\n]
80
+ s << %Q[ <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n]
81
+ v.each_pair do |name, val|
82
+ next if name == "name"
83
+ s << %Q[ <meta refines="##{role}-#{i}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n]
84
+ end
85
+ else
86
+ s << %Q[ <dc:contributor id="#{role}-#{i}">#{CGI.escapeHTML(v)}</dc:contributor>\n]
87
+ s << %Q[ <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n]
88
+ end
89
+
90
+ if role == "prt" || role == "pbl"
91
+ if v.instance_of?(Hash)
92
+ s << %Q[ <dc:publisher id="pub-#{role}-#{i}">#{CGI.escapeHTML(v["name"])}</dc:publisher>\n]
93
+ s << %Q[ <meta refines="#pub-#{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n]
94
+ v.each_pair do |name, val|
95
+ next if name == "name"
96
+ s << %Q[ <meta refines="#pub-#{role}-#{i}" property="#{name}">#{CGI.escapeHTML(val)}</meta>\n]
97
+ end
98
+ else
99
+ s << %Q[ <dc:publisher id="pub-#{role}-#{i}">#{CGI.escapeHTML(v)}</dc:publisher>\n]
100
+ s << %Q[ <meta refines="#pub-#{role}-#{i}" property="role" scheme="marc:relators">prt</meta>\n]
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ s
107
+ end
108
+
109
+ def opf_manifest
110
+ s = ""
111
+ s << <<EOT
112
+ <manifest>
113
+ <item properties="nav" id="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}" href="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}" media-type="application/xhtml+xml"/>
114
+ <item id="#{@producer.params["bookname"]}" href="#{@producer.params["cover"]}" media-type="application/xhtml+xml"/>
115
+ EOT
116
+
117
+ if @producer.params["coverimage"]
118
+ @producer.contents.each do |item|
119
+ if item.media =~ /\Aimage/ && File.basename(item.file) == @producer.params["coverimage"]
120
+ s << %Q[ <item properties="cover-image" id="cover-#{item.id}" href="#{item.file}" media-type="#{item.media}"/>\n]
121
+ item.id = nil
122
+ break
123
+ end
124
+ end
125
+ end
126
+
127
+ @producer.contents.each do |item|
128
+ next if item.file =~ /#/ || item.id.nil? # skip subgroup, or id=nil (for cover)
129
+ propstr = ""
130
+ if item.properties.size > 0
131
+ propstr = %Q[ properties="#{item.properties.sort.uniq.join(" ")}"]
132
+ end
133
+ s << %Q[ <item id="#{item.id}" href="#{item.file}" media-type="#{item.media}"#{propstr}/>\n]
134
+ end
135
+ s << %Q[ </manifest>\n]
136
+
137
+ s
138
+ end
139
+
140
+ def opf_tocx
141
+ if @producer.params["epubmaker"]["cover_linear"] && @producer.params["epubmaker"]["cover_linear"] != "no"
142
+ cover_linear = "yes"
143
+ else
144
+ cover_linear = "no"
145
+ end
146
+
147
+ s = ""
148
+ if @producer.params["direction"]
149
+ s << %Q[ <spine page-progression-direction="#{@producer.params["direction"]}">\n]
150
+ else
151
+ s << %Q[ <spine>\n]
152
+ end
153
+ s << %Q[ <itemref idref="#{@producer.params["bookname"]}" linear="#{cover_linear}"/>\n]
154
+ s << %Q[ <itemref idref="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}" />\n] if @producer.params["toc"]
155
+
156
+ @producer.contents.each do |item|
157
+ next if item.media !~ /xhtml\+xml/ # skip non XHTML
158
+ s << %Q[ <itemref idref="#{item.id}"/>\n] if item.notoc.nil?
159
+ end
160
+ s << %Q[ </spine>\n]
161
+
162
+ s
163
+ end
164
+
165
+ def ncx(indentarray)
166
+ if @producer.params["epubmaker"]["flattoc"].nil?
167
+ ncx_main = hierarchy_ncx("ol")
168
+ else
169
+ ncx_main = flat_ncx("ol", @producer.params["epubmaker"]["flattocindent"])
170
+ end
171
+
172
+ @body = <<EOT
173
+ <nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
174
+ <h1 class="toc-title">#{@producer.res.v("toctitle")}</h1>
175
+ #{ncx_main} </nav>
176
+ EOT
177
+
178
+ @title = @producer.res.v("toctitle")
179
+ @language = @producer.params['language']
180
+ @stylesheets = @producer.params["stylesheet"]
181
+ tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
182
+ tmpl = ReVIEW::Template.load(tmplfile)
183
+ return tmpl.result(binding)
184
+ end
185
+
186
+ # Produce EPUB file +epubfile+.
187
+ # +basedir+ points the directory has contents.
188
+ # +tmpdir+ defines temporary directory.
189
+ def produce(epubfile, basedir, tmpdir)
190
+ produce_write_common(basedir, tmpdir)
191
+
192
+ File.open("#{tmpdir}/OEBPS/#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}", "w") {|f| @producer.ncx(f, @producer.params["epubmaker"]["ncxindent"]) }
193
+
194
+ @producer.call_hook(@producer.params["epubmaker"]["hook_prepack"], tmpdir)
195
+ export_zip(tmpdir, epubfile)
196
+ end
197
+
198
+ private
199
+
200
+ # Return cover pointer for opf file
201
+ def cover_in_opf
202
+ s = ""
203
+
204
+ if @producer.params["coverimage"]
205
+ @producer.contents.each do |item|
206
+ if item.media =~ /\Aimage/ && item.file =~ /#{@producer.params["coverimage"]}\Z/
207
+ s << <<EOT
208
+ <item id="#{item.id}" href="#{item.file}" media-type="#{item.media}"/>
209
+ EOT
210
+ break
211
+ end
212
+ end
213
+ end
214
+
215
+ s << <<EOT
216
+ <item id="#{@producer.params["bookname"]}" href="#{@producer.params["cover"]}" media-type="application/xhtml+xml"/>
217
+ EOT
218
+ s
219
+ end
220
+
221
+ end
222
+ end
@@ -0,0 +1,330 @@
1
+ # encoding: utf-8
2
+ # = producer.rb -- EPUB producer.
3
+ #
4
+ # Copyright (c) 2010-2015 Kenshi Muto
5
+ #
6
+ # This program is free software.
7
+ # You can distribute or modify this program under the terms of
8
+ # the GNU LGPL, Lesser General Public License version 2.1.
9
+ # For details of the GNU LGPL, see the file "COPYING".
10
+
11
+ require 'tmpdir'
12
+ require 'fileutils'
13
+ require 'yaml'
14
+ require 'uuid'
15
+ require 'epubmaker/content'
16
+ require 'epubmaker/epubv2'
17
+ require 'epubmaker/epubv3'
18
+ require 'review/i18n'
19
+
20
+ module EPUBMaker
21
+ # EPUBMaker produces EPUB file.
22
+ class Producer
23
+ # Array of content objects.
24
+ attr_accessor :contents
25
+ # Parameter hash.
26
+ attr_accessor :params
27
+ # Message resource object.
28
+ attr_reader :res
29
+
30
+ # Take YAML +file+ and return parameter hash.
31
+ def Producer.load(file)
32
+ raise "Can't open #{yamlfile}." if file.nil? || !File.exist?(file)
33
+ return YAML.load_file(file)
34
+ end
35
+
36
+ # Take YAML +file+ and update parameter hash.
37
+ def load(file)
38
+ raise "Can't open #{yamlfile}." if file.nil? || !File.exist?(file)
39
+ merge_params(@params.merge(YAML.load_file(file)))
40
+ end
41
+
42
+ # Construct producer object.
43
+ # +params+ takes initial parameter hash. This parameters can be overriden by EPUBMaker#load or EPUBMaker#merge_params.
44
+ # +version+ takes EPUB version (default is 2).
45
+ def initialize(params=nil, version=nil)
46
+ @contents = []
47
+ @params = {}
48
+ @epub = nil
49
+ @params["epubversion"] = version unless version.nil?
50
+ @res = ReVIEW::I18n
51
+
52
+ unless params.nil?
53
+ merge_params(params)
54
+ end
55
+ end
56
+
57
+ def coverimage
58
+ if !params["coverimage"]
59
+ return nil
60
+ end
61
+ @contents.each do |item|
62
+ if item.media =~ /\Aimage/ && item.file =~ /#{params["coverimage"]}\Z/ # /
63
+ return item.file
64
+ end
65
+ end
66
+ return nil
67
+ end
68
+
69
+ # Update parameters by merging from new parameter hash +params+.
70
+ def merge_params(params)
71
+ @params = @params.merge(params)
72
+ complement
73
+
74
+ unless @params["epubversion"].nil?
75
+ case @params["epubversion"].to_i
76
+ when 2
77
+ @epub = EPUBMaker::EPUBv2.new(self)
78
+ when 3
79
+ @epub = EPUBMaker::EPUBv3.new(self)
80
+ else
81
+ raise "Invalid EPUB version (#{@params["epubversion"]}.)"
82
+ end
83
+ end
84
+ if params["language"]
85
+ ReVIEW::I18n.locale = params["language"]
86
+ end
87
+ support_legacy_maker
88
+ end
89
+
90
+ # Write mimetype file to IO object +wobj+.
91
+ def mimetype(wobj)
92
+ s = @epub.mimetype
93
+ wobj.print s if !s.nil? && !wobj.nil?
94
+ end
95
+
96
+ # Write opf file to IO object +wobj+.
97
+ def opf(wobj)
98
+ s = @epub.opf
99
+ wobj.puts s if !s.nil? && !wobj.nil?
100
+ end
101
+
102
+ # Write ncx file to IO object +wobj+. +indentarray+ defines prefix
103
+ # string for each level.
104
+ def ncx(wobj, indentarray=[])
105
+ s = @epub.ncx(indentarray)
106
+ wobj.puts s if !s.nil? && !wobj.nil?
107
+ end
108
+
109
+ # Write container file to IO object +wobj+.
110
+ def container(wobj)
111
+ s = @epub.container
112
+ wobj.puts s if !s.nil? && !wobj.nil?
113
+ end
114
+
115
+ # Write cover file to IO object +wobj+.
116
+ # If Producer#params["coverimage"] is defined, it will be used for
117
+ # the cover image.
118
+ def cover(wobj)
119
+ type = (@params["epubversion"] >= 3) ? "cover" : nil
120
+ s = @epub.cover(type)
121
+ wobj.puts s if !s.nil? && !wobj.nil?
122
+ end
123
+
124
+ # Write title file (copying) to IO object +wobj+.
125
+ def titlepage(wobj)
126
+ s = @epub.titlepage
127
+ wobj.puts s if !s.nil? && !wobj.nil?
128
+ end
129
+
130
+ # Write colophon file to IO object +wobj+.
131
+ def colophon(wobj)
132
+ s = @epub.colophon
133
+ wobj.puts s if !s.nil? && !wobj.nil?
134
+ end
135
+
136
+ # Write own toc file to IO object +wobj+.
137
+ def mytoc(wobj)
138
+ s = @epub.mytoc
139
+ wobj.puts s if !s.nil? && !wobj.nil?
140
+ end
141
+
142
+ # Add informations of figure files in +path+ to contents array.
143
+ # +base+ defines a string to remove from path name.
144
+ def import_imageinfo(path, base=nil, allow_exts=nil)
145
+ return nil unless File.exist?(path)
146
+ allow_exts = @params["image_ext"] if allow_exts.nil?
147
+ Dir.foreach(path) do |f|
148
+ next if f =~ /\A\./
149
+ if f =~ /\.(#{allow_exts.join("|")})\Z/i
150
+ path.chop! if path =~ /\/\Z/
151
+ if base.nil?
152
+ @contents.push(EPUBMaker::Content.new({"file" => "#{path}/#{f}"}))
153
+ else
154
+ @contents.push(EPUBMaker::Content.new({"file" => "#{path.sub(base + "/", '')}/#{f}"}))
155
+ end
156
+ end
157
+ if FileTest.directory?("#{path}/#{f}")
158
+ import_imageinfo("#{path}/#{f}", base)
159
+ end
160
+ end
161
+ end
162
+
163
+ alias_method :importImageInfo, :import_imageinfo
164
+
165
+ # Produce EPUB file +epubfile+.
166
+ # +basedir+ points the directory has contents (default: current directory.)
167
+ # +tmpdir+ defines temporary directory.
168
+ def produce(epubfile, basedir=nil, tmpdir=nil)
169
+ current = Dir.pwd
170
+ basedir = current if basedir.nil?
171
+
172
+ _tmpdir = tmpdir.nil? ? Dir.mktmpdir : tmpdir
173
+ epubfile = "#{current}/#{epubfile}" if epubfile !~ /\A\// # /
174
+
175
+ # FIXME: error check
176
+ File.unlink(epubfile) if File.exist?(epubfile)
177
+
178
+ begin
179
+ @epub.produce(epubfile, basedir, _tmpdir)
180
+ ensure
181
+ FileUtils.rm_r(_tmpdir) if tmpdir.nil?
182
+ end
183
+ end
184
+
185
+ def call_hook(filename, *params)
186
+ if !filename.nil? && File.exist?(filename) && FileTest.executable?(filename)
187
+ if ENV["REVIEW_SAFE_MODE"].to_i & 1 > 0
188
+ warn "hook is prohibited in safe mode. ignored."
189
+ else
190
+ system(filename, *params)
191
+ end
192
+ end
193
+ end
194
+
195
+ private
196
+
197
+ # Complement parameters.
198
+ def complement
199
+ @params["htmlext"] = "html" if @params["htmlext"].nil?
200
+ defaults = {
201
+ "cover" => "#{@params["bookname"]}.#{@params["htmlext"]}",
202
+ "title" => @params["booktitle"],
203
+ "language" => "ja",
204
+ "date" => Time.now.strftime("%Y-%m-%d"),
205
+ "modified" => Time.now.strftime("%Y-%02m-%02dT%02H:%02M:%02SZ"),
206
+ "urnid" => "urn:uid:#{UUID.create}",
207
+ "isbn" => nil,
208
+ "toclevel" => 2,
209
+ "stylesheet" => [],
210
+ "epubversion" => 2,
211
+ "htmlversion" => 4,
212
+ "secnolevel" => 2,
213
+ "pre_secnolevel" => 0,
214
+ "post_secnolevel" => 1,
215
+ "part_secnolevel" => 1,
216
+ "titlepage" => nil,
217
+ "titlefile" => nil,
218
+ "originaltitlefile" => nil,
219
+ "profile" => nil,
220
+ "colophon" => nil,
221
+ "epubmaker" => {
222
+ "flattoc" => nil,
223
+ "flattocindent" => true,
224
+ "ncx_indent" => [],
225
+ "zip_stage1" => "zip -0Xq",
226
+ "zip_stage2" => "zip -Xr9Dq",
227
+ "zip_addpath" => nil,
228
+ "hook_beforeprocess" => nil,
229
+ "hook_afterfrontmatter" => nil,
230
+ "hook_afterbody" => nil,
231
+ "hook_afterbackmatter" => nil,
232
+ "hook_aftercopyimage" => nil,
233
+ "hook_prepack" => nil,
234
+ "rename_for_legacy" => nil,
235
+ "verify_target_images" => nil,
236
+ "force_include_images" => [],
237
+ "cover_linear" => nil,
238
+ },
239
+ "imagedir" => "images",
240
+ "fontdir" => "fonts",
241
+ "image_ext" => %w(png gif jpg jpeg svg ttf woff otf),
242
+ "font_ext" => %w(ttf woff otf),
243
+ }
244
+
245
+ defaults.each_pair do |k, v|
246
+ if k == "epubmaker" && !@params[k].nil?
247
+ v.each_pair do |k2, v2|
248
+ @params[k][k2] = v2 if @params[k][k2].nil?
249
+ end
250
+ else
251
+ @params[k] = v if @params[k].nil?
252
+ end
253
+ end
254
+
255
+ deprecated_parameters = {
256
+ "ncxindent" => "epubmaker:ncxindent",
257
+ "flattoc" => "epubmaker:flattoc",
258
+ "flattocindent" => "epubmaker:flattocindent",
259
+ "hook_beforeprocess" => "epubmaker:hook_beforeprocess",
260
+ "hook_afterfrontmatter" => "epubmaker:hook_afterfrontmatter",
261
+ "hook_afterbody" => "epubmaker:hook_afterbody",
262
+ "hook_afterbackmatter" => "epubmaker:hook_afterbackmatter",
263
+ "hook_aftercopyimage" => "epubmaker:hook_aftercopyimage",
264
+ "hook_prepack" => "epubmaker:hook_prepack",
265
+ "rename_for_legacy" => "epubmaker:rename_for_legacy",
266
+ "zip_stage1" => "epubmaker:zip_stage1",
267
+ "zip_stage2" => "epubmaker:zip_stage2",
268
+ "zip_addpath" => "epubmaker:zip_addpath",
269
+ "verify_target_images" => "epubmaker:verify_target_images",
270
+ "force_include_images" => "epubmaker:force_include_images",
271
+ "cover_linear" => "epubmaker:cover_linear",
272
+ }
273
+
274
+ deprecated_parameters.each_pair do |k, v|
275
+ unless @params[k].nil?
276
+ sa = v.split(":", 2)
277
+ warn "Parameter #{k} is deprecated. Use:\n#{sa[0]}:\n #{sa[1]}: ...\n\n"
278
+ @params[sa[0]][sa[1]] = @params[k]
279
+ @params.delete(k)
280
+ end
281
+ end
282
+
283
+ @params["htmlversion"] = 5 if @params["epubversion"] >= 3
284
+
285
+ %w[bookname title].each do |k|
286
+ raise "Key #{k} must have a value. Abort." if @params[k].nil?
287
+ end
288
+ # array
289
+ %w[subject aut a-adp a-ann a-arr a-art a-asn a-aqt a-aft a-aui a-ant a-bkp a-clb a-cmm a-dsr a-edt a-ill a-lyr a-mdc a-mus a-nrt a-oth a-pht a-prt a-red a-rev a-spn a-ths a-trc a-trl adp ann arr art asn aut aqt aft aui ant bkp clb cmm dsr edt ill lyr mdc mus nrt oth pht pbl prt red rev spn ths trc trl stylesheet rights].each do |item|
290
+ @params[item] = [@params[item]] if !@params[item].nil? && @params[item].instance_of?(String)
291
+ end
292
+ # optional
293
+ # type, format, identifier, source, relation, coverpage, aut
294
+ end
295
+
296
+ def support_legacy_maker
297
+ # legacy review-epubmaker support
298
+ if @params["flag_legacy_coverfile"].nil? && !@params["coverfile"].nil? && File.exist?(@params["coverfile"])
299
+ @params["cover"] = "#{@params["bookname"]}-cover.#{@params["htmlext"]}"
300
+ @epub.legacy_cover_and_title_file(@params["coverfile"], @params["cover"])
301
+ @params["flag_legacy_coverfile"] = true
302
+ warn "Parameter 'coverfile' is obsolete. Please use 'cover' and make complete html file with header and footer."
303
+ end
304
+
305
+ if @params["flag_legacy_titlepagefile"].nil? && !@params["titlepagefile"].nil? && File.exist?(@params["titlepagefile"])
306
+ @params["titlefile"] = "#{@params["bookname"]}-title.#{@params["htmlext"]}"
307
+ @params["titlepage"] = true
308
+ @epub.legacy_cover_and_title_file(@params["titlepagefile"], @params["titlefile"])
309
+ @params["flag_legacy_titlepagefile"] = true
310
+ warn "Parameter 'titlepagefile' is obsolete. Please use 'titlefile' and make complete html file with header and footer."
311
+ end
312
+
313
+ if @params["flag_legacy_backcoverfile"].nil? && !@params["backcoverfile"].nil? && File.exist?(@params["backcoverfile"])
314
+ @params["backcover"] = "#{@params["bookname"]}-backcover.#{@params["htmlext"]}"
315
+ @epub.legacy_cover_and_title_file(@params["backcoverfile"], @params["backcover"])
316
+ @params["flag_legacy_backcoverfile"] = true
317
+ warn "Parameter 'backcoverfile' is obsolete. Please use 'backcover' and make complete html file with header and footer."
318
+ end
319
+
320
+ if @params["flag_legacy_pubhistory"].nil? && !@params["pubhistory"].nil?
321
+ @params["history"] = [[]]
322
+ @params["pubhistory"].split("\n").each do |date|
323
+ @params["history"][0].push(date.sub(/(\d+)年(\d+)月(\d+)日/, '\1-\2-\3'))
324
+ end
325
+ @params["flag_legacy_pubhistory"] = true
326
+ warn "Parameter 'pubhistory' is obsolete. Please use 'history' array."
327
+ end
328
+ end
329
+ end
330
+ end