Class: Uniword::TemplateCLI
- Inherits:
-
Thor
- Object
- Thor
- Uniword::TemplateCLI
- Includes:
- CLIHelpers
- Defined in:
- lib/uniword/cli/template_cli.rb
Overview
Template subcommands for Uniword CLI.
Manages a library of .docx template files – create templates from existing documents, list the library, and apply templates with data to generate new documents.
Uses TemplateCLI (not Template) to avoid collision with the Uniword::Template module.
Instance Method Summary collapse
Methods included from CLIHelpers
Instance Method Details
#apply(template_name, output_path) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/uniword/cli/template_cli.rb', line 129 def apply(template_name, output_path) data = load_template_data say "Applying template '#{template_name}'...", :green if [:verbose] if [:verbose] && data.any? say " Data keys: #{data.keys.join(', ')}", :cyan end Uniword::TemplateManager.apply( template_name, data, output_path, template_dir: [:dir], ) say "Applied template '#{template_name}' -> #{output_path}", :green rescue ArgumentError => e say "Error: #{e.}", :red exit 1 rescue Uniword::Error => e handle_error(e) rescue StandardError => e handle_error(e, verbose: [:verbose]) end |
#create(name, source) ⇒ Object
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 106 107 |
# File 'lib/uniword/cli/template_cli.rb', line 75 def create(name, source) unless File.exist?(source) say "Source file not found: #{source}", :red exit 1 end if [:verbose] say "Creating template '#{name}' from #{source}...", :green end = Uniword::TemplateManager.create( name, source, [:dir], description: [:description], ) say "Created template '#{name}' in #{[:dir]}/", :green if [:verbose] say " Description: #{[:description] || '(none)'}", :cyan say " Markers: #{[:markers]}", :cyan say " Created: #{[:created_at]}", :cyan end rescue ArgumentError => e say "Error: #{e.}", :red exit 1 rescue Uniword::Error => e handle_error(e) rescue StandardError => e handle_error(e, verbose: [:verbose]) end |
#list ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/uniword/cli/template_cli.rb', line 33 def list templates = Uniword::TemplateManager.list([:dir]) if templates.empty? say "No templates found in #{[:dir]}/", :yellow say "Use 'uniword template create' to add templates.", :yellow return end say "Available templates (#{templates.count}):", :green templates.each do |t| if [:verbose] say " #{t[:name]}:", :cyan say " Path: #{t[:path]}" say " Description: #{t[:description] || '(none)'}" say " Source: #{t[:source] || '(unknown)'}" say " Markers: #{t[:markers] || 0}" say " Created: #{t[:created_at] || '(unknown)'}" else label = t[:description] ? " - #{t[:description]}" : "" say " - #{t[:name]}#{label}" end end rescue StandardError => e handle_error(e, verbose: [:verbose]) end |