qrcode-generator 0.0.4 → 0.0.5

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
  SHA1:
3
- metadata.gz: ae066987e5e305e5081c978834e60c3b6ef08c7e
4
- data.tar.gz: 48cd990f0cbae5103b0e9b29d689e1386abfcd91
3
+ metadata.gz: 1411dca3b267e8fbac4620ff52cec07538e5f87b
4
+ data.tar.gz: ff9eb98b3f4d59c40526a23d98b36aa7b45ebcb1
5
5
  SHA512:
6
- metadata.gz: f587ee371329b4f27770a119e5ea1ec99d29d0f33ef8fe9e54702c177debfb34ca3847801aa346fac812fde1c04631fa6b6224e4be7ae84a71861bd671693c68
7
- data.tar.gz: bcfc86f029e4f6e7d7281344ce39a18f0af5d15dae085505c7ace840fdebd1d03837c392f475602376ee1314bd082e44fbf623b5cee5c3bd7234a0829350efec
6
+ metadata.gz: 0a8d87be9bf4d107080d96e57b198b2a05702a716d425a07f0c4fd5a54eb707f3b8024827880fcf2896fe9f92f3642560c1f566ebda1c7cea830a56305ea78d3
7
+ data.tar.gz: 361b0e7451638d8b4b3fd25a1efb5ab749089d27607b33c46c00fbafade7aebb8d899cc121c4ffc3abaf7d3171a8d76077141b33f17dd5428afa51412039aa26
@@ -1,73 +1,77 @@
1
- #!/usr/bin/env ruby
2
- $:.unshift File.expand_path('../lib', __dir__)
3
-
4
- require 'qrcode-generator'
5
-
6
- require 'optparse'
7
-
8
- ARGV << '--help' if ARGV.empty?
9
-
10
- options = {}
11
- OptionParser.new do |opts|
12
- opts.banner = %Q{
13
- usage: qrcode-generator <command> [options]
14
-
15
- the commands are:
16
- init \tinit workspace in current directory
17
- run \tstart generator qrcodes
18
-
19
- the options are:
20
- }
21
-
22
- options[:width] = 400
23
- options[:size] = 4
24
- options[:border] = 1
25
- options[:level] = :h
26
- options[:logo] = true
27
- options[:logo_denominator] = 3
28
- options[:tag] = nil
29
-
30
- opts.on("--width=400", "chnage qrcode image width") do |n|
31
- width = n.to_i
32
- options[:width] = width if width > 0
33
- end
34
-
35
- opts.on("--size=4", "change qrcode size") do |n|
36
- size = n.to_i
37
- options[:size] = size if size > 0
38
- end
39
-
40
- opts.on("--border=1", "change qrcode border width") do |n|
41
- border = n.to_i
42
- options[:border] = border if border >= 0
43
- end
44
-
45
- opts.on("--level=h", "change qrcode level, support: l,m,q,h") do |n|
46
- options[:level] = n.to_sym if ["l", "m", "q", "h"].include?(n)
47
- end
48
-
49
- opts.on("--logo=true", "if you don't want logo, set false") do |n|
50
- options[:logo] = false if n=="false"
51
- end
52
-
53
- opts.on("--logo_denominator=3", "if set 3, then the logo's width will equal to 1/3 of whole image width") do |n|
54
- logo_denominator = n.to_i
55
- options[:logo_denominator] = logo_denominator if logo_denominator > 0
56
- end
57
-
58
- opts.on("--tag=someString", "if set 'someString', will add '(someString)' to the end of filename") do |n|
59
- tag = n.to_s.strip
60
- options[:tag] = tag if !tag.empty?
61
- end
62
-
63
- end.parse!
64
-
65
- command = ARGV[0]
66
- case command
67
- when "init"
68
- QrcodeGenerator::Initer.new(Dir.pwd).init
69
- when "run"
70
- QrcodeGenerator::Runner.new(Dir.pwd, options).run
71
- else
72
- puts "unknown command: #{command}"
73
- end
1
+ #!/usr/bin/env ruby
2
+
3
+ Encoding.default_internal='utf-8'
4
+ Encoding.default_external='utf-8'
5
+
6
+ $:.unshift File.expand_path('../lib', __dir__)
7
+
8
+ require 'qrcode-generator'
9
+
10
+ require 'optparse'
11
+
12
+ ARGV << '--help' if ARGV.empty?
13
+
14
+ options = {}
15
+ OptionParser.new do |opts|
16
+ opts.banner = %Q{
17
+ usage: qrcode-generator <command> [options]
18
+
19
+ the commands are:
20
+ init \tinit workspace in current directory
21
+ run \tstart generator qrcodes
22
+
23
+ the options are:
24
+ }
25
+
26
+ options[:width] = 400
27
+ options[:size] = 4
28
+ options[:border] = 1
29
+ options[:level] = :h
30
+ options[:logo] = true
31
+ options[:logo_denominator] = 3
32
+ options[:tag] = nil
33
+
34
+ opts.on("--width=400", "chnage qrcode image width") do |n|
35
+ width = n.to_i
36
+ options[:width] = width if width > 0
37
+ end
38
+
39
+ opts.on("--size=4", "change qrcode size") do |n|
40
+ size = n.to_i
41
+ options[:size] = size if size > 0
42
+ end
43
+
44
+ opts.on("--border=1", "change qrcode border width") do |n|
45
+ border = n.to_i
46
+ options[:border] = border if border >= 0
47
+ end
48
+
49
+ opts.on("--level=h", "change qrcode level, support: l,m,q,h") do |n|
50
+ options[:level] = n.to_sym if ["l", "m", "q", "h"].include?(n)
51
+ end
52
+
53
+ opts.on("--logo=true", "if you don't want logo, set false") do |n|
54
+ options[:logo] = false if n=="false"
55
+ end
56
+
57
+ opts.on("--logo_denominator=3", "if set 3, then the logo's width will equal to 1/3 of whole image width") do |n|
58
+ logo_denominator = n.to_i
59
+ options[:logo_denominator] = logo_denominator if logo_denominator > 0
60
+ end
61
+
62
+ opts.on("--tag=someString", "if set 'someString', will add '(someString)' to the end of filename") do |n|
63
+ tag = n.to_s.strip
64
+ options[:tag] = tag if !tag.empty?
65
+ end
66
+
67
+ end.parse!
68
+
69
+ command = ARGV[0]
70
+ case command
71
+ when "init"
72
+ QrcodeGenerator::Initer.new(Dir.pwd).init
73
+ when "run"
74
+ QrcodeGenerator::Runner.new(Dir.pwd, options).run
75
+ else
76
+ puts "unknown command: #{command}"
77
+ end
@@ -1,4 +1,4 @@
1
-
2
- require "qrcode-generator/version"
3
- require "qrcode-generator/initer"
1
+
2
+ require "qrcode-generator/version"
3
+ require "qrcode-generator/initer"
4
4
  require "qrcode-generator/runner"
@@ -1,44 +1,44 @@
1
-
2
- require 'find'
3
- require 'fileutils'
4
-
5
- module QrcodeGenerator
6
-
7
- class Initer
8
-
9
- attr_reader :workspace
10
-
11
- def initialize(workspace)
12
- @workspace = workspace
13
- end
14
-
15
- def init
16
- if !workspace_empty?
17
- puts "ERROR: Count not init, because current folder is not empty"
18
- return
19
- end
20
-
21
- init_workspace
22
- end
23
-
24
- def workspace_empty?
25
- total_size = 0
26
-
27
- Find.find(workspace) do |path|
28
- next if path == workspace or File.basename(path)[0] == ?.
29
- total_size += 1
30
- end
31
-
32
- total_size == 0
33
- end
34
-
35
- #copy files into workspace
36
- def init_workspace
37
- source = File.expand_path("../workspace", __dir__)
38
-
39
- FileUtils.cp_r("#{source}/.", workspace)
40
- end
41
-
42
- end
43
-
1
+
2
+ require 'find'
3
+ require 'fileutils'
4
+
5
+ module QrcodeGenerator
6
+
7
+ class Initer
8
+
9
+ attr_reader :workspace
10
+
11
+ def initialize(workspace)
12
+ @workspace = workspace
13
+ end
14
+
15
+ def init
16
+ if !workspace_empty?
17
+ puts "ERROR: Count not init, because current folder is not empty"
18
+ return
19
+ end
20
+
21
+ init_workspace
22
+ end
23
+
24
+ def workspace_empty?
25
+ total_size = 0
26
+
27
+ Find.find(workspace) do |path|
28
+ next if path == workspace or File.basename(path)[0] == ?.
29
+ total_size += 1
30
+ end
31
+
32
+ total_size == 0
33
+ end
34
+
35
+ #copy files into workspace
36
+ def init_workspace
37
+ source = File.expand_path("../workspace", __dir__)
38
+
39
+ FileUtils.cp_r("#{source}/.", workspace)
40
+ end
41
+
42
+ end
43
+
44
44
  end
@@ -1,131 +1,131 @@
1
-
2
- require 'fileutils'
3
- require 'rmagick'
4
- require 'rqrcode'
5
-
6
- module QrcodeGenerator
7
-
8
- class Runner
9
-
10
- attr_reader :workspace, :options, :destSpace
11
-
12
- def initialize(workspace, options)
13
- @workspace = workspace
14
- @options = options
15
-
16
- time = Time.now
17
-
18
- dayStr = time.strftime('%m-%d')
19
- clockStr = time.strftime('%H.%M.%S')
20
- @destSpace = File.join(workspace, "qrcode #{dayStr} #{clockStr}")
21
- end
22
-
23
- def run
24
- txtPath = File.join(workspace, "links.txt")
25
- if !File.file?(txtPath)
26
- puts "ERROR: Could not find file 'links.txt' in current directory"
27
- return
28
- end
29
-
30
- FileUtils.mkdir(destSpace) if !File.directory?(destSpace)
31
-
32
- filename_tag = parse_filename_tag
33
-
34
- File.readlines(txtPath).each do |line|
35
- line = line.strip.chomp
36
- next if line.empty?
37
-
38
- line = line.gsub("\t", " ") #tab to space
39
-
40
- if reverse_point = line.reverse.index(/[ ,]/)
41
- point = line.length - reverse_point
42
- link = line[point..-1]
43
- title = line[0, point].strip.chomp(",").strip
44
- title = link if title.empty?
45
- else
46
- link = line
47
- title = link
48
- end
49
-
50
- filename = sanitize_filename(title) + filename_tag + ".png"
51
-
52
- to_qrcode_file(link, filename)
53
- end
54
- end
55
-
56
- def parse_filename_tag
57
- options[:tag] ? "(#{options[:tag]})" : ""
58
- end
59
-
60
- def sanitize_filename(basename)
61
-
62
- basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_')
63
- end
64
-
65
- def to_qrcode_file(url,filename)
66
-
67
- img = draw_qrcode_image(url, options[:width])
68
-
69
- img.format = "PNG"
70
-
71
- img.write(File.join(destSpace, filename))
72
- end
73
-
74
- def draw_qrcode_image(url, width)
75
- qr_width = 17 + 4 * options[:size] + 2 * options[:border]
76
- width = width < qr_width ? qr_width : width
77
- img = Magick::Image.new(qr_width,qr_width)
78
- build_qrcode(url).draw(img)
79
- img = img.resize(width,width,Magick::PointFilter)
80
-
81
- if options[:logo]
82
- logo_size = width/(options[:logo_denominator]*1.0)
83
- build_logo(img, logo_size)
84
- end
85
-
86
- img
87
- end
88
-
89
- def build_qrcode(url)
90
- qr = RQRCode::QRCode.new(url, size: options[:size], level: options[:level])
91
- border = options[:border]
92
- qrcode = Magick::Draw.new
93
- qr.modules.each_index do |x|
94
- qr.modules.each_index do |y|
95
- qrcode.fill( qr.dark?(x,y) ? 'black' : 'white' )
96
- qrcode.point(y+border,x+border)
97
- end
98
- end
99
- qrcode
100
- end
101
-
102
- def build_logo(img,size)
103
- logo_filepath = File.join(workspace, "logo.png")
104
-
105
- if File.exist?(logo_filepath)
106
- logo = Magick::Image.read(logo_filepath)[0]
107
- logo = logo.resize(size,size)
108
-
109
- img.composite!(logo, Magick::CenterGravity, Magick::OverCompositeOp)
110
- end
111
-
112
- img
113
- end
114
-
115
- def to_qrcode_blob(url,width)
116
-
117
- img = draw_qrcode_image(url, width)
118
-
119
- blob = img.to_blob {
120
- self.format = 'PNG'
121
- }
122
- blob
123
- end
124
-
125
- def to_qrcode_base64(blob)
126
- "data:image/png;base64,#{Base64.encode64(blob)}"
127
- end
128
-
129
- end
130
-
1
+
2
+ require 'fileutils'
3
+ require 'rmagick'
4
+ require 'rqrcode'
5
+
6
+ module QrcodeGenerator
7
+
8
+ class Runner
9
+
10
+ attr_reader :workspace, :options, :destSpace
11
+
12
+ def initialize(workspace, options)
13
+ @workspace = workspace
14
+ @options = options
15
+
16
+ time = Time.now
17
+
18
+ dayStr = time.strftime('%m-%d')
19
+ clockStr = time.strftime('%H.%M.%S')
20
+ @destSpace = File.join(workspace, "qrcode #{dayStr} #{clockStr}")
21
+ end
22
+
23
+ def run
24
+ txtPath = File.join(workspace, "links.txt")
25
+ if !File.file?(txtPath)
26
+ puts "ERROR: Could not find file 'links.txt' in current directory"
27
+ return
28
+ end
29
+
30
+ FileUtils.mkdir(destSpace) if !File.directory?(destSpace)
31
+
32
+ filename_tag = parse_filename_tag
33
+
34
+ File.readlines(txtPath).each do |line|
35
+ line = line.strip.chomp
36
+ next if line.empty?
37
+
38
+ line = line.gsub("\t", " ") #tab to space
39
+
40
+ if reverse_point = line.reverse.index(/[ ,]/)
41
+ point = line.length - reverse_point
42
+ link = line[point..-1]
43
+ title = line[0, point].strip.chomp(",").strip
44
+ title = link if title.empty?
45
+ else
46
+ link = line
47
+ title = link
48
+ end
49
+
50
+ filename = sanitize_filename(title) + filename_tag + ".png"
51
+
52
+ to_qrcode_file(link, filename)
53
+ end
54
+ end
55
+
56
+ def parse_filename_tag
57
+ options[:tag] ? "(#{options[:tag]})" : ""
58
+ end
59
+
60
+ def sanitize_filename(basename)
61
+
62
+ basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_')
63
+ end
64
+
65
+ def to_qrcode_file(url,filename)
66
+
67
+ img = draw_qrcode_image(url, options[:width])
68
+
69
+ img.format = "PNG"
70
+
71
+ img.write(File.join(destSpace, filename))
72
+ end
73
+
74
+ def draw_qrcode_image(url, width)
75
+ qr_width = 17 + 4 * options[:size] + 2 * options[:border]
76
+ width = width < qr_width ? qr_width : width
77
+ img = Magick::Image.new(qr_width,qr_width)
78
+ build_qrcode(url).draw(img)
79
+ img = img.resize(width,width,Magick::PointFilter)
80
+
81
+ if options[:logo]
82
+ logo_size = width/(options[:logo_denominator]*1.0)
83
+ build_logo(img, logo_size)
84
+ end
85
+
86
+ img
87
+ end
88
+
89
+ def build_qrcode(url)
90
+ qr = RQRCode::QRCode.new(url, size: options[:size], level: options[:level])
91
+ border = options[:border]
92
+ qrcode = Magick::Draw.new
93
+ qr.modules.each_index do |x|
94
+ qr.modules.each_index do |y|
95
+ qrcode.fill( qr.dark?(x,y) ? 'black' : 'white' )
96
+ qrcode.point(y+border,x+border)
97
+ end
98
+ end
99
+ qrcode
100
+ end
101
+
102
+ def build_logo(img,size)
103
+ logo_filepath = File.join(workspace, "logo.png")
104
+
105
+ if File.exist?(logo_filepath)
106
+ logo = Magick::Image.read(logo_filepath)[0]
107
+ logo = logo.resize(size,size)
108
+
109
+ img.composite!(logo, Magick::CenterGravity, Magick::OverCompositeOp)
110
+ end
111
+
112
+ img
113
+ end
114
+
115
+ def to_qrcode_blob(url,width)
116
+
117
+ img = draw_qrcode_image(url, width)
118
+
119
+ blob = img.to_blob {
120
+ self.format = 'PNG'
121
+ }
122
+ blob
123
+ end
124
+
125
+ def to_qrcode_base64(blob)
126
+ "data:image/png;base64,#{Base64.encode64(blob)}"
127
+ end
128
+
129
+ end
130
+
131
131
  end
@@ -1,6 +1,6 @@
1
-
2
- module QrcodeGenerator
3
-
4
- Version = "0.0.4"
5
-
1
+
2
+ module QrcodeGenerator
3
+
4
+ Version = "0.0.5"
5
+
6
6
  end
@@ -1,20 +1,20 @@
1
-
2
- http://baidu.com
3
-
4
- a1.这一行是自定义文件名,http://weibo.com
5
-
6
- a2.上一行用英文逗号分隔,也可以用空格分隔 http://tmall.com
7
-
8
- a3.一行一个, 空行会被忽略 http://zhihu.com
9
-
10
- a4.多余的空格不影响识别 http://z.cn
11
-
12
- 最后一个值 会解析成 链接,前面 的值 会合并成 文件名 http://taobao.com
13
-
14
- ,, , , , 测试兼容性, , , , , , , , http://stackoverflow.com
15
-
16
- , http://renren.com
17
-
18
-
19
-
20
-
1
+
2
+ http://baidu.com
3
+
4
+ a1.这一行是自定义文件名,http://weibo.com
5
+
6
+ a2.上一行用英文逗号分隔,也可以用空格分隔 http://tmall.com
7
+
8
+ a3.一行一个, 空行会被忽略 http://zhihu.com
9
+
10
+ a4.多余的空格不影响识别 http://z.cn
11
+
12
+ 最后一个值 会解析成 链接,前面 的值 会合并成 文件名 http://taobao.com
13
+
14
+ ,, , , , 测试兼容性, , , , , , , , http://stackoverflow.com
15
+
16
+ , http://renren.com
17
+
18
+
19
+
20
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qrcode-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeffrey