-
Notifications
You must be signed in to change notification settings - Fork 5
/
latex2epub.rb
executable file
·133 lines (112 loc) · 3.9 KB
/
latex2epub.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/ruby
# encoding: utf-8
#
# Copyright (c) 2010 Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
# For details of the GNU LGPL, see the file "COPYING".
$KCODE='u'
require 'pathname'
require 'rexml/document'
include REXML
begin
require 'epubmaker'
rescue LoadError
$LOAD_PATH.unshift("#{ENV["HOME"]}/review/lib")
require 'epubmaker'
end
include EPUBMaker
def main
@params = {
"toclevel" => 3,
"secnolevel" => 2,
"latexml" => "latexml --quiet", # command and options
"latexmlpost" => "latexmlpost --split", # command and options
"latex2epubdir" => Pathname.new(__FILE__).realpath.dirname,
"epubversion" => 2,
"mathml" => nil,
"latexcmd" => "latex",
"dvipscmd" => "dvips -q -S1 -i -E -j0",
}
# FIXME: more options
if ARGV.size != 2
STDERR.puts <<EOT
latex2epub.rb TeXfile YAMLfile
EOT
exit 1
end
yamlfile = ARGV[1]
@params = @params.merge(Producer.load(yamlfile))
@epub = Producer.new(@params)
bookname = @params["bookname"]
texfile = ARGV[0]
xmlfile = File.basename(texfile).sub(/\.tex\Z/i, '.xml')
xhtmlfile = File.basename(texfile).sub(/\.tex\Z/i, '.xhtml')
basetmp = "#{bookname}-output"
basetmp = Dir.mktmpdir if @params["basedebug"].nil?
# Override
ENV["PERL5LIB"] = "#{@params["latex2epubdir"]}/libs/perl"
ENV["LATEXMLLATEXCMD"] = @params["latexcmd"]
ENV["LATEXMLDVIPSCMD"] = @params["dvipscmd"]
fork {
exec("#{@params['latexml']} --destination=#{basetmp}/#{xmlfile} #{texfile}")
}
Process.waitall
mathparams = @params["mathml"].nil? ? "--mathimages --stylesheet=#{@params["latex2epubdir"]}/libs/style-#{@params["language"]}/LaTeXML-epub-nomathml.xsl" :
"--stylesheet=#{@params["latex2epubdir"]}/libs/style-#{@params["language"]}/LaTeXML-epub.xsl"
# FIXME: needs to replace pool also...
fork {
exec("#{@params['latexmlpost']} --format=xhtml #{mathparams} --sourcedirectory=#{File.dirname(texfile)} --destination=#{basetmp}/#{xhtmlfile} #{basetmp}/#{xmlfile}")
}
Process.waitall
File.unlink("#{basetmp}/#{xmlfile}") if @params["basedebug"].nil?
@epub.contents.push(Content.new({ "file" => xhtmlfile,
"title" => @params["title"],
"level" => 1,
}))
@registered = [xhtmlfile]
tocparse(basetmp, xhtmlfile)
importotherfiles(basetmp, basetmp)
epubtmpdir = @params["debug"].nil? ? nil : "#{Dir.pwd}/#{bookname}"
Dir.mkdir(bookname) unless @params["debug"].nil?
@epub.produce("#{bookname}.epub", basetmp, epubtmpdir)
FileUtils.rm_r(basetmp) if @params["basedebug"].nil?
end
def importotherfiles(dir, base=nil)
# loop and register
Dir.foreach(dir) do |f|
next if f =~ /\A\./ || @registered.include?(f)
next if f =~ /\.cache\Z/ || f =~ /\.xml\Z/
if FileTest.directory?("#{dir}/#{f}")
importotherfiles("#{dir}/#{f}", base)
else
dir.chop! if dir =~ /\/\Z/
if base.nil?
@epub.contents.push(Content.new({ "file" => "#{dir}/#{f}"}))
else
if dir == base
@epub.contents.push(Content.new({ "file" => "#{f}"}))
else
@epub.contents.push(Content.new({ "file" => "#{dir.sub(base + "/", '')}/#{f}"}))
end
end
end
end
end
def tocparse(base, file)
# parse toppage file and get toc
doc = Document.new(File.new("#{base}/#{file}")).root
doc.each_element(%Q(//head/link[@rel="next"])) do |e|
level = e.attributes["href"].split(".").size - 1
@epub.contents.push(Content.new({
"file" => e.attributes["href"],
"title" => e.attributes["title"],
"level" => level,
}))
@registered << e.attributes["href"]
tocparse(base, e.attributes["href"])
end
end
main