webify_ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e1ac9a0368f62f2da77e358e8d3408f55edd0f1
4
+ data.tar.gz: 93e629bea658ff3106955d14a6d964f24de191ee
5
+ SHA512:
6
+ metadata.gz: 0d6205202ec1e13660097aa0702d0c62b73ca5fa7be06937802fab7d8180202965ede42d81328ff294f4479d51dd9a06aed0406939f3a939c5b7fac687bbc4a2
7
+ data.tar.gz: 8e985e2b6699c961fbc011878d1f07a492b86a5603e493cc3ede1d62b565775b4f7abf3b0436b48ed2c28e16de4b515bb90b9a506b649c20032d4af193166ed3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 David Natsvlishvili
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,160 @@
1
+ = WebifyRuby - Haskell Webify bindings gem for Ruby and Rails Applications
2
+
3
+ Version 0.0.1 :: Lazi
4
+
5
+ == Usage
6
+
7
+ ==== Option #1: Install executables depending on your system
8
+
9
+ You can find compiled executables for Linux 32, Linux 64, Mac OS X 64, Win 32 here
10
+
11
+ https://github.com/dachi-gh/webify/tree/executables
12
+
13
+ ==== Option #2: Compile Haskell application for your desired OS from here
14
+
15
+ https://github.com/dachi-gh/webify
16
+
17
+ ==== Add to your Gemfile
18
+ gem 'webify_ruby', :git => 'git://github.com/dachi-gh/webify_ruby.git'
19
+
20
+ You can require a library to use in your Ruby application
21
+ require 'webify_ruby'
22
+
23
+ You don't have to do so if you're using rails as bundler can do it for you
24
+ Bundler.require(:default, Rails.env)
25
+
26
+ ==== Make a Rake task for a quick example
27
+
28
+ namespace :webify_ruby do
29
+ desc 'Convert a file and distribute'
30
+ task :convert, [:source, :dir_fonts, :dir_css] => :environment do |t, args|
31
+ pp WebifyRuby::Convert.new(
32
+ args.source,
33
+ dir: args.dir_fonts,
34
+ css: args.dir_css
35
+ )
36
+ end
37
+ end
38
+
39
+ and run it as
40
+ $ rake webify_ruby:convert['my_fonts/sample.ttf','demo/convert','demo/css']
41
+
42
+ If successful you should get find converted files under created directory demo/convert and stylesheet css file with relative links under demo/css directory
43
+
44
+ == Explore
45
+
46
+ ==== Convert and place in the same directory
47
+ Convert constructor requires one parameter at least, which is a ttf or otf file
48
+ WebifyRuby::Convert.new('fonts/font.ttf')
49
+
50
+ This should normally generate svg, woff and eot and place all four fonts in the same directory without css
51
+
52
+ ==== Convert and place in desired directory
53
+
54
+ Constructor can take a directory parameter
55
+ WebifyRuby::Convert.new('fonts/font.ttf', dir: 'webfonts')
56
+
57
+ This example creates webfonts directory if necessary and puts four webfont files there
58
+
59
+ ==== Generate stylesheets along the fonts
60
+
61
+ It is possible to pass a parameter to the class constructor, so it knows you want stylesheets too
62
+ WebifyRuby::Convert.new('fonts/my_font.ttf, css: 'stylesheets/fonts')
63
+
64
+ Would create a stylesheets directory if necessary and place my_font.css with relative urls
65
+
66
+ ==== Generate stylesheet with custom urls
67
+
68
+ There might be cases when you're working with subdomains for example and relative urls are not for you
69
+ WebifyRuby::Convert.new('fonts/my_font.ttf, css: 'stylesheets/custom', link_to: 'http://fonts.example.com')
70
+
71
+ Writes a my_font.css file under stylesheets/custom directory with custom url prepended to filename, like this:
72
+
73
+ @font-face {
74
+ font-family: 'my_font';
75
+ src: url('http://fonts.example.com/my_font.eot');
76
+ src: url('http://fonts.example.com/my_font.eot?#iefix') format('embedded-opentype'),
77
+ url('http://fonts.example.com/my_font.svg#my_font') format('svg'),
78
+ url('http://fonts.example.com/my_font.woff') format('woff');
79
+ font-weight: normal;
80
+ font-style: normal;
81
+ }
82
+
83
+ ==== Get stylesheet without creating files
84
+
85
+ You might need a css styles to write to a file by yourself, in this case you need to look for returned values
86
+ convert = WebifyRuby::Convert.new('fonts/my_font.ttf, css: true, link_to: 'http://example.com/fonts')
87
+ puts convert.styles
88
+
89
+ would result in
90
+
91
+ > puts convert.styles
92
+ @font-face {
93
+ font-family: 'my_font';
94
+ src: url('http://fonts.example.com/my_font.eot');
95
+ src: url('http://fonts.example.com/my_font.eot?#iefix') format('embedded-opentype'),
96
+ url('http://fonts.example.com/my_font.svg#my_font') format('svg'),
97
+ url('http://fonts.example.com/my_font.woff') format('woff');
98
+ font-weight: normal;
99
+ font-style: normal;
100
+ }
101
+ => nil
102
+
103
+ So you can make use of generated styles according to your needs
104
+
105
+ ==== Values that you get after converting
106
+
107
+ You might need to know about directory names or other information that library operates on. After initializing a Convert class, you get an object that should have everything you need. An example is here
108
+
109
+ 2.1.0-preview1 :028 > convert = WebifyRuby::Convert.new('public/fonts/my_font.ttf',
110
+ dir:'my_dir_fonts',
111
+ css: 'my_dir_css',
112
+ link_to: 'http://example.com/my_dir_fonts')
113
+ => #<WebifyRuby::Convert:0x007fbc1af6fd00
114
+ @desired_dir="my_dir_fonts",
115
+ @css="my_dir_css",
116
+ @link_to="http://example.com/my_dir_fonts",
117
+ @original_file="public/fonts/my_font.ttf",
118
+ @original_dir="public/fonts",
119
+ @result_dir="my_dir_fonts/d20140211-56782-4cglvv",
120
+ @file="my_dir_fonts/d20140211-56782-4cglvv/my_font.ttf",
121
+ @command="webify my_dir_fonts/d20140211-56782-4cglvv/my_font.ttf",
122
+ @output="Generating my_dir_fonts/d20140211-56782-4cglvv/my_font.eot
123
+ Generating my_dir_fonts/d20140211-56782-4cglvv/my_font.woff
124
+ Generating my_dir_fonts/d20140211-56782-4cglvv/my_font.svg
125
+ Available cmaps
126
+ -----------------------------------------------------------------------------
127
+ PlatformId | EncodingId | Description
128
+ 0 | 3 | Unicode 2.0 and onwards semantics, Unicode BMP only
129
+ 1 | 0 | Macintosh Roman 8-bit simple
130
+ | 1 | Microsoft Unicode BMP (UCS-2)
131
+ -----------------------------------------------------------------------------
132
+ Selecting platformId 3 encodingId 1 -- Microsoft Unicode BMP (UCS-2)",
133
+ @generated=["my_dir_fonts/d20140211-56782-4cglvv/my_font.eot",
134
+ "my_dir_fonts/d20140211-56782-4cglvv/my_font.woff",
135
+ "my_dir_fonts/d20140211-56782-4cglvv/my_font.svg"],
136
+ @styles="@font-face {
137
+ font-family: 'my_font';
138
+ src: url('http://example.com/my_dir_fonts/my_font.eot');
139
+ src: url('http://example.com/my_dir_fonts/my_font.eot?#iefix') format('embedded-opentype'),
140
+ url('http://example.com/my_dir_fonts/my_font.svg#my_font') format('svg'),
141
+ url('http://example.com/my_dir_fonts/my_font.woff') format('woff');
142
+ font-weight: normal;\nfont-style: normal;
143
+ }">
144
+
145
+ That way you can for example know a generated directory path by running
146
+ convert.result_dir
147
+
148
+
149
+ == Gem Author
150
+
151
+ Dachi Natsvlishvili mailto:dnatsvlishvili@gmail.com
152
+
153
+ == Todo
154
+
155
+ 1. Add support for kerning option
156
+ 2. Add support for desired output files
157
+
158
+ == Licence
159
+
160
+ https://github.com/dachi-gh/webify_ruby/blob/master/MIT-LICENSE
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'WebifyRuby'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require 'webify_ruby/tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'lib'
25
+ t.libs << 'test'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = false
28
+ end
29
+
30
+ task default: :test
@@ -0,0 +1,8 @@
1
+ require 'webify_ruby/version'
2
+
3
+ namespace :webify_ruby do
4
+ desc 'Shows a WebifyRuby gem version'
5
+ task :version do
6
+ puts "You are running WebifyRuby v.#{WebifyRuby::VERSION} so far."
7
+ end
8
+ end
@@ -0,0 +1,99 @@
1
+ require 'fileutils'
2
+ require 'open3'
3
+ require 'tmpdir'
4
+ require 'shellwords'
5
+
6
+ module WebifyRuby
7
+ class Convert
8
+ attr_reader :file, :original_file, :command, :output, :generated, :original_dir, :result_dir, :desired_dir, :css, :link_to, :styles
9
+
10
+ def initialize(file, dir: nil, css: nil, link_to: nil)
11
+ [file, dir]
12
+
13
+ @desired_dir = dir
14
+ @css = css
15
+ @link_to = link_to
16
+
17
+ raise Errno::ENOENT, "The font file '#{file}' does not exist" unless File.exists?(file)
18
+ @original_file = file
19
+
20
+ raise Error, "The font file '#{file}' is not valid" unless WebifyRuby::EXT.include? File.extname(@original_file)
21
+
22
+ @original_dir = File.dirname(@original_file)
23
+ raise Errno::ENOENT, "Can't find directory '#{@original_dir}'" unless File.directory? @original_dir
24
+
25
+ @result_dir = Dir.mktmpdir(nil, destination_dir)
26
+
27
+ FileUtils.cp(@original_file, @result_dir)
28
+
29
+ @file = File.join(@result_dir, File.basename(@original_file))
30
+
31
+ process
32
+
33
+ if affected_files.to_a.length == 0
34
+ WebifyRuby.logger.info "Host did not create any files\n@command\n#{@command}\n@output\n#{@output}\n"
35
+ end
36
+
37
+ generate_css unless @css.nil?
38
+ end
39
+
40
+ def affected_files
41
+ Dir[@result_dir + '/*.{ttf,eot,woff,svg}'].reject { |f| f[@file] }
42
+ end
43
+
44
+ def is_valid?
45
+ false if not @output.include? 'Generating' or @output.include? 'Failed'
46
+ true
47
+ end
48
+
49
+ def generate_css
50
+ needs = affected_files.map { |m| File.extname(m)[1..-1].to_sym }
51
+
52
+ if should_write_css?
53
+ WebifyRuby::Css.relative_from = @link_to ? nil : @css
54
+ WebifyRuby::Css.link_to = @link_to
55
+ end
56
+
57
+ css = WebifyRuby::Css.new(File.basename(@file, ".*"), @file, *needs)
58
+ @styles = css.result
59
+
60
+ css.write @css if should_write_css?
61
+ end
62
+
63
+ protected
64
+
65
+ def should_write_css?
66
+ @css.respond_to?(:to_str) and not @css.to_s.empty?
67
+ end
68
+
69
+ private
70
+
71
+ def destination_dir
72
+ if @desired_dir.nil?
73
+ @original_dir
74
+ else
75
+ if not File.directory?(@desired_dir)
76
+ FileUtils.mkdir_p(@desired_dir)
77
+ else
78
+ @desired_dir
79
+ end
80
+ end
81
+ end
82
+
83
+ def process
84
+ @command = "#{WebifyRuby.webify_binary} #{Shellwords.escape(@file)}"
85
+ @output = Open3.popen3(@command) { |stdin, stdout, stderr| stdout.read }
86
+
87
+ if not is_valid?
88
+ WebifyRuby.logger.fatal "Invalid input received\n@command\n#{@command}\n@output\n#{@output}\n"
89
+ raise Error, "Binary responded with failure:\n#{@output}"
90
+ end
91
+
92
+ @generated = Shellwords.escape(@output).split("'\n'").select{|s| s.match('Generating')}.join().split('Generating\\ ')[1..-1]
93
+
94
+ if @generated.to_a.empty?
95
+ WebifyRuby.logger.info "No file output received\n@command\n#{@command}\n@output\n#{@output}\n"
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,60 @@
1
+ require 'erb'
2
+ require 'fileutils'
3
+ require 'pathname'
4
+
5
+ module WebifyRuby
6
+ TEMPLATE = <<-CSS.gsub /^\s*/, ''
7
+ @font-face {
8
+ font-family: '<%= @name %>';
9
+ <% if has_eot %>src: url('<%= @url %>.eot'); <% end %>
10
+ <% if has_eot %>src: url('<%= @url %>.eot?#iefix') format('embedded-opentype')<%if has_ttf or has_woff or has_svg %>,<%end%><%else%>src:<% end %>
11
+ <% if has_svg %> url('<%= @url %>.svg#<%= @name %>') format('svg')<%if has_ttf or has_woff %>,<%end%><% end %>
12
+ <% if has_woff %> url('<%= @url %>.woff') format('woff')<%if has_ttf%>,<%end%><% end %>
13
+ <% if has_ttf %> url('<%= @url %>.ttf') format('truetype')<% end %>;
14
+ font-weight: normal;
15
+ font-style: normal;
16
+ }
17
+ CSS
18
+
19
+ class Css
20
+ attr_reader :result, :filename, :url, :dir, :css_file, :output
21
+
22
+ %w(eot svg woff ttf).each do |ext|
23
+ define_method('has_' + ext) { @has.include? ext.to_sym }
24
+ end
25
+
26
+ class << self;attr_accessor :relative_from, :link_to;end
27
+
28
+ def initialize(name, file, *has)
29
+ [name, file, has]
30
+
31
+ @has = has
32
+ @name = name
33
+
34
+ @filename = File.basename(file, '.*')
35
+
36
+ @url = (self.class.relative_from.nil? ?
37
+ (self.class.link_to ? (self.class.link_to + '/' + File.basename(file)) : file)
38
+ : Pathname.new(file).relative_path_from(Pathname.new(self.class.relative_from))).to_s[/.*(?=\..+$)/]
39
+
40
+ make_css
41
+ end
42
+
43
+ def write(dir)
44
+ @dir = FileUtils.mkdir_p dir
45
+ @css_file = File.join(@dir, @filename + '.css')
46
+
47
+ File.delete(@css_file) if File.exist?(@css_file)
48
+ @output = File.open(@css_file, 'w') { |file| file.write(@result) }
49
+ end
50
+
51
+ private
52
+
53
+ def make_css
54
+ template = ERB.new TEMPLATE
55
+ result = template.result binding
56
+ (0..3).each { result.gsub!(/\n;\n/m, ";\n") }
57
+ @result = result.gsub /^$\n/, ''
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module WebifyRuby
2
+ class Error < StandardError;end
3
+ end
@@ -0,0 +1,5 @@
1
+ module WebifyRuby
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks { WebifyRuby::load_tasks }
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module WebifyRuby
2
+ def self.load_tasks
3
+ Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f; }
4
+ end
5
+
6
+ self.load_tasks unless defined?(Rails)
7
+ end
@@ -0,0 +1,3 @@
1
+ module WebifyRuby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'logger'
4
+
5
+ require 'webify_ruby/version'
6
+ require 'webify_ruby/errors'
7
+ require 'webify_ruby/convert'
8
+ require 'webify_ruby/css'
9
+ require 'webify_ruby/tasks'
10
+ require 'webify_ruby/railtie' if defined?(Rails)
11
+
12
+ module WebifyRuby
13
+
14
+ EXT = %w(.ttf .otf)
15
+
16
+ def self.logger=(log)
17
+ @logger = log
18
+ end
19
+
20
+ def self.logger
21
+ return @logger if @logger
22
+ logger = Logger.new(STDOUT)
23
+ logger.level = Logger::INFO
24
+ @logger = logger
25
+ end
26
+
27
+ def self.webify_binary=(bin)
28
+ @webify_binary = bin
29
+ end
30
+
31
+ def self.webify_binary
32
+ @webify_binary || 'webify'
33
+ end
34
+ end
File without changes
File without changes
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webify_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dachi Natsvlishvili
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ description: WebifyRuby communicates with Webify and provides nice and easy working
28
+ interface
29
+ email:
30
+ - dnatsvlishvili@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
+ - lib/tasks/version.rake
39
+ - lib/webify_ruby.rb
40
+ - lib/webify_ruby/convert.rb
41
+ - lib/webify_ruby/css.rb
42
+ - lib/webify_ruby/errors.rb
43
+ - lib/webify_ruby/railtie.rb
44
+ - lib/webify_ruby/tasks.rb
45
+ - lib/webify_ruby/version.rb
46
+ - test/dummy/db/test.sqlite3
47
+ - test/dummy/log/test.log
48
+ homepage: https://github.com/dachi-gh/webify_ruby
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.2.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A Ruby wrapper for Webify application written in Haskell
72
+ test_files:
73
+ - test/dummy/db/test.sqlite3
74
+ - test/dummy/log/test.log