docbook_xsl_wrapper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +9 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rvmrc +48 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +1 -0
- data/bin/docbook_xsl_wrapper +76 -0
- data/docbook_xsl_wrapper.gemspec +25 -0
- data/lib/docbook_xsl_wrapper/epub.rb +228 -0
- data/lib/docbook_xsl_wrapper/version.rb +3 -0
- data/lib/docbook_xsl_wrapper.rb +5 -0
- data/spec/spec_helper.rb +9 -0
- data/xslt/obfuscate.xsl +12 -0
- metadata +127 -0
data/.autotest
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p374@docbook"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.18.5 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
35
|
+
|
36
|
+
# If you use bundler, this might be useful to you:
|
37
|
+
# if [[ -s Gemfile ]] && {
|
38
|
+
# ! builtin command -v bundle >/dev/null ||
|
39
|
+
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
40
|
+
# }
|
41
|
+
# then
|
42
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
43
|
+
# gem install bundler
|
44
|
+
# fi
|
45
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
46
|
+
# then
|
47
|
+
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
48
|
+
# fi
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mike Cook
|
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.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# DocBook XSL Wrapper
|
2
|
+
|
3
|
+
_Initial 'gemification' of the DocBook XSL Ruby script. Please consider
|
4
|
+
this as an Alpha release._
|
5
|
+
|
6
|
+
The DocBook XSL stylesheets are very powerful and provide an easy way to output
|
7
|
+
your DocBook XML documents into a usable format such as EPUB and PDF. This GEM
|
8
|
+
hopes to make using these stylesheets even easier.
|
9
|
+
|
10
|
+
At present the wrapper will only convert DocBook to EPUB 2 and is
|
11
|
+
intended to be run from the command-line - future versions will have more
|
12
|
+
functionality (see Future Improvements).
|
13
|
+
|
14
|
+
|
15
|
+
## Requirements
|
16
|
+
|
17
|
+
* DocBook
|
18
|
+
* DocBook XSL (~> v1.78.0)
|
19
|
+
* xsltproc
|
20
|
+
|
21
|
+
### NOTE
|
22
|
+
|
23
|
+
The Docbook XSL Wrapper uses xsltproc, which allows all stylesheets to be
|
24
|
+
pulled from the http://docbook.sourceforge.net... URI, but if you have the
|
25
|
+
stylesheets installed locally, xsltproc will rewrite the URI to use local files.
|
26
|
+
|
27
|
+
On my OSX Lion system, "docbook" was installed via Homebrew, but two issues
|
28
|
+
needed fixing before everything worked correctly.
|
29
|
+
|
30
|
+
The catalog file ($XML_CATALOG_FILES) needed updating;
|
31
|
+
|
32
|
+
1. Add an entry to your 1.78.0 path.
|
33
|
+
2. Remove older XSL entries (e.g. 1.76.0 & 1.77.0).
|
34
|
+
|
35
|
+
_Please make sure that xsltproc uses the *1.78.0* stylesheets as default_
|
36
|
+
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
$ gem install docbook_xsl_wrapper
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
$ docbook_xsl_wrapper [OPTIONS] [DocBook File]
|
45
|
+
|
46
|
+
## Future Improvements
|
47
|
+
|
48
|
+
* Better design so it can be used from within other GEMs, as part of a larger tool-chain.
|
49
|
+
* Test that the GEM also works on Linux.
|
50
|
+
* EPUB3 output
|
51
|
+
* Other output formats (e.g. PDF)
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,76 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This program converts DocBook documents into .epub files.
|
3
|
+
#
|
4
|
+
# Usage: dbtoepub [OPTIONS] [DocBook Files]
|
5
|
+
#
|
6
|
+
# .epub is defined by the IDPF at www.idpf.org and is made up of 3 standards:
|
7
|
+
# - Open Publication Structure (OPS)
|
8
|
+
# - Open Packaging Format (OPF)
|
9
|
+
# - Open Container Format (OCF)
|
10
|
+
#
|
11
|
+
# Specific options:
|
12
|
+
# -c, --css [FILE] Use FILE for CSS on generated XHTML.
|
13
|
+
# -d, --debug Show debugging output.
|
14
|
+
# -f, --font [OTF FILE] Embed OTF FILE in .epub.
|
15
|
+
# -h, --help Display usage info.
|
16
|
+
# -s, --stylesheet [XSL FILE] Use XSL FILE as a customization
|
17
|
+
# layer (imports epub/docbook.xsl).
|
18
|
+
# -v, --verbose Make output verbose.
|
19
|
+
|
20
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
21
|
+
|
22
|
+
|
23
|
+
require 'fileutils'
|
24
|
+
require 'optparse'
|
25
|
+
require 'tmpdir'
|
26
|
+
|
27
|
+
require 'docbook_xsl_wrapper'
|
28
|
+
|
29
|
+
verbose = false
|
30
|
+
debug = false
|
31
|
+
css_file = nil
|
32
|
+
otf_files = []
|
33
|
+
customization_layer = nil
|
34
|
+
output_file = nil
|
35
|
+
|
36
|
+
#$DEBUG=true
|
37
|
+
|
38
|
+
# Set up the OptionParser
|
39
|
+
opts = OptionParser.new
|
40
|
+
opts.banner = "Usage: #{File.basename($0)} [OPTIONS] [DocBook Files]
|
41
|
+
|
42
|
+
#{File.basename($0)} converts DocBook <book> and <article>s into to .epub files.
|
43
|
+
|
44
|
+
.epub is defined by the IDPF at www.idpf.org and is made up of 3 standards:
|
45
|
+
- Open Publication Structure (OPS)
|
46
|
+
- Open Packaging Format (OPF)
|
47
|
+
- Open Container Format (OCF)
|
48
|
+
|
49
|
+
Specific options:"
|
50
|
+
opts.on("-c", "--css [FILE]", "Use FILE for CSS on generated XHTML.") {|f| css_file = f}
|
51
|
+
opts.on("-d", "--debug", "Show debugging output.") {debug = true; verbose = true}
|
52
|
+
opts.on("-f", "--font [OTF FILE]", "Embed OTF FILE in .epub.") {|f| otf_files << f}
|
53
|
+
opts.on("-h", "--help", "Display usage info.") {puts opts.to_s; exit 0}
|
54
|
+
opts.on("-o", "--output [OUTPUT FILE]", "Output ePub file as OUTPUT FILE.") {|f| output_file = f}
|
55
|
+
opts.on("-s", "--stylesheet [XSL FILE]", "Use XSL FILE as a customization layer (imports epub/docbook.xsl).") {|f| customization_layer = f}
|
56
|
+
opts.on("-v", "--verbose", "Make output verbose.") {verbose = true}
|
57
|
+
|
58
|
+
db_files = opts.parse(ARGV)
|
59
|
+
if db_files.size == 0
|
60
|
+
puts opts.to_s
|
61
|
+
exit 0
|
62
|
+
end
|
63
|
+
|
64
|
+
db_files.each {|docbook_file|
|
65
|
+
dir = File.expand_path(File.join(Dir.tmpdir, ".epubtmp#{Time.now.to_f.to_s}"))
|
66
|
+
FileUtils.mkdir_p(dir)
|
67
|
+
e = DocbookXslWrapper::Epub.new(docbook_file, dir, css_file, customization_layer, otf_files)
|
68
|
+
|
69
|
+
if output_file
|
70
|
+
epub_file = output_file
|
71
|
+
else
|
72
|
+
epub_file = File.join(File.dirname(docbook_file), File.basename(docbook_file, ".xml") + ".epub")
|
73
|
+
end
|
74
|
+
puts "Rendering DocBook file #{docbook_file} to #{epub_file}" if verbose
|
75
|
+
e.render_to_file(epub_file)
|
76
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/docbook_xsl_wrapper/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "docbook_xsl_wrapper"
|
6
|
+
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.}
|
11
|
+
gem.homepage = ""
|
12
|
+
|
13
|
+
gem.platform = Gem::Platform::RUBY
|
14
|
+
gem.files = `git ls-files`.split($\)
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
|
19
|
+
gem.required_ruby_version = '~> 1.9.3'
|
20
|
+
|
21
|
+
gem.add_development_dependency('rspec', '~> 2.12.0')
|
22
|
+
gem.add_development_dependency('autotest-standalone')
|
23
|
+
gem.add_development_dependency('autotest-growl')
|
24
|
+
gem.add_development_dependency('autotest-fsevent')
|
25
|
+
end
|
@@ -0,0 +1,228 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'rexml/parsers/pullparser'
|
3
|
+
|
4
|
+
module DocbookXslWrapper
|
5
|
+
|
6
|
+
class Epub
|
7
|
+
DOCBOOK_URI = "http://docbook.sourceforge.net/release/xsl/current"
|
8
|
+
CHECKER = "epubcheck"
|
9
|
+
STYLESHEET = File.join(DOCBOOK_URI, "epub", "docbook.xsl")
|
10
|
+
CALLOUT_PATH = File.join('images', 'callouts')
|
11
|
+
CALLOUT_FULL_PATH = File.join(DOCBOOK_URI, CALLOUT_PATH)
|
12
|
+
CALLOUT_LIMIT = 15
|
13
|
+
CALLOUT_EXT = ".png"
|
14
|
+
XSLT_PROCESSOR = "xsltproc"
|
15
|
+
OUTPUT_DIR = ".epubtmp#{Time.now.to_f.to_s}"
|
16
|
+
MIMETYPE = "application/epub+zip"
|
17
|
+
META_DIR = "META-INF"
|
18
|
+
OEBPS_DIR = "OEBPS"
|
19
|
+
ZIPPER = "zip"
|
20
|
+
|
21
|
+
attr_reader :output_dir
|
22
|
+
|
23
|
+
def initialize(docbook_file, output_dir=OUTPUT_DIR, css_file=nil, customization_layer=nil, embedded_fonts=[])
|
24
|
+
@docbook_file = docbook_file
|
25
|
+
@output_dir = output_dir
|
26
|
+
@meta_dir = File.join(@output_dir, META_DIR)
|
27
|
+
@oebps_dir = File.join(@output_dir, OEBPS_DIR)
|
28
|
+
@css_file = css_file ? File.expand_path(css_file) : css_file
|
29
|
+
@embedded_fonts = embedded_fonts
|
30
|
+
@to_delete = []
|
31
|
+
|
32
|
+
if customization_layer
|
33
|
+
@stylesheet = File.expand_path(customization_layer)
|
34
|
+
else
|
35
|
+
@stylesheet = STYLESHEET
|
36
|
+
end
|
37
|
+
|
38
|
+
unless File.exist?(@docbook_file)
|
39
|
+
raise ArgumentError.new("File #{@docbook_file} does not exist")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def render_to_file(output_file, verbose=false)
|
44
|
+
render_to_epub(output_file, verbose)
|
45
|
+
bundle_epub(output_file, verbose)
|
46
|
+
cleanup_files(@to_delete)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.invalid?(file)
|
50
|
+
# Obnoxiously, we can't just check for a non-zero output...
|
51
|
+
cmd = %Q(#{CHECKER} "#{file}")
|
52
|
+
output = `#{cmd} 2>&1`
|
53
|
+
|
54
|
+
if $?.to_i == 0
|
55
|
+
return false
|
56
|
+
else
|
57
|
+
STDERR.puts output if $DEBUG
|
58
|
+
return output
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
def render_to_epub(output_file, verbose)
|
64
|
+
@collapsed_docbook_file = collapse_docbook()
|
65
|
+
|
66
|
+
chunk_quietly = "--stringparam chunk.quietly " + (verbose ? '0' : '1')
|
67
|
+
callout_path = "--stringparam callout.graphics.path #{CALLOUT_PATH}/"
|
68
|
+
callout_limit = "--stringparam callout.graphics.number.limit #{CALLOUT_LIMIT}"
|
69
|
+
callout_ext = "--stringparam callout.graphics.extension #{CALLOUT_EXT}"
|
70
|
+
html_stylesheet = "--stringparam html.stylesheet #{File.basename(@css_file)}" if @css_file
|
71
|
+
base = "--stringparam base.dir #{OEBPS_DIR}/"
|
72
|
+
unless @embedded_fonts.empty?
|
73
|
+
embedded_fonts = @embedded_fonts.map {|f| File.basename(f)}.join(',')
|
74
|
+
font = "--stringparam epub.embedded.fonts \"#{embedded_fonts}\""
|
75
|
+
end
|
76
|
+
meta = "--stringparam epub.metainf.dir #{META_DIR}/"
|
77
|
+
oebps = "--stringparam epub.oebps.dir #{OEBPS_DIR}/"
|
78
|
+
options = [chunk_quietly,
|
79
|
+
callout_path,
|
80
|
+
callout_limit,
|
81
|
+
callout_ext,
|
82
|
+
base,
|
83
|
+
font,
|
84
|
+
meta,
|
85
|
+
oebps,
|
86
|
+
html_stylesheet,
|
87
|
+
].join(" ")
|
88
|
+
# Double-quote stylesheet & file to help Windows cmd.exe
|
89
|
+
db2epub_cmd = %Q(cd "#{@output_dir}" && #{XSLT_PROCESSOR} #{options} "#{@stylesheet}" "#{@collapsed_docbook_file}")
|
90
|
+
STDERR.puts db2epub_cmd if $DEBUG
|
91
|
+
success = system(db2epub_cmd)
|
92
|
+
raise "Could not render as .epub to #{output_file} (#{db2epub_cmd})" unless success
|
93
|
+
@to_delete << Dir["#{@meta_dir}/*"]
|
94
|
+
@to_delete << Dir["#{@oebps_dir}/*"]
|
95
|
+
end
|
96
|
+
|
97
|
+
def bundle_epub(output_file, verbose)
|
98
|
+
|
99
|
+
quiet = verbose ? "" : "-q"
|
100
|
+
mimetype_filename = write_mimetype()
|
101
|
+
meta = File.basename(@meta_dir)
|
102
|
+
oebps = File.basename(@oebps_dir)
|
103
|
+
images = copy_images()
|
104
|
+
csses = copy_csses()
|
105
|
+
fonts = copy_fonts()
|
106
|
+
callouts = copy_callouts()
|
107
|
+
# zip -X -r ../book.epub mimetype META-INF OEBPS
|
108
|
+
# Double-quote stylesheet & file to help Windows cmd.exe
|
109
|
+
zip_cmd = %Q(cd "#{@output_dir}" && #{ZIPPER} #{quiet} -X -r "#{File.expand_path(output_file)}" "#{mimetype_filename}" "#{meta}" "#{oebps}")
|
110
|
+
puts zip_cmd if $DEBUG
|
111
|
+
success = system(zip_cmd)
|
112
|
+
raise "Could not bundle into .epub file to #{output_file}" unless success
|
113
|
+
end
|
114
|
+
|
115
|
+
# Input must be collapsed because REXML couldn't find figures in files that
|
116
|
+
# were XIncluded or added by ENTITY
|
117
|
+
# http://sourceforge.net/tracker/?func=detail&aid=2750442&group_id=21935&atid=373747
|
118
|
+
def collapse_docbook
|
119
|
+
# Double-quote stylesheet & file to help Windows cmd.exe
|
120
|
+
collapsed_file = File.join(File.expand_path(File.dirname(@docbook_file)),
|
121
|
+
'.collapsed.' + File.basename(@docbook_file))
|
122
|
+
entity_collapse_command = %Q(xmllint --loaddtd --noent -o "#{collapsed_file}" "#{@docbook_file}")
|
123
|
+
entity_success = system(entity_collapse_command)
|
124
|
+
raise "Could not collapse named entites in #{@docbook_file}" unless entity_success
|
125
|
+
|
126
|
+
xinclude_collapse_command = %Q(xmllint --xinclude -o "#{collapsed_file}" "#{collapsed_file}")
|
127
|
+
xinclude_success = system(xinclude_collapse_command)
|
128
|
+
raise "Could not collapse XIncludes in #{@docbook_file}" unless xinclude_success
|
129
|
+
|
130
|
+
@to_delete << collapsed_file
|
131
|
+
return collapsed_file
|
132
|
+
end
|
133
|
+
|
134
|
+
def copy_callouts
|
135
|
+
new_callout_images = []
|
136
|
+
if has_callouts?
|
137
|
+
calloutglob = "#{CALLOUT_FULL_PATH}/*#{CALLOUT_EXT}"
|
138
|
+
Dir.glob(calloutglob).each {|img|
|
139
|
+
img_new_filename = File.join(@oebps_dir, CALLOUT_PATH, File.basename(img))
|
140
|
+
|
141
|
+
# TODO: What to rescue for these two?
|
142
|
+
FileUtils.mkdir_p(File.dirname(img_new_filename))
|
143
|
+
FileUtils.cp(img, img_new_filename)
|
144
|
+
@to_delete << img_new_filename
|
145
|
+
new_callout_images << img
|
146
|
+
}
|
147
|
+
end
|
148
|
+
return new_callout_images
|
149
|
+
end
|
150
|
+
|
151
|
+
def copy_fonts
|
152
|
+
new_fonts = []
|
153
|
+
@embedded_fonts.each {|font_file|
|
154
|
+
font_new_filename = File.join(@oebps_dir, File.basename(font_file))
|
155
|
+
FileUtils.cp(font_file, font_new_filename)
|
156
|
+
new_fonts << font_file
|
157
|
+
}
|
158
|
+
return new_fonts
|
159
|
+
end
|
160
|
+
|
161
|
+
def copy_csses
|
162
|
+
if @css_file
|
163
|
+
css_new_filename = File.join(@oebps_dir, File.basename(@css_file))
|
164
|
+
FileUtils.cp(@css_file, css_new_filename)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def copy_images
|
169
|
+
image_references = get_image_refs()
|
170
|
+
new_images = []
|
171
|
+
image_references.each {|img|
|
172
|
+
# TODO: It'd be cooler if we had a filetype lookup rather than just
|
173
|
+
# extension
|
174
|
+
if img =~ /\.(svg|png|gif|jpe?g|xml)/i
|
175
|
+
img_new_filename = File.join(@oebps_dir, img)
|
176
|
+
img_full = File.join(File.expand_path(File.dirname(@docbook_file)), img)
|
177
|
+
|
178
|
+
# TODO: What to rescue for these two?
|
179
|
+
FileUtils.mkdir_p(File.dirname(img_new_filename))
|
180
|
+
puts(img_full + ": " + img_new_filename) if $DEBUG
|
181
|
+
FileUtils.cp(img_full, img_new_filename)
|
182
|
+
@to_delete << img_new_filename
|
183
|
+
new_images << img_full
|
184
|
+
end
|
185
|
+
}
|
186
|
+
return new_images
|
187
|
+
end
|
188
|
+
|
189
|
+
def write_mimetype
|
190
|
+
mimetype_filename = File.join(@output_dir, "mimetype")
|
191
|
+
File.open(mimetype_filename, "w") {|f| f.print MIMETYPE}
|
192
|
+
@to_delete << mimetype_filename
|
193
|
+
return File.basename(mimetype_filename)
|
194
|
+
end
|
195
|
+
|
196
|
+
def cleanup_files(file_list)
|
197
|
+
file_list.flatten.each {|f|
|
198
|
+
# Yikes
|
199
|
+
FileUtils.rm_r(f, :force => true )
|
200
|
+
}
|
201
|
+
end
|
202
|
+
|
203
|
+
# Returns an Array of all of the (image) @filerefs in a document
|
204
|
+
def get_image_refs
|
205
|
+
parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
|
206
|
+
image_refs = []
|
207
|
+
while parser.has_next?
|
208
|
+
el = parser.pull
|
209
|
+
if el.start_element? and (el[0] == "imagedata" or el[0] == "graphic")
|
210
|
+
image_refs << el[1]['fileref']
|
211
|
+
end
|
212
|
+
end
|
213
|
+
return image_refs.uniq
|
214
|
+
end
|
215
|
+
|
216
|
+
# Returns true if the document has code callouts
|
217
|
+
def has_callouts?
|
218
|
+
parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
|
219
|
+
while parser.has_next?
|
220
|
+
el = parser.pull
|
221
|
+
if el.start_element? and (el[0] == "calloutlist" or el[0] == "co")
|
222
|
+
return true
|
223
|
+
end
|
224
|
+
end
|
225
|
+
return false
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/xslt/obfuscate.xsl
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
<?xml version="1.0"?>
|
2
|
+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
|
3
|
+
<xsl:output method="xml" omit-xml-declaration="no" doctype-public="-//OASIS//DTD DocBook XML V4.4//EN" doctype-system="http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" indent="no"/>
|
4
|
+
<xsl:template match="@*|*|comment()|processing-instruction()">
|
5
|
+
<xsl:copy>
|
6
|
+
<xsl:apply-templates select="@*|node()"/>
|
7
|
+
</xsl:copy>
|
8
|
+
</xsl:template>
|
9
|
+
<xsl:template match="text()">
|
10
|
+
<xsl:value-of select="replace(replace(., '[a-z]', 'x'), '[0-9]', 'd')"/>
|
11
|
+
</xsl:template>
|
12
|
+
</xsl:stylesheet>
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: docbook_xsl_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Cook
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.12.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.12.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: autotest-standalone
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: autotest-growl
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: autotest-fsevent
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: DocBook XSL Wrapper let's you easily convert DocBook XML to EPUB using
|
79
|
+
the official DocBook XSL stylesheets.
|
80
|
+
email:
|
81
|
+
- m@mikecook.co.uk
|
82
|
+
executables:
|
83
|
+
- docbook_xsl_wrapper
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .autotest
|
88
|
+
- .gitignore
|
89
|
+
- .rspec
|
90
|
+
- .rvmrc
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- bin/docbook_xsl_wrapper
|
96
|
+
- docbook_xsl_wrapper.gemspec
|
97
|
+
- lib/docbook_xsl_wrapper.rb
|
98
|
+
- lib/docbook_xsl_wrapper/epub.rb
|
99
|
+
- lib/docbook_xsl_wrapper/version.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- xslt/obfuscate.xsl
|
102
|
+
homepage: ''
|
103
|
+
licenses: []
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ~>
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 1.9.3
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.25
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Wrapper for the DocBook XSL stylesheets for easy XML to EPUB
|
126
|
+
test_files:
|
127
|
+
- spec/spec_helper.rb
|