Module: Ariadna::Tools::Frontmatter
- Defined in:
- lib/ariadna/tools/frontmatter.rb
Constant Summary collapse
- SCHEMAS =
{ "plan" => %w[phase plan type], "summary" => %w[phase plan subsystem], "verification" => %w[phase] }.freeze
Class Method Summary collapse
- .body(content) ⇒ Object
- .dispatch(argv, raw: false) ⇒ Object
- .extract(content) ⇒ Object
-
.get(file, field: nil, raw: false) ⇒ Object
— CLI commands —.
- .merge(file, data:, raw: false) ⇒ Object
- .reconstruct(obj) ⇒ Object
- .set(file, field:, value:, raw: false) ⇒ Object
- .splice(content, new_obj) ⇒ Object
- .validate(file, schema:, raw: false) ⇒ Object
Class Method Details
.body(content) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/ariadna/tools/frontmatter.rb', line 116 def self.body(content) return content unless content.start_with?("---\n") end_index = content.index("---", 3) return content unless end_index content[(end_index + 3)..].lstrip end |
.dispatch(argv, raw: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ariadna/tools/frontmatter.rb', line 13 def self.dispatch(argv, raw: false) subcommand = argv.shift file = argv.shift case subcommand when "get" field = extract_flag(argv, "--field") get(file, field: field, raw: raw) when "set" field = extract_flag(argv, "--field") value = extract_flag(argv, "--value") set(file, field: field, value: value, raw: raw) when "merge" data = extract_flag(argv, "--data") merge(file, data: data, raw: raw) when "validate" schema = extract_flag(argv, "--schema") validate(file, schema: schema, raw: raw) else Output.error("Unknown frontmatter subcommand. Available: get, set, merge, validate") end end |
.extract(content) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/ariadna/tools/frontmatter.rb', line 36 def self.extract(content) return {} unless content.start_with?("---\n") end_index = content.index("---", 3) return {} unless end_index yaml_str = content[3...end_index].strip parse_yaml(yaml_str) end |
.get(file, field: nil, raw: false) ⇒ Object
— CLI commands —
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ariadna/tools/frontmatter.rb', line 127 def self.get(file, field: nil, raw: false) cwd = Dir.pwd path = File.(file, cwd) content = File.read(path) fm = extract(content) if field value = fm[field] Output.json({ field => value }, raw: raw, raw_value: value.to_s) else Output.json(fm, raw: raw) end rescue Errno::ENOENT Output.error("File not found: #{file}") end |
.merge(file, data:, raw: false) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ariadna/tools/frontmatter.rb', line 159 def self.merge(file, data:, raw: false) Output.error("--data required") unless data cwd = Dir.pwd path = File.(file, cwd) content = File.read(path) fm = extract(content) merge_data = JSON.parse(data) fm.merge!(merge_data) new_content = splice(content, fm) File.write(path, new_content) Output.json({ merged: true, fields: merge_data.keys }, raw: raw, raw_value: "true") rescue Errno::ENOENT Output.error("File not found: #{file}") rescue JSON::ParserError Output.error("Invalid JSON in --data") end |
.reconstruct(obj) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ariadna/tools/frontmatter.rb', line 46 def self.reconstruct(obj) lines = [] obj.each do |key, value| next if value.nil? if value.is_a?(Array) if value.empty? lines << "#{key}: []" elsif value.all?(String) && value.length <= 3 && value.join(", ").length < 60 lines << "#{key}: [#{value.join(', ')}]" else lines << "#{key}:" value.each do |item| item_str = item.to_s lines << if item_str.include?(":") || item_str.include?("#") " - \"#{item_str}\"" else " - #{item_str}" end end end elsif value.is_a?(Hash) lines << "#{key}:" value.each do |subkey, subval| next if subval.nil? if subval.is_a?(Array) if subval.empty? lines << " #{subkey}: []" else lines << " #{subkey}:" subval.each { |item| lines << " - #{item}" } end elsif subval.is_a?(Hash) lines << " #{subkey}:" subval.each do |k, v| next if v.nil? lines << " #{k}: #{v}" end else sv = subval.to_s lines << if sv.include?(":") || sv.include?("#") " #{subkey}: \"#{sv}\"" else " #{subkey}: #{sv}" end end end else sv = value.to_s lines << if sv.include?(":") || sv.include?("#") || sv.start_with?("[") || sv.start_with?("{") "#{key}: \"#{sv}\"" else "#{key}: #{sv}" end end end lines.join("\n") end |
.set(file, field:, value:, raw: false) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ariadna/tools/frontmatter.rb', line 143 def self.set(file, field:, value:, raw: false) Output.error("field and value required") unless field && value cwd = Dir.pwd path = File.(file, cwd) content = File.read(path) fm = extract(content) parsed_value = parse_value(value) fm[field] = parsed_value new_content = splice(content, fm) File.write(path, new_content) Output.json({ updated: true, field: field, value: parsed_value }, raw: raw, raw_value: "true") rescue Errno::ENOENT Output.error("File not found: #{file}") end |
.splice(content, new_obj) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/ariadna/tools/frontmatter.rb', line 107 def self.splice(content, new_obj) yaml_str = reconstruct(new_obj) if content.match?(/\A---\n[\s\S]+?\n---/) content.sub(/\A---\n[\s\S]+?\n---/, "---\n#{yaml_str}\n---") else "---\n#{yaml_str}\n---\n\n#{content}" end end |
.validate(file, schema:, raw: false) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/ariadna/tools/frontmatter.rb', line 177 def self.validate(file, schema:, raw: false) Output.error("--schema required") unless schema required = SCHEMAS[schema] Output.error("Unknown schema: #{schema}. Available: #{SCHEMAS.keys.join(', ')}") unless required cwd = Dir.pwd path = File.(file, cwd) content = File.read(path) fm = extract(content) missing = required.reject { |f| fm.key?(f) } valid = missing.empty? Output.json({ valid: valid, missing: missing, schema: schema }, raw: raw, raw_value: valid.to_s) rescue Errno::ENOENT Output.error("File not found: #{file}") end |