victor-cli 0.1.0 → 0.2.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: 126abee42ab8abfde3a3f12704e20c5bb59175bf7e8789acee1423728c733c17
4
- data.tar.gz: 5799eb27256c286e10bc8f710fcf08d1d6a4d7726553e99765a843839b8e3f06
3
+ metadata.gz: 9acf122e53e375434a1cad7cf0a5c1e93c7b804304f02e0907d0b95776739d4e
4
+ data.tar.gz: 93bbaa1caa3ccc7747f291f60de666fefd90696ed69c48f055f6ef0136c11bce
5
5
  SHA512:
6
- metadata.gz: dd97b8db3ac7653addec886f4f4a079cf8a2edff1fe037ba4e01e7b80542032d983a4281441febcaf73fd848a336b0a76bfefdfc131a2b0d7b80df9483c63612
7
- data.tar.gz: d1ccb5205691afe44679219e018dfd4f44873fc01536197ef9ad1fc1515e1967c6aecdce162f6a941f8685e3f7f20f60be1f9cf13a226e2a1bb7e58355082098
6
+ metadata.gz: f8f6f15561beecff52a10e4ab4c02837a10ac2037a65ea62467bc1a27d78140917b0f6a0f59d597b41f746b42835e30a3613e45b395d891eaa1113efe241c45f
7
+ data.tar.gz: de421ce4109502beaffc76f8a3df3b9d27e4f4a5117c411b1f9d417ecc85f01de61030c3623ee76d32e666f04401f94cf803eacd224e2cd80d64d8d0a073e9a9
data/README.md CHANGED
@@ -14,31 +14,60 @@ Command line interface for [Victor][victor], the SVG Library.
14
14
 
15
15
  $ gem install victor-cli
16
16
 
17
-
18
17
  ## Usage
19
18
 
19
+ ### Convert SVG to Ruby
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:
31
+
32
+ ```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
+
20
45
  ```
21
- $ victor generate --help
22
- Generate Ruby code from SVG
23
46
 
24
- Usage:
25
- victor generate SVG_FILE [RUBY_FILE]
26
- victor generate (-h|--help)
47
+ ### Convert Ruby to SVG
27
48
 
28
- Options:
29
- -h --help
30
- Show this help
49
+ Given this Ruby code:
31
50
 
32
- Parameters:
33
- SVG_FILE
34
- Input SVG file
51
+ ```ruby
52
+ # example.rb
53
+ setup width: 140, height: 100
35
54
 
36
- RUBY_FILE
37
- Output Ruby file. Leave empty to write to stdout
55
+ build do
56
+ circle cx: 50, cy: 50, r: 30, fill: "yellow"
57
+ end
58
+ ```
59
+
60
+ Run this command:
61
+ ```shell
62
+ $ victor to-svg example.rb --template minimal
63
+ ```
38
64
 
39
- Examples:
40
- victor generate example.svg example.rb
65
+ To generate this code:
41
66
 
67
+ ```xml
68
+ <svg width="140" height="100">
69
+ <circle cx="50" cy="50" r="30" fill="yellow"/>
70
+ </svg>
42
71
  ```
43
72
 
44
73
  ---
data/bin/victor CHANGED
@@ -1,13 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'victor/cli'
3
+ require 'pretty_trace/enable-trim'
4
+
3
5
  include Colsole
6
+ PrettyTrace.filter [/gem/, /lib/, /bin\/victor/]
4
7
 
5
8
  router = Victor::CLI::CommandLine.router
6
-
7
- begin
8
- exit router.run ARGV
9
- rescue => e
10
- puts e.backtrace.reverse if ENV['DEBUG']
11
- say! "!txtred!#{e.class}: #{e.message}"
12
- exit 1
13
- end
9
+ exit router.run ARGV
@@ -1,5 +1,7 @@
1
+ require 'victor'
1
2
  require_relative 'cli/parser'
2
3
  require_relative 'cli/code_generator'
4
+ require_relative 'cli/ruby_code'
3
5
  require_relative 'cli/command_line'
4
6
 
5
7
  require 'byebug' if ENV['BYEBUG']
@@ -3,24 +3,25 @@ require "rufo"
3
3
  module Victor
4
4
  module CLI
5
5
  class CodeGenerator
6
- def initialize(svg_tree)
6
+ attr_reader :svg_tree, :template
7
+
8
+ def initialize(svg_tree, template: nil)
7
9
  @svg_tree = svg_tree
10
+ @template = template || :cli
8
11
  end
9
12
 
10
13
  def generate
11
14
  Rufo::Formatter.format(code_for_node(svg_tree))
12
15
  end
13
16
 
14
- private
15
-
16
- attr_reader :svg_tree
17
+ private
17
18
 
18
19
  def code_for_node(node)
19
20
  case node.first
20
21
  when "svg"
21
- root_to_ruby(node)
22
+ root_to_ruby node
22
23
  else
23
- node_to_ruby(node)
24
+ node_to_ruby node
24
25
  end
25
26
  end
26
27
 
@@ -48,16 +49,32 @@ module Victor
48
49
 
49
50
  def root_to_ruby(node)
50
51
  _, attrs, children = node
51
- <<~RUBY
52
- require "victor"
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"
53
62
 
54
- svg = Victor::SVG.new #{attrs_to_ruby(attrs)}
55
- svg.build do
56
- #{nodes_to_ruby(children)}
57
- end
63
+ unless File.exist? filename
64
+ raise "Template not found #{name}\nAvailable templates: #{available_templates.join ', '}"
65
+ end
58
66
 
59
- svg.save "generated"
60
- RUBY
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
61
78
  end
62
79
  end
63
80
  end
@@ -1,6 +1,7 @@
1
1
  require 'mister_bin'
2
2
  require_relative 'commands/base'
3
- require_relative 'commands/generate'
3
+ require_relative 'commands/generate_ruby'
4
+ require_relative 'commands/generate_svg'
4
5
 
5
6
  module Victor
6
7
  module CLI
@@ -9,7 +10,8 @@ module Victor
9
10
  router = MisterBin::Runner.new version: VERSION,
10
11
  header: "Victor SVG Utilities"
11
12
 
12
- router.route 'generate', to: Commands::Generate
13
+ router.route 'to-ruby', to: Commands::GenerateRuby
14
+ router.route 'to-svg', to: Commands::GenerateSVG
13
15
 
14
16
  router
15
17
  end
@@ -0,0 +1,38 @@
1
+ module Victor
2
+ module CLI
3
+ module Commands
4
+ class GenerateRuby < Base
5
+ summary "Convert SVG to Ruby code"
6
+
7
+ usage "victor to-ruby SVG_FILE [RUBY_FILE --template NAME]"
8
+ usage "victor to-ruby (-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 "SVG_FILE", "Input SVG file"
16
+ param "RUBY_FILE", "Output Ruby file. Leave empty to write to stdout"
17
+
18
+ example "victor to-ruby example.svg example.rb"
19
+
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']
24
+
25
+ code = generator.generate
26
+ ruby_file = args["RUBY_FILE"]
27
+
28
+ if ruby_file
29
+ File.write ruby_file, code
30
+ say "Saved #{ruby_file}"
31
+ else
32
+ puts code
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
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
@@ -3,21 +3,28 @@ require "nokogiri"
3
3
  module Victor
4
4
  module CLI
5
5
  class Parser
6
+ attr_reader :raw_svg
7
+
6
8
  def initialize(raw_svg)
7
9
  @raw_svg = raw_svg
8
10
  end
9
11
 
10
12
  def parse
11
- parse_node(svg_root)
13
+ parse_node svg_root
12
14
  end
13
15
 
14
- private
16
+ private
15
17
 
16
- attr_reader :raw_svg
17
18
 
18
19
  def parse_node(node)
19
- return parse_text(node) if node.is_a?(Nokogiri::XML::Text)
20
- parse_normal_node(node)
20
+ case node
21
+ when Nokogiri::XML::Comment
22
+ nil
23
+ when Nokogiri::XML::Text
24
+ parse_text node
25
+ else
26
+ parse_normal_node node
27
+ end
21
28
  end
22
29
 
23
30
  def parse_text(node)
@@ -46,7 +53,7 @@ module Victor
46
53
  end
47
54
 
48
55
  def xml_doc
49
- Nokogiri::XML(raw_svg)
56
+ Nokogiri::XML raw_svg
50
57
  end
51
58
 
52
59
  def svg_root
@@ -0,0 +1,33 @@
1
+ module Victor
2
+ module CLI
3
+ class RubyCode
4
+ include Victor::DSL
5
+ attr_reader :code
6
+
7
+ def initialize(code)
8
+ @code = code
9
+ end
10
+
11
+ def evaluate
12
+ instance_eval code
13
+ end
14
+
15
+ def template(template)
16
+ if built_in_templates.include? template
17
+ template = template.to_sym
18
+ elsif !File.exist? template
19
+ raise "Template not found #{template}\nAvailable templates: #{built_in_templates.join ', '}, or a file path"
20
+ end
21
+
22
+ svg.template = template
23
+ end
24
+
25
+ private
26
+
27
+ def built_in_templates
28
+ %w[default minimal html]
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ # Render this template by running 'victor to-svg FILE'
2
+
3
+ setup %{attributes}
4
+
5
+ build do
6
+ %{nodes}
7
+ end
8
+
@@ -0,0 +1,12 @@
1
+ # Render this template by running 'ruby FILE' or 'victor to-svg FILE'
2
+
3
+ require 'victor/script'
4
+
5
+ setup %{attributes}
6
+
7
+ build do
8
+ %{nodes}
9
+ end
10
+
11
+ puts render
12
+ save 'output'
@@ -0,0 +1,10 @@
1
+ # Render this template by running 'ruby FILE' or 'victor to-svg FILE'
2
+
3
+ require "victor"
4
+
5
+ svg = Victor::SVG.new %{attributes}
6
+ svg.build do
7
+ %{nodes}
8
+ end
9
+
10
+ svg.save "generated"
@@ -1,5 +1,5 @@
1
1
  module Victor
2
2
  module CLI
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-28 00:00:00.000000000 Z
12
+ date: 2020-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mister_bin
@@ -45,14 +45,14 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.2'
48
+ version: '0.3'
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.2'
55
+ version: '0.3'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: nokogiri
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0.12'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pretty_trace
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '0.2'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '0.2'
84
98
  description: CLI for Victor, the SVG Library
85
99
  email: db@dannyben.com
86
100
  executables:
@@ -94,8 +108,13 @@ files:
94
108
  - lib/victor/cli/code_generator.rb
95
109
  - lib/victor/cli/command_line.rb
96
110
  - lib/victor/cli/commands/base.rb
97
- - lib/victor/cli/commands/generate.rb
111
+ - lib/victor/cli/commands/generate_ruby.rb
112
+ - lib/victor/cli/commands/generate_svg.rb
98
113
  - 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
99
118
  - lib/victor/cli/version.rb
100
119
  homepage: https://github.com/dannyben/victor-cli
101
120
  licenses:
@@ -1,30 +0,0 @@
1
- module Victor
2
- module CLI
3
- module Commands
4
- class Generate < Base
5
- summary "Generate Ruby code from SVG"
6
-
7
- usage "victor generate SVG_FILE [RUBY_FILE]"
8
- usage "victor generate (-h|--help)"
9
-
10
- param "SVG_FILE", "Input SVG file"
11
- param "RUBY_FILE", "Output Ruby file. Leave empty to write to stdout"
12
-
13
- example "victor generate example.svg example.rb"
14
-
15
- def run
16
- svg_file = File.read(args["SVG_FILE"])
17
- svg_tree = Parser.new(svg_file).parse
18
- code = CodeGenerator.new(svg_tree).generate
19
- ruby_file = args["RUBY_FILE"]
20
-
21
- if ruby_file
22
- File.write ruby_file, code
23
- else
24
- puts code
25
- end
26
- end
27
- end
28
- end
29
- end
30
- end