Class: Texd::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/texd/config.rb

Defined Under Namespace

Classes: InvalidConfig

Constant Summary collapse

DEFAULT_CONFIGURATION =

This is the default configuration. It is applied in the constructor.

{
  endpoint:       ENV.fetch("TEXD_ENDPOINT", "http://localhost:2201/"),
  open_timeout:   ENV.fetch("TEXD_OPEN_TIMEOUT", 60),
  read_timeout:   ENV.fetch("TEXD_READ_TIMEOUT", 180),
  write_timeout:  ENV.fetch("TEXD_WRITE_TIMEOUT", 60),
  error_format:   ENV.fetch("TEXD_ERRORS", "full"),
  error_handler:  ENV.fetch("TEXD_ERROR_HANDLER", "raise"),
  tex_engine:     ENV.fetch("TEXD_ENGINE", nil),
  tex_image:      ENV.fetch("TEXD_IMAGE", nil),
  helpers:        Set.new,
  lookup_paths:   [], # Rails.root.join("app/tex") is inserted in railtie.rb
  ref_cache_size: 128,
}.freeze
ENDPOINT_CLASSES =

Supported endpoint protocols.

[URI::HTTP, URI::HTTPS].freeze
ERROR_FORMATS =

Supported error formats.

%w[json full condensed].freeze
ERROR_HANDLERS =

Default error handlers. One might provide a custom proc, if desired.

{
  "raise"  => proc { |err, _doc| raise err },
  "stderr" => proc { |err, _doc| err.write_to($stderr) },
  "ignore" => proc { |_err, _doc| },
}.freeze
TEX_ENGINES =

Supported TeX engines.

%w[xelatex lualatex pdflatex].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Configuration

Returns a new instance of Configuration.



141
142
143
144
145
# File 'lib/texd/config.rb', line 141

def initialize(**options)
  DEFAULT_CONFIGURATION.each do |key, default_value|
    public_send "#{key}=", options.fetch(key, default_value.dup)
  end
end

Instance Attribute Details

#endpointObject

Endpoint is a URI pointing to the texd server instance.

The default is ‘localhost:2201/` and can be overriden by the `TEXD_ENDPOINT` environment variable.



57
58
59
# File 'lib/texd/config.rb', line 57

def endpoint
  @endpoint
end

#error_formatObject

The texd server usually reports errors in JSON format, however, when the compilation fails, the TeX compiler’s output ist often most useful.

Supported values are described in ERROR_FORMATS.

The default is “full” and can be overriden by the ‘TEXD_ERRORS` environment variable.



86
87
88
# File 'lib/texd/config.rb', line 86

def error_format
  @error_format
end

#error_handlerObject

This setting defines how to handle Texd::Client::CompilationError errors.

Supported values are:

  • “raise”, which will not process the error,

  • “stderr”, which will print the error to stderr,

  • “ignore”, which will silently discard,

  • a Proc instance, which will delegate the error handling to it.

The setter will lookup “raise”, “stderr”, and “ignore” from ERROR_HANDLERS, so this attribute will always be of kind Proc.

The default value is “raise” and can be overridden by the ‘TEXD_ERROR_HANDLER` environment variable.



102
103
104
# File 'lib/texd/config.rb', line 102

def error_handler
  @error_handler
end

#helpersObject

List of additional helper modules to make available in the template views. Texd::Helpers is always included, and you may add additional ones.

This can’t be influenced by environment variables.



122
123
124
# File 'lib/texd/config.rb', line 122

def helpers
  @helpers
end

#lookup_pathsObject

Set of paths to perform file lookups in. The set is searched in order, meaning files found in later entries won’t be returned if entries with the same name exist in earlier entries.

By default, this only contains ‘Rails.root.join(“app/tex”)`, however Rails engines might append additional entries.

A Texd::LookupContext is constructed from this set.



132
133
134
# File 'lib/texd/config.rb', line 132

def lookup_paths
  @lookup_paths
end

#open_timeoutObject

Timeout (in seconds) for the initial connect to the endpoint.

The default is 60 (1 min) and can be overriden by the ‘TEXD_OPEN_TIMEOUT` environment variable.



63
64
65
# File 'lib/texd/config.rb', line 63

def open_timeout
  @open_timeout
end

#read_timeoutObject

Timeout (in seconds) for reads from the endpoint. You want this value to be in the same ballbark as texd’s ‘–compile-timoeut` option.

The default is 180 (3 min) and can be overriden by the ‘TEXD_OPEN_TIMEOUT` environment variable.



70
71
72
# File 'lib/texd/config.rb', line 70

def read_timeout
  @read_timeout
end

#ref_cache_sizeObject

Cache size for file hashes computed by Texd::Attachment::Reference. Cannot be changed after the first document (using the ‘texd_reference` helper) was renderered.

By default, the cache keeps hashes of the last 128 reference files.



139
140
141
# File 'lib/texd/config.rb', line 139

def ref_cache_size
  @ref_cache_size
end

#tex_engineObject

This is the selected TeX engine. Supported values are described in TEX_ENGINES.

The default is blank (meaning the server shall default to its ‘–tex-engine` option), and can be overriden by the `TEXD_ENGINE` environment variable.



109
110
111
# File 'lib/texd/config.rb', line 109

def tex_engine
  @tex_engine
end

#tex_imageObject

When texd runs in container mode, it may provide multiple Docker images to select from. This setting selects a specific container image.

The default value is blank (meaning texd will select an image), and can be overriden byt the ‘TEXD_IMAGE` environment variable.



116
117
118
# File 'lib/texd/config.rb', line 116

def tex_image
  @tex_image
end

#write_timeoutObject

Timeout (in seconds) for writing the request to the endpoint. You want this value to be in the same ballpark as texd’s ‘–queue-timeout` option.

The default is 60 (1 min) and can be overriden by the ‘TEXD_WRITE_TIMEOUT` environment variable.



77
78
79
# File 'lib/texd/config.rb', line 77

def write_timeout
  @write_timeout
end

Instance Method Details

#default_render_paramsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



154
155
156
157
158
159
160
# File 'lib/texd/config.rb', line 154

def default_render_params
  {
    errors: error_format,
    engine: tex_engine,
    image:  tex_image,
  }.compact
end

#to_hObject



147
148
149
150
151
# File 'lib/texd/config.rb', line 147

def to_h
  DEFAULT_CONFIGURATION.keys.index_with do |key|
    public_send(key)
  end
end