Class: ReVIEW::Retrovert::YamlConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/review/retrovert/yamlconfig.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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



191
192
193
# File 'lib/review/retrovert/yamlconfig.rb', line 191

def catalogfile()
  File.join(@basedir, @config['catalogfile'])
end

#commentout(yamlfile, key) ⇒ Object



108
109
110
111
112
# File 'lib/review/retrovert/yamlconfig.rb', line 108

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



114
115
116
# File 'lib/review/retrovert/yamlconfig.rb', line 114

def commentout_root_yml(key)
  commentout(File.join(@basedir, @basename), key)
end

#convert_dates_to_strings(obj) ⇒ Object

Recursively convert Date and Time objects to ISO 8601 strings



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/review/retrovert/yamlconfig.rb', line 162

def convert_dates_to_strings(obj)
  case obj
  when Hash
    obj.transform_values { |v| convert_dates_to_strings(v) }
  when Array
    obj.map { |v| convert_dates_to_strings(v) }
  when Date, Time
    obj.strftime('%Y-%m-%d')
  else
    obj
  end
end

#copy(outdir) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# 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
  # Only quote date fields for review 5.3 and earlier (< 5.4.0)
  # These versions have YAML Date parsing issues with Ruby 3.1+
  if Gem::Version.new(ReVIEW::VERSION) < Gem::Version.new('5.4.0')
    quote_date_fields()
  end
  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



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/review/retrovert/yamlconfig.rb', line 175

def load_yaml(filepath)
  begin
    yaml = YAML.load_file(filepath, permitted_classes: [Date, Time])
  rescue => e
    error "load error #{e.message}"
  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.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) ⇒ 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.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 ⇒ Object



187
188
189
# File 'lib/review/retrovert/yamlconfig.rb', line 187

def path()
  File.join(@basedir, @basename)
end

#quote_date_fields ⇒ Object

Quote date fields to avoid YAML Date parsing issues with Ruby 3.1+ and review 5.3 or earlier



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/review/retrovert/yamlconfig.rb', line 96

def quote_date_fields()
  @config_files.each { |current_file|
    yamlfile = File.join(@basedir, current_file)
    content = File.read(yamlfile)
    # Match date fields like "date: 2021-07-10" and quote them
    content.gsub!(/^(\s*)(date|history):\s*(\d{4}-\d{2}-\d{2})\s*$/, '\1\2: "\3"')
    # Match date in arrays like "  - 2021-07-10"
    content.gsub!(/^(\s*-\s*)(\d{4}-\d{2}-\d{2})\s*$/, '\1"\2"')
    File.write(yamlfile, content)
  }
end

#rewrite_retrovert_yml ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/review/retrovert/yamlconfig.rb', line 142

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')
    # Convert Date/Time objects to strings to avoid YAML loading issues
    # with Ruby 3.1+ and review 5.3 or earlier
    if Gem::Version.new(ReVIEW::VERSION) < Gem::Version.new('5.4.0')
      yaml = convert_dates_to_strings(yaml)
    end
    # YAML.dump(yaml, File.open(yamlfile, "w"))
    content = Psych.dump(yaml)
    content.gsub!('---','')
    File.write(yamlfile, content)
  }
end

#rewrite_yml(key, val) ⇒ Object



124
125
126
127
128
# File 'lib/review/retrovert/yamlconfig.rb', line 124

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



118
119
120
121
122
# File 'lib/review/retrovert/yamlconfig.rb', line 118

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



136
137
138
139
140
# File 'lib/review/retrovert/yamlconfig.rb', line 136

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



130
131
132
133
134
# File 'lib/review/retrovert/yamlconfig.rb', line 130

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