793: def self.run(args)
794: config = OpenStruct.new
795: config.regen = false
796: config.cache = true
797: config.compressed = false
798:
799: opts = OptionParser.new do |opt|
800: opt.banner = PDF::Writer::Lang[:techbook_usage_banner] % [ File.basename($0) ]
801: PDF::Writer::Lang[:techbook_usage_banner_1].each do |ll|
802: opt.separator " #{ll}"
803: end
804: opt.on('-f', '--force-regen', *PDF::Writer::Lang[:techbook_help_force_regen]) { config.regen = true }
805: opt.on('-n', '--no-cache', *PDF::Writer::Lang[:techbook_help_no_cache]) { config.cache = false }
806: opt.on('-z', '--compress', *PDF::Writer::Lang[:techbook_help_compress]) { config.compressed = true }
807: opt.on_tail ""
808: opt.on_tail("--help", *PDF::Writer::Lang[:techbook_help_help]) { $stderr << opt; exit(0) }
809: end
810: opts.parse!(args)
811:
812: config.document = args[0]
813:
814: unless config.document
815: config.document = "manual.pwd"
816: unless File.exist?(config.document)
817: dirn = File.dirname(__FILE__)
818: config.document = File.join(dirn, File.basename(config.document))
819: unless File.exist?(config.document)
820: dirn = File.join(dirn, "..")
821: config.document = File.join(dirn, File.basename(config.document))
822: unless File.exist?(config.document)
823: dirn = File.join(dirn, "..")
824: config.document = File.join(dirn,
825: File.basename(config.document))
826: unless File.exist?(config.document)
827: $stderr.puts PDF::Writer::Lang[:techbook_cannot_find_document]
828: exit(1)
829: end
830: end
831: end
832: end
833:
834: $stderr.puts PDF::Writer::Lang[:techbook_using_default_doc] % config.document
835: end
836:
837: dirn = File.dirname(config.document)
838: extn = File.extname(config.document)
839: base = File.basename(config.document, extn)
840:
841: files = {
842: :document => config.document,
843: :cache => "#{base}._mc",
844: :pdf => "#{base}.pdf"
845: }
846:
847: unless config.regen
848: if File.exist?(files[:cache])
849: _tm_doc = File.mtime(config.document)
850: _tm_prg = File.mtime(__FILE__)
851: _tm_cch = File.mtime(files[:cache])
852:
853:
854:
855: if (_tm_doc < _tm_cch) and (_tm_prg < _tm_cch)
856: $stderr.puts PDF::Writer::Lang[:techbook_using_cached_doc] % File.basename(files[:cache])
857: pdf = File.open(files[:cache], "rb") { |cf| Marshal.load(cf.read) }
858: pdf.save_as(files[:pdf])
859: File.open(files[:pdf], "wb") { |pf| pf.write pdf.render }
860: exit(0)
861: else
862: $stderr.puts PDF::Writer::Lang[:techbook_regenerating]
863: end
864: end
865: else
866: $stderr.puts PDF::Writer::Lang[:techbook_ignoring_cache] if File.exist?(files[:cache])
867: end
868:
869:
870: pdf = PDF::TechBook.new
871: pdf.compressed = config.compressed
872: pdf.techbook_source_dir = File.expand_path(dirn)
873:
874: document = open(files[:document]) { |io| io.read.split($/) }
875: progress = ProgressBar.new(base.capitalize, document.size)
876: pdf.techbook_parse(document, progress)
877: progress.finish
878:
879: if pdf.generate_table_of_contents?
880: progress = ProgressBar.new("TOC", pdf.table_of_contents.size)
881: pdf.techbook_toc(progress)
882: progress.finish
883: end
884:
885: if config.cache
886: File.open(files[:cache], "wb") { |f| f.write Marshal.dump(pdf) }
887: end
888:
889: pdf.save_as(files[:pdf])
890: end