public
Description: Ruby LaTeX to PDF preprocessor (and Rails plugin)
Homepage: http://rtex.rubyforge.org
Clone URL: git://github.com/bruce/rtex.git
Click here to lend your support to: rtex and make a donation at www.pledgie.com !
rtex / bin / rtex
100755 74 lines (60 sloc) 2.141 kb
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
#!/usr/bin/env ruby
 
require 'optparse'
require 'fileutils'
 
require File.dirname(__FILE__) << '/../lib/rtex'
 
options = {}
 
opts = OptionParser.new do |opts|
  
  opts.banner = [
    "RTeX v#{RTeX::Version::STRING} (c) 2006-2007 Bruce Williams, Wiebe Cazemier",
    "USAGE: rtex [OPTIONS]"
  ].join("\n")
  
  opts.on_tail('-h', '--help', "Show this message") do
    STDERR.puts opts
    exit
  end
  
  opts.on('-l LAYOUT', '--layout LAYOUT', 'Path to layout file (use <%= yield %>)') do |layout|
    if File.exists?(layout)
      options[:layout] = File.read(layout)
    else
      STDERR.puts "Layout file not found: #{layout}"
      exit 1
    end
  end
  
  opts.on('-o OUTFILE', '--output OUTFILE', "Output to file (defaults to STDOUT)") do |path|
    options[:outfile] = path
  end
  
  filters = RTeX.filters.keys.map { |k| k.to_s }.sort.join(', ')
  opts.on('-f FILTER', '--filter FILTER', "Filter input (supported: #{filters})", "(Wraps in a basic `article' document; use --layout to customize)") do |filter|
    options[:filter] = filter
  end
  
  opts.on('--no-pdf', "Don't generate PDF (just output TeX)") do
    options[:tex] = true
  end
  
  opts.on('-i PATH', '--install PATH', "Install as plugin into Rails/Merb app at PATH") do |path|
    unless Dir[plugins = File.join(path, 'vendor/plugins')].any?
      STDERR.puts "Could not find application at #{path}"
      exit 1
    end
    plugin_path = File.join(plugins, 'rtex')
    FileUtils.cp_r(File.dirname(__FILE__) << '/..', plugin_path)
    %w(Rakefile Manifest.txt bin tasks).each do |prune|
      FileUtils.rm_rf(File.join(plugin_path, prune)) rescue nil
    end
    STDERR.puts "Installed at #{plugin_path}"
    exit
  end
  
end
opts.parse!(ARGV)
 
if options[:filter] && !options[:layout]
  STDERR.puts "Warning: Using default `article' layout (see --help on '--layout')"
  options[:layout] = RTeX.basic_layout
end
 
document = RTeX::Document.new(ARGF.read, options)
location = File.expand_path(options[:outfile]) rescue nil
document.to_pdf(nil) do |filename|
  if location
    FileUtils.move filename, location
  else
    STDOUT.print File.read(filename)
  end
end