#!/usr/bin/env ruby # # $Id: review-compile 4326 2010-01-12 14:10:17Z kmuto $ # # Copyright (c) 1999-2007 Minero Aoki # # 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". # require 'pathname' bindir = Pathname.new(__FILE__).realpath.dirname $LOAD_PATH.unshift((bindir + '../lib').realpath) require 'review' require 'review/compiler' require 'review/book' require 'fileutils' require 'optparse' def main Signal.trap(:INT) { exit 1 } Signal.trap(:PIPE, "IGNORE") _main rescue Errno::EPIPE exit 0 end def _main $KCODE = 'UTF-8' unless defined?(Encoding) mode = :files basedir = nil if /\Areview2/ =~ File.basename($0) target = File.basename($0, '.rb').sub(/review2/, '') else target = nil end check_only = false param = { "secnolevel" => 2, # for IDGXML and HTML "tableopt" => nil, # for IDGXML "nolf" => nil, # for IDGXML "chapref" => nil, # for IDGXML "inencoding" => "UTF-8", "outencoding" => "UTF-8", "subdirmode" => nil, "singledirmode" => nil, "stylesheet" => [], # for HTML "mathml" => nil, # for HTML "language" => "ja", # for HTML "deprecated-blocklines" => nil, "footnotetext" => false, "htmlext" => "html", "htmlversion" => 4, } parser = OptionParser.new parser.banner = "Usage: #{File.basename($0)} [--target=FMT]" parser.on('--yaml=YAML', 'Read configurations from YAML file.') do |yaml| require 'yaml' param = param.merge(YAML.load_file(yaml)) end parser.on('--inencoding=ENCODING', 'Set input encoding. (UTF-8, EUC, JIS, and SJIS)') {|enc| param["inencoding"] = enc } parser.on('--outencoding=ENCODING', 'Set output encoding. (UTF-8[default], EUC, JIS, and SJIS)') {|enc| param["outencoding"] = enc } parser.on('-c', '--check', 'Check manuscript') { check_only = true } parser.on('--level=LVL', 'Section level to append number.') {|lvl| param["secnolevel"] = lvl.to_i } parser.on('--nolfinxml', 'Do not insert LF in XML. (idgxml)') { param["nolf"] = true } parser.on('--table=WIDTH', 'Default table width. (idgxml)') {|tbl| param["tableopt"] = tbl } parser.on('--listinfo', 'Append listinfo tag to lists to indicate begin/end. (idgxml)') { param["listinfo"] = true } parser.on('--chapref="before,middle,after"', 'Chapref decoration.') {|cdec| param["chapref"] = cdec } parser.on('--subdirmode', 'Use chapter/id.ext path style to find images.') { param["subdirmode"] = true } parser.on('--singledirmode', 'Use id.ext path style to find images.') { param["singledirmode"] = true } parser.on('--chapterlink', 'make chapref hyperlink') { param["chapterlink"] = true } parser.on('--stylesheet=file', 'Stylesheet file for HTML (comma separated)') {|files| param["stylesheet"] = files.split(/\s*,\s*/) } parser.on('--mathml', 'Use MathML for TeX equation in HTML') do param["mathml"] = true require 'math_ml' end parser.on('--htmlversion=VERSION', 'HTML version.') do |v| v = v.to_i param["htmlversion"] = v if v == 4 || v == 5 end parser.on('--hdnumberingmode', 'Output numbering headlines. (deprecated)') { param["hdnumberingmode"] = true } parser.on('--deprecated-blocklines', 'Disable paragrahs in block tags. Treat physical line as a paragraph. (deprecated)') { param["deprecated-blocklines"] = true } parser.on('--target=FMT', 'Target format.') {|fmt| target = fmt } unless target parser.on('--footnotetext', 'Use footnotetext and footnotemark instead of footnote (latex)') { param["footnotetext"] = true } parser.on('--draft', 'use draft mode(inline comment)') { param["draft"] = true } parser.on('-a', '--all', 'Compile all chapters.') do mode = :dir basedir = nil end parser.on('--directory=DIR', 'Compile all chapters in DIR.') do |path| mode = :dir basedir = path end parser.on('--help', 'Prints this message and quit.') do puts parser.help exit 0 end begin parser.parse! unless target if check_only target = 'html' else raise OptionParser::ParseError, "no target given" end end rescue OptionParser::ParseError => err error err.message $stderr.puts parser.help exit 1 end # array parameter ["stylesheet"].each do |k| if param["k"].instance_of?(String) param["k"] = [param["k"]] end end begin compiler = ReVIEW::Compiler.new(load_strategy_class(target, check_only)) ReVIEW.book.param = param case mode when :files if ARGV.empty? error 'no input' exit 1 end ReVIEW::Chapter.intern_pathes(ARGV).each do |chap| result = compiler.compile(chap) puts result unless check_only end when :dir book = basedir ? ReVIEW::Book.load(basedir) : ReVIEW.book book.chapters.each do |chap| str = compiler.compile(chap) write "#{chap.name}#{compiler.strategy.extname}", str unless check_only end else raise "must not happen: #{mode}" end rescue ReVIEW::ApplicationError => err raise if $DEBUG error err.message exit 1 end end def error(msg) $stderr.puts "#{File.basename($0, '.*')}: error: #{msg}" end def load_strategy_class(target, strict) require "review/#{target}builder" ReVIEW.const_get("#{target.upcase}Builder").new(strict) end def write(path, str) File.open(path, 'w') {|f| f.puts str } end main