victor-cli 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9acf122e53e375434a1cad7cf0a5c1e93c7b804304f02e0907d0b95776739d4e
4
- data.tar.gz: 93bbaa1caa3ccc7747f291f60de666fefd90696ed69c48f055f6ef0136c11bce
3
+ metadata.gz: fc2b1c5e3ce6627c982daf91c5ecb7c8427f61064f5df3474619ab4eb1e5f220
4
+ data.tar.gz: 04c775439539bb96ddb866354d7c8f3925ade9ddb768cb6b9e23f1cae22a017f
5
5
  SHA512:
6
- metadata.gz: f8f6f15561beecff52a10e4ab4c02837a10ac2037a65ea62467bc1a27d78140917b0f6a0f59d597b41f746b42835e30a3613e45b395d891eaa1113efe241c45f
7
- data.tar.gz: de421ce4109502beaffc76f8a3df3b9d27e4f4a5117c411b1f9d417ecc85f01de61030c3623ee76d32e666f04401f94cf803eacd224e2cd80d64d8d0a073e9a9
6
+ metadata.gz: 2453dcea1067196ae6e71ba9ef1c0c509ccc45c2c9ea109632612892a1b115fba64303b43aff65aa590bc75f4ea1a5ba3b8e91d3516a49478750492b2f7b104c
7
+ data.tar.gz: d5099606414fe52ff0e27c5da5b411751c38c47c8d98ac5968077b9895158f1de881030a7b00b5e405565618a233aac1a51f728ac201aaa0f4682eeb96be8d2c
data/README.md CHANGED
@@ -16,35 +16,15 @@ Command line interface for [Victor][victor], the SVG Library.
16
16
 
17
17
  ## Usage
18
18
 
19
- ### Convert SVG to Ruby
19
+ ### `init`: Create a sample Ruby file
20
20
 
21
- Given this SVG file:
22
-
23
- ```xml
24
- <!-- example.svg -->
25
- <svg width="140" height="100">
26
- <circle cx="50" cy="50" r="30" fill="yellow"/>
27
- </svg>
28
- ```
29
-
30
- Run this command:
21
+ Run this command to create an initial sample file:
31
22
 
32
23
  ```shell
33
- $ victor to-ruby example.svg
34
- ```
35
-
36
- To generate this Ruby code:
37
-
38
- ```ruby
39
- setup width: "140", height: "100"
40
-
41
- build do
42
- circle cx: "50", cy: "50", r: "30", fill: "yellow"
43
- end
44
-
24
+ $ victor init example
45
25
  ```
46
26
 
47
- ### Convert Ruby to SVG
27
+ ### `render`: Render Ruby to SVG
48
28
 
49
29
  Given this Ruby code:
50
30
 
@@ -59,7 +39,7 @@ end
59
39
 
60
40
  Run this command:
61
41
  ```shell
62
- $ victor to-svg example.rb --template minimal
42
+ $ victor render example.rb --template minimal
63
43
  ```
64
44
 
65
45
  To generate this code:
@@ -70,6 +50,33 @@ To generate this code:
70
50
  </svg>
71
51
  ```
72
52
 
53
+ ### `convert`: Convert SVG to Ruby
54
+
55
+ Given this SVG file:
56
+
57
+ ```xml
58
+ <!-- example.svg -->
59
+ <svg width="140" height="100">
60
+ <circle cx="50" cy="50" r="30" fill="yellow"/>
61
+ </svg>
62
+ ```
63
+
64
+ Run this command:
65
+
66
+ ```shell
67
+ $ victor convert example.svg
68
+ ```
69
+
70
+ To generate this Ruby code:
71
+
72
+ ```ruby
73
+ setup width: 140, height: 100
74
+
75
+ build do
76
+ circle cx: 50, cy: 50, r: 30, fill: "yellow"
77
+ end
78
+
79
+ ```
73
80
  ---
74
81
 
75
82
  [victor]: https://github.com/DannyBen/victor
data/bin/victor CHANGED
@@ -4,6 +4,7 @@ require 'pretty_trace/enable-trim'
4
4
 
5
5
  include Colsole
6
6
  PrettyTrace.filter [/gem/, /lib/, /bin\/victor/]
7
+ PrettyTrace.debug_tip
7
8
 
8
9
  router = Victor::CLI::CommandLine.router
9
10
  exit router.run ARGV
@@ -1,7 +1,11 @@
1
1
  require 'victor'
2
+ require 'requires'
3
+ requires 'cli/refinements'
2
4
  require_relative 'cli/parser'
3
- require_relative 'cli/code_generator'
4
- require_relative 'cli/ruby_code'
5
+ require_relative 'cli/svg_source'
6
+ require_relative 'cli/ruby_source'
5
7
  require_relative 'cli/command_line'
8
+ require_relative 'cli/xml_node'
9
+ require_relative 'cli/xml_text'
6
10
 
7
11
  require 'byebug' if ENV['BYEBUG']
@@ -1,7 +1,5 @@
1
1
  require 'mister_bin'
2
- require_relative 'commands/base'
3
- require_relative 'commands/generate_ruby'
4
- require_relative 'commands/generate_svg'
2
+ requires 'commands/base', 'commands'
5
3
 
6
4
  module Victor
7
5
  module CLI
@@ -10,8 +8,9 @@ module Victor
10
8
  router = MisterBin::Runner.new version: VERSION,
11
9
  header: "Victor SVG Utilities"
12
10
 
13
- router.route 'to-ruby', to: Commands::GenerateRuby
14
- router.route 'to-svg', to: Commands::GenerateSVG
11
+ router.route 'init', to: Commands::Init
12
+ router.route 'convert', to: Commands::Convert
13
+ router.route 'render', to: Commands::Render
15
14
 
16
15
  router
17
16
  end
@@ -1,11 +1,11 @@
1
1
  module Victor
2
2
  module CLI
3
3
  module Commands
4
- class GenerateRuby < Base
4
+ class Convert < Base
5
5
  summary "Convert SVG to Ruby code"
6
6
 
7
- usage "victor to-ruby SVG_FILE [RUBY_FILE --template NAME]"
8
- usage "victor to-ruby (-h|--help)"
7
+ usage "victor convert SVG_FILE [RUBY_FILE --template NAME]"
8
+ usage "victor convert (-h|--help)"
9
9
 
10
10
  option "-t, --template NAME", "Name of the Ruby template to use. Can be:\n" +
11
11
  " standalone a full standalone Ruby script\n" +
@@ -15,14 +15,13 @@ module Victor
15
15
  param "SVG_FILE", "Input SVG file"
16
16
  param "RUBY_FILE", "Output Ruby file. Leave empty to write to stdout"
17
17
 
18
- example "victor to-ruby example.svg example.rb"
18
+ example "victor convert example.svg example.rb"
19
19
 
20
20
  def run
21
- svg_file = File.read(args["SVG_FILE"])
22
- svg_tree = Parser.new(svg_file).parse
23
- generator = CodeGenerator.new svg_tree, template: args['--template']
21
+ svg_data = File.read(args["SVG_FILE"])
22
+ svg_source = SVGSource.new svg_data, template: args['--template']
24
23
 
25
- code = generator.generate
24
+ code = svg_source.ruby_code
26
25
  ruby_file = args["RUBY_FILE"]
27
26
 
28
27
  if ruby_file
@@ -0,0 +1,71 @@
1
+ module Victor
2
+ module CLI
3
+ module Commands
4
+ class Init < Base
5
+ summary "Create a sample Victor Ruby script"
6
+
7
+ usage "victor init RUBY_FILE [--template TEMPLATE]"
8
+ usage "victor init (-h|--help)"
9
+
10
+ option "-t, --template NAME", "Name of the Ruby template to use. Can be:\n" +
11
+ " standalone a full standalone Ruby script\n" +
12
+ " dsl a Victor DSL script\n" +
13
+ " cli a Victor CLI compatible DSL script (default)"
14
+
15
+ param "RUBY_FILE", "Output Ruby file"
16
+
17
+ example "victor init ghost.rb"
18
+ example "victor init ghost.rb --template standalone"
19
+
20
+ def run
21
+ raise "File already exists #{filename}" if File.exist? filename
22
+
23
+ basename = File.basename filename, '.rb'
24
+ vars = { filename: filename, basename: basename }
25
+ content = template_content(template) % vars
26
+
27
+ File.write filename, content
28
+ say "Saved #{filename}"
29
+
30
+ if template == 'cli'
31
+ say %Q[Run !txtblu!victor render "#{filename}"!txtrst! to render]
32
+ else
33
+ say %Q[Run !txtblu!ruby "#{filename}"!txtrst! to render]
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def filename
40
+ @filename ||= if args["RUBY_FILE"].end_with? '.rb'
41
+ args["RUBY_FILE"]
42
+ else
43
+ args['RUBY_FILE'] + ".rb"
44
+ end
45
+ end
46
+
47
+ def template
48
+ @template ||= args['--template'] || 'cli'
49
+ end
50
+
51
+ def template_content(name)
52
+ filename = File.join templates_path, "#{name}.rb"
53
+
54
+ unless available_templates.include? name
55
+ raise "Invalid template #{name}\nAvailable templates: #{available_templates.join ', '}"
56
+ end
57
+
58
+ File.read filename
59
+ end
60
+
61
+ def templates_path
62
+ @templates_path ||= File.expand_path "../templates/init", __dir__
63
+ end
64
+
65
+ def available_templates
66
+ ['cli', 'dsl', 'standalone']
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,73 @@
1
+ require 'filewatcher'
2
+
3
+ module Victor
4
+ module CLI
5
+ module Commands
6
+ class Render < Base
7
+ summary "Render Ruby code to SVG"
8
+
9
+ usage "victor render RUBY_FILE [SVG_FILE] [options]"
10
+ usage "victor render (-h|--help)"
11
+
12
+ option '-t, --template TEMPLATE', "Set SVG template\n"+
13
+ "Can be: default, html, minimal, or a file path"
14
+
15
+ option '-w, --watch', 'Watch the source file and regenerate on change'
16
+
17
+ param "RUBY_FILE", "Input Ruby file"
18
+ param "SVG_FILE", "Output SVG file. Leave empty to write to stdout"
19
+
20
+ example "victor render input.rb output.svg"
21
+ example "victor render input.rb --template minimal"
22
+
23
+ def run
24
+ if args['--watch']
25
+ watch { generate }
26
+ else
27
+ generate
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def generate
34
+ code = File.read ruby_file
35
+
36
+ ruby_source = RubySource.new code, ruby_file
37
+ ruby_source.evaluate
38
+ ruby_source.template template if template
39
+
40
+ if svg_file
41
+ ruby_source.svg.save svg_file
42
+ say "Saved #{svg_file}"
43
+ else
44
+ puts ruby_source.svg.render
45
+ end
46
+ end
47
+
48
+ def watch
49
+ say "Watching #{ruby_file} for changes"
50
+ file_watcher.watch do |file, event|
51
+ yield unless event == :deleted
52
+ end
53
+ end
54
+
55
+ def file_watcher
56
+ @file_watcher ||= Filewatcher.new(ruby_file, immediate: true)
57
+ end
58
+
59
+ def ruby_file
60
+ args["RUBY_FILE"]
61
+ end
62
+
63
+ def svg_file
64
+ args["SVG_FILE"]
65
+ end
66
+
67
+ def template
68
+ args['--template']
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -15,7 +15,6 @@ module Victor
15
15
 
16
16
  private
17
17
 
18
-
19
18
  def parse_node(node)
20
19
  case node
21
20
  when Nokogiri::XML::Comment
@@ -28,18 +27,16 @@ module Victor
28
27
  end
29
28
 
30
29
  def parse_text(node)
31
- inner_text = node.text
32
- cleaned_text = inner_text.strip
33
- return nil unless cleaned_text.length > 0
34
- return ["_", {}, cleaned_text]
30
+ text_node = XMLText.new(node.text)
31
+ return text_node if text_node.text.length > 0
35
32
  end
36
33
 
37
34
  def parse_normal_node(node)
38
- [
35
+ XMLNode.new(
39
36
  node.name,
40
37
  node_attrs(node),
41
38
  node.children.map(&method(:parse_node)).compact,
42
- ]
39
+ )
43
40
  end
44
41
 
45
42
  def node_attrs(node)
@@ -0,0 +1,19 @@
1
+ module Victor
2
+ module CLI
3
+ module Refinements
4
+ refine String do
5
+ def format_as_key
6
+ gsub('-', '_')
7
+ end
8
+
9
+ def format_as_value
10
+ if to_f.to_s == self or to_i.to_s == self
11
+ self
12
+ else
13
+ %Q["#{self}"]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,15 +1,20 @@
1
1
  module Victor
2
2
  module CLI
3
- class RubyCode
3
+ class RubySource
4
4
  include Victor::DSL
5
- attr_reader :code
5
+ attr_reader :code, :filename
6
6
 
7
- def initialize(code)
7
+ def initialize(code, filename = nil)
8
8
  @code = code
9
+ @filename = filename
9
10
  end
10
11
 
11
12
  def evaluate
12
- instance_eval code
13
+ if filename
14
+ instance_eval code, filename
15
+ else
16
+ instance_eval code
17
+ end
13
18
  end
14
19
 
15
20
  def template(template)
@@ -0,0 +1,105 @@
1
+ require "rufo"
2
+
3
+ module Victor
4
+ module CLI
5
+ class SVGSource
6
+ using Refinements
7
+ attr_reader :svg_tree, :template
8
+
9
+ def initialize(svg_tree, template: nil)
10
+ @svg_tree = svg_tree
11
+ @svg_tree = Parser.new(@svg_tree).parse if @svg_tree.is_a? String
12
+ @template = template || :cli
13
+ end
14
+
15
+ def ruby_code
16
+ @ruby_code ||= Rufo::Formatter.format(code_for_node(svg_tree))
17
+ end
18
+
19
+ private
20
+
21
+ def code_for_node(node)
22
+ return text_to_ruby node if node.is_a?(XMLText)
23
+
24
+ case node.type
25
+ when "svg"
26
+ root_to_ruby node
27
+ when "text", "tspan"
28
+ text_node_to_ruby node
29
+ else
30
+ node_to_ruby node
31
+ end
32
+ end
33
+
34
+ def text_node_to_ruby(node)
35
+ children = node.children
36
+ if children.length == 1 && children.first.is_a?(XMLText)
37
+ short_text_to_ruby node
38
+ else
39
+ node_to_ruby node
40
+ end
41
+ end
42
+
43
+ def short_text_to_ruby(node)
44
+ attrs = node.attributes.empty? ? "" : ",#{attrs_to_ruby(node.attributes)}"
45
+ inner_text = node.children.first.text
46
+ "text #{inner_text.inspect} #{attrs}"
47
+ end
48
+
49
+ def node_to_ruby(node)
50
+ code = "#{node.type} #{attrs_to_ruby(node.attributes)} "
51
+ unless node.children.empty?
52
+ code << " do\n"
53
+ code << nodes_to_ruby(node.children)
54
+ code << "\nend\n"
55
+ end
56
+ code
57
+ end
58
+
59
+ def nodes_to_ruby(nodes)
60
+ nodes.map do |node|
61
+ code_for_node node
62
+ end.join "\n"
63
+ end
64
+
65
+ def text_to_ruby(node)
66
+ "_ #{node.text.inspect}"
67
+ end
68
+
69
+ def attrs_to_ruby(attrs)
70
+ attrs.map do |key, value|
71
+ "#{key.format_as_key}: #{value.format_as_value}"
72
+ end.join ", "
73
+ end
74
+
75
+ def root_to_ruby(node)
76
+ values = {
77
+ attributes: attrs_to_ruby(node.attributes),
78
+ nodes: nodes_to_ruby(node.children),
79
+ }
80
+
81
+ template_content(template) % values
82
+ end
83
+
84
+ def template_content(name)
85
+ filename = File.join templates_path, "#{name}.rb"
86
+
87
+ unless File.exist? filename
88
+ raise "Template not found #{name}\nAvailable templates: #{available_templates.join ', '}"
89
+ end
90
+
91
+ File.read filename
92
+ end
93
+
94
+ def templates_path
95
+ @templates_path ||= File.expand_path "templates/import", __dir__
96
+ end
97
+
98
+ def available_templates
99
+ @available_templates ||= Dir["#{templates_path}/*.rb"].map do |path|
100
+ File.basename path, '.rb'
101
+ end.sort
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,8 @@
1
+ # Render this template by running 'victor render FILE'
2
+
3
+ setup %{attributes}
4
+
5
+ build do
6
+ %{nodes}
7
+ end
8
+
@@ -1,4 +1,4 @@
1
- # Render this template by running 'ruby FILE' or 'victor to-svg FILE'
1
+ # Render this template by running 'ruby FILE' or 'victor render FILE'
2
2
 
3
3
  require 'victor/script'
4
4
 
@@ -1,4 +1,4 @@
1
- # Render this template by running 'ruby FILE' or 'victor to-svg FILE'
1
+ # Render this template by running 'ruby FILE' or 'victor render FILE'
2
2
 
3
3
  require "victor"
4
4
 
@@ -0,0 +1,15 @@
1
+ # Render this by running:
2
+ # victor render "%{filename}"
3
+
4
+ setup viewBox: "0 0 100 100"
5
+
6
+ build do
7
+ circle cx: 50, cy: 50, r: 40, fill: 'yellow'
8
+ rect x: 10, y: 50, width: 80, height: 50, fill: 'yellow'
9
+
10
+ [25, 50].each do |y|
11
+ circle cx: y, cy: 40, r: 8, fill: 'white'
12
+ end
13
+
14
+ path d: "M11 100 l13 -15 l13 15 l13 -15 l13 15 l13 -15 l13 15 Z", fill: 'white'
15
+ end
@@ -0,0 +1,19 @@
1
+ # Render this by running
2
+ # ruby "%{filename}"
3
+ require 'victor/script'
4
+
5
+ setup viewBox: "0 0 100 100"
6
+
7
+ build do
8
+ circle cx: 50, cy: 50, r: 40, fill: 'yellow'
9
+ rect x: 10, y: 50, width: 80, height: 50, fill: 'yellow'
10
+
11
+ [25, 50].each do |y|
12
+ circle cx: y, cy: 40, r: 8, fill: 'white'
13
+ end
14
+
15
+ path d: "M11 100 l13 -15 l13 15 l13 -15 l13 15 l13 -15 l13 15 Z", fill: 'white'
16
+ end
17
+
18
+ puts render
19
+ save "%{basename}"
@@ -0,0 +1,19 @@
1
+ # Render this by running
2
+ # ruby "%{filename}"
3
+ require 'victor'
4
+
5
+ svg = Victor::SVG.new viewBox: "0 0 100 100"
6
+
7
+ svg.build do
8
+ circle cx: 50, cy: 50, r: 40, fill: 'yellow'
9
+ rect x: 10, y: 50, width: 80, height: 50, fill: 'yellow'
10
+
11
+ [25, 50].each do |y|
12
+ circle cx: y, cy: 40, r: 8, fill: 'white'
13
+ end
14
+
15
+ path d: "M11 100 l13 -15 l13 15 l13 -15 l13 15 l13 -15 l13 15 Z", fill: 'white'
16
+ end
17
+
18
+ puts svg.render
19
+ svg.save "%{basename}"
@@ -1,5 +1,5 @@
1
1
  module Victor
2
2
  module CLI
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -0,0 +1,14 @@
1
+ module Victor
2
+ module CLI
3
+ class XMLNode
4
+ attr_reader :type, :attributes, :children
5
+
6
+ def initialize(type, attributes, children)
7
+ @type = type
8
+ @attributes = attributes
9
+ @children = children
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Victor
2
+ module CLI
3
+ class XMLText
4
+ attr_reader :text
5
+
6
+ def initialize(text)
7
+ @text = text.strip
8
+ end
9
+ end
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: victor-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
@@ -9,64 +9,64 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-29 00:00:00.000000000 Z
12
+ date: 2020-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: mister_bin
15
+ name: filewatcher
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '0.7'
20
+ version: '1.1'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '0.7'
27
+ version: '1.1'
28
28
  - !ruby/object:Gem::Dependency
29
- name: colsole
29
+ name: nokogiri
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0.7'
34
+ version: '1.10'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0.7'
41
+ version: '1.10'
42
42
  - !ruby/object:Gem::Dependency
43
- name: victor
43
+ name: pretty_trace
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.3'
48
+ version: 0.2.5
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0.3'
55
+ version: 0.2.5
56
56
  - !ruby/object:Gem::Dependency
57
- name: nokogiri
57
+ name: requires
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '1.10'
62
+ version: '0.2'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '1.10'
69
+ version: '0.2'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rufo
72
72
  requirement: !ruby/object:Gem::Requirement
@@ -82,19 +82,47 @@ dependencies:
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0.12'
84
84
  - !ruby/object:Gem::Dependency
85
- name: pretty_trace
85
+ name: colsole
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
88
  - - "~>"
89
89
  - !ruby/object:Gem::Version
90
- version: '0.2'
90
+ version: '0.7'
91
91
  type: :runtime
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
- version: '0.2'
97
+ version: '0.7'
98
+ - !ruby/object:Gem::Dependency
99
+ name: mister_bin
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '0.7'
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '0.7'
112
+ - !ruby/object:Gem::Dependency
113
+ name: victor
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '0.3'
119
+ type: :runtime
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '0.3'
98
126
  description: CLI for Victor, the SVG Library
99
127
  email: db@dannyben.com
100
128
  executables:
@@ -105,17 +133,24 @@ files:
105
133
  - README.md
106
134
  - bin/victor
107
135
  - lib/victor/cli.rb
108
- - lib/victor/cli/code_generator.rb
109
136
  - lib/victor/cli/command_line.rb
110
137
  - lib/victor/cli/commands/base.rb
111
- - lib/victor/cli/commands/generate_ruby.rb
112
- - lib/victor/cli/commands/generate_svg.rb
138
+ - lib/victor/cli/commands/convert.rb
139
+ - lib/victor/cli/commands/init.rb
140
+ - lib/victor/cli/commands/render.rb
113
141
  - lib/victor/cli/parser.rb
114
- - lib/victor/cli/ruby_code.rb
115
- - lib/victor/cli/templates/cli.rb
116
- - lib/victor/cli/templates/dsl.rb
117
- - lib/victor/cli/templates/standalone.rb
142
+ - lib/victor/cli/refinements/stirng_refinements.rb
143
+ - lib/victor/cli/ruby_source.rb
144
+ - lib/victor/cli/svg_source.rb
145
+ - lib/victor/cli/templates/import/cli.rb
146
+ - lib/victor/cli/templates/import/dsl.rb
147
+ - lib/victor/cli/templates/import/standalone.rb
148
+ - lib/victor/cli/templates/init/cli.rb
149
+ - lib/victor/cli/templates/init/dsl.rb
150
+ - lib/victor/cli/templates/init/standalone.rb
118
151
  - lib/victor/cli/version.rb
152
+ - lib/victor/cli/xml_node.rb
153
+ - lib/victor/cli/xml_text.rb
119
154
  homepage: https://github.com/dannyben/victor-cli
120
155
  licenses:
121
156
  - MIT
@@ -1,81 +0,0 @@
1
- require "rufo"
2
-
3
- module Victor
4
- module CLI
5
- class CodeGenerator
6
- attr_reader :svg_tree, :template
7
-
8
- def initialize(svg_tree, template: nil)
9
- @svg_tree = svg_tree
10
- @template = template || :cli
11
- end
12
-
13
- def generate
14
- Rufo::Formatter.format(code_for_node(svg_tree))
15
- end
16
-
17
- private
18
-
19
- def code_for_node(node)
20
- case node.first
21
- when "svg"
22
- root_to_ruby node
23
- else
24
- node_to_ruby node
25
- end
26
- end
27
-
28
- def node_to_ruby(node)
29
- name, attrs, children = node
30
- code = "#{name} #{attrs_to_ruby(attrs)} "
31
- unless children.empty?
32
- code << " do\n"
33
- code << nodes_to_ruby(children)
34
- code << "\nend\n"
35
- end
36
- code
37
- end
38
-
39
- def nodes_to_ruby(nodes)
40
- nodes.map(&method(:code_for_node)).join("\n")
41
- end
42
-
43
- def attrs_to_ruby(attrs)
44
- attrs.reduce([]) do |acc, (k, v)|
45
- acc << "#{k.to_sym.inspect[1..-1]}: #{v.inspect}"
46
- acc
47
- end.join(",")
48
- end
49
-
50
- def root_to_ruby(node)
51
- _, attrs, children = node
52
- values = {
53
- attributes: attrs_to_ruby(attrs),
54
- nodes: nodes_to_ruby(children)
55
- }
56
-
57
- template_content(template) % values
58
- end
59
-
60
- def template_content(name)
61
- filename = File.join templates_path, "#{name}.rb"
62
-
63
- unless File.exist? filename
64
- raise "Template not found #{name}\nAvailable templates: #{available_templates.join ', '}"
65
- end
66
-
67
- File.read filename
68
- end
69
-
70
- def templates_path
71
- @templates_path ||= File.expand_path "templates", __dir__
72
- end
73
-
74
- def available_templates
75
- @available_templates ||= Dir["#{templates_path}/*.rb"].map do |path|
76
- File.basename path, '.rb'
77
- end
78
- end
79
- end
80
- end
81
- end
@@ -1,39 +0,0 @@
1
- module Victor
2
- module CLI
3
- module Commands
4
- class GenerateSVG < Base
5
- summary "Convert Ruby code to SVG"
6
-
7
- usage "victor to-svg RUBY_FILE [SVG_FILE --template TEMPLATE]"
8
- usage "victor to-svg (-h|--help)"
9
-
10
- option '-t, --template TEMPLATE', "Set SVG template\n"+
11
- "Can be: default, html, minimal, or a file path"
12
-
13
- param "RUBY_FILE", "Input Ruby file"
14
- param "SVG_FILE", "Output SVG file. Leave empty to write to stdout"
15
-
16
- example "victor to-svg input.rb output.svg"
17
- example "victor to-svg input.rb --template minimal"
18
-
19
- def run
20
- ruby_file = args["RUBY_FILE"]
21
- svg_file = args["SVG_FILE"]
22
- template = args['--template']
23
- code = File.read ruby_file
24
-
25
- ruby_code = RubyCode.new code
26
- ruby_code.evaluate
27
- ruby_code.template template if template
28
-
29
- if svg_file
30
- ruby_code.svg.save svg_file
31
- say "Saved #{svg_file}"
32
- else
33
- puts ruby_code.svg.render
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,8 +0,0 @@
1
- # Render this template by running 'victor to-svg FILE'
2
-
3
- setup %{attributes}
4
-
5
- build do
6
- %{nodes}
7
- end
8
-