Skip to content

Instantly share code, notes, and snippets.

@nladart
Created May 4, 2015 07:02
Show Gist options
  • Save nladart/d00886745fb98f89ed7b to your computer and use it in GitHub Desktop.
Save nladart/d00886745fb98f89ed7b to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.14)
// Compass (v1.0.1)
// ----
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
// Google Font Mixin – v. 1.0.2 beta
//
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
// Helper functions by Hugo Giraudel
// Shamelessly ripped Sassy List Functions
// Go get the real thing: http://sassylists.com/
//
// Note! just import sassylists and google-font
// will switch to this
//
@function str-replace($string, $search, $replace: '') {$index: str-index($string, $search); @if $index { @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); }@return $string; }
@function ___gf-list-missing($functions...) {$missing-dependencies: (); @each $function in $functions { @if not function-exists($function) {$missing-dependencies: append($missing-dependencies, $function, comma); } } @return length($missing-dependencies) != 0; }
@function ___gf-list-multival($list) { @return length($list) > 1; }
@function ___gf-list-flatten($list) { @if ___gf-list-missing(___gf-list-multival) == true { @return null; } $result: (); @each $item in $list { @if ___gf-list-multival($item) { $flatten: ___gf-list-flatten($item); @each $i in $flatten { $result: append($result, $i, list-separator($list)); }} @else { $result: append($result, $item, list-separator($list)); }} @return $result;}
@function ___gf-list-unique($list) { @if ___gf-list-missing(___gf-list-to-list) == true { @return null; } $result: (); @each $item in $list { @if not index($result, $item) { $result: append($result, $item, list-separator($list)); }} @return ___gf-list-to-list($result); }
@function ___gf-list-merge($lists...) { @if ___gf-list-missing(___gf-list-flatten, ___gf-list-unique) == true { @return null; } $result: ___gf-list-unique(___gf-list-flatten($lists)); @return $result; }
@function ___gf-list-explode($string, $delimiter: '', $separator: "space") { $result: (); $length: str-length($string); @if not index("space" "comma", $separator) { $separator: "space"; }@if str-length($delimiter) == 0 {@for $i from 1 through $length { $result: append($result, str-slice($string, $i, $i)); } @return $result; } $running: true; $remaining: $string; @while $running { $index: str-index($remaining, $delimiter); @if not $index { $running: false; } @else { $slice: str-slice($remaining, 1, $index - 1); $result: append($result, $slice, $separator); $remaining: str-slice($remaining, $index + str-length($delimiter)); } } @return append($result, $remaining, $separator); }
@function ___gf-list-to-list($value, $separator: list-separator($value)) { @if type-of($value) != "list" or list-separator($value) != $separator { $new-list: if($separator == "comma", sl-comma-list(), ()); @each $item in $value { $new-list: append($new-list, $item, $separator); } @return $new-list; } @return $value; }
// Map helper functions
@function __gf-str-replace($arglist...) { @return str-replace($arglist...); }
@function __gf-list-flatten($arglist...){ @return if(function-exists(sl-flatten), sl-flatten($arglist...), ___gf-list-flatten($arglist...)); }
@function __gf-list-unique($arglist...) { @return if(function-exists(sl-remove-duplicates), sl-remove-duplicates($arglist...), ___gf-list-unique($arglist...)); }
@function __gf-list-merge($arglist...) { @return if(function-exists(sl-union), sl-union($arglist...), ___gf-list-merge($arglist...)); }
@function __gf-list-explode($arglist...){ @return if(function-exists(sl-explode), sl-explode($arglist...), ___gf-list-explode($arglist...)); }
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
// General notes:
//
// If a text value is passed we can't add it to
// a combined request – as it will affect all.
// Also we do not need a subset
//
// Subsets are treated as globals why you only
// need to add it once in a combined request.
// Though it is possible to define subsets in
// the mixin it is recommended to use the
// $google-font-subsets map.
//
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
// Google font subsets (true = enabled)
// By default google will deliver Latin
// Why it is only required if needed in
// combination with another subset
//
$google-font-subsets: (
'arabic' :false
, 'cyrillic' :false
, 'cyrillic-ext':false
, 'devanagari' :false
, 'freek' :false
, 'greek-ext' :false
, 'hebrew' :false
, 'khmer' :false
, 'latin' :false
, 'latin-ext' :false
, 'telugu' :false
, 'vietnamese' :false
) !default;
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
// Google font list and context (internal use only)
//
$__gf: () !default;
$__gf-combine: false !default;
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
// Google Font Mixin
// $name : The name of the font
// $weight : Font weight 100-900 (default 400)
// $style : Font style normal/italic/oblique (default normal)
// $subset : Font subsets
// $text : Reduce font to these characters ()
@mixin google-font(
$name: null, // string
$weight: 400, // number
$style: normal, // string
$subset: null, // list or quoted string
$text: null // quoted string
){
//
// TODO - Add parser to handle positional arguments
// and keywords (switch to arglist...)
//
//
// Base url
//
$URL: '//fonts.googleapis.com/css?family=';
//
// Add font to list or print out @import
//
@if $name {
//
// Clean up name, style and weight
//
$name: __gf-str-replace(inspect(unquote($name)), ' ', '+');
$style: to-lower-case($style);
$weight: if($style == normal, $weight, $weight+$style);
//
// If subset is passed as a string explode it into a list
//
@if type-of($subset) == string {
$delimiter: if(str-index($subset, ','), ',', ' ');
$subset: __gf-list-explode($subset, $delimiter);
}
//
// Add enabled subsets from $google-font-subsets
//
@each $key, $value in $google-font-subsets {
@if $value { $subset: append($subset, unquote($key), comma); }
}
//
// If not in a combine context or text value
// is passed print out the @import.
// Else add the font as map to the global list
//
@if $__gf-combine == false or $text{
$weight: ':' + $weight;
$subset: if($subset and $subset !=() and not $text, '&subset='+$subset, '');
//
// Encode special characters
//
@if $text {
$encode:('!':'%21','#':'%23','$':'%24','&':'%26','\'':'%27',
'(':'%28',')':'%29','*':'%2A','+':'%2B',',':'%2C',
'/':'%2F',':':'%3A',';':'%3B','=':'%3D','?':'%3F',
'@':'%40','[':'%5B',']':'%5D',' ':'%20');
$chars : __gf-list-unique(__gf-list-explode($text));
$text: '';
@for $i from 1 through length($chars){
$replace: map-get($encode, nth($chars,$i));
$text: $text + if($replace, $replace, nth($chars,$i));
}
$text:'&text='+$text
} @else {
$text:'';
}
@import url(__gf-str-replace(#{$URL}#{$name}#{$weight}#{$subset},' ','')#{$text});
}@else {
$subset: if($subset, $subset, null);
$__gf: append($__gf, (name:$name, weight: $weight, subset: $subset)) !global;
}
}
//
// Combine
//
@else {
//
// Enable combine context
//
$__gf-combine: true !global;
//
// Nested mixins – now in a combine context
//
@content;
//
// Map to hold the combined fonts
//
$combine-map:();
//
// Loop through the global list and add
// font map values to the combined map
//
@for $i from 1 through length($__gf){
//
// Get the font map, name and already
// mapped values
//
$font : nth($__gf,$i);
$name : map-get($font, name);
$mapped: map-get($combine-map, $name);
//
// If font is not mapped add it
// to the combined map
//
@if not $mapped {
$combine-map: map-merge($combine-map, ($name: $font));
}
//
// If font is mapped merge weights and subsets
// with the existing values
//
@else {
$weight: map-get($font, weight);
@if $weight {
$weight: __gf-list-merge(map-get($mapped, weight), $weight);
}
$subset: map-get($font, subset);
@if $subset {
$subset: __gf-list-merge(map-get($mapped,subset), $subset);
}
$combine-map: map-merge($combine-map, ($name: (name:$name, weight: $weight, subset: $subset)));
}
}
//
// Create the combined query string
//
$name-weight:'';
$subsets:();
@each $key, $value in $combine-map {
$name : map-get($value, name);
$weight : map-get($value, weight);
$subset : map-get($value, subset);
@for $i from 1 through length($subset){
@if nth($subset, $i) and not index($subsets, nth($subset, $i)){
$subsets: append($subsets, nth($subset, $i), comma);
}
}
$subset: if($subsets and $subsets !=(), '&subset='+$subsets, '');
$name-weight: $name-weight + if($name-weight =='','','|')+ $name + ':' + $weight;
}
@import url(__gf-str-replace(__gf-str-replace(#{$URL}#{$name-weight}#{$subset},' ',''),'"',''));
$__gf-combine: false !global;
}
}
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
// TEST
//
// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
$google-font-subsets: (
'arabic' :false
, 'cyrillic' :false
, 'cyrillic-ext':false
, 'devanagari' :false
, 'freek' :false
, 'greek-ext' :false
, 'hebrew' :false
, 'khmer' :false
, 'latin' :false
, 'latin-ext' :false
, 'telugu' :false
, 'vietnamese' :false
);
@include google-font(Raleway, 300);
@include google-font(Raleway, 400);
@include google-font {
@include google-font(Lato, 300);
@include google-font(Lato, 400);
@include google-font(Open Sans, 300);
@include google-font(Open Sans, 400);
@include google-font(Open Sans, 800 );
@include google-font(Open Sans, 300, italic, $text:'Lorem & Ipsum: "dolor sit amet!"');
@include google-font('Marck Script', $text:'F ');
}
@import url(//fonts.googleapis.com/css?family=Raleway:300);
@import url(//fonts.googleapis.com/css?family=Raleway:400);
@import url(//fonts.googleapis.com/css?family=Open+Sans:300italic&text=Lorem%20%26Ipsu%3A"dlita%21);
@import url(//fonts.googleapis.com/css?family=Marck+Script:400&text=F%20);
@import url(//fonts.googleapis.com/css?family=Lato:300,400|Open+Sans:300,400,800);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment