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
- #lint(input) ⇒ Object
- #roundtrip(*inputs) ⇒ Object
- #validate(input) ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
260 261 262 |
# File 'lib/docbook/cli.rb', line 260 def self.exit_on_failure? true end |
Instance Method Details
#build(input = nil) ⇒ Object
41 42 43 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 |
# File 'lib/docbook/cli.rb', line 41 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 raise CliError, "Please provide an XML file. Usage: docbook build INPUT" unless input build_file_params(input) end FileUtils.mkdir_p(File.dirname(output_path)) verbose_step("Parsing #{File.basename(xml_path)}...") start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) page = Docbook::Output::SinglePage.new( xml_path: xml_path, output_path: output_path, image_search_dirs: search_dirs, image_strategy: [:image_strategy].to_sym, sort_glossary: [:sort_glossary], title: title, ) page.generate elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time if verbose? stats = build_stats(output_path) verbose_step(" Completed in #{format_time(elapsed)}") verbose_step(" Output: #{output_path} (#{stats})") end say "Built #{output_path}" end |
#export(input) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/docbook/cli.rb', line 87 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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/docbook/cli.rb', line 173 def format(input) xml_string = File.read(input) raw_doc = Nokogiri::XML(xml_string) if ![:xinclude] && input_xinclude?(raw_doc) warn "Warning: Document contains XIncludes. Use --xinclude to resolve them." end xml_string = read_input(input, [:xinclude]) parsed = parse_input(xml_string) output = parsed.to_xml(pretty: true, declaration: true, encoding: "utf-8") if [:output] File.write([:output], output) say "Written to #{[:output]}" else $stdout.write(output) end end |
#info(input) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/docbook/cli.rb', line 108 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 |
#lint(input) ⇒ Object
195 196 197 198 199 200 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 |
# File 'lib/docbook/cli.rb', line 195 def lint(input) xml_path = File.(input) raise FileNotFoundError, "File not found: #{xml_path}. Check the path and try again." unless File.exist?(xml_path) 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 { |e| "#{e[:message]} (#{e[:location]})" }.join("\n") raise LintError, "#{linter.errors.size} issue#{"s" unless linter.errors.size == 1} found:\n#{}" end end |
#roundtrip(*inputs) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/docbook/cli.rb', line 242 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" raise ValidationError, "#{failures} roundtrip failure#{"s" unless failures == 1}" if failures.positive? end |
#validate(input) ⇒ Object
136 137 138 139 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 |
# File 'lib/docbook/cli.rb', line 136 def validate(input) xml_string = File.read(input) doc = Nokogiri::XML(xml_string) # Well-formedness check if doc.errors.any? = doc.errors.map { |e| "#{input}: #{e}" }.join("\n") raise ValidationError, end verbose_step(" Well-formedness: OK") # Schema validation if [:schema] && ![:wellformed] errors = validate_schema(doc, input) if errors.any? = errors.map { |e| "#{input}: #{e}" }.join("\n") raise ValidationError, end verbose_step(" Schema (RELAX NG): OK") end # Additional checks in verbose mode if verbose? parsed = parse_input(xml_string) 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 |