class ReVIEW::Retrovert::YamlConfig

Attributes

basedir[RW]
basename[RW]
config[RW]
config_files[RW]
retrovert_configs[RW]

Public Class Methods

new() click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 8
def initialize
  @config_files = []
  @retrovert_configs = []
  @config = ReVIEW::Configure.values
end

Public Instance Methods

catalogfile() click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 154
def catalogfile()
  File.join(@basedir, @config['catalogfile'])
end
commentout(yamlfile, key) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 90
def commentout(yamlfile, key)
  content = File.read(yamlfile)
  content.gsub!(/^(\s*)#{key}:(.*)$/, "#\\1#{key}:\\2")
  File.write(yamlfile, content)
end
commentout_root_yml(key) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 96
def commentout_root_yml(key)
  commentout(File.join(@basedir, @basename), key)
end
copy(outdir) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 82
def copy(outdir)
  @config_files.each { |current_file|
    FileUtils.copy(File.expand_path(current_file, @basedir), File.join(outdir, current_file))
  }
  @basedir = outdir
  rewrite_retrovert_yml()
end
error(msg) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 14
def error(msg)
  ReVIEW.logger.error msg
  exit 1
end
load_yaml(filepath) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 138
def load_yaml(filepath)
  begin
    yaml = YAML.load_file(filepath)
  rescue => e
    error "load error #{e.message}"
  end
  if yaml.class == FalseClass
    raise "#{current_file} is malformed."
  end
  return yaml
end
open(yamlfile) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 19
def open(yamlfile)
  error "#{yamlfile} not found." unless File.exist?(yamlfile)

  begin
    loader = ReVIEW::YAMLLoader.new
    @config.deep_merge!(loader.load_file(yamlfile))
  rescue => e
    error "yaml error #{e.message}"
  end

  @basedir = File.absolute_path(File.dirname(yamlfile))
  @basename = File.basename(yamlfile)

  begin
    @config.check_version(ReVIEW::VERSION)
  rescue ReVIEW::ConfigError => e
    warn e.message
  end

  # version 2 compatibility
  unless @config['texdocumentclass']
    if @config.check_version(2, exception: false)
      @config['texdocumentclass'] = ['jsbook', 'uplatex,oneside']
    else
      @config['texdocumentclass'] = @config['_texdocumentclass']
    end
  end

  file_queue = [ @basename ]
  loaded_files = {}
  while file_queue.present?
    current_file = file_queue.shift
    yaml = load_yaml(File.expand_path(current_file, @basedir))
    @config_files << current_file

    if yaml.key?('inherit')
      inherit_files = parse_inherit(yaml, yamlfile, loaded_files)
      file_queue = inherit_files + file_queue
    end
    if yaml.key?('retrovert')
      @retrovert_configs << current_file
    end
  end
end
parse_inherit(yaml, yamlfile, loaded_files) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 64
def parse_inherit(yaml, yamlfile, loaded_files)
  files = []

  yaml['inherit'].reverse_each do |item|
    inherit_file = File.expand_path(item, File.dirname(yamlfile))

    # Check loop
    if loaded_files[inherit_file]
      error "Found circular YAML inheritance '#{inherit_file}' in #{yamlfile}."
    end

    loaded_files[inherit_file] = true
    files << item
  end

  files
end
path() click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 150
def path()
  File.join(@basedir, @basename)
end
rewrite_retrovert_yml() click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 124
def rewrite_retrovert_yml()
  @retrovert_configs.each { |current_file|
    yamlfile = File.expand_path(current_file, @basedir)
    yaml = load_yaml(yamlfile)
    retrovert = yaml['retrovert']
    yaml.deep_merge!(retrovert)
    yaml.delete('retrovert')
    # YAML.dump(yaml, File.open(yamlfile, "w"))
    content = Psych.dump(yaml)
    content.gsub!('---','')
    File.write(yamlfile, content)
  }
end
rewrite_yml(key, val) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 106
def rewrite_yml(key, val)
  config_files.each { |current_file|
    rewrite_yml_(File.join(@basedir, current_file), key, val)
  }
end
rewrite_yml_(yamlfile, key, val) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 100
def rewrite_yml_(yamlfile, key, val)
  content = File.read(yamlfile)
  content.gsub!(/^(\s*)#{key}:.*$/, "\\1#{key}: #{val}")
  File.write(yamlfile, content)
end
rewrite_yml_array(key, val) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 118
def rewrite_yml_array(key, val)
  config_files.each { |current_file|
    rewrite_yml_array_(File.join(@basedir, current_file), key, val)
  }
end
rewrite_yml_array_(yamlfile, key, val) click to toggle source
# File lib/review/retrovert/yamlconfig.rb, line 112
def rewrite_yml_array_(yamlfile, key, val)
  content = File.read(yamlfile)
  content.gsub!(/^(\s*)#{key}:\s*\[.*?\]/m, '\1' + "#{key}: #{val}")
  File.write(yamlfile, content)
end