inch-badge 0.1.1 → 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
  SHA1:
3
- metadata.gz: 591654afe92662eedea503fc0417d0e70858ac02
4
- data.tar.gz: 4625c99d4d4f73b51b1a080deb98faedb66b435a
3
+ metadata.gz: 81c85a0aa45308466719b944224699b4b6e62326
4
+ data.tar.gz: e3409f537dc99bc9e0b1bc1b9b07cb3ab98b4aba
5
5
  SHA512:
6
- metadata.gz: 677451c6bbfe6c20dc8a1746439c3301532cd8203ab64d1524368d8a4492ddd16b3199209857849d537b75aa351e94397d9230d0bc9d96f2372f83cecc44ce1f
7
- data.tar.gz: 973dd06a0924c0713b76db26ecaa400c801a0f2994540157f3512e4eb5ab77de8bf72fa9243c2582813a98897e448af0a6cb89cc6bb46961c66e2e4668fbc3a3
6
+ metadata.gz: c94399d410cce68b0d6f762702cfc29d35c735fbdf50fef7a6a24947732c1e6cdb25ba429b797c6f47251c1f9393da630ff96b9ea9891d617118d36ecc6d3923
7
+ data.tar.gz: 1c119e4f073b4c4a7124e2b2c6935cda722e4f7f841b35dca60cc9572590fed27a91f4a556bdec604ea7df0ce87d79c2b86080338f2c78764b9f241db4912910
@@ -0,0 +1,18 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="77" height="18">
2
+ <linearGradient id="a" x2="0" y2="100%">
3
+ <stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-opacity=".3"/><stop offset="1" stop-opacity=".5"/>
4
+ </linearGradient>
5
+ <rect rx="4" width="77" height="18" fill="#555"/>
6
+
7
+ <% x = 0 %>
8
+ <% @grades.each do |grade| %>
9
+ <rect x="<%= 34+x %>" y="4" width="<%= grade.width %>" height="10" fill="<%= grade.color %>"/>
10
+ <% x += grade.width %>
11
+ <% end %>
12
+
13
+ <rect rx="4" width="77" height="18" fill="url(#a)"/>
14
+ <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
15
+ <text x="18.5" y="13" fill="#010101" fill-opacity=".3">docs</text>
16
+ <text x="18.5" y="12">docs</text>
17
+ </g>
18
+ </svg>
data/bin/inch-badge CHANGED
@@ -19,4 +19,4 @@ if ARGV.size != 5
19
19
  exit 1
20
20
  end
21
21
 
22
- Inch::Badge::Image.new(ARGV.shift, ARGV).save
22
+ Inch::Badge::Image.create(ARGV.shift, ARGV)
@@ -2,6 +2,7 @@ module Inch
2
2
  module Badge
3
3
  class Config
4
4
  GRADE_ORDER = %w(A B C U)
5
+ GRADE_COLOR = %w(#46b01e #87ae10 #c05946 #8f7eb4)
5
6
  SECTION_WIDTH = 40 # px
6
7
 
7
8
  class << self
@@ -0,0 +1,24 @@
1
+ module Inch
2
+ module Badge
3
+ class GradeSection < Struct.new(:name, :color, :size, :overall)
4
+ def prefix(x)
5
+ if x == 0
6
+ "begin"
7
+ elsif x == section_width - 1
8
+ "end"
9
+ else
10
+ "body"
11
+ end
12
+ end
13
+
14
+ def width
15
+ return 0 if overall == 0
16
+ ((size / overall.to_f) * section_width).round
17
+ end
18
+
19
+ def section_width
20
+ Config::SECTION_WIDTH
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ module Inch
2
+ module Badge
3
+ module Image
4
+ class Base
5
+ def initialize(filename, numbers)
6
+ @filename = filename
7
+ @numbers = numbers.map(&:to_i)
8
+ @overall = @numbers.inject(0, :+)
9
+ @grades = Config::GRADE_ORDER.map.with_index do |name, index|
10
+ GradeSection.new(name, Config::GRADE_COLOR[index], @numbers[index], @overall)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,34 @@
1
+ require 'chunky_png'
2
+
3
+ module Inch
4
+ module Badge
5
+ module Image
6
+ class PNG < Base
7
+ def save
8
+ base_image = ChunkyPNG::Image.from_file( Config.image_path('bg.png') )
9
+
10
+ x = 0
11
+ @grades.each do |grade|
12
+ grade.width.times do |i|
13
+ if x < grade.section_width
14
+ badge = load_image( Config.image_path("grade-#{grade.name}-#{grade.prefix(x)}.png") )
15
+ base_image.compose!(badge, 34+x, 0)
16
+ end
17
+ x += 1
18
+ end
19
+ end
20
+
21
+ base_image.save(@filename, :fast_rgba)
22
+ end
23
+
24
+ private
25
+
26
+ def load_image(filename)
27
+ @cache ||= {}
28
+ @cache[filename] ||= ChunkyPNG::Image.from_file(filename)
29
+ @cache[filename]
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ require 'erb'
2
+
3
+ module Inch
4
+ module Badge
5
+ module Image
6
+ class SVG < Base
7
+ def save
8
+ template_content = File.read( Config.image_path('badge.svg.erb') )
9
+ renderer = ERB.new(template_content)
10
+ output = renderer.result(binding)
11
+
12
+ File.open(@filename, 'w') {|f| f.write(output) }
13
+ return
14
+
15
+ x = 0
16
+ @grades.each do |grade|
17
+ grade.width.times do |i|
18
+ if x < grade.section_width
19
+ badge = load_image( Config.image_path("grade-#{grade.name}-#{grade.prefix(x)}.png") )
20
+ base_image.compose!(badge, 34+x, 0)
21
+ end
22
+ x += 1
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ private
29
+
30
+ def load_image(filename)
31
+ @cache ||= {}
32
+ @cache[filename] ||= ChunkyPNG::Image.from_file(filename)
33
+ @cache[filename]
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,63 +1,20 @@
1
- require 'chunky_png'
2
1
 
3
2
  module Inch
4
3
  module Badge
5
- class GradeSection < Struct.new(:name, :size, :overall)
6
- def prefix(x)
7
- if x == 0
8
- "begin"
9
- elsif x == section_width - 1
10
- "end"
4
+ module Image
5
+ def self.create(filename, numbers)
6
+ if filename =~ /\.png$/
7
+ PNG.new(filename, numbers).save
8
+ elsif filename =~ /\.svg$/
9
+ SVG.new(filename, numbers).save
11
10
  else
12
- "body"
11
+ raise "Unknown data format: #{filename}"
13
12
  end
14
13
  end
15
-
16
- def width
17
- return 0 if overall == 0
18
- ((size / overall.to_f) * section_width).round
19
- end
20
-
21
- def section_width
22
- Config::SECTION_WIDTH
23
- end
24
- end
25
-
26
- class Image
27
- def initialize(filename, numbers)
28
- @filename = filename
29
- @numbers = numbers.map(&:to_i)
30
- @overall = @numbers.inject(0, :+)
31
- end
32
-
33
- def save
34
- @grades = Config::GRADE_ORDER.map.with_index do |name, index|
35
- GradeSection.new(name, @numbers[index], @overall)
36
- end
37
-
38
- base_image = ChunkyPNG::Image.from_file( Config.image_path('bg.png') )
39
-
40
- x = 0
41
- @grades.each do |grade|
42
- grade.width.times do |i|
43
- if x < grade.section_width
44
- badge = load_image( Config.image_path("grade-#{grade.name}-#{grade.prefix(x)}.png") )
45
- base_image.compose!(badge, 34+x, 0)
46
- end
47
- x += 1
48
- end
49
- end
50
-
51
- base_image.save(@filename, :fast_rgba)
52
- end
53
-
54
- private
55
-
56
- def load_image(filename)
57
- @cache ||= {}
58
- @cache[filename] ||= ChunkyPNG::Image.from_file(filename)
59
- @cache[filename]
60
- end
61
14
  end
62
15
  end
63
- end
16
+ end
17
+
18
+ require_relative 'image/base'
19
+ require_relative 'image/png'
20
+ require_relative 'image/svg'
@@ -1,5 +1,5 @@
1
1
  module Inch
2
2
  module Badge
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/lib/inch/badge.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'inch/badge/config'
2
+ require 'inch/badge/grade_section'
2
3
  require 'inch/badge/image'
3
4
  require 'inch/badge/version'
4
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inch-badge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - René Föhring
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-06 00:00:00.000000000 Z
11
+ date: 2014-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png
@@ -67,6 +67,7 @@ files:
67
67
  - LICENSE.txt
68
68
  - README.md
69
69
  - Rakefile
70
+ - assets/badge.svg.erb
70
71
  - assets/bg.png
71
72
  - assets/grade-A-begin.png
72
73
  - assets/grade-A-body.png
@@ -84,7 +85,11 @@ files:
84
85
  - inch-badge.gemspec
85
86
  - lib/inch/badge.rb
86
87
  - lib/inch/badge/config.rb
88
+ - lib/inch/badge/grade_section.rb
87
89
  - lib/inch/badge/image.rb
90
+ - lib/inch/badge/image/base.rb
91
+ - lib/inch/badge/image/png.rb
92
+ - lib/inch/badge/image/svg.rb
88
93
  - lib/inch/badge/version.rb
89
94
  homepage: ''
90
95
  licenses: