Class: ReVIEW::Retrovert::YamlConfig
- Inherits:
-
Object
- Object
- ReVIEW::Retrovert::YamlConfig
- Defined in:
- lib/review/retrovert/yamlconfig.rb
Instance Attribute Summary collapse
-
#basedir ⇒ Object
Returns the value of attribute basedir.
-
#basename ⇒ Object
Returns the value of attribute basename.
-
#config ⇒ Object
Returns the value of attribute config.
-
#config_files ⇒ Object
Returns the value of attribute config_files.
-
#retrovert_configs ⇒ Object
Returns the value of attribute retrovert_configs.
Instance Method Summary collapse
- #catalogfile ⇒ Object
- #commentout(yamlfile, key) ⇒ Object
- #commentout_root_yml(key) ⇒ Object
- #copy(outdir) ⇒ Object
- #error(msg) ⇒ Object
-
#initialize ⇒ YamlConfig
constructor
A new instance of YamlConfig.
- #load_yaml(filepath) ⇒ Object
- #open(yamlfile) ⇒ Object
- #parse_inherit(yaml, yamlfile, loaded_files) ⇒ Object
- #path ⇒ Object
- #rewrite_retrovert_yml ⇒ Object
- #rewrite_yml(key, val) ⇒ Object
- #rewrite_yml_(yamlfile, key, val) ⇒ Object
- #rewrite_yml_array(key, val) ⇒ Object
- #rewrite_yml_array_(yamlfile, key, val) ⇒ Object
Constructor Details
#initialize ⇒ YamlConfig
Returns a new instance of YamlConfig.
8 9 10 11 12 |
# File 'lib/review/retrovert/yamlconfig.rb', line 8 def initialize @config_files = [] @retrovert_configs = [] @config = ReVIEW::Configure.values end |
Instance Attribute Details
#basedir ⇒ Object
Returns the value of attribute basedir.
6 7 8 |
# File 'lib/review/retrovert/yamlconfig.rb', line 6 def basedir @basedir end |
#basename ⇒ Object
Returns the value of attribute basename.
6 7 8 |
# File 'lib/review/retrovert/yamlconfig.rb', line 6 def basename @basename end |
#config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/review/retrovert/yamlconfig.rb', line 6 def config @config end |
#config_files ⇒ Object
Returns the value of attribute config_files.
6 7 8 |
# File 'lib/review/retrovert/yamlconfig.rb', line 6 def config_files @config_files end |
#retrovert_configs ⇒ Object
Returns the value of attribute retrovert_configs.
6 7 8 |
# File 'lib/review/retrovert/yamlconfig.rb', line 6 def retrovert_configs @retrovert_configs end |
Instance Method Details
#catalogfile ⇒ Object
154 155 156 |
# File 'lib/review/retrovert/yamlconfig.rb', line 154 def catalogfile() File.join(@basedir, @config['catalogfile']) end |
#commentout(yamlfile, key) ⇒ Object
90 91 92 93 94 |
# 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) ⇒ Object
96 97 98 |
# File 'lib/review/retrovert/yamlconfig.rb', line 96 def commentout_root_yml(key) commentout(File.join(@basedir, @basename), key) end |
#copy(outdir) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/review/retrovert/yamlconfig.rb', line 82 def copy(outdir) @config_files.each { |current_file| FileUtils.copy(File.(current_file, @basedir), File.join(outdir, current_file)) } @basedir = outdir rewrite_retrovert_yml() end |
#error(msg) ⇒ Object
14 15 16 17 |
# File 'lib/review/retrovert/yamlconfig.rb', line 14 def error(msg) ReVIEW.logger.error msg exit 1 end |
#load_yaml(filepath) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/review/retrovert/yamlconfig.rb', line 138 def load_yaml(filepath) begin yaml = YAML.load_file(filepath) rescue => e error "load error #{e.}" end if yaml.class == FalseClass raise "#{current_file} is malformed." end return yaml end |
#open(yamlfile) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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.}" end @basedir = File.absolute_path(File.dirname(yamlfile)) @basename = File.basename(yamlfile) begin @config.check_version(ReVIEW::VERSION) rescue ReVIEW::ConfigError => e warn e. 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.(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) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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.(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 ⇒ Object
150 151 152 |
# File 'lib/review/retrovert/yamlconfig.rb', line 150 def path() File.join(@basedir, @basename) end |
#rewrite_retrovert_yml ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/review/retrovert/yamlconfig.rb', line 124 def rewrite_retrovert_yml() @retrovert_configs.each { |current_file| yamlfile = File.(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) ⇒ Object
106 107 108 109 110 |
# 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) ⇒ Object
100 101 102 103 104 |
# 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) ⇒ Object
118 119 120 121 122 |
# 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) ⇒ Object
112 113 114 115 116 |
# 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 |