Class: Pandocomatic::Configuration
- Inherits:
-
Object
- Object
- Pandocomatic::Configuration
- 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
-
#config_files ⇒ Object
readonly
Returns the value of attribute config_files.
-
#data_dir ⇒ Object
readonly
Returns the value of attribute data_dir.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ String
readonly
Get the output file name.
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
Instance Method Summary collapse
-
#clean_up! ⇒ Object
Clean up this configuration.
-
#config? ⇒ Boolean
Is the config CLI option given?.
-
#configure(settings, path) ⇒ Object
Configure pandocomatic based on a settings Hash.
-
#convert?(src) ⇒ Boolean
Should the source file be converted given this Configuration?.
-
#data_dir? ⇒ Boolean
Is the data dir CLI option given?.
-
#debug? ⇒ Boolean
Is the debug CLI option given?.
-
#determine_template(src) ⇒ String
Determine the template to use with this source document given this Configuration.
-
#determine_templates(src) ⇒ Array[String]
Determine the templates to use with this source document given this Configuration.
-
#directory? ⇒ Boolean
Is this Configuration for converting directories?.
-
#dry_run? ⇒ Boolean
Is the dry run CLI option given?.
-
#find_extension(template_name, metadata) ⇒ String
Find the extension of the destination file given this Confguration, template, and metadata.
-
#follow_links? ⇒ Boolean
Should pandocomatic follow symbolic links given this Configuration?.
-
#get_template(template_name) ⇒ Template
Get the template with template_name from this Configuration.
-
#initialize(options, input) ⇒ Configuration
constructor
Create a new Configuration instance based on the command-line options.
-
#input? ⇒ Boolean
Have input CLI options be given?.
-
#input_file ⇒ String
Get the input file name.
-
#load(filename) ⇒ Configuration
Read a configuration file and create a pandocomatic configuration object.
-
#markdown_file?(filename) ⇒ Boolean
Is filename a markdown file according to its extension?.
-
#match_all_templates? ⇒ Boolean
Should pandocomatic convert a file with all matching templates or only with the first matching template? Note.
-
#match_first_template? ⇒ Boolean
Should pandocomatic convert a file with the first matching templates or with all matching templates? Note.
-
#modified_only? ⇒ Boolean
Is the modified only CLI option given?.
-
#output? ⇒ Boolean
Is the output CLI option given and can that output be used?.
-
#quiet? ⇒ Boolean
Run pandocomatic in quiet mode?.
-
#reconfigure(filename) ⇒ Configuration
Update this configuration with a configuration file and return a new configuration.
-
#recursive? ⇒ Boolean
Should pandocomatic be run recursively given this Configuration?.
-
#root_path? ⇒ Boolean
Is the root path CLI option given?.
-
#set_destination(dst, template_name, metadata) ⇒ Object
Set the destination file given this Confguration, template, and metadata.
-
#set_extension(dst, template_name, metadata) ⇒ Object
Set the extension of the destination file given this Confguration, template, and metadata.
-
#show_help? ⇒ Boolean
Is the help CLI option given?.
-
#show_version? ⇒ Boolean
Is the version CLI option given?.
-
#skip?(src) ⇒ Boolean
Should the source file be skipped given this Configuration?.
-
#src_root ⇒ String
Get the source root directory.
-
#stdout? ⇒ Boolean
Is the stdout CLI option given?.
-
#template?(template_name) ⇒ Boolean
Is there a template with template_name in this Configuration?.
-
#to_s ⇒ String
Convert this Configuration to a String.
-
#verbose? ⇒ Boolean
Is the verbose CLI option given?.
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(, input) data_dirs = determine_data_dirs @options = @data_dir = data_dirs.first @settings = DEFAULT_SETTINGS @templates = {} @convert_patterns = {} load_configuration_hierarchy , 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? [:output] elsif to_stdout? Tempfile.new(@input.base) elsif @input.is_a? Input @input.base end @root_path = Path.determine_root_path # 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_files ⇒ Object (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_dir ⇒ Object (readonly)
Returns the value of attribute data_dir.
118 119 120 |
# File 'lib/pandocomatic/configuration.rb', line 118 def data_dir @data_dir end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
118 119 120 |
# File 'lib/pandocomatic/configuration.rb', line 118 def input @input end |
#output ⇒ String (readonly)
Get the output file name
307 308 309 |
# File 'lib/pandocomatic/configuration.rb', line 307 def output @output end |
#root_path ⇒ Object (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?
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.
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?
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?
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?
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.
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.
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?
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?
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
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 . if !ext.nil? extension = ext elsif ..key? 'to' extension = strip_extensions.call(.['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?
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
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?
317 318 319 |
# File 'lib/pandocomatic/configuration.rb', line 317 def input? @options[:input_given] end |
#input_file ⇒ String
Get the input file name
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
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?
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.
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.
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?
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?
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?
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
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?
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?
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
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 . rename_script = .['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
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?
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?
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?
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_root ⇒ String
Get the source root directory
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?
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?
526 527 528 |
# File 'lib/pandocomatic/configuration.rb', line 526 def template?(template_name) @templates.key? template_name end |
#to_s ⇒ String
Convert this Configuration to a 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?
237 238 239 |
# File 'lib/pandocomatic/configuration.rb', line 237 def verbose? @options[:verbose_given] and @options[:verbose] end |