Class: Coradoc::CLI
- Inherits:
-
Thor
- Object
- Thor
- Coradoc::CLI
- Defined in:
- lib/coradoc/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #convert(file) ⇒ Object
- #formats ⇒ Object
- #info(file) ⇒ Object
- #query(file, selector) ⇒ Object
- #validate(file) ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
9 10 11 |
# File 'lib/coradoc/cli.rb', line 9 def self.exit_on_failure? true end |
Instance Method Details
#convert(file) ⇒ Object
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 |
# File 'lib/coradoc/cli.rb', line 25 def convert(file) source_format = resolve_format(file, :from) target_format = [:to] ? Coradoc.normalize_format([:to]) : Coradoc.resolve_output_format([: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}" opts = result = Coradoc.convert_file(file, from: source_format, to: target_format, **opts) write_output(result, [:output]) rescue Coradoc::Error => e error "Error: #{e.}" exit 1 rescue StandardError => e error "Error: #{e.}" verbose_log e.backtrace.join("\n") if [:verbose] exit 1 end |
#formats ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/coradoc/cli.rb', line 54 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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/coradoc/cli.rb', line 140 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.}" exit 1 rescue StandardError => e error "Error: #{e.}" verbose_log e.backtrace.join("\n") if [:verbose] exit 1 end |
#query(file, selector) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/coradoc/cli.rb', line 103 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 [: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.}" exit 1 rescue StandardError => e error "Error: #{e.}" verbose_log e.backtrace.join("\n") if [:verbose] exit 1 end |
#validate(file) ⇒ Object
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 |
# File 'lib/coradoc/cli.rb', line 74 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.}" exit 1 rescue StandardError => e error "Error: #{e.}" verbose_log e.backtrace.join("\n") if [:verbose] exit 1 end |