forked from nextcloud/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateIconsDoc.php
More file actions
executable file
·165 lines (140 loc) · 4.46 KB
/
Copy pathgenerateIconsDoc.php
File metadata and controls
executable file
·165 lines (140 loc) · 4.46 KB
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
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
<?php
/**
* @copyright Copyright (c) 2018 John Molakvoæ <[email protected]>
*
* @author John Molakvoæ <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
include __DIR__ . '/vendor/autoload.php';
use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\Formatter\Crunched;
function command_exist($cmd) {
$return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
return !empty($return);
}
// You need svgexport to run this script
if (!command_exist('svgexport')) {
print("\n\nYou need svgexport to run this script. Please install it with `npm install svgexport -g`\n\n");
exit(1);
}
$sourceDirectory = __DIR__ . '/server';
$destinationDirectory = __DIR__ . '/../developer_manual/html_css_design/';
// Init scss compiler
$scss = new Compiler();
$scss->setImportPaths([
__DIR__ . '/server/core/css'
]);
// Continue after throw
$scss->setIgnoreErrors(true);
$scss->setFormatter(Crunched::class);
$compiledScss = $scss->compile(
'@import "variables.scss";' .
'@import "functions.scss";' .
// override the path generator function
'@function icon-color-path($icon, $dir, $color, $version: 1, $core: false) {
$color: remove-hash-from-color($color);
@if $core {
@return "/core/img/#{$dir}/#{$icon}";
} @else {
@return "/apps/#{$dir}/img/#{$icon}";
}
}'.
'@import "icons.scss";'
);
$icons = [];
$lines = explode('}', $compiledScss);
$reIcon = '/^\.(icon-[a-z-]+)/i';
$reUrl = '/url\(\"([a-z0-9-.\/]+)/i';
print("\nParsing icons... \n");
// get all icons and urls
foreach($lines as $line) {
if (preg_match($reIcon, $line, $matches)) {
$icon = $matches[1];
if (preg_match($reUrl, $line, $matches)) {
$icons[$icon] = $matches[1];
}
}
}
$count = count($icons);
print(" - $count icons found!\n");
print("\nFormating rst file and converting icons... \n");
// format rst
$rst = '';
foreach($icons as $class => $icon) {
/**
* removing unwanted path and removing last slash
* /core/img/actions/caret -> actions/caret
*/
$path = implode('/', array_slice(explode('/', $icon), 3, 2));
$inPath = $sourceDirectory . $icon . '.svg';
if (file_exists($inPath)) {
$isWhite = substr($class, -strlen('white')) === 'white';
if ($isWhite) {
$path .= '-white'; //adding white suffix
}
$outPath = $destinationDirectory . 'img/' . $path . '.png';
// filling the rst file
$rst .= ".. figure:: img/$path.*\n";
if ($isWhite) {
$rst .= " :class: white-icon\n";
}
$rst .= " :height: 32\n";
$rst .= " :width: 32\n\n";
$rst .= " $class\n\n";
// create directory
$dir = implode('/', array_slice(explode('/', $destinationDirectory . 'img/' . $path), 0, -1));
if (!file_exists($dir)) {
print(" - creating dir $dir \n");
mkdir($dir, 0777, true);
}
// ! can't use svg in rst
// copy original icon
// if (!@copy($sourceDirectory . $icon . '.svg', $destinationDirectory . 'img/' . $path . '.svg')) {
// print(' - error while copying ' . $sourceDirectory . $icon . '.svg' . "\n");
// }
// converting
if ($isWhite) {
exec("svgexport $sourceDirectory$icon.svg $outPath 64: '
circle:not([fill='none']),
rect:not([fill=\"none\"]),
path:not([fill=\"none\"]) {
fill: white;
}
circle[stroke],
rect[stroke],
path[stroke] {
stroke: white;
}
'");
} else {
exec("svgexport $sourceDirectory$icon.svg $outPath 64:");
}
} else {
print(" - error while converting $inPath\n");
}
}
print("\nWriting rst file... \n");
// write file
$file = fopen($destinationDirectory . 'icons.txt', 'w');
fwrite($file, $rst);
fclose($file);
print(" - done! \n");
// path = path.split('/')
// localpath = '/'.join(path[3:5])
// result += ".. figure:: img/" + localpath + ".*\n :height: 32\n :width: 32\n\n " + icon[1:] + "\n\n"
// os.system('inkscape -z img/' + localpath + '.svg -e img/' + localpath + '.png')