Class: RTeX::Document

Inherits:
Object
  • Object
show all
Extended by:
Escaping
Defined in:
lib/rtex/document.rb

Defined Under Namespace

Classes: ExecutableNotFoundError, FilterError, GenerationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Escaping

escape, replacements

Constructor Details

#initialize(content, options = {}) ⇒ Document

Create a new RTeX::Document in either the current directory (default) or a specified, pre-existing directory.

The content parameter should contain the document source.

The options hash may contain:

:processor

Executable with which to output the document (default: ‘pdflatex’)

:preprocessor

Executable to use during preprocessing (particularly for longer documents containing a table-of-contents or bibliography section (default: ‘latex’)

:preprocess

Either a boolean specifying whether to preprocess the input file(s), or an integer for the number of times to preprocess (default: false / 0)

:tmpdir

Location of temporary directory (default: Dir.getwd)

:shell_redirect

Option redirection for shell output, e.g. “> /dev/null 2>&1” (default: nil).

:command_prefix

String (or array) of environment variable settings for the :processor and :preprocessor commands.



57
58
59
60
61
62
63
64
# File 'lib/rtex/document.rb', line 57

def initialize(content, options={})
  @options = self.class.options.merge(options)
  if @options[:processed]
    @source = content
  else
    @erb = ERB.new(content)
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



125
126
127
# File 'lib/rtex/document.rb', line 125

def options
  @options
end

Class Method Details

.create(content, options = {}) ⇒ Object

Create a new RTeX::Document. A temporary directory will be created for the lifetime of the document, and then destroyed at the end of the block.

RTeX::Document.create(source, :processor => "xelatex") do |document|
  puts "working in #{document.tempdir}"

  document.to_pdf do |filename|
    # process PDF file
    # ...
  end
end

Options passed to #create are the same as those for #new.

To specify which temporary directory should be created for processing the document, set :tempdir.



30
31
32
33
34
35
# File 'lib/rtex/document.rb', line 30

def self.create(content, options={})
  Tempdir.open(options[:tempdir]) do |dir|
    doc = Document.new(content, options.merge(:tempdir => dir))
    yield doc
  end
end

.optionsObject

:nodoc:



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rtex/document.rb', line 113

def self.options #:nodoc:
  @options ||= {
    :preprocessor => 'latex',
    :preprocess => false,
    :processor => 'pdflatex',
    :shell_redirect => nil,
    :tex_inputs => nil,
    :command_prefix => nil,
    :tempdir => Dir.getwd # Current directory unless otherwise set
  }
end

Instance Method Details

#filter(text) ⇒ Object

Process through defined filter



74
75
76
77
78
79
80
81
# File 'lib/rtex/document.rb', line 74

def filter(text) #:nodoc:
  return text unless @options[:filter]
  if (process = RTeX.filters[@options[:filter]])
    process[text]
  else
    raise FilterError, "No `#{@options[:filter]}' filter"
  end
end

#preprocessorObject

:nodoc:



105
106
107
# File 'lib/rtex/document.rb', line 105

def preprocessor #:nodoc:
  @preprocessor ||= check_path_for @options[:preprocessor]
end

#processorObject

:nodoc:



101
102
103
# File 'lib/rtex/document.rb', line 101

def processor #:nodoc:
  @processor ||= check_path_for @options[:processor]
end

#source(binding = nil) ⇒ Object

Get the compiled source for the entire document



67
68
69
70
71
# File 'lib/rtex/document.rb', line 67

def source(binding=nil) #:nodoc:
  @source ||= wrap_in_layout do
    filter @erb.result(binding)
  end
end

#system_pathObject

:nodoc:



109
110
111
# File 'lib/rtex/document.rb', line 109

def system_path #:nodoc:
  ENV['PATH']
end

#to_pdf(binding = nil, &file_handler) ⇒ Object

Generate PDF output:

to_pdf # => PDF in a String
to_pdf { |filename| ... }


97
98
99
# File 'lib/rtex/document.rb', line 97

def to_pdf(binding=nil, &file_handler)
  process_pdf_from(source(binding), &file_handler)
end

#wrap_in_layoutObject

Wrap content in optional layout



84
85
86
87
88
89
90
# File 'lib/rtex/document.rb', line 84

def wrap_in_layout #:nodoc:
  if @options[:layout]
    ERB.new(@options[:layout]).result(binding)
  else
    yield
  end
end