svgr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d224bf9a5fc726732a3fc41025e5e427b854e64af0e98b885dfa07463b8f3163
4
+ data.tar.gz: 50ab5833da2e78c564b801297d366807e28bd158029bc311753cf54efde76245
5
+ SHA512:
6
+ metadata.gz: 90ff675a77946193b6065b75346748f7b12fde67753ef4d516e8cfc152c20a198a04ac9cd54b07cf62f9dc86f1e0e8e3a70d033844d15bceed36884da57697bb
7
+ data.tar.gz: cda5463648daf778a6e1dc9e2f11bf2b993f3ea96c63fdfbff6c0ea013d0f953fc4a6e1642ee50c491e4cde42a5798c2487d9ff1d3632798552714b0c5156940
data/bin/svgr ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/svgr'
4
+
5
+ # The first command-line argument will be the subcommand (e.g., "combine")
6
+ subcommand = ARGV.shift
7
+
8
+ case subcommand
9
+ when 'combine'
10
+ Svgr::CombineSvgs.start(ARGV)
11
+ else
12
+ puts "Unknown subcommand: #{subcommand}"
13
+ puts "Usage: svgr combine <source_directory> <output_file> <rows> <columns> [options]"
14
+ exit(1)
15
+ end
@@ -0,0 +1,132 @@
1
+ require "nokogiri"
2
+ require "optparse"
3
+
4
+ module Svgr
5
+ class CombineSvgs
6
+ def self.start(argv)
7
+ options = {
8
+ scaling_factor: 1,
9
+ margin_top: 0,
10
+ margin_left: 0,
11
+ sort: "default"
12
+ }
13
+
14
+ OptionParser
15
+ .new do |opts|
16
+ opts.banner =
17
+ "Usage: svgr combine [options] <source_directory> <output_file> <rows> <columns>"
18
+
19
+ opts.on(
20
+ "-s",
21
+ "--scaling-factor FACTOR",
22
+ Float,
23
+ "Scaling factor for the SVG elements"
24
+ ) { |s| options[:scaling_factor] = s }
25
+
26
+ opts.on(
27
+ "-t",
28
+ "--margin-top MARGIN",
29
+ Integer,
30
+ "Top margin between the SVG elements"
31
+ ) { |t| options[:margin_top] = t }
32
+
33
+ opts.on(
34
+ "-l",
35
+ "--margin-left MARGIN",
36
+ Integer,
37
+ "Left margin between the SVG elements"
38
+ ) { |l| options[:margin_left] = l }
39
+
40
+ opts.on(
41
+ "--sort SORT",
42
+ %w[default random],
43
+ "Sorting option for the SVG files (default, random)"
44
+ ) { |sort| options[:sort] = sort }
45
+ end
46
+ .parse!(argv)
47
+
48
+ if argv.length < 4
49
+ puts "Usage: svgr combine [options] <source_directory> <output_file> <rows> <columns>"
50
+ exit(1)
51
+ end
52
+
53
+ source_directory, output_file, rows, columns = argv.shift(4)
54
+ rows = rows.to_i
55
+ columns = columns.to_i
56
+ svg_files =
57
+ list_svg_files(source_directory, options[:sort])[0...rows * columns]
58
+
59
+ combined_elements =
60
+ svg_files.flat_map do |file|
61
+ content = read_svg_file(file)
62
+ extract_svg_elements(content)
63
+ end
64
+
65
+ combined_svg =
66
+ create_combined_svg(
67
+ combined_elements,
68
+ rows,
69
+ columns,
70
+ options[:scaling_factor],
71
+ options[:margin_top],
72
+ options[:margin_left]
73
+ )
74
+
75
+ save_svg_to_file(combined_svg, output_file)
76
+ end
77
+
78
+ def self.list_svg_files(directory, sort_option)
79
+ svg_files = Dir.glob(File.join(directory, "*.svg"))
80
+
81
+ case sort_option
82
+ when "default"
83
+ svg_files.sort_by { |file| File.basename(file, ".svg").to_i }
84
+ when "random"
85
+ svg_files.shuffle
86
+ else
87
+ puts "Invalid sorting option. Allowed options: default, random"
88
+ exit(1)
89
+ end
90
+ end
91
+
92
+ def self.read_svg_file(file)
93
+ File.read(file)
94
+ end
95
+
96
+ def self.extract_svg_elements(svg_content)
97
+ doc = Nokogiri.XML(svg_content)
98
+ doc.remove_namespaces!
99
+ doc.xpath("//svg/g")
100
+ end
101
+
102
+ def self.create_combined_svg(
103
+ elements,
104
+ rows,
105
+ columns,
106
+ scaling_factor,
107
+ margin_top,
108
+ margin_left
109
+ )
110
+ combined_svg =
111
+ Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
112
+ xml.svg(xmlns: "http://www.w3.org/2000/svg") do
113
+ elements.each_with_index do |element, index|
114
+ row = index / columns
115
+ col = index % columns
116
+
117
+ # Adjust the 'transform' attribute to position and scale the element in the grid
118
+ transform =
119
+ "translate(#{col * (100 + margin_left) * scaling_factor}, #{row * (100 + margin_top) * scaling_factor}) scale(#{scaling_factor})"
120
+ element["transform"] = transform
121
+ xml << element.to_xml
122
+ end
123
+ end
124
+ end
125
+ combined_svg.to_xml
126
+ end
127
+
128
+ def self.save_svg_to_file(svg, file)
129
+ File.write(file, svg)
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,14 @@
1
+ def count_files_by_type(directory, file_extension)
2
+ Dir.glob(File.join(directory, "*.#{file_extension}")).count
3
+ end
4
+
5
+ if ARGV.length != 2
6
+ puts "Usage: ruby count_files_by_type.rb <source_directory> <file_extension>"
7
+ exit(1)
8
+ end
9
+
10
+ source_directory = ARGV[0]
11
+ file_extension = ARGV[1]
12
+ file_count = count_files_by_type(source_directory, file_extension)
13
+
14
+ puts "Number of #{file_extension.upcase} files in #{source_directory}: #{file_count}"
@@ -0,0 +1,32 @@
1
+ require "fileutils"
2
+
3
+ def get_files_by_type(directory, file_extension)
4
+ Dir
5
+ .glob(File.join(directory, "*.#{file_extension}"))
6
+ .sort_by { |f| File.basename(f).to_i }
7
+ end
8
+
9
+ def delete_file(file)
10
+ File.delete(file)
11
+ end
12
+
13
+ if ARGV.length != 2
14
+ puts "Usage: ruby delete_files_by_type.rb <source_directory> <file_extension>"
15
+ exit(1)
16
+ end
17
+
18
+ source_directory = ARGV[0]
19
+ file_extension = ARGV[1]
20
+
21
+ files = get_files_by_type(source_directory, file_extension)
22
+
23
+ files.each do |file|
24
+ puts "File: #{file}"
25
+ print "Do you want to delete this file? [y/N]: "
26
+ answer = STDIN.gets.chomp.downcase
27
+
28
+ if answer == "y"
29
+ delete_file(file)
30
+ puts "Deleted #{file}"
31
+ end
32
+ end
data/lib/svgr.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "svgr/combine_svgs"
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svgr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nassredean Nasseri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ description: A Ruby gem that provides a command-line interface for various SVG utilities
28
+ email:
29
+ executables:
30
+ - svgr
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/svgr
35
+ - lib/svgr.rb
36
+ - lib/svgr/combine_svgs.rb
37
+ - lib/svgr/count_files_by_type.rb
38
+ - lib/svgr/delete_files_by_type.rb
39
+ homepage: https://github.com/nassredean/svgr
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.4.6
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: A tool for working with SVG files
62
+ test_files: []