-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch-iconfont.php
113 lines (104 loc) · 3.18 KB
/
fetch-iconfont.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
#!/usr/bin/env php
<?php
// 同步 iconfont 的图标资源
// create by hightman
// -------------------------
$fa_i = 'https://at.alicdn.com/t/font_1558663_i3f0ftm4vgs.css';
$fa_svg = '';
/**
* @param string $url 资源 URL
* @return bool|string
*/
function fetchIconFontColor($url)
{
$url = preg_replace('/\.\w+$/', '.js', $url);
echo 'Download .js file from ', $url, ' ...', PHP_EOL;
$content = file_get_contents($url);
if ($content === false) {
echo 'ERROR: failed to download svg.js.', PHP_EOL;
exit(-1);
}
echo 'Parsing symbol tags ...', PHP_EOL;
$data = [];
preg_match_all('#<symbol .+?</symbol>#', $content, $matches, PREG_PATTERN_ORDER);
foreach ($matches[0] as $part) {
$pos1 = strpos($part, 'id="');
if ($pos1 === false) {
continue;
}
$pos1 += 4;
$pos2 = strpos($part, '"', $pos1);
$name = substr($part, $pos1, $pos2 - $pos1);
if (strpos($name, '-tabbar-') !== false) {
continue;
}
$pos1 = strpos($part, 'fill="', $pos1);
if ($pos1 === false) {
continue;
}
$pos1+= 6;
$pos2 = strpos($part, '"', $pos1);
$color = substr($part, $pos1, $pos2 - $pos1);
$data[$name] = strtolower($color);
}
ksort($data);
echo 'Updating css file ...', PHP_EOL;
$content = '';
foreach ($data as $name => $color) {
$content .= '.' . $name . ' { color: ' . $color . '; }' . PHP_EOL;
}
echo 'OK', PHP_EOL;
return $content;
}
/**
* @param string $url 资源 URL
* @param string $outFile 存储位置
* @return bool
*/
function fetchIconFontUrl($url, $outFile)
{
if (empty($url)) {
return;
}
$url = preg_replace('/\?.*$/', '', $url);
if (substr($url, 0, 2) === '//') {
$url = 'https:' . $url;
}
$content = @file_get_contents($url);
if ($content === false) {
return false;
}
$inDotPos = strrpos($url, '.');
if ($inDotPos === false) {
$inDotPos = strlen($url);
}
$inExtName = substr($url, $inDotPos + 1);
switch ($inExtName) {
case 'css':
$content = '/* stylelint-disable */' . PHP_EOL . $content . PHP_EOL . fetchIconFontColor($url);
break;
case 'js':
$content = '/* eslint-disable */' . PHP_EOL . $content;
break;
}
$outDotPos = strrpos($outFile, '.');
if ($outDotPos === false) {
$outDotPos = strlen($outFile);
}
$imports = [];
preg_match_all('#url\(\'(//.+?)\'#', $content, $matches, PREG_PATTERN_ORDER);
foreach ($matches[1] as $part) {
$val = preg_replace('/\?.*$/', '', $part);
$imports[$part] = substr($val, strrpos($val, '.'));
}
foreach ($imports as $key => $value) {
$saveFile = substr($outFile, 0, $outDotPos) . $value;
$saveFileBaseName = basename($saveFile);
if (fetchIconFontUrl($key, $saveFile)) {
$content = str_replace("'{$key}'", "'./{$saveFileBaseName}'", $content);
}
}
return file_put_contents($outFile, $content);
}
fetchIconFontUrl($fa_i, 'src/fonts/fa-i/index.scss');
fetchIconFontUrl($fa_svg, 'src/fonts/fa-svg/index.js');