Class: ReVIEW::PDFMaker
- Includes:
- FileUtils, LaTeXUtils
- Defined in:
- lib/review/pdfmaker.rb
Constant Summary
Constants included from LaTeXUtils
LaTeXUtils::METACHARS, LaTeXUtils::METACHARS_INVERT, LaTeXUtils::METACHARS_RE
Class Method Summary collapse
Instance Method Summary collapse
- #build_path(config) ⇒ Object
- #call_hook(hookname, config) ⇒ Object
- #check_book(config) ⇒ Object
- #check_compile_status(ignore_errors) ⇒ Object
- #copy_images(from, to) ⇒ Object
- #copyStyToDir(dirname, copybase, extname = "sty") ⇒ Object
- #error(msg) ⇒ Object
- #execute(*args) ⇒ Object
- #generate_pdf(config, yamlfile) ⇒ Object
- #get_template(config) ⇒ Object
- #join_with_separator(value, sep) ⇒ Object
- #make_authors(config) ⇒ Object
- #make_colophon(config) ⇒ Object
- #make_colophon_role(role, config) ⇒ Object
- #make_custom_titlepage(coverfile) ⇒ Object
- #output_chaps(filename, config, yamlfile) ⇒ Object
- #parse_opts(args) ⇒ Object
- #system_or_raise(*args) ⇒ Object
- #warn(msg) ⇒ Object
Methods included from LaTeXUtils
#escape_index, #escape_latex, #escape_url, #macro, #unescape_latex
Class Method Details
.execute(*args) ⇒ Object
57 58 59 |
# File 'lib/review/pdfmaker.rb', line 57 def self.execute(*args) self.new.execute(*args) end |
Instance Method Details
#build_path(config) ⇒ Object
43 44 45 |
# File 'lib/review/pdfmaker.rb', line 43 def build_path(config) "./#{config["bookname"]}-pdf" end |
#call_hook(hookname, config) ⇒ Object
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/review/pdfmaker.rb', line 308 def call_hook(hookname, config) if config["pdfmaker"].instance_of?(Hash) && config["pdfmaker"][hookname] hook = File.absolute_path(config["pdfmaker"][hookname], @basedir) if ENV["REVIEW_SAFE_MODE"].to_i & 1 > 0 warn "hook configuration is prohibited in safe mode. ignored." else system_or_raise("#{hook} #{Dir.pwd} #{@basedir}") end end end |
#check_book(config) ⇒ Object
38 39 40 41 |
# File 'lib/review/pdfmaker.rb', line 38 def check_book(config) pdf_file = config["bookname"]+".pdf" File.unlink(pdf_file) if File.exist?(pdf_file) end |
#check_compile_status(ignore_errors) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/review/pdfmaker.rb', line 47 def check_compile_status(ignore_errors) return unless @compile_errors if ignore_errors $stderr.puts "compile error, but try to generate PDF file" else error "compile error, No PDF file output." end end |
#copy_images(from, to) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/review/pdfmaker.rb', line 203 def copy_images(from, to) if File.exist?(from) Dir.mkdir(to) ReVIEW::MakerHelper.copy_images_to_dir(from, to) Dir.chdir(to) do images = Dir.glob("**/*").find_all{|f| File.file?(f) and f =~ /\.(jpg|jpeg|png|pdf)\z/ } break if images.empty? system("extractbb", *images) unless system("extractbb", "-m", *images) system_or_raise("ebb", *images) end end end end |
#copyStyToDir(dirname, copybase, extname = "sty") ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/review/pdfmaker.rb', line 291 def copyStyToDir(dirname, copybase, extname = "sty") unless File.directory?(dirname) $stderr.puts "No such directory - #{dirname}" return end Dir.open(dirname) {|dir| dir.each {|fname| next if fname =~ /^\./ if fname =~ /\.(#{extname})$/i Dir.mkdir(copybase) unless File.exist?(copybase) FileUtils.cp "#{dirname}/#{fname}", copybase end } } end |
#error(msg) ⇒ Object
29 30 31 32 |
# File 'lib/review/pdfmaker.rb', line 29 def error(msg) $stderr.puts "#{File.basename($0, '.*')}: error: #{msg}" exit 1 end |
#execute(*args) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/review/pdfmaker.rb', line 87 def execute(*args) config = ReVIEW::Configure.values cmd_config, yamlfile = parse_opts(args) config.merge!(YAML.load_file(yamlfile)) # YAML configs will be overridden by command line options. config.merge!(cmd_config) I18n.setup(config["language"]) generate_pdf(config, yamlfile) end |
#generate_pdf(config, yamlfile) ⇒ Object
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/review/pdfmaker.rb', line 98 def generate_pdf(config, yamlfile) check_book(config) @basedir = Dir.pwd @path = build_path(config) bookname = config["bookname"] Dir.mkdir(@path) @chaps_fnames = Hash.new{|h, key| h[key] = ""} @compile_errors = nil book = ReVIEW::Book.load(@basedir) book.config = config book.parts.each do |part| if part.name.present? if part.file? output_chaps(part.name, config, yamlfile) @chaps_fnames["CHAPS"] << %Q|\\input{#{part.name}.tex}\n| else @chaps_fnames["CHAPS"] << %Q|\\part{#{part.name}}\n| end end part.chapters.each do |chap| filename = File.basename(chap.path, ".*") output_chaps(filename, config, yamlfile) @chaps_fnames["PREDEF"] << "\\input{#{filename}.tex}\n" if chap.on_PREDEF? @chaps_fnames["CHAPS"] << "\\input{#{filename}.tex}\n" if chap.on_CHAPS? @chaps_fnames["APPENDIX"] << "\\input{#{filename}.tex}\n" if chap.on_APPENDIX? @chaps_fnames["POSTDEF"] << "\\input{#{filename}.tex}\n" if chap.on_POSTDEF? end end check_compile_status(config["ignore-errors"]) config["pre_str"] = @chaps_fnames["PREDEF"] config["chap_str"] = @chaps_fnames["CHAPS"] config["appendix_str"] = @chaps_fnames["APPENDIX"] config["post_str"] = @chaps_fnames["POSTDEF"] config["usepackage"] = "" if config["texstyle"] config["usepackage"] = "\\usepackage{#{config['texstyle']}}" end copy_images("./images", "#{@path}/images") copyStyToDir(Dir.pwd + "/sty", @path) copyStyToDir(Dir.pwd + "/sty", @path, "fd") copyStyToDir(Dir.pwd + "/sty", @path, "cls") copyStyToDir(Dir.pwd, @path, "tex") Dir.chdir(@path) { template = get_template(config) File.open("./book.tex", "wb"){|f| f.write(template)} call_hook("hook_beforetexcompile", config) ## do compile enc = config["params"].to_s.split(/\s+/).find{|i| i =~ /\A--outencoding=/ } kanji = 'utf8' texcommand = "platex" = "-kanji=#{kanji}" dvicommand = "dvipdfmx" = "-d 5" if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0 warn "command configuration is prohibited in safe mode. ignored." else texcommand = config["texcommand"] if config["texcommand"] dvicommand = config["dvicommand"] if config["dvicommand"] = config["dvioptions"] if config["dvioptions"] if enc kanji = enc.split(/\=/).last.gsub(/-/, '').downcase = "-kanji=#{kanji}" end = config["texoptions"] if config["texoptions"] end 3.times do system_or_raise("#{texcommand} #{} book.tex") end call_hook("hook_aftertexcompile", config) if File.exist?("book.dvi") system_or_raise("#{dvicommand} #{} book.dvi") end } call_hook("hook_afterdvipdf", config) FileUtils.cp("#{@path}/book.pdf", "#{@basedir}/#{bookname}.pdf") unless config["debug"] remove_entry_secure @path end end |
#get_template(config) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/review/pdfmaker.rb', line 270 def get_template(config) dclass = config["texdocumentclass"] || [] documentclass = dclass[0] || "jsbook" documentclassoption = dclass[1] || "oneside" okuduke = make_colophon(config) = (config) custom_titlepage = make_custom_titlepage(config["coverfile"]) template = File.('layout.tex.erb', File.dirname(__FILE__)) layout_file = File.join(@basedir, "layouts", "layout.tex.erb") if File.exist?(layout_file) template = layout_file end erb = ERB.new(File.open(template).read) values = config # must be 'values' for legacy files erb.result(binding) end |
#join_with_separator(value, sep) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/review/pdfmaker.rb', line 229 def join_with_separator(value, sep) if value.kind_of? Array value.join(sep) else value end end |
#make_authors(config) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/review/pdfmaker.rb', line 253 def (config) = "" if config["aut"].present? = join_with_separator(config["aut"], ReVIEW::I18n.t("names_splitter")) = ReVIEW::I18n.t("author_with_label", ) end if config["csl"].present? csl_names = join_with_separator(config["csl"], ReVIEW::I18n.t("names_splitter")) += " \\\\\n"+ ReVIEW::I18n.t("supervisor_with_label", csl_names) end if config["trl"].present? trl_names = join_with_separator(config["trl"], ReVIEW::I18n.t("names_splitter")) += " \\\\\n"+ ReVIEW::I18n.t("translator_with_label", trl_names) end end |
#make_colophon(config) ⇒ Object
245 246 247 248 249 250 251 |
# File 'lib/review/pdfmaker.rb', line 245 def make_colophon(config) colophon = "" config["colophon_order"].each do |role| colophon += make_colophon_role(role, config) end colophon end |
#make_colophon_role(role, config) ⇒ Object
237 238 239 240 241 242 243 |
# File 'lib/review/pdfmaker.rb', line 237 def make_colophon_role(role, config) if config[role].present? return "#{ReVIEW::I18n.t(role)} & #{escape_latex(join_with_separator(config[role], ReVIEW::I18n.t("names_splitter")))} \\\\\n" else "" end end |
#make_custom_titlepage(coverfile) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/review/pdfmaker.rb', line 220 def make_custom_titlepage(coverfile) coverfile_sty = coverfile.to_s.sub(/\.[^.]+$/, ".tex") if File.exist?(coverfile_sty) File.read(coverfile_sty) else nil end end |
#output_chaps(filename, config, yamlfile) ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/review/pdfmaker.rb', line 192 def output_chaps(filename, config, yamlfile) $stderr.puts "compiling #{filename}.tex" cmd = "#{ReVIEW::MakerHelper.bindir}/review-compile --yaml=#{yamlfile} --target=latex --level=#{config["secnolevel"]} --toclevel=#{config["toclevel"]} #{config["params"]} #{filename}.re > #{@path}/#{filename}.tex" if system cmd # OK else @compile_errors = true warn cmd end end |
#parse_opts(args) ⇒ Object
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 |
# File 'lib/review/pdfmaker.rb', line 61 def parse_opts(args) cmd_config = Hash.new opts = OptionParser.new opts. = "Usage: review-pdfmaker configfile" opts.version = ReVIEW::VERSION opts.on('--help', 'Prints this message and quit.') do puts opts.help exit 0 end opts.on('--[no-]debug', 'Keep temporary files.') do |debug| cmd_config["debug"] = debug end opts.on('--ignore-errors', 'Ignore review-compile errors.') do cmd_config["ignore-errors"] = true end opts.parse!(args) if args.size != 1 puts opts.help exit 0 end return cmd_config, args[0] end |
#system_or_raise(*args) ⇒ Object
25 26 27 |
# File 'lib/review/pdfmaker.rb', line 25 def system_or_raise(*args) Kernel.system(*args) or raise("failed to run command: #{args.join(' ')}") end |
#warn(msg) ⇒ Object
34 35 36 |
# File 'lib/review/pdfmaker.rb', line 34 def warn(msg) $stderr.puts "#{File.basename($0, '.*')}: warning: #{msg}" end |