graphite-sass 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: 585c7020278533126d93285d965e63bd3085feaf
4
+ data.tar.gz: e44e791882c7a17221b5b033bf6d4b4d48dbeb7b
5
+ SHA512:
6
+ metadata.gz: 08104a1f32b485ee103002bdeb0cd4aafe74e8b0fefd3590bc62e85120c9059835babe48cad2b2d6c0c2eac3e7de8377bfbed90539ffe1f24eac9cc80e9d5f69
7
+ data.tar.gz: 86ea08fcab288c367338fe149ffe031e64412bd74bf99ff815e0ae382bec55a7326cd046f01b0c72131f2a77dc1b817280e50f63708d979bce67ec8fc6e153ad
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Graphite [![Gem Version](https://badge.fury.io/rb/Graphite.svg)](http://badge.fury.io/rb/Graphite)
2
+
3
+ Graphite imports a folder of fonts and automagically outputs font-face directives for each font into your stylesheet.
4
+
5
+ ## Installation
6
+
7
+ 1. `gem install Graphite`
8
+ 2. Add `require "graphite"` to your `config.rb`
9
+ 3. Import into your stylesheet with `@import "graphite";`
10
+
11
+ ## Documentation
12
+
13
+ #### File naming convention
14
+
15
+ In order for Graphite to successfully import your fonts, please follow this convention for naming your font files: `name-weightnum-style.extension`
data/lib/Graphite.rb ADDED
@@ -0,0 +1,173 @@
1
+ require 'compass'
2
+ require 'sassy_hash'
3
+
4
+ extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
5
+ Compass::Frameworks.register('graphite', :path => extension_path)
6
+
7
+ module Graphite
8
+ VERSION = "0.0.1"
9
+ DATE = "2014-06-24"
10
+ end
11
+
12
+ # Graphite : import fonts from font_dir into map
13
+ # ----------------------------------------------------------------------------------------------------
14
+ # @param font_dir [string] : path to top-level font directory
15
+ # @param legacy_ie [bool] : support legacy versions of ie
16
+ # ----------------------------------------------------------------------------------------------------
17
+ # @return map of fonts, variable for each font
18
+
19
+ module Sass::Script::Functions
20
+ def graphite(font_dir, legacy_ie = false)
21
+
22
+ def opts(value)
23
+ value.options = options
24
+ value
25
+ end
26
+
27
+ # Set Sass variable to environment
28
+ def set_sass_var(literal, name = literal)
29
+ environment.global_env.set_var("#{name}", Sass::Script::Value::String.new(literal))
30
+ end
31
+
32
+ # Get weights and styles for passed font
33
+ def font_has_weights?(font)
34
+ # Keep hash of weights found
35
+ weights = {}
36
+ # Valid weights
37
+ valid_weights = ["100", "200", "300", "400", "500", "600", "700", "800", "900"]
38
+ # valid styles
39
+ valid_styles = ["normal", "italic"]
40
+ # Get weights for font
41
+ valid_weights.each do |weight|
42
+ # Check filename for weight
43
+ if font.include?(weight)
44
+ # Get styles for weight
45
+ styles = []
46
+ # Check filename for style
47
+ valid_styles.each do |style|
48
+ if font.include?(style)
49
+ # Save to styles
50
+ styles << style
51
+ end
52
+ end
53
+ # Keep hash of each weight
54
+ w = {
55
+ weight => {
56
+ "styles" => styles
57
+ }
58
+ }
59
+ # Merge weight into weights hash
60
+ weights.merge!(w)
61
+ end
62
+ end
63
+ # Return weights
64
+ return weights
65
+ end
66
+
67
+ # Add style to passed weight
68
+ def set_font_style(font, style)
69
+ font.merge!(style) do |k, old, new|
70
+ # Merge styles
71
+ if old.is_a?(Hash)
72
+ # Get key => value of old map
73
+ old.each do |key, value|
74
+ # Does new map have key?
75
+ if new.has_key?(key)
76
+ # Then concat values
77
+ new.each do |k, v|
78
+ value.concat(v)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ # Check if passed dir has fonts
87
+ def dir_has_fonts?(dir)
88
+ # Keep hash of all fonts found
89
+ fonts = {}
90
+ # Valid font extensions
91
+ valid_extensions = ["eot", "otf", "woff", "ttf", "svg"]
92
+ # Change dir to font_dir
93
+ Dir.chdir(dir) do
94
+ # Search each dir
95
+ Dir.glob("**/") do |d|
96
+ # Check if dir has fonts
97
+ Dir.chdir(d) do
98
+ path = d
99
+ # Clean up font name
100
+ family = d.delete("/")
101
+ # Get matched extensions
102
+ extensions = []
103
+ # Get matched weights and styles
104
+ variations = {}
105
+ # Check dir for matching extensions
106
+ valid_extensions.each do |ext|
107
+ Dir.glob("*.#{ext}") do |filename|
108
+ if filename.size > 0
109
+ # Check if filename has weights
110
+ w = font_has_weights?(filename.to_s)
111
+ # Merge weights and styles
112
+ set_font_style(variations, w)
113
+ # Save extensions that exist
114
+ if extensions.include?(ext) === false
115
+ extensions << ext
116
+ end
117
+ end
118
+ end
119
+ end
120
+ # Build out font hash
121
+ font = {
122
+ "#{family}" => {
123
+ "path" => path,
124
+ "extensions" => extensions,
125
+ "weights" => variations
126
+ }
127
+ }
128
+ # Merge into fonts hash
129
+ fonts = fonts.merge(font)
130
+ # Set Sass variable for font family
131
+ set_sass_var(family)
132
+ end
133
+ end
134
+ end
135
+ # Return hash of all fonts
136
+ return fonts
137
+ end
138
+
139
+ # Assert types
140
+ assert_type font_dir, :String, :font_dir
141
+
142
+ # Define root path up to current working directory
143
+ root = Dir.pwd
144
+
145
+ # Clean up font_dir
146
+ font_dir = unquote(font_dir).to_s
147
+
148
+ # Define dir path
149
+ dir_path = root
150
+ dir_path += font_dir
151
+
152
+ # Normalize windows path
153
+ dir_path = Sass::Util.pathname(dir_path)
154
+
155
+ # Create Sass variable for font path
156
+ set_sass_var(font_dir, 'graphite_font_dir')
157
+
158
+ # Font naming convention : name-weight[-variant].extension
159
+ fonts = {
160
+ "legacy-ie" => legacy_ie,
161
+ }
162
+
163
+ # Get all fonts in dir
164
+ f = dir_has_fonts?(dir_path)
165
+
166
+ # Merge results into fonts
167
+ fonts = fonts.merge(f)
168
+
169
+ # Convert hash to map, return
170
+ Sass::Script::Value::Map.new(SassyHash[fonts])
171
+ end
172
+ declare :Graphite, [:font_dir]
173
+ end
@@ -0,0 +1,165 @@
1
+ // Filename seperator
2
+ // ----
3
+ $graphite_seperator: "-" !global;
4
+
5
+ // Prepend directory path
6
+ // ----
7
+ $graphite_chdir: ".." !global;
8
+
9
+ // Checks if item is map
10
+ // ----
11
+ // @param $n [literal] : item
12
+ // ----
13
+ // @return [bool]
14
+
15
+ @function is-map($n) {
16
+ @return type-of($n) == "map";
17
+ }
18
+
19
+ // Fetch value from key in map
20
+ // ----
21
+ // @dependence `map-fetche()`
22
+ // ----
23
+ // @param $i [map] : map
24
+ // @param $n [list] : keys
25
+ // ----
26
+ // @return fetched value | false
27
+
28
+ @function map-fetch($map, $keys) {
29
+ $key: nth($keys, 1);
30
+ $length: length($keys);
31
+ $value: map-get($map, $key);
32
+
33
+ // check if value equals null (meaning the @param was incorrect and the map doesn't exist)
34
+ @if $value == null {
35
+ @warn "Invalid arguments padded to function: `map-fetch(#{$map}, #{$keys})`";
36
+ @return false;
37
+ }
38
+
39
+ @else {
40
+ @if $length > 1 {
41
+ $rest: ();
42
+
43
+ @for $i from 2 through $length {
44
+ $rest: append($rest, nth($keys, $i))
45
+ }
46
+
47
+ @return map-fetch($value, $rest);
48
+
49
+ } @else {
50
+ @return $value;
51
+ }
52
+ }
53
+ }
54
+
55
+ // Import fonts from $dir
56
+ // ----
57
+ // @param $dir [string] : directory to import fonts
58
+ // @param $legacy-ie [bool] : support legacy ie
59
+ // ----
60
+ // @return $fonts [map] : imports fonts from $dir and creates
61
+ // a $var for each font family containing [string] name
62
+
63
+ @mixin graphite($dir, $legacy-ie: false) {
64
+ $graphite_fonts: graphite("#{$dir}", $legacy-ie) !global;
65
+
66
+ // Make sure it's a valid map before attempting the loop
67
+ @if is-map($graphite_fonts) {
68
+ // Start legacy with null value
69
+ $legacy-ie: null;
70
+
71
+ // Loop over each label => font and output a @font-face directive
72
+ @each $key, $value in $graphite_fonts {
73
+
74
+ // Support legacy IE?
75
+ @if $key == "legacy-ie" {
76
+ $legacy-ie: map-get($graphite_fonts, "legacy-ie");
77
+ }
78
+
79
+ @if is-map($value) {
80
+ // Font name
81
+ $name: $key;
82
+ // Path to top-level font directory
83
+ $path: $graphite_font_dir;
84
+ // Empty list of extensions
85
+ $extensions: ();
86
+ // Empty list of declared fonts
87
+ $history: ();
88
+
89
+ // Break down the font map
90
+ @each $k, $v in $value {
91
+
92
+ // Font directory string
93
+ @if $k == "path" {
94
+ // Define path to font directory
95
+ $path: $graphite_chdir + $path + "/" + $v;
96
+ }
97
+
98
+ // List of extensions found
99
+ @if $k == "extensions" {
100
+ // Get each extension
101
+ @each $ext in $v {
102
+ // Append ext to list
103
+ $extensions: append($extensions, $ext);
104
+ }
105
+ }
106
+
107
+ // Map of font weights
108
+ @if $k == "weights" {
109
+ $weights: map-fetch($graphite_fonts, $key $k);
110
+
111
+ // Iterate over each weight
112
+ @each $weight, $styles in $weights {
113
+ // Get list of styles for weight
114
+ $styles: map-fetch($weights, $weight "styles");
115
+
116
+ // Iterate over each style
117
+ @each $style in $styles {
118
+ // Contatenate vars to create filename
119
+ $filename: "#{$name + $graphite_seperator + $weight + $graphite_seperator + $style}";
120
+
121
+ // If filename has not already been declared
122
+ @if not index($history, "#{$filename}") {
123
+ // Add filename to history
124
+ $history: append($history, $filename, "comma");
125
+
126
+ @font-face {
127
+ // Empty list of valid extensions
128
+ $types: ();
129
+
130
+ // Define font family
131
+ font-family: "#{$name}";
132
+
133
+ // Output weight and style
134
+ font-weight: $weight;
135
+ font-style: $style;
136
+
137
+ // IE9
138
+ @if index($extensions, "eot") {
139
+ src: url("#{$path + $filename}.eot");
140
+ }
141
+
142
+ // Define value for each type
143
+ $ie: if(index($extensions, "eot") and $legacy-ie, url("#{$path + $filename}.eot?#iefix") format("embedded-opentype"), null);
144
+ $woff: if(index($extensions, "woff"), url("#{$path + $filename}.woff") format("woff"), null);
145
+ $ttf: if(index($extensions, "ttf"), url("#{$path + $filename}.ttf") format("truetype"), null);
146
+ $svg: if(index($extensions, "svg"), url("#{$path + $filename + '.svg#' + $name}") format("svg"), null);
147
+
148
+ // Append to types list if not null
149
+ $types: if($ie != null, append($types, #{$ie}, "comma"), $types);
150
+ $types: if($woff != null, append($types, #{$woff}, "comma"), $types);
151
+ $types: if($ttf != null, append($types, #{$ttf}, "comma"), $types);
152
+ $types: if($svg != null, append($types, #{$svg}, "comma"), $types);
153
+
154
+ // Output types
155
+ src: #{$types};
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
163
+ }
164
+ }
165
+ }
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphite-sass
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ezekiel Gabrielse
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sass
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: compass
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: sassy_hash
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.6
55
+ description: Graphite imports a folder of fonts and automagically outputs font-face
56
+ directives for each font into your stylesheet.
57
+ email:
58
+ - ezekg@yahoo.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - README.md
64
+ - lib/Graphite.rb
65
+ - stylesheets/Graphite.scss
66
+ homepage: https://github.com/ezekg/Graphite/
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ - stylesheets
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.3.6
85
+ requirements: []
86
+ rubyforge_project: graphite
87
+ rubygems_version: 2.2.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Graphite imports a folder of fonts and automagically outputs their font-face
91
+ directives.
92
+ test_files: []