Class: Pandocomatic::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/pandocomatic/configuration.rb

Overview

Configuration models a pandocomatic configuration.

Constant Summary collapse

CONFIG_FILE =

Pandocomatic’s default configuration file

'pandocomatic.yaml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, input) ⇒ Configuration

Create a new Configuration instance based on the command-line options



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pandocomatic/configuration.rb', line 124

def initialize(options, input)
  data_dirs = determine_data_dirs options
  @options = options
  @data_dir = data_dirs.first
  @settings = DEFAULT_SETTINGS
  @templates = {}
  @convert_patterns = {}

  load_configuration_hierarchy options, data_dirs

  @input = if input.nil? || input.empty?
             nil
           elsif input.size > 1
             MultipleFilesInput.new(input, self)
           else
             Input.new(input)
           end

  @output = if output?
              options[:output]
            elsif to_stdout? options
              Tempfile.new(@input.base)
            elsif @input.is_a? Input
              @input.base
            end

  @root_path = Path.determine_root_path options

  # Extend the command classes by setting the source tree root
  # directory, and the options quiet and dry-run, which are used when
  # executing a command: if dry-run the command is not actually
  # executed and if quiet the command is not printed to STDOUT
  Command.reset(self)
end

Instance Attribute Details

#config_filesObject (readonly)

Returns the value of attribute config_files.



118
119
120
# File 'lib/pandocomatic/configuration.rb', line 118

def config_files
  @config_files
end

#data_dirObject (readonly)

Returns the value of attribute data_dir.



118
119
120
# File 'lib/pandocomatic/configuration.rb', line 118

def data_dir
  @data_dir
end

#inputObject (readonly)

Returns the value of attribute input.



118
119
120
# File 'lib/pandocomatic/configuration.rb', line 118

def input
  @input
end

#outputString (readonly)

Get the output file name

Returns:

  • (String)


307
308
309
# File 'lib/pandocomatic/configuration.rb', line 307

def output
  @output
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



118
119
120
# File 'lib/pandocomatic/configuration.rb', line 118

def root_path
  @root_path
end

Instance Method Details

#clean_up!Object

Clean up this configuration. This will remove temporary files created for the conversion process guided by this Configuration.



341
342
343
344
345
# File 'lib/pandocomatic/configuration.rb', line 341

def clean_up!
  # If a temporary file has been created while concatenating
  # multiple input files, ensure it is removed.
  @input.destroy! if @input.is_a? MultipleFilesInput
end

#config?Boolean

Is the config CLI option given?

Returns:

  • (Boolean)


293
294
295
# File 'lib/pandocomatic/configuration.rb', line 293

def config?
  @options[:config_given]
end

#configure(settings, path) ⇒ Object

Configure pandocomatic based on a settings Hash

Configuration.

Parameters:

  • settings (Hash)

    a settings Hash to mixin in this

  • path (String)

    the configuration’s path or filename



203
204
205
206
207
208
209
210
211
# File 'lib/pandocomatic/configuration.rb', line 203

def configure(settings, path)
  reset_settings settings['settings'] if settings.key? 'settings'

  return unless settings.key? 'templates'

  settings['templates'].each do |name, template|
    reset_template Template.new(name, template, path)
  end
end

#convert?(src) ⇒ Boolean

Should the source file be converted given this Configuration?

Parameters:

  • src (String)

    True if this source file matches the ‘glob’ patterns in a template, false otherwise.

Returns:

  • (Boolean)


364
365
366
# File 'lib/pandocomatic/configuration.rb', line 364

def convert?(src)
  @convert_patterns.values.flatten.any? { |glob| File.fnmatch glob, File.basename(src) }
end

#data_dir?Boolean

Is the data dir CLI option given?

Returns:

  • (Boolean)


279
280
281
# File 'lib/pandocomatic/configuration.rb', line 279

def data_dir?
  @options[:data_dir_given]
end

#debug?Boolean

Is the debug CLI option given?

Returns:

  • (Boolean)


244
245
246
# File 'lib/pandocomatic/configuration.rb', line 244

def debug?
  @options[:debug_given] and @options[:debug]
end

#determine_template(src) ⇒ String

Determine the template to use with this source document given this Configuration.

Parameters:

  • src (String)

    path to the source document

Returns:

  • (String)

    the template’s name to use



544
545
546
547
548
# File 'lib/pandocomatic/configuration.rb', line 544

def determine_template(src)
  @convert_patterns.select do |_, globs|
    globs.any? { |glob| File.fnmatch glob, File.basename(src) }
  end.keys.first
end

#determine_templates(src) ⇒ Array[String]

Determine the templates to use with this source document given this Configuration.

Parameters:

  • src (String)

    path to the source document

Returns:

  • (Array[String])

    the template’s name to use



555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/pandocomatic/configuration.rb', line 555

def determine_templates(src)
  matches = @convert_patterns.select do |_, globs|
    globs.any? { |glob| File.fnmatch glob, File.basename(src) }
  end.keys

  if matches.empty?
    []
  elsif match_all_templates?
    matches
  else
    [matches.first]
  end
end

#directory?Boolean

Is this Configuration for converting directories?

Returns:

  • (Boolean)


335
336
337
# File 'lib/pandocomatic/configuration.rb', line 335

def directory?
  !@input.nil? and @input.directory?
end

#dry_run?Boolean

Is the dry run CLI option given?

Returns:

  • (Boolean)


223
224
225
# File 'lib/pandocomatic/configuration.rb', line 223

def dry_run?
  @options[:dry_run_given] and @options[:dry_run]
end

#find_extension(template_name, metadata) ⇒ String

Find the extension of the destination file given this Confguration, template, and metadata

Parameters:

  • template_name (String)

    the name of the template used to convert to destination

  • metadata (PandocMetadata)

    the metadata in the source file

Returns:

  • (String)

    the extension to use for the destination file



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/pandocomatic/configuration.rb', line 476

def find_extension(template_name, )
  extension = 'html'

  # Pandoc supports enabling / disabling extensions
  # using +EXTENSION and -EXTENSION
  strip_extensions = ->(format) { format.split(/[+-]/).first }
  use_extension = lambda do |pandoc|
    pandoc['use-extension'] if pandoc.key? 'use-extension'
  end

  if template_name.nil? || template_name.empty?
    ext = use_extension.call .pandoc_options
    if !ext.nil?
      extension = ext
    elsif .pandoc_options.key? 'to'
      extension = strip_extensions.call(.pandoc_options['to'])
    end
  elsif @templates[template_name].pandoc?
    pandoc = @templates[template_name].pandoc
    ext = use_extension.call pandoc

    if !ext.nil?
      extension = ext
    elsif pandoc.key? 'to'
      extension = strip_extensions.call(pandoc['to'])
    end
  end

  DEFAULT_EXTENSION[extension] || extension
end

#follow_links?Boolean

Should pandocomatic follow symbolic links given this Configuration?

Returns:

  • (Boolean)

    True if the setting ‘follow_links’ is true, false otherwise



380
381
382
# File 'lib/pandocomatic/configuration.rb', line 380

def follow_links?
  @settings.key? 'follow_links' and @settings['follow_links']
end

#get_template(template_name) ⇒ Template

Get the template with template_name from this Configuration

Parameters:

  • template_name (String)

    a template’s name

Returns:

  • (Template)

    The template with template_name.



535
536
537
# File 'lib/pandocomatic/configuration.rb', line 535

def get_template(template_name)
  @templates[template_name]
end

#input?Boolean

Have input CLI options be given?

Returns:

  • (Boolean)


317
318
319
# File 'lib/pandocomatic/configuration.rb', line 317

def input?
  @options[:input_given]
end

#input_fileString

Get the input file name

Returns:

  • (String)


324
325
326
327
328
329
330
# File 'lib/pandocomatic/configuration.rb', line 324

def input_file
  if @input.nil?
    nil
  else
    @input.name
  end
end

#load(filename) ⇒ Configuration

Read a configuration file and create a pandocomatic configuration object

Parameters:

  • filename (String)

    Path to the configuration yaml file

Returns:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pandocomatic/configuration.rb', line 163

def load(filename)
  begin
    path = File.absolute_path filename
    settings = PandocomaticYAML.load_file path
    if settings['settings'] && settings['settings']['data-dir']
      data_dir = settings['settings']['data-dir']
      src_dir = File.dirname filename
      @data_dir = if data_dir.start_with? '.'
                    File.absolute_path data_dir, src_dir
                  else
                    data_dir
                  end
    end
  rescue StandardError => e
    raise ConfigurationError.new(:unable_to_load_config_file, e, filename)
  end

  configure settings, filename
end

#markdown_file?(filename) ⇒ Boolean

Is filename a markdown file according to its extension?

Parameters:

  • filename (String)

    the filename to check

Returns:

  • (Boolean)

    True if filename has a markdown extension.



511
512
513
514
515
516
517
518
# File 'lib/pandocomatic/configuration.rb', line 511

def markdown_file?(filename)
  if filename.nil?
    false
  else
    ext = File.extname(filename).delete_prefix('.')
    DEFAULT_EXTENSION.key(ext) == 'markdown'
  end
end

#match_all_templates?Boolean

Should pandocomatic convert a file with all matching templates or only with the first matching template? Note. A ‘use-template’ statement in a document will overrule this setting.

otherwise.

Returns:

  • (Boolean)

    True if the setting ‘match-files’ is ‘all’, false



390
391
392
# File 'lib/pandocomatic/configuration.rb', line 390

def match_all_templates?
  @settings.key? 'match-files' and @settings['match-files'] == 'all'
end

#match_first_template?Boolean

Should pandocomatic convert a file with the first matching templates or with all matching templates? Note. Multiple ‘use-template’ statements in a document will overrule this setting.

otherwise.

Returns:

  • (Boolean)

    True if the setting ‘match-files’ is ‘first’, false



400
401
402
# File 'lib/pandocomatic/configuration.rb', line 400

def match_first_template?
  @settings.key? 'match-files' and @settings['match-files'] == 'first'
end

#modified_only?Boolean

Is the modified only CLI option given?

Returns:

  • (Boolean)


258
259
260
# File 'lib/pandocomatic/configuration.rb', line 258

def modified_only?
  @options[:modified_only_given] and @options[:modified_only]
end

#output?Boolean

Is the output CLI option given and can that output be used?

Returns:

  • (Boolean)


300
301
302
# File 'lib/pandocomatic/configuration.rb', line 300

def output?
  !@options.nil? and @options[:output_given] and @options[:output]
end

#quiet?Boolean

Run pandocomatic in quiet mode?

Returns:

  • (Boolean)


251
252
253
# File 'lib/pandocomatic/configuration.rb', line 251

def quiet?
  [verbose?, debug?, dry_run?].none?
end

#reconfigure(filename) ⇒ Configuration

Update this configuration with a configuration file and return a new configuration

Parameters:

  • filename (String)

    path to the configuration file

Returns:



189
190
191
192
193
194
195
196
# File 'lib/pandocomatic/configuration.rb', line 189

def reconfigure(filename)
  settings = PandocomaticYAML.load_file filename
  new_config = Marshal.load(Marshal.dump(self))
  new_config.configure settings, filename
  new_config
rescue StandardError => e
  raise ConfigurationError.new(:unable_to_load_config_file, e, filename)
end

#recursive?Boolean

Should pandocomatic be run recursively given this Configuration?

Returns:

  • (Boolean)

    True if the setting ‘recursive’ is true, false otherwise



372
373
374
# File 'lib/pandocomatic/configuration.rb', line 372

def recursive?
  @settings.key? 'recursive' and @settings['recursive']
end

#root_path?Boolean

Is the root path CLI option given?

Returns:

  • (Boolean)


286
287
288
# File 'lib/pandocomatic/configuration.rb', line 286

def root_path?
  @options[:root_path_given]
end

#set_destination(dst, template_name, metadata) ⇒ Object

Set the destination file given this Confguration, template, and metadata

Parameters:

  • dst (String)

    path to a destination file

  • template_name (String)

    the name of the template used to convert to destination

  • metadata (PandocMetadata)

    the metadata in the source file



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/pandocomatic/configuration.rb', line 425

def set_destination(dst, template_name, )
  return dst if dst.is_a? Tempfile

  dir = File.dirname dst

  # Use the output option when set.
  determine_output_in_pandoc = lambda do |pandoc|
    if pandoc.key? 'output'
      output = pandoc['output']
      unless output.start_with? '/'
        # Put it relative to the current directory
        output = File.join dir, output
      end
      output
    end
  end

  # Output options in pandoc property have precedence
  destination = determine_output_in_pandoc.call .pandoc_options
  rename_script = .pandoc_options['rename']

  # Output option in template's pandoc property is next
  if destination.nil? && !template_name.nil? && !template_name.empty? && @templates[template_name].pandoc?
    pandoc = @templates[template_name].pandoc
    destination = determine_output_in_pandoc.call pandoc
    rename_script ||= pandoc['rename']
  end

  # Else fall back to taking the input file as output file with the
  # extension updated to the output format
  if destination.nil?
    destination = set_extension dst, template_name, 

    destination = rename_destination(rename_script, destination) unless rename_script.nil?
  end

  # If there is a single file input without output specified, set
  # the output now that we know what the output filename is.
  @output = destination.delete_prefix './' unless output?

  destination
end

#set_extension(dst, template_name, metadata) ⇒ Object

Set the extension of the destination file given this Confguration, template, and metadata

Parameters:

  • dst (String)

    path to a destination file

  • template_name (String)

    the name of the template used to convert to destination

  • metadata (PandocMetadata)

    the metadata in the source file



411
412
413
414
415
416
# File 'lib/pandocomatic/configuration.rb', line 411

def set_extension(dst, template_name, )
  dir = File.dirname dst
  ext = File.extname dst
  basename = File.basename dst, ext
  File.join dir, "#{basename}.#{find_extension(template_name, )}"
end

#show_help?Boolean

Is the help CLI option given?

Returns:

  • (Boolean)


272
273
274
# File 'lib/pandocomatic/configuration.rb', line 272

def show_help?
  @options[:help_given]
end

#show_version?Boolean

Is the version CLI option given?

Returns:

  • (Boolean)


265
266
267
# File 'lib/pandocomatic/configuration.rb', line 265

def show_version?
  @options[:version_given]
end

#skip?(src) ⇒ Boolean

Should the source file be skipped given this Configuration?

Parameters:

  • src (String)

    path to a source file

Returns:

  • (Boolean)

    True if this source file matches the pattern in the ‘skip’ setting, false otherwise.



352
353
354
355
356
357
358
# File 'lib/pandocomatic/configuration.rb', line 352

def skip?(src)
  if @settings.key? 'skip'
    @settings['skip'].any? { |glob| File.fnmatch glob, File.basename(src) }
  else
    false
  end
end

#src_rootString

Get the source root directory

Returns:

  • (String)


312
313
314
# File 'lib/pandocomatic/configuration.rb', line 312

def src_root
  @input&.absolute_path
end

#stdout?Boolean

Is the stdout CLI option given?

Returns:

  • (Boolean)


230
231
232
# File 'lib/pandocomatic/configuration.rb', line 230

def stdout?
  !@options.nil? and @options[:stdout_given] and @options[:stdout]
end

#template?(template_name) ⇒ Boolean

Is there a template with template_name in this Configuration?

Parameters:

  • template_name (String)

    a template’s name

Returns:

  • (Boolean)

    True if there is a template with name equal to template_name in this Configuration



526
527
528
# File 'lib/pandocomatic/configuration.rb', line 526

def template?(template_name)
  @templates.key? template_name
end

#to_sString

Convert this Configuration to a String

Returns:

  • (String)


216
217
218
# File 'lib/pandocomatic/configuration.rb', line 216

def to_s
  marshal_dump
end

#verbose?Boolean

Is the verbose CLI option given?

Returns:

  • (Boolean)


237
238
239
# File 'lib/pandocomatic/configuration.rb', line 237

def verbose?
  @options[:verbose_given] and @options[:verbose]
end