Class: Coradoc::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/coradoc/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/coradoc/cli.rb', line 9

def self.exit_on_failure?
  true
end

Instance Method Details

#convert(file) ⇒ Object



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
63
64
# File 'lib/coradoc/cli.rb', line 35

def convert(file)
  source_format = resolve_format(file, :from)
  target_format = options[:to] ? Coradoc.normalize_format(options[:to]) : Coradoc.resolve_output_format(options[:output])

  unless source_format && target_format
    error 'Error: Could not determine format. Use --from and --to options.'
    exit 1
  end

  unless Coradoc.serialize_format?(target_format)
    error "Error: Converting to #{target_format} is not yet supported."
    exit 1
  end

  verbose_log "Converting #{file} (#{source_format}) to #{target_format}"

  doc = Coradoc.parse_file(file, format: source_format)
  doc = resolve_includes!(doc, file) if options[:resolve_includes]

  opts = build_convert_options
  result = Coradoc.serialize(doc, to: target_format, **opts)
  write_output(result, options[:output])
rescue Coradoc::Error => e
  error "Error: #{e.message}"
  exit 1
rescue StandardError => e
  error "Error: #{e.message}"
  verbose_log e.backtrace.join("\n") if options[:verbose]
  exit 1
end

#formatsObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/coradoc/cli.rb', line 67

def formats
  caps = Coradoc.format_capabilities

  puts 'Supported formats:'
  puts ''
  puts '  Source formats (can read):'
  caps.each { |name, c| puts "    - #{name}" if c[:parse] }
  puts ''
  puts '  Target formats (can write):'
  caps.each { |name, c| puts "    - #{name}" if c[:serialize] }
end

#info(file) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/coradoc/cli.rb', line 153

def info(file)
  source_format = resolve_format(file)
  unless source_format
    error 'Error: Could not determine format. Use --format option.'
    exit 1
  end

  verbose_log "Analyzing #{file} (#{source_format})"

  doc = Coradoc.parse_file(file, format: source_format)
  stats = Coradoc.document_stats(doc)
  fi = Coradoc.file_info(file)

  puts 'Document Information'
  puts '=' * 40
  puts "Format: #{source_format}"
  puts "File size: #{fi[:size]} bytes"
  puts "Line count: #{fi[:lines]}" if fi[:lines]
  puts "Title: #{stats[:title]}" if stats[:title]
  puts "Child elements: #{stats[:child_count]}" if stats[:child_count]

  if stats[:element_counts]&.any?
    puts ''
    puts 'Element Counts:'
    stats[:element_counts].each { |type, count| puts "  #{type}: #{count}" }
  end
rescue Coradoc::Error => e
  error "Error: #{e.message}"
  exit 1
rescue StandardError => e
  error "Error: #{e.message}"
  verbose_log e.backtrace.join("\n") if options[:verbose]
  exit 1
end

#query(file, selector) ⇒ Object



116
117
118
119
120
121
122
123
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
# File 'lib/coradoc/cli.rb', line 116

def query(file, selector)
  source_format = resolve_format(file)
  unless source_format
    error 'Error: Could not determine format. Use --format option.'
    exit 1
  end

  verbose_log "Querying #{file} with selector: #{selector}"

  doc = Coradoc.parse_file(file, format: source_format)
  results = Coradoc::Query.query(doc, selector)

  if results.empty?
    puts "No elements found matching: #{selector}"
  else
    case options[:output]
    when 'json'
      require 'json'
      puts JSON.pretty_generate(results.map { |r| Coradoc.describe_element(r) })
    else
      puts "Found #{results.length} element(s):"
      results.each_with_index do |elem, i|
        puts "  #{i + 1}. #{Coradoc.describe_element(elem)}"
      end
    end
  end
rescue Coradoc::Error => e
  error "Error: #{e.message}"
  exit 1
rescue StandardError => e
  error "Error: #{e.message}"
  verbose_log e.backtrace.join("\n") if options[:verbose]
  exit 1
end

#validate(file) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/coradoc/cli.rb', line 87

def validate(file)
  source_format = resolve_format(file)
  unless source_format
    error 'Error: Could not determine format. Use --format option.'
    exit 1
  end

  verbose_log "Validating #{file} (#{source_format})"

  result = Coradoc.validate_file(file, format: source_format)

  if result.valid?
    puts '✓ Document is valid'
  else
    error "#{result}"
    exit 1
  end
rescue Coradoc::Error => e
  error "Error: #{e.message}"
  exit 1
rescue StandardError => e
  error "Error: #{e.message}"
  verbose_log e.backtrace.join("\n") if options[:verbose]
  exit 1
end

#versionObject



80
81
82
# File 'lib/coradoc/cli.rb', line 80

def version
  puts "Coradoc #{Coradoc::VERSION}"
end