Module: Sass::Script::Functions
- Defined in:
- lib/graphite.rb
Overview
Graphite : import fonts from font_dir into map
Instance Method Summary collapse
-
#dir_has_fonts?(dir, debug) ⇒ Boolean
Check if passed dir has fonts.
-
#font_has_weights?(font) ⇒ Boolean
Get weights and styles for passed font.
- #graphite(font_dir, legacy_ie, debug) ⇒ Object
- #opts(value) ⇒ Object
-
#set_font_style(font, style) ⇒ Object
Add style to passed weight.
-
#set_sass_var(literal, name = literal) ⇒ Object
Set Sass variable to environment.
Instance Method Details
#dir_has_fonts?(dir, debug) ⇒ Boolean
Check if passed dir has fonts
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/graphite.rb', line 86 def dir_has_fonts?(dir, debug) # Keep hash of all fonts found fonts = {} # Valid font extensions valid_extensions = ["eot", "otf", "woff", "ttf", "svg"] # Check if directory exists dir_name = File.dirname(dir) if File.exists?(dir_name) # Print debug puts "Fonts directory found: #{dir}" if debug # Change dir to font_dir Dir.chdir(dir) do # Search each dir Dir.glob("**/") do | d | # Check if directory exists dir_name = File.dirname(d) if File.exists?(dir_name) # Check if dir has fonts Dir.chdir(d) do path = d # Clean up font name family = d.delete("/") # Print debug puts "Font family found: #{family}" if debug # Get matched extensions extensions = [] # Get matched weights and styles variations = {} # Check dir for matching extensions valid_extensions.each do | ext | Dir.glob("*.#{ext}") do | filename | puts "Font found: #{filename}" if debug # Check if filename has weights w = font_has_weights?(filename.to_s) # Merge weights and styles set_font_style(variations, w) # Save extensions that don't already exist extensions << ext if extensions.include?(ext) === false end end # Build out font hash font = { "#{family}" => { "path" => path, "extensions" => extensions, "weights" => variations } } # Merge into fonts hash fonts = fonts.merge(font) # Set Sass variable for font family set_sass_var(family) end else puts "Directory was not found: #{dir}. Aborting mission." end end end else puts "Directory was not found: #{dir}. Aborting mission." end # Return hash of all fonts return fonts end |
#font_has_weights?(font) ⇒ Boolean
Get weights and styles for passed font
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/graphite.rb', line 34 def font_has_weights?(font) # Keep hash of weights found weights = {} # Valid weights valid_weights = ["100", "200", "300", "400", "500", "600", "700", "800", "900"] # valid styles valid_styles = ["normal", "italic"] # Get weights for font valid_weights.each do | weight | # Check filename for weight if font.include?(weight) # Get styles for weight styles = [] # Check filename for style valid_styles.each do | style | # Save to styles styles << style if font.include?(style) end # Keep hash of each weight w = { weight => { "styles" => styles } } # Merge weight into weights hash weights.merge!(w) end end # Return weights return weights end |
#graphite(font_dir, legacy_ie, debug) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/graphite.rb', line 21 def graphite(font_dir, legacy_ie, debug) def opts(value) value. = value end # Set Sass variable to environment def set_sass_var(literal, name = literal) environment.global_env.set_var("#{name}", Sass::Script::Value::String.new(literal)) end # Get weights and styles for passed font def font_has_weights?(font) # Keep hash of weights found weights = {} # Valid weights valid_weights = ["100", "200", "300", "400", "500", "600", "700", "800", "900"] # valid styles valid_styles = ["normal", "italic"] # Get weights for font valid_weights.each do | weight | # Check filename for weight if font.include?(weight) # Get styles for weight styles = [] # Check filename for style valid_styles.each do | style | # Save to styles styles << style if font.include?(style) end # Keep hash of each weight w = { weight => { "styles" => styles } } # Merge weight into weights hash weights.merge!(w) end end # Return weights return weights end # Add style to passed weight def set_font_style(font, style) font.merge!(style) do | k, o, n | # Merge styles if o.is_a?(Hash) # Get key => value of old map o.each do | key, value | # Does new map have key? if n.has_key?(key) # Then concat values n.each do | k, v | value.concat(v) end end end end end end # Check if passed dir has fonts def dir_has_fonts?(dir, debug) # Keep hash of all fonts found fonts = {} # Valid font extensions valid_extensions = ["eot", "otf", "woff", "ttf", "svg"] # Check if directory exists dir_name = File.dirname(dir) if File.exists?(dir_name) # Print debug puts "Fonts directory found: #{dir}" if debug # Change dir to font_dir Dir.chdir(dir) do # Search each dir Dir.glob("**/") do | d | # Check if directory exists dir_name = File.dirname(d) if File.exists?(dir_name) # Check if dir has fonts Dir.chdir(d) do path = d # Clean up font name family = d.delete("/") # Print debug puts "Font family found: #{family}" if debug # Get matched extensions extensions = [] # Get matched weights and styles variations = {} # Check dir for matching extensions valid_extensions.each do | ext | Dir.glob("*.#{ext}") do | filename | puts "Font found: #{filename}" if debug # Check if filename has weights w = font_has_weights?(filename.to_s) # Merge weights and styles set_font_style(variations, w) # Save extensions that don't already exist extensions << ext if extensions.include?(ext) === false end end # Build out font hash font = { "#{family}" => { "path" => path, "extensions" => extensions, "weights" => variations } } # Merge into fonts hash fonts = fonts.merge(font) # Set Sass variable for font family set_sass_var(family) end else puts "Directory was not found: #{dir}. Aborting mission." end end end else puts "Directory was not found: #{dir}. Aborting mission." end # Return hash of all fonts return fonts end # Assert types assert_type font_dir, :String, :font_dir assert_type legacy_ie, :Bool, :legacy_ie assert_type debug, :Bool, :debug # Cast to bool legacy_ie = legacy_ie.to_bool debug = debug.to_bool # Define root path up to current working directory root = Dir.pwd # Clean up font_dir font_dir = unquote(font_dir).to_s # Define dir path dir_path = root dir_path += font_dir # Normalize windows path dir_path = Sass::Util.pathname(dir_path) # Create Sass variable for font path set_sass_var(font_dir, 'graphite_font_dir') # Font naming convention : name-weight-variant.extension fonts = { "legacy-ie" => legacy_ie, } # Get all fonts in dir f = dir_has_fonts?(dir_path, debug) # Merge results into fonts fonts = fonts.merge(f) # Convert hash to map, return Sass::Script::Value::Map.new(SassyHash[fonts]) end |
#opts(value) ⇒ Object
23 24 25 26 |
# File 'lib/graphite.rb', line 23 def opts(value) value. = value end |
#set_font_style(font, style) ⇒ Object
Add style to passed weight
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/graphite.rb', line 67 def set_font_style(font, style) font.merge!(style) do | k, o, n | # Merge styles if o.is_a?(Hash) # Get key => value of old map o.each do | key, value | # Does new map have key? if n.has_key?(key) # Then concat values n.each do | k, v | value.concat(v) end end end end end end |
#set_sass_var(literal, name = literal) ⇒ Object
Set Sass variable to environment
29 30 31 |
# File 'lib/graphite.rb', line 29 def set_sass_var(literal, name = literal) environment.global_env.set_var("#{name}", Sass::Script::Value::String.new(literal)) end |