Skip to content

Commit

Permalink
Merge pull request #56 from TingyuHuang/master
Browse files Browse the repository at this point in the history
fix: font-stretch doesn't match the regex
  • Loading branch information
battlesnake authored Sep 29, 2021
2 parents 01580c0 + 2eecf26 commit 3e89058
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/node_modules/
example*/out/
example*/node_modules
.*.swp
.*.swo
package-lock.json
1 change: 1 addition & 0 deletions example5/fonts.list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Open+Sans:300&subset=cyrillic,cyrillic-ext,greek,greek-ext,latin-ext
13 changes: 13 additions & 0 deletions example5/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var gulp = require('gulp');
var googleWebFonts = require('../');

var options = { };

gulp.task('fonts', function () {
return gulp.src('./fonts.list')
.pipe(googleWebFonts(options))
.pipe(gulp.dest('out/fonts'))
;
});

gulp.task('default', ['fonts']);
11 changes: 11 additions & 0 deletions example5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "example5",
"version": "0.0.1",
"description": "",
"main": "gulpfile.js",
"scripts": {
"test": "gulp"
},
"author": "Tingyu Huang",
"license": "ISC"
}
23 changes: 15 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,22 @@ function getter(options) {
// since Google doesn't put there '.svg' suffix for some reason
// (but does that for other extensions).
var re = new RegExp([
"\\s*font-family:\\s*'([^']+)';",
"\\s*font-style:\\s*(\\w+);",
"\\s*font-weight:\\s*(\\w+);",
"\\s*src:([^;]*);",
".*(?:unicode-range:([^;]+);)?",
"\\s*font-family:\\s*'(?<family>[^']+)';",
"\\s*font-style:\\s*(?<style>\\w+);",
"\\s*font-weight:\\s*(?<weight>\\w+);",
".*(?:\\s*font-stretch:\\s(?<stretch>[^;]+);)?",
"\\s*src:(?<src>[^;]*);",
".*(?:unicode-range:(?<range>[^;]+);)?",
].join(''), 'm');

return formatData.apply(null, block.match(re, 'm'));

let found = block.match(re, 'm');
if (found === null) {
throw new Error("faild to match block");
}
return formatData(found.groups);

function formatData(block, family, style, weight, src, range) {
function formatData(data) {
const { family, style, weight, stretch, src, range } = data;
var name = [family, style, weight].join('-') + '.' + ext;
var url = src.match(/url\(([^)]+)\)/)[1];
var locals = src.match(/local\([^)]+\)/g) || [];
Expand All @@ -257,6 +262,7 @@ function getter(options) {
name: name.replace(/\s/g, '_'),
url: url,
locals: locals,
stretch: stretch || 'normal',
range: range || 'U+0-10FFFF'
};
}
Expand All @@ -280,6 +286,7 @@ function getter(options) {
' font-style: $style;',
' font-weight: $weight;',
' font-display: ' + options.fontDisplayType + ';',
' font-stretch: $stretch;',
' src: $src' + format + ';',
' unicode-range: $range;',
'}'
Expand Down

0 comments on commit 3e89058

Please sign in to comment.