Module: BadgesToSVG
- Defined in:
- lib/badges2svg.rb
Overview
This module’s main method is BadgesToSVG.replace. It’s used to parse a string and replace links to PNG badge images with resolution-independent (SVG) ones. This is used by a command-line tool called badges2svg
.
Constant Summary collapse
- RULES =
Rules for PNG to SVG replacements. This array is intentionally ordered, because some rules might match the same pattern, so the greedier ones should go first. A rule is a hash with the following keys:
-
:name
: the rule’s name. This must be unique. -
:pattern
the PNG URL pattern. See compile_pattern for a pattern overview. -
:string
the URL replacement. The protocol shouldn’t be specified. If you wish to use theshields.io
start your replacement with a slash (/
). -
:domain
(optional): if you specified a custom domain in the:string
key, set this one totrue
to tellBadgesToSVG
not to prepend the default domain.
-
[ { :name => :travis_branch, :pattern => 'https?://(?:secure.)?travis-ci.org/%{user}/%{repo}.png' + '\\?branch=%{branch}', :string => 'travis-ci.org/%{user}/%{repo}.svg?branch=%{branch}', :domain => true }, { :name => :travis, :pattern => 'https?://(?:secure.)?travis-ci.org/%{user}/%{repo}.png', :string => 'travis-ci.org/%{user}/%{repo}.svg', :domain => true }, { :name => :gratipay, :pattern => 'https?://img.shields.io/gittip/%{user}.(?:png|svg)', :string => '/gratipay/user/%{user}.svg' }, { :name => :coveralls_branch, :pattern => 'https?://coveralls.io/r(epos)?/%{user}/%{repo}/badge.png' + '\\?branch=%{branch}', :string => '/coveralls/%{user}/%{repo}/%{branch}.svg' }, { :name => :coveralls, :pattern => 'https?://coveralls.io/r(epos)?/%{user}/%{repo}/badge.png', :string => '/coveralls/%{user}/%{repo}.svg' }, { :name => :gemnasium, :pattern => 'https?://gemnasium.com/%{user}/%{repo}.png', :string => 'gemnasium.com/%{user}/%{repo}.svg', :domain => true }, { :name => :code_climate, :pattern => 'https://codeclimate.com/github/%{user}/%{repo}.png', :string => '/codeclimate/github/%{user}/%{repo}.svg' }, { :name => :gem_version, :pattern => 'https?://badge.fury.io/rb/%{repo}(?:@2x)?.png', :string => '/gem/v/%{repo}.svg' }, { :name => :npm_version, :pattern => 'https?://badge.fury.io/js/%{repo}(?:@2x)?.png', :string => '/npm/v/%{repo}.svg' }, { :name => :pypi_version, :pattern => 'https?://badge.fury.io/py/%{repo}(?:@2x)?.png', :string => '/pypi/v/%{repo}.svg' }, { :name => :packagist_version, :pattern => 'https?://poser.pugx.org/%{user}/%{repo}/version.png', :string => '/packagist/v/%{user}/%{repo}.svg' }, { :name => :packagist_downloads, :pattern => 'https?://poser.pugx.org/%{user}/%{repo}/d/total.png', :string => '/packagist/dm/%{user}/%{repo}.svg' }, { :name => :pypi_downloads, :pattern => 'https?://pypip.in/d/%{repo}/badge.png', :string => '/pypi/dm/%{repo}.svg' }, { :name => :inch_ci, :pattern => 'https?://inch-ci.org/github/%{user}/%{repo}.png\\?branch=%{branch}', :string => 'inch-ci.org/github/%{user}/%{repo}.svg?branch=%{branch}', :domain => true, }, { :name => :misc_png, :pattern => 'https?://img.shields.io/%{path}.png', :string => '/%{path}.svg' }, ]
Instance Attribute Summary collapse
-
#domain ⇒ String
readonly
Default domain.
-
#protocol ⇒ String
readonly
Default protocol.
Class Method Summary collapse
-
.compile_pattern(pattern) ⇒ Regexp
Compile a pattern into a regular expression.
-
.replace(content, opts = {}) ⇒ String
Replace PNG image URLs with SVG ones when possible in a given string content.
-
.root_url(opts = {}) ⇒ Object
Create a root URL.
-
.version ⇒ String
Current gem version.
Instance Attribute Details
#domain ⇒ String (readonly)
Returns default domain.
10 11 12 |
# File 'lib/badges2svg.rb', line 10 def domain @domain end |
#protocol ⇒ String (readonly)
Returns default protocol.
7 8 9 |
# File 'lib/badges2svg.rb', line 7 def protocol @protocol end |
Class Method Details
.compile_pattern(pattern) ⇒ Regexp
Compile a pattern into a regular expression. Patterns are used as handy shortcuts to extract a part of an URL. A pattern written as %{foo} in a string is compiled into a Regexp
that matches an alphanumeric word into a group called foo.
134 135 136 137 138 139 |
# File 'lib/badges2svg.rb', line 134 def compile_pattern(pattern) # don't use .gsub! here or it’ll modify the variable itself, outside of # the function call pattern = pattern.gsub(/\./, '\\.') Regexp.new ("\\b#{pattern.gsub(/%\{(\w+)\}/, "(?<\\1>.+?)")}\\b") end |
.replace(content, opts = {}) ⇒ String
Replace PNG image URLs with SVG ones when possible in a given string content. This is meant to be used on a README or similar file.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/badges2svg.rb', line 149 def replace(content, opts={}) root = root_url(opts) RULES.each do |r| if r[:domain] rule_opts = {:protocol => r[:protocol], :domain => ''} myroot = root_url({}.update(opts).update(rule_opts)) else myroot = root end pat = compile_pattern(r[:pattern]) replacement = myroot + r[:string].gsub(/%\{(\w+)\}/, "\\\\k<\\1>") content.gsub!(pat, replacement) end content end |
.root_url(opts = {}) ⇒ Object
Create a root URL. If nothing is passed, it uses the default protocol and default domain
124 125 126 |
# File 'lib/badges2svg.rb', line 124 def root_url(opts={}) "#{opts[:protocol] || @protocol}://#{opts[:domain] || @domain}" end |
.version ⇒ String
Returns current gem version.
114 115 116 |
# File 'lib/badges2svg.rb', line 114 def version '0.1.4' end |