victor-cli 0.0.1 → 0.1.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: faa6c7f45d58ac8e15c5b5fbcd41aa10f650e53fea9afd71f3bb3234aca33e48
4
- data.tar.gz: 58b0c74ca23ad4d8790ecc0445dc85cb22b66432905299fef5e248c787126657
3
+ metadata.gz: 126abee42ab8abfde3a3f12704e20c5bb59175bf7e8789acee1423728c733c17
4
+ data.tar.gz: 5799eb27256c286e10bc8f710fcf08d1d6a4d7726553e99765a843839b8e3f06
5
5
  SHA512:
6
- metadata.gz: 5a61444e5bc7555fd71741bbacf436e5a16be5081323c6d702e8bb5f60f8e4929adb434858e7afd584e6748c936168150c87ad4617ddc53e7a8f5a575cffd4d4
7
- data.tar.gz: b962dbe7c7b6d6678eba7683650b8423d814d280f1940acef90a80038593fb15318280c6487bd0882290f36126dd8e108a32506219c1a52a66e3de4e3c3088db
6
+ metadata.gz: dd97b8db3ac7653addec886f4f4a079cf8a2edff1fe037ba4e01e7b80542032d983a4281441febcaf73fd848a336b0a76bfefdfc131a2b0d7b80df9483c63612
7
+ data.tar.gz: d1ccb5205691afe44679219e018dfd4f44873fc01536197ef9ad1fc1515e1967c6aecdce162f6a941f8685e3f7f20f60be1f9cf13a226e2a1bb7e58355082098
data/README.md CHANGED
@@ -1,25 +1,46 @@
1
- Victor
2
- ==================================================
1
+ # Victor CLI
3
2
 
4
3
  [![Gem Version](https://badge.fury.io/rb/victor-cli.svg)](https://badge.fury.io/rb/victor-cli)
5
- [![Build Status](https://travis-ci.com/DannyBen/victor-cli.svg?branch=master)](https://travis-ci.com/DannyBen/victor-cli)
6
- [![Maintainability](https://api.codeclimate.com/v1/badges/.../maintainability)](https://codeclimate.com/github/DannyBen/victor-cli/maintainability)
7
- [![Test Coverage](https://api.codeclimate.com/v1/badges/.../test_coverage)](https://codeclimate.com/github/DannyBen/victor-cli/test_coverage)
4
+ [![Build Status](https://github.com/DannyBen/victor-cli/workflows/Test/badge.svg)](https://github.com/DannyBen/victor-cli/actions?query=workflow%3ATest)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/ab7a46b42d268e374ee1/maintainability)](https://codeclimate.com/github/DannyBen/victor-cli/maintainability)
8
6
 
9
7
  ---
10
8
 
11
- CLI for Victor, the SVG Library
9
+ Command line interface for [Victor][victor], the SVG Library.
12
10
 
13
11
  ---
14
12
 
15
- Installation
16
- --------------------------------------------------
13
+ ## Installation
17
14
 
18
15
  $ gem install victor-cli
19
16
 
20
17
 
18
+ ## Usage
21
19
 
22
- Usage
23
- --------------------------------------------------
20
+ ```
21
+ $ victor generate --help
22
+ Generate Ruby code from SVG
24
23
 
25
- TODO
24
+ Usage:
25
+ victor generate SVG_FILE [RUBY_FILE]
26
+ victor generate (-h|--help)
27
+
28
+ Options:
29
+ -h --help
30
+ Show this help
31
+
32
+ Parameters:
33
+ SVG_FILE
34
+ Input SVG file
35
+
36
+ RUBY_FILE
37
+ Output Ruby file. Leave empty to write to stdout
38
+
39
+ Examples:
40
+ victor generate example.svg example.rb
41
+
42
+ ```
43
+
44
+ ---
45
+
46
+ [victor]: https://github.com/DannyBen/victor
data/bin/victor CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require 'victor-cli'
2
+ require 'victor/cli'
3
3
  include Colsole
4
4
 
5
5
  router = Victor::CLI::CommandLine.router
@@ -0,0 +1,64 @@
1
+ require "rufo"
2
+
3
+ module Victor
4
+ module CLI
5
+ class CodeGenerator
6
+ def initialize(svg_tree)
7
+ @svg_tree = svg_tree
8
+ end
9
+
10
+ def generate
11
+ Rufo::Formatter.format(code_for_node(svg_tree))
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :svg_tree
17
+
18
+ def code_for_node(node)
19
+ case node.first
20
+ when "svg"
21
+ root_to_ruby(node)
22
+ else
23
+ node_to_ruby(node)
24
+ end
25
+ end
26
+
27
+ def node_to_ruby(node)
28
+ name, attrs, children = node
29
+ code = "#{name} #{attrs_to_ruby(attrs)} "
30
+ unless children.empty?
31
+ code << " do\n"
32
+ code << nodes_to_ruby(children)
33
+ code << "\nend\n"
34
+ end
35
+ code
36
+ end
37
+
38
+ def nodes_to_ruby(nodes)
39
+ nodes.map(&method(:code_for_node)).join("\n")
40
+ end
41
+
42
+ def attrs_to_ruby(attrs)
43
+ attrs.reduce([]) do |acc, (k, v)|
44
+ acc << "#{k.to_sym.inspect[1..-1]}: #{v.inspect}"
45
+ acc
46
+ end.join(",")
47
+ end
48
+
49
+ def root_to_ruby(node)
50
+ _, attrs, children = node
51
+ <<~RUBY
52
+ require "victor"
53
+
54
+ svg = Victor::SVG.new #{attrs_to_ruby(attrs)}
55
+ svg.build do
56
+ #{nodes_to_ruby(children)}
57
+ end
58
+
59
+ svg.save "generated"
60
+ RUBY
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,5 +1,6 @@
1
- require 'victor-cli/commands/base'
2
- require 'victor-cli/commands/generate'
1
+ require 'mister_bin'
2
+ require_relative 'commands/base'
3
+ require_relative 'commands/generate'
3
4
 
4
5
  module Victor
5
6
  module CLI
@@ -8,7 +9,7 @@ module Victor
8
9
  router = MisterBin::Runner.new version: VERSION,
9
10
  header: "Victor SVG Utilities"
10
11
 
11
- router.route 'generate', to: Commands::Generate
12
+ router.route 'generate', to: Commands::Generate
12
13
 
13
14
  router
14
15
  end
File without changes
@@ -0,0 +1,30 @@
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
@@ -0,0 +1,57 @@
1
+ require "nokogiri"
2
+
3
+ module Victor
4
+ module CLI
5
+ class Parser
6
+ def initialize(raw_svg)
7
+ @raw_svg = raw_svg
8
+ end
9
+
10
+ def parse
11
+ parse_node(svg_root)
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :raw_svg
17
+
18
+ def parse_node(node)
19
+ return parse_text(node) if node.is_a?(Nokogiri::XML::Text)
20
+ parse_normal_node(node)
21
+ end
22
+
23
+ def parse_text(node)
24
+ inner_text = node.text
25
+ cleaned_text = inner_text.strip
26
+ return nil unless cleaned_text.length > 0
27
+ return ["_", {}, cleaned_text]
28
+ end
29
+
30
+ def parse_normal_node(node)
31
+ [
32
+ node.name,
33
+ node_attrs(node),
34
+ node.children.map(&method(:parse_node)).compact,
35
+ ]
36
+ end
37
+
38
+ def node_attrs(node)
39
+ node.attributes.values.reduce({}) do |acc, attr|
40
+ name = attr.name
41
+ value = attr.value
42
+ key = attr.respond_to?(:prefix) ? "#{attr.prefix}:#{name}" : name
43
+ acc[key] = value
44
+ acc
45
+ end
46
+ end
47
+
48
+ def xml_doc
49
+ Nokogiri::XML(raw_svg)
50
+ end
51
+
52
+ def svg_root
53
+ xml_doc.children.last
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,5 +1,5 @@
1
1
  module Victor
2
2
  module CLI
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
data/lib/victor/cli.rb ADDED
@@ -0,0 +1,5 @@
1
+ require_relative 'cli/parser'
2
+ require_relative 'cli/code_generator'
3
+ require_relative 'cli/command_line'
4
+
5
+ require 'byebug' if ENV['BYEBUG']
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: victor-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
+ - Max Brosnahan
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2020-04-26 00:00:00.000000000 Z
12
+ date: 2020-04-28 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: mister_bin
@@ -52,6 +53,34 @@ dependencies:
52
53
  - - "~>"
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0.2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: nokogiri
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.10'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.10'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rufo
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.12'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.12'
55
84
  description: CLI for Victor, the SVG Library
56
85
  email: db@dannyben.com
57
86
  executables:
@@ -61,12 +90,13 @@ extra_rdoc_files: []
61
90
  files:
62
91
  - README.md
63
92
  - bin/victor
64
- - lib/victor-cli.rb
65
- - lib/victor-cli/command_line.rb
66
- - lib/victor-cli/commands/base.rb
67
- - lib/victor-cli/commands/generate.rb
68
- - lib/victor-cli/parser.rb
69
- - lib/victor-cli/version.rb
93
+ - lib/victor/cli.rb
94
+ - lib/victor/cli/code_generator.rb
95
+ - lib/victor/cli/command_line.rb
96
+ - lib/victor/cli/commands/base.rb
97
+ - lib/victor/cli/commands/generate.rb
98
+ - lib/victor/cli/parser.rb
99
+ - lib/victor/cli/version.rb
70
100
  homepage: https://github.com/dannyben/victor-cli
71
101
  licenses:
72
102
  - MIT
@@ -1,21 +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"
12
-
13
- example "victor generate example.svg example.rb"
14
-
15
- def run
16
- p args
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,6 +0,0 @@
1
- module Victor
2
- module CLI
3
- class Parser
4
- end
5
- end
6
- end
data/lib/victor-cli.rb DELETED
@@ -1,5 +0,0 @@
1
- require 'mister_bin'
2
- require 'victor-cli/parser'
3
- require 'victor-cli/command_line'
4
-
5
- require 'byebug' if ENV['BYEBUG']