docbook_xsl_wrapper 0.1.2 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/CHANGELOG +41 -0
  4. data/README.md +9 -17
  5. data/bin/docbook_xsl_wrapper +9 -14
  6. data/docbook_xsl_wrapper.gemspec +8 -7
  7. data/lib/docbook_xsl_wrapper.rb +24 -1
  8. data/lib/docbook_xsl_wrapper/epub.rb +124 -122
  9. data/lib/docbook_xsl_wrapper/options.rb +1 -1
  10. data/lib/docbook_xsl_wrapper/validate.rb +22 -0
  11. data/lib/docbook_xsl_wrapper/version.rb +1 -1
  12. data/schema/docbookxi.rng +18654 -0
  13. data/spec/docbook_xsl_wrapper/validate_spec.rb +64 -0
  14. data/xsl/AUTHORS +4 -0
  15. data/xsl/COPYING +48 -0
  16. data/xsl/REVISION +1 -0
  17. data/xsl/VERSION.xsl +115 -0
  18. data/xsl/common/addns.xsl +124 -0
  19. data/xsl/common/af.xml +1272 -0
  20. data/xsl/common/am.xml +1272 -0
  21. data/xsl/common/ar.xml +1272 -0
  22. data/xsl/common/as.xml +703 -0
  23. data/xsl/common/ast.xml +703 -0
  24. data/xsl/common/autoidx-kimber.xsl +45 -0
  25. data/xsl/common/autoidx-kosek.xsl +155 -0
  26. data/xsl/common/az.xml +715 -0
  27. data/xsl/common/bg.xml +767 -0
  28. data/xsl/common/bn.xml +1272 -0
  29. data/xsl/common/bn_in.xml +703 -0
  30. data/xsl/common/bs.xml +705 -0
  31. data/xsl/common/ca.xml +703 -0
  32. data/xsl/common/charmap.xsl +222 -0
  33. data/xsl/common/common.xsl +2129 -0
  34. data/xsl/common/cs.xml +743 -0
  35. data/xsl/common/cy.xml +1288 -0
  36. data/xsl/common/da.xml +707 -0
  37. data/xsl/common/de.xml +709 -0
  38. data/xsl/common/el.xml +772 -0
  39. data/xsl/common/en.xml +1272 -0
  40. data/xsl/common/entities.ent +67 -0
  41. data/xsl/common/eo.xml +1272 -0
  42. data/xsl/common/es.xml +719 -0
  43. data/xsl/common/et.xml +1272 -0
  44. data/xsl/common/eu.xml +1272 -0
  45. data/xsl/common/fa.xml +703 -0
  46. data/xsl/common/fi.xml +713 -0
  47. data/xsl/common/fr.xml +733 -0
  48. data/xsl/common/ga.xml +1272 -0
  49. data/xsl/common/gentext.xsl +855 -0
  50. data/xsl/common/gl.xml +1272 -0
  51. data/xsl/common/gu.xml +703 -0
  52. data/xsl/common/he.xml +1272 -0
  53. data/xsl/common/hi.xml +703 -0
  54. data/xsl/common/hr.xml +705 -0
  55. data/xsl/common/hu.xml +721 -0
  56. data/xsl/common/id.xml +1272 -0
  57. data/xsl/common/insertfile.xsl +113 -0
  58. data/xsl/common/is.xml +715 -0
  59. data/xsl/common/it.xml +1272 -0
  60. data/xsl/common/ja.xml +703 -0
  61. data/xsl/common/ka.xml +743 -0
  62. data/xsl/common/kn.xml +1272 -0
  63. data/xsl/common/ko.xml +1272 -0
  64. data/xsl/common/ky.xml +775 -0
  65. data/xsl/common/l10n.dtd +64 -0
  66. data/xsl/common/l10n.xml +78 -0
  67. data/xsl/common/l10n.xsl +598 -0
  68. data/xsl/common/la.xml +1272 -0
  69. data/xsl/common/labels.xsl +933 -0
  70. data/xsl/common/lt.xml +721 -0
  71. data/xsl/common/lv.xml +1272 -0
  72. data/xsl/common/ml.xml +703 -0
  73. data/xsl/common/mn.xml +773 -0
  74. data/xsl/common/mr.xml +703 -0
  75. data/xsl/common/nb.xml +1272 -0
  76. data/xsl/common/nds.xml +709 -0
  77. data/xsl/common/nl.xml +703 -0
  78. data/xsl/common/nn.xml +1272 -0
  79. data/xsl/common/olink.xsl +1284 -0
  80. data/xsl/common/or.xml +1272 -0
  81. data/xsl/common/pa.xml +703 -0
  82. data/xsl/common/pi.xml +168 -0
  83. data/xsl/common/pi.xsl +347 -0
  84. data/xsl/common/pl.xml +721 -0
  85. data/xsl/common/pt.xml +1272 -0
  86. data/xsl/common/pt_br.xml +1272 -0
  87. data/xsl/common/refentry.xsl +1353 -0
  88. data/xsl/common/ro.xml +1272 -0
  89. data/xsl/common/ru.xml +769 -0
  90. data/xsl/common/sk.xml +1272 -0
  91. data/xsl/common/sl.xml +1272 -0
  92. data/xsl/common/sq.xml +1272 -0
  93. data/xsl/common/sr.xml +763 -0
  94. data/xsl/common/sr_Latn.xml +722 -0
  95. data/xsl/common/subtitles.xsl +181 -0
  96. data/xsl/common/sv.xml +707 -0
  97. data/xsl/common/ta.xml +703 -0
  98. data/xsl/common/table.xsl +515 -0
  99. data/xsl/common/targetdatabase.dtd +49 -0
  100. data/xsl/common/targets.xsl +338 -0
  101. data/xsl/common/te.xml +703 -0
  102. data/xsl/common/th.xml +1272 -0
  103. data/xsl/common/titles.xsl +850 -0
  104. data/xsl/common/tl.xml +1272 -0
  105. data/xsl/common/tr.xml +709 -0
  106. data/xsl/common/uk.xml +769 -0
  107. data/xsl/common/ur.xml +705 -0
  108. data/xsl/common/utility.xsl +291 -0
  109. data/xsl/common/vi.xml +1272 -0
  110. data/xsl/common/xh.xml +1272 -0
  111. data/xsl/common/zh.xml +703 -0
  112. data/xsl/common/zh_cn.xml +703 -0
  113. data/xsl/common/zh_tw.xml +703 -0
  114. data/xsl/epub/docbook.xsl +1769 -0
  115. data/xsl/epub3/chunk.xsl +30 -0
  116. data/xsl/epub3/docbook-epub.css.xml +142 -0
  117. data/xsl/epub3/docbook.xsl +20 -0
  118. data/xsl/epub3/epub3-chunk-mods.xsl +52 -0
  119. data/xsl/epub3/epub3-element-mods.xsl +2533 -0
  120. data/xsl/epub3/titlepage.templates.xml +712 -0
  121. data/xsl/epub3/titlepage.templates.xsl +3842 -0
  122. data/xsl/lib/lib.xsl +533 -0
  123. data/xsl/xhtml/admon.xsl +142 -0
  124. data/xsl/xhtml/annotations.xsl +160 -0
  125. data/xsl/xhtml/autoidx.xsl +831 -0
  126. data/xsl/xhtml/autotoc.xsl +706 -0
  127. data/xsl/xhtml/biblio-iso690.xsl +1302 -0
  128. data/xsl/xhtml/biblio.xsl +1371 -0
  129. data/xsl/xhtml/block.xsl +584 -0
  130. data/xsl/xhtml/callout.xsl +210 -0
  131. data/xsl/xhtml/chunk-code.xsl +675 -0
  132. data/xsl/xhtml/chunk-common.xsl +1627 -0
  133. data/xsl/xhtml/chunker.xsl +317 -0
  134. data/xsl/xhtml/component.xsl +440 -0
  135. data/xsl/xhtml/division.xsl +214 -0
  136. data/xsl/xhtml/ebnf.xsl +331 -0
  137. data/xsl/xhtml/footnote.xsl +346 -0
  138. data/xsl/xhtml/formal.xsl +496 -0
  139. data/xsl/xhtml/glossary.xsl +602 -0
  140. data/xsl/xhtml/graphics.xsl +1537 -0
  141. data/xsl/xhtml/html-rtf.xsl +321 -0
  142. data/xsl/xhtml/html.xsl +690 -0
  143. data/xsl/xhtml/htmltbl.xsl +102 -0
  144. data/xsl/xhtml/index.xsl +264 -0
  145. data/xsl/xhtml/info.xsl +48 -0
  146. data/xsl/xhtml/inline.xsl +1508 -0
  147. data/xsl/xhtml/its.xsl +109 -0
  148. data/xsl/xhtml/keywords.xsl +38 -0
  149. data/xsl/xhtml/lists.xsl +1227 -0
  150. data/xsl/xhtml/math.xsl +286 -0
  151. data/xsl/xhtml/param.xsl +449 -0
  152. data/xsl/xhtml/pi.xsl +1237 -0
  153. data/xsl/xhtml/qandaset.xsl +440 -0
  154. data/xsl/xhtml/refentry.xsl +307 -0
  155. data/xsl/xhtml/sections.xsl +564 -0
  156. data/xsl/xhtml/synop.xsl +1623 -0
  157. data/xsl/xhtml/table.xsl +1179 -0
  158. data/xsl/xhtml/task.xsl +76 -0
  159. data/xsl/xhtml/titlepage.templates.xsl +4005 -0
  160. data/xsl/xhtml/titlepage.xsl +1108 -0
  161. data/xsl/xhtml/toc.xsl +334 -0
  162. data/xsl/xhtml/verbatim.xsl +389 -0
  163. data/xsl/xhtml/xref.xsl +1268 -0
  164. data/xsl/xhtml5/docbook.xsl +22 -0
  165. data/xsl/xhtml5/html5-chunk-mods.xsl +112 -0
  166. data/xsl/xhtml5/html5-element-mods.xsl +791 -0
  167. data/xsl/xhtml5/xhtml-docbook.xsl +531 -0
  168. metadata +207 -27
  169. data/.rvmrc +0 -48
  170. data/xslt/obfuscate.xsl +0 -12
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b742b5b8c9a3e4e949bb1326bd7636849b772f56e9dc056e8598b5bcd16b5973
4
+ data.tar.gz: 91de30769f58801d6248f363c4c1264b50fb6e50f0faba800d12238c972ec202
5
+ SHA512:
6
+ metadata.gz: e5e3629a4080d708ebab618ca1c560000f463a704655433b0f4ef1522525f49c79af08df546cf7935d1ffada325ab49cb05ffb8610ada4cae576ed9c6fa59894
7
+ data.tar.gz: 5a9aae5725da000feb11cd98666b7bba4bab67f2bb4a5ccff10d9973ed3a0dc6d4002227c0aaaeb4059977c7a97df6779928d87f02d4e98e52cc406852d5a76c
data/.gitignore CHANGED
@@ -3,6 +3,8 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .ruby-gemset
7
+ .ruby-version
6
8
  Gemfile.lock
7
9
  InstalledFiles
8
10
  _yardoc
@@ -0,0 +1,41 @@
1
+ = Release History
2
+
3
+ == 0.1.2 / 2013-04-02 Michael Cook
4
+
5
+ * Now using Namespaced XSL: should produce less errors with some XML
6
+
7
+
8
+ == 0.1.0 / 2013-03-27 Michael Cook
9
+
10
+ * EPUB3 output supported
11
+ * Some other small refactoring
12
+
13
+
14
+ == 0.0.7 / 2013-03-05 Michael Cook
15
+
16
+ * Refactored the way images are copied
17
+
18
+
19
+ == 0.0.6 / 2013-03-05 Michael Cook
20
+
21
+ * Fix bg with the OPF path in container.xml
22
+
23
+
24
+ == 0.0.4 / 2013-03-02 Michael Cook
25
+
26
+ * Bg refactor of the Epub class
27
+
28
+
29
+ == 0.0.3 / 2013-02-27 Michael Cook
30
+
31
+ * Removed development dependency on 'autotest'
32
+
33
+
34
+ == 0.0.2 / 2013-02-23 Michael Cook
35
+
36
+ * Better command-line options
37
+
38
+
39
+ == 0.0.1 / 2013-02-22 Michael Cook
40
+
41
+ * Initial gemification of the original Ruby script
data/README.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # DocBook XSL Wrapper
2
2
 
3
+ **IMPORTANT NOTICE - THIS GEM IS NO LONGER UNDER DEVELOPMENT**
4
+
5
+ I've been struggling to re-work this GEM to be usable as a library, rather than just as a command-line tool. On top of that I think perhaps the GEM naming is not the best.
6
+
7
+ For these reasons, I'm unlikely to develop this GEM any further. I may release an alternate _Docbook to EPUB_ libary at a later date, so keep an eye on my Github account for that.
8
+
9
+ ====
10
+
3
11
  _Please consider this as an Alpha release._
4
12
 
5
13
  The DocBook XSL stylesheets are very powerful and provide an easy way to output
@@ -16,24 +24,8 @@ The original Ruby script can be found at: http://docbook.sourceforge.net/release
16
24
  ## Requirements
17
25
 
18
26
  * DocBook
19
- * DocBook XSL (~> v1.78.1)
20
- * xsltproc
21
-
22
- ### NOTE
23
-
24
- The Docbook XSL Wrapper uses xsltproc, which allows all stylesheets to be
25
- pulled from the http://docbook.sourceforge.net... URI, but if you have the
26
- stylesheets installed locally, xsltproc will rewrite the URI to use local files.
27
-
28
- On my OSX Lion system, "docbook" was installed via Homebrew, but two issues
29
- needed fixing before everything worked correctly.
30
-
31
- The catalog file ($XML_CATALOG_FILES) needed updating;
32
-
33
- 1. Add an entry to your 1.78.1 path.
34
- 2. Remove older XSL entries (e.g. 1.76.0 & 1.77.0).
27
+ * DocBook XSL (included in this repo)
35
28
 
36
- _Please make sure that xsltproc uses the *1.78.1* stylesheets as default_
37
29
 
38
30
 
39
31
  ## Installation
@@ -2,24 +2,19 @@
2
2
 
3
3
  $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
4
4
 
5
- require 'tmpdir'
6
5
  require 'docbook_xsl_wrapper'
7
6
 
8
7
 
9
8
  options = DocbookXslWrapper::Options.parse(ARGV)
9
+ docbook = options.docbook
10
10
 
11
- unless File.exist?(options.docbook)
12
- raise ArgumentError.new("File #{options.docbook} does not exist")
13
- end
14
-
15
- tmp_dir = Dir.mktmpdir
16
- options.destination = tmp_dir
11
+ raise ArgumentError.new("File #{docbook} does not exist") unless File.exist?(docbook)
17
12
 
18
- begin
19
- puts "Rendering DocBook file #{options.docbook} to #{options.output}\n\n" if options.verbose
20
-
21
- epub = DocbookXslWrapper::Epub.new(options)
22
- epub.create
23
- ensure
24
- FileUtils.remove_entry_secure tmp_dir
13
+ doc = DocbookXslWrapper::Validate.new(File.open(docbook, 'rb'))
14
+ unless doc.valid?
15
+ puts "YOUR DOCBOOK XML DOES NOT SEEM TO BE VALID - PLEASE FIX."
16
+ puts
17
+ puts doc.errors
25
18
  end
19
+
20
+ DocbookXslWrapper.build_epub(options)
@@ -1,13 +1,14 @@
1
- # -*- encoding: utf-8 -*-
2
1
  require File.expand_path('../lib/docbook_xsl_wrapper/version', __FILE__)
3
2
 
4
3
  Gem::Specification.new do |gem|
5
4
  gem.name = "docbook_xsl_wrapper"
6
5
  gem.version = DocbookXslWrapper::VERSION
7
- gem.authors = ["Mike Cook"]
8
- gem.email = ["m@mikecook.co.uk"]
9
- gem.summary = %q{Wrapper for the DocBook XSL stylesheets for easy XML to EPUB}
10
- gem.description = %q{DocBook XSL Wrapper let's you easily convert DocBook XML to EPUB using the official DocBook XSL stylesheets.}
6
+ gem.authors = ["Michael R. Cook"]
7
+ gem.email = ["git@mrcook.uk"]
8
+ gem.summary = %q{DocBook XSL Wrapper: validate/transform DocBook 5.x XML documents}
9
+ gem.description = %q{DocBook XSL Wrapper let's you easily validate then convert DocBook 5.x XML documents to EPUB using the official DocBook XSL stylesheets.}
10
+ gem.license = 'MIT'
11
+ gem.homepage = 'https://mrcook.uk'
11
12
 
12
13
  gem.platform = Gem::Platform::RUBY
13
14
  gem.files = `git ls-files`.split($\)
@@ -15,7 +16,7 @@ Gem::Specification.new do |gem|
15
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
18
 
18
- gem.required_ruby_version = '~> 1.9.3'
19
+ gem.add_dependency 'nokogiri', '~> 1.6', '>= 1.6.1'
19
20
 
20
- gem.add_development_dependency('rspec', '~> 2.13.0')
21
+ gem.add_development_dependency 'rspec', '~> 2.14', '>= 2.14.1'
21
22
  end
@@ -1,4 +1,27 @@
1
- require "docbook_xsl_wrapper/version"
1
+ require 'tmpdir'
2
+ require 'nokogiri'
3
+
2
4
  require 'docbook_xsl_wrapper/epub'
3
5
  require 'docbook_xsl_wrapper/options'
6
+ require 'docbook_xsl_wrapper/validate'
7
+
8
+ require "docbook_xsl_wrapper/version"
9
+
10
+ module DocbookXslWrapper
11
+ GEM_PATH = File.expand_path(File.join('..', '..'), __FILE__)
12
+
13
+ def self.build_epub(options)
14
+ tmp_dir = Dir.mktmpdir
15
+ options.destination = tmp_dir
16
+
17
+ begin
18
+ puts "Rendering DocBook file #{options.docbook} to #{options.output}\n\n" if options.verbose
19
+
20
+ epub = Epub.new(options)
21
+ epub.create
22
+ ensure
23
+ FileUtils.remove_entry_secure tmp_dir
24
+ end
25
+ end
4
26
 
27
+ end
@@ -4,184 +4,154 @@ require 'rexml/parsers/pullparser'
4
4
  module DocbookXslWrapper
5
5
 
6
6
  class Epub
7
- attr_reader :options
7
+ attr_reader :options, :xml
8
8
 
9
9
  def initialize(options)
10
10
  @options = options
11
-
12
- case options.format
13
- when 'epub3'
14
- xsl = File.join('epub3', 'chunk.xsl')
15
- else
16
- xsl = File.join('epub', 'docbook.xsl')
17
- end
18
- official_docbook_xsl = File.join('http://docbook.sourceforge.net/release/xsl-ns/current', xsl)
19
-
20
- @options.stylesheet = official_docbook_xsl unless options.stylesheet
11
+ @xml = Nokogiri::XML(File.open(@options.docbook, 'rb'))
21
12
  end
22
13
 
23
14
  def create
24
- begin
25
- render_to_epub
26
- bundle_epub
27
- ensure
28
- FileUtils.remove_entry_secure @collapsed_docbook_file
29
- end
15
+ # Nokogiri doesn't create directories, so we do it manually
16
+ Dir.mkdir(File.join(options.destination, meta_inf_directory))
17
+ Dir.mkdir(File.join(options.destination, oebps_directory))
18
+
19
+ render_to_epub
20
+ bundle_epub
30
21
  end
31
22
 
32
23
  private
33
24
 
34
25
  def render_to_epub
35
- @collapsed_docbook_file = collapse_docbook
26
+ errors = xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
27
+ raise "Could not render as .epub to #{options.output}\n\n(#{errors})" unless errors.empty?
28
+ end
36
29
 
37
- # Double-quote stylesheet & file to help Windows cmd.exe
38
- db2epub_cmd = %Q(cd "#{options.destination}" && xsltproc #{xsl_parser_options} "#{options.stylesheet}" "#{@collapsed_docbook_file}")
39
- STDERR.puts db2epub_cmd if options.debug
40
- success = system(db2epub_cmd)
41
- raise "Could not render as .epub to #{options.output} (#{db2epub_cmd})" unless success
42
- end
43
-
44
- def xsl_parser_options
45
- chunk_quietly = "--stringparam chunk.quietly 1" if options.verbose == false
46
- css = "--stringparam html.stylesheet #{File.basename(options.css)}/" if options.css
47
- base = "--stringparam base.dir #{oebps_path}/"
48
- unless options.fonts.empty?
49
- fonts = options.fonts.map {|f| File.basename(f)}.join(',')
50
- font = "--stringparam epub.embedded.fonts \"#{fonts}\""
30
+ def xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
31
+ begin
32
+ errors = stylesheet.transform(xml, params)
33
+ rescue
34
+ errors = ''
51
35
  end
52
- meta = "--stringparam epub.metainf.dir #{meta_inf_directory}/"
53
- oebps = "--stringparam epub.oebps.dir #{oebps_directory}/"
36
+ errors
37
+ end
38
+
39
+ def stylesheet
40
+ xsl = docbook_xsl_path
41
+ xsl = options.stylesheet unless options.stylesheet.empty?
42
+
43
+ Nokogiri::XSLT(File.open(xsl, 'rb'))
44
+ end
54
45
 
55
- [
56
- chunk_quietly,
57
- base,
58
- font,
59
- meta,
60
- oebps,
61
- css,
62
- ].join(" ")
46
+ def docbook_xsl_path
47
+ case options.format
48
+ when 'epub3'
49
+ File.join(GEM_PATH, 'xsl', 'epub3', 'chunk.xsl')
50
+ else
51
+ File.join(GEM_PATH, 'xsl', 'epub', 'docbook.xsl')
52
+ end
63
53
  end
64
54
 
65
- def collapse_docbook
66
- # Input must be collapsed because REXML couldn't find figures in files that
67
- # were XIncluded or added by ENTITY
68
- # http://sourceforge.net/tracker/?func=detail&aid=2750442&group_id=21935&atid=373747
55
+ def params
56
+ params_list = [
57
+ 'chunk.quietly', "#{verbosity}",
58
+ 'chunk.first.sections', 1,
59
+ 'othercredit.like.author.enabled', 1,
60
+ 'chapter.autolabel', 0,
61
+ 'section.autolabel', 0,
62
+ 'part.autolabel', 0,
63
+ 'base.dir', File.join(options.destination, '/'),
64
+ ]
65
+ params_list.concat(css) if options.css
66
+ params_list.concat(fonts) unless options.fonts.empty?
69
67
 
70
- # Double-quote stylesheet & file to help Windows cmd.exe
71
- collapsed_file = File.join(File.expand_path(File.dirname(options.docbook)),
72
- '.collapsed.' + File.basename(options.docbook))
73
- entity_collapse_command = %Q(xmllint --loaddtd --noent -o "#{collapsed_file}" "#{options.docbook}")
74
- entity_success = system(entity_collapse_command)
75
- raise "Could not collapse named entites in #{options.docbook}" unless entity_success
68
+ Nokogiri::XSLT.quote_params(params_list)
69
+ end
76
70
 
77
- xinclude_collapse_command = %Q(xmllint --xinclude -o "#{collapsed_file}" "#{collapsed_file}")
78
- xinclude_success = system(xinclude_collapse_command)
79
- raise "Could not collapse XIncludes in #{options.docbook}" unless xinclude_success
71
+ def fonts
72
+ ['epub.embedded.fonts', options.fonts.map {|f| File.basename(f)}.join(',')]
73
+ end
80
74
 
81
- collapsed_file
75
+ def css
76
+ ['html.stylesheet', File.join(File.basename(options.css), '/')]
82
77
  end
83
78
 
84
79
  def bundle_epub
85
- quiet = options.verbose ? "" : "-q"
86
- mimetype_filename = write_mimetype
87
- images = copy_images
88
- csses = copy_css
89
- fonts = copy_fonts
90
- callouts = copy_callouts
91
- # zip -X -r ../book.epub mimetype META-INF OEBPS
80
+ copy_media_files_to_epub_dir
81
+ create_mimetype_file if options.format == 'epub' # EPUB3 stylesheet creates this automatically
82
+
83
+ quiet = options.verbose ? '' : '-q'
92
84
  # Double-quote stylesheet & file to help Windows cmd.exe
93
- zip_cmd = %Q(cd "#{options.destination}" && zip #{quiet} -X -r "#{File.expand_path(options.output)}" "#{mimetype_filename}" "#{meta_inf_directory}" "#{oebps_directory}")
85
+ zip_cmd = %Q(cd "#{options.destination}" && zip #{quiet} -X -r "#{File.expand_path(options.output)}" "mimetype" "#{meta_inf_directory}" "#{oebps_directory}")
86
+
94
87
  puts zip_cmd if options.debug
95
88
  success = system(zip_cmd)
96
89
  raise "Could not bundle into .epub file to #{options.output}" unless success
97
90
  end
98
91
 
99
- def write_mimetype
92
+ def create_mimetype_file
100
93
  filename = File.join(options.destination, "mimetype")
101
94
  File.open(filename, "w") {|f| f.print "application/epub+zip"}
102
95
  File.basename(filename)
103
96
  end
104
97
 
105
- def copy_callouts
106
- return unless has_callouts?
107
-
108
- images = Array.new
109
- calloutglob = "#{options.callout_full_path}/*#{options.callout_ext}"
110
- Dir.glob(calloutglob).each do |image|
111
- new_filename = File.join(oebps_path, options.callout_path, File.basename(image))
112
-
113
- # TODO: What to rescue for these two?
114
- FileUtils.mkdir_p(File.dirname(new_filename))
115
- FileUtils.cp(image, new_filename)
116
- images << image
117
- end
118
- images
119
- end
98
+ def copy_media_files_to_epub_dir
99
+ copy_fonts
100
+ copy_css
101
+ copy_images
120
102
 
121
- # Returns true if the document has code callouts
122
- def has_callouts?
123
- parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
124
- while parser.has_next?
125
- element = parser.pull
126
- return true if element.start_element? and (element[0] == "calloutlist" or element[0] == "co")
127
- end
128
- false
103
+ # Callouts disabled in this release until more testing can be done
104
+ #copy_callouts
129
105
  end
130
106
 
131
107
  def copy_fonts
132
- fonts = Array.new
133
- options.fonts.each {|font|
134
- new_filename = File.join(oebps_path, File.basename(font))
135
- FileUtils.cp(font, new_filename)
136
- fonts << font
137
- }
138
- fonts
108
+ return if options.fonts.empty?
109
+
110
+ font_directory = File.join(options.destination, oebps_directory, 'fonts')
111
+ Dir.mkdir(font_directory)
112
+
113
+ options.fonts.each do |font|
114
+ FileUtils.cp(font, File.join(font_directory, File.basename(font)))
115
+ end
139
116
  end
140
117
 
141
118
  def copy_css
142
119
  return unless options.css
143
120
 
144
- filename = File.join(oebps_path, File.basename(options.css))
145
- FileUtils.cp(options.css, filename)
121
+ FileUtils.cp(options.css, File.join(options.destination, oebps_directory, File.basename(options.css)))
146
122
  end
147
123
 
148
124
  def copy_images
149
- images = Array.new
150
125
  xml_image_references.each do |image|
151
- images << copy_image(image)
126
+ copy_image(image)
152
127
  end
153
- images
154
- end
155
-
156
- def copy_image(image)
157
- source_file = File.join(File.expand_path(File.dirname(options.docbook)), image)
158
- destination_file = File.join(oebps_path, image)
159
-
160
- FileUtils.mkdir_p(File.dirname(destination_file))
161
-
162
- puts "Copying image: #{source_file} to #{destination_file}" if options.debug
163
- FileUtils.cp(source_file, destination_file)
164
-
165
- destination_file
166
128
  end
167
129
 
168
130
  def xml_image_references
169
- parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
170
- references = Array.new
171
- while parser.has_next?
172
- element = parser.pull
173
- references << element[1]['fileref'] if is_valid_image_reference?(element)
131
+ refs = Array.new
132
+ xml.xpath('//xmlns:imagedata', '//xmlns:graphic', 'xmlns' => 'http://docbook.org/ns/docbook').each do |node|
133
+ img = node.attribute('fileref').value
134
+ refs << img if is_valid_image?(img)
174
135
  end
175
- references.uniq
136
+
137
+ refs.uniq
176
138
  end
177
139
 
178
- def is_valid_image_reference?(element)
179
- return false unless element.start_element?
180
- return false unless element[0] == 'graphic' or element[0] == 'imagedata'
181
- return true if element[1]['fileref'].match(/\.(jpe?g|png|gif|svg|xml)\Z/i)
140
+ def is_valid_image?(image)
141
+ return true if File.extname(image).match(/\.(jpe?g|png|gif|svg|xml)\z/i)
182
142
  false
183
143
  end
184
144
 
145
+ def copy_image(image)
146
+ source = File.join(File.dirname(options.docbook), image)
147
+ destination = File.join(options.destination, oebps_directory, image)
148
+
149
+ FileUtils.mkdir_p(File.dirname(destination))
150
+
151
+ puts "Copying image: #{source} to #{destination}" if options.debug
152
+ FileUtils.cp(source, destination)
153
+ end
154
+
185
155
  def oebps_directory
186
156
  'OEBPS'
187
157
  end
@@ -194,5 +164,37 @@ module DocbookXslWrapper
194
164
  'META-INF'
195
165
  end
196
166
 
167
+ def verbosity
168
+ return 0 if options.verbose == true
169
+ return 1
170
+ end
171
+
172
+
173
+ # TODO: This method is not being called for the moment....needs to be tested
174
+ def copy_callouts
175
+ return unless has_callouts?
176
+
177
+ images = Array.new
178
+ calloutglob = "#{options.callout_full_path}/*#{options.callout_ext}"
179
+ Dir.glob(calloutglob).each do |image|
180
+ new_filename = File.join(oebps_path, options.callout_path, File.basename(image))
181
+
182
+ # TODO: What to rescue for these two?
183
+ FileUtils.mkdir_p(File.dirname(new_filename))
184
+ FileUtils.cp(image, new_filename)
185
+ images << image
186
+ end
187
+ images
188
+ end
189
+
190
+ def has_callouts?
191
+ parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
192
+ while parser.has_next?
193
+ element = parser.pull
194
+ return true if element.start_element? and (element[0] == "calloutlist" or element[0] == "co")
195
+ end
196
+ false
197
+ end
198
+
197
199
  end
198
200
  end