Class: Docbook::CLI
- Inherits:
-
Thor
- Object
- Thor
- Docbook::CLI
- Defined in:
- lib/docbook/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build(input = nil) ⇒ Object
- #export(input) ⇒ Object
- #format(input) ⇒ Object
- #info(input) ⇒ Object
- #library(input) ⇒ Object
- #lint(input) ⇒ Object
- #roundtrip(*inputs) ⇒ Object
- #validate(input) ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
322 323 324 |
# File 'lib/docbook/cli.rb', line 322 def self.exit_on_failure? true end |
Instance Method Details
#build(input = nil) ⇒ Object
44 45 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 |
# File 'lib/docbook/cli.rb', line 44 def build(input = nil) xml_path, output_path, search_dirs, title = if [:demo] demo_name = [:demo] demo_name = "xslTNG" if [ true, "demo" ].include?(demo_name) build_demo_params(demo_name) else unless input raise CliError, "Please provide an XML file. Usage: docbook build INPUT" end build_file_params(input) end start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) builder = Docbook::Output::Builder.new( xml_path: xml_path, output_path: output_path, format: [:format].to_sym, image_search_dirs: search_dirs, image_strategy: [:image_strategy].to_sym, sort_glossary: [:sort_glossary], title: title, dist_dir: [:dist_dir], ) result_path = builder.build elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time if verbose? stats = build_stats(result_path) verbose_step(" Completed in #{format_time(elapsed)}") verbose_step(" Output: #{result_path} (#{stats})") end say "Built #{result_path}" end |
#export(input) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/docbook/cli.rb', line 92 def export(input) require_relative "mirror" require_relative "output/docbook_mirror" xml_string = read_input(input, [:xinclude]) parsed = parse_input(xml_string) mirror_output = Docbook::Output::DocbookMirror.new(parsed, sort_glossary: [:sort_glossary]) output = if [:pretty] mirror_output.to_pretty_json else mirror_output.to_json end if [:output] File.write([:output], output) puts "Written to #{[:output]}" else $stdout.write(output) end end |
#format(input) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/docbook/cli.rb', line 176 def format(input) unless [:xinclude] raw_doc = Nokogiri::XML(File.read(input)) if input_xinclude?(raw_doc) warn "Warning: Document contains XIncludes. Use --xinclude to resolve them." end end formatter = Docbook::Services::Formatter.new( input_path: File.(input), resolve_xinclude: [:xinclude], ) output = formatter.format if [:output] File.write([:output], output) say "Written to #{[:output]}" else $stdout.write(output) end end |
#info(input) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/docbook/cli.rb', line 114 def info(input) xml_string = read_input(input, true) parsed = parse_input(xml_string) stats = Docbook::Services::DocumentStats.new(parsed).generate case [:format] when "json" require "json" puts JSON.pretty_generate(stats) else puts "Title: #{stats["title"]}" if stats["title"] puts "Author: #{stats["author"]}" if stats["author"] puts "Root: #{stats["root_element"]}" puts "Sections: #{stats["sections"]}" puts "Images: #{stats["images"]}" puts "Code blocks: #{stats["code_blocks"]}" puts "Tables: #{stats["tables"]}" puts "Index terms: #{stats["index_terms"]}" puts "Bibliography entries: #{stats["bibliography_entries"]}" end end |
#library(input) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/docbook/cli.rb', line 265 def library(input) input_path = File.(input) unless File.exist?(input_path) raise FileNotFoundError, "Path not found: #{input_path}" end output_path = File.([:output] || derive_library_output(input)) title = [:title] || derive_library_title(input_path) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) builder = Docbook::Output::LibraryBuilder.new( input_path: input_path, output_path: output_path, format: [:format].to_sym, image_strategy: [:image_strategy].to_sym, sort_glossary: [:sort_glossary], title: title, dist_dir: [:dist_dir], ) result_path = builder.build elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time if verbose? stats = build_stats(result_path) verbose_step(" Completed in #{format_time(elapsed)}") verbose_step(" Output: #{result_path} (#{stats})") end say "Built library: #{result_path}" end |
#lint(input) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/docbook/cli.rb', line 201 def lint(input) xml_path = File.(input) unless File.exist?(xml_path) raise FileNotFoundError, "File not found: #{xml_path}. Check the path and try again." end xml_string = File.read(xml_path) raw_doc = Nokogiri::XML(xml_string) # Quick well-formedness check if raw_doc.errors.any? = raw_doc.errors.map { |e| "#{input}: #{e}" }.join("\n") raise ValidationError, end verbose_step(" Well-formedness: OK") parsed = begin parse_input(xml_string) rescue StandardError => e raise ParseError, "Parse error: #{e.}. Check for duplicate xml:id values or invalid markup." end linter = Docbook::Services::Linter.new(parsed, input_path: xml_path) linter.check(strict: [:strict]) linter.errors.each do |err| warn "\e[31mError:\e[0m #{err[:message]} (#{err[:location]})" end linter.warnings.each do |warn_msg| warn "\e[33mWarning:\e[0m #{warn_msg[:message]} (#{warn_msg[:location]})" end if linter.ok? summary = if [:strict] "#{linter.errors.size} errors, #{linter.warnings.size} warnings" else "#{linter.errors.size} errors, #{linter.warnings.size} warnings (use --strict for xref/image checks)" end say "#{input}: #{linter.warnings.any? ? "ok (with #{linter.warnings.size} warning#{"s" unless linter.warnings.size == 1})" : "ok"}" verbose_step(" #{summary}") else = linter.errors.map do |e| "#{e[:message]} (#{e[:location]})" end.join("\n") raise LintError, "#{linter.errors.size} issue#{"s" unless linter.errors.size == 1} found:\n#{}" end end |
#roundtrip(*inputs) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/docbook/cli.rb', line 301 def roundtrip(*inputs) failures = 0 inputs.each do |input| xml_string = File.read(input) begin parsed = parse_input(xml_string) output = parsed.to_xml(declaration: true, encoding: "utf-8") parse_input(output) puts "#{input}: OK" rescue StandardError => e warn "#{input}: FAIL - #{e.}" failures += 1 end end warn "#{inputs.size} files, #{failures} failures" if failures.positive? raise ValidationError, "#{failures} roundtrip failure#{"s" unless failures == 1}" end end |
#validate(input) ⇒ Object
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 |
# File 'lib/docbook/cli.rb', line 142 def validate(input) validator = Docbook::Services::Validator.new(input_path: File.(input)) wf = validator.check_wellformedness unless wf.valid? raise ValidationError, wf.errors.join("\n") end verbose_step(" Well-formedness: OK") if [:schema] && ![:wellformed] schema = validator.check_schema unless schema.valid? raise ValidationError, schema.errors.join("\n") end verbose_step(" Schema (RELAX NG): OK") end if verbose? parsed = parse_input(File.read(input)) stats = Docbook::Services::DocumentStats.new(parsed).generate verbose_step(" Sections: #{stats["sections"]}") verbose_step(" Images: #{stats["images"]}") verbose_step(" Code blocks: #{stats["code_blocks"]}") end say "#{input}: valid" end |