forked from globau/logbot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev-inline-svg
More file actions
executable file
·113 lines (94 loc) · 2.9 KB
/
Copy pathdev-inline-svg
File metadata and controls
executable file
·113 lines (94 loc) · 2.9 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
#!/usr/bin/perl
use strict;
use warnings;
use local::lib;
use v5.10;
use Cwd qw( abs_path );
use File::Find qw( find );
use FindBin qw( $RealBin );
use Mojo::File ();
use Mojo::Util qw( getopt );
use YAML::Tiny ();
$| = 1;
my ($inline, $import);
#<<<
getopt
'inline' => \$inline,
'import:s' => \$import;
#>>>
die <<'EOF' unless $inline || $import;
syntax:
dev-inline-svg --inline
dev-inline-svg --import <font-awesome pro path>
EOF
my $templates_path = "$RealBin/web/templates";
my $svg_path = $RealBin . '/web/svg';
my $fa_path = $svg_path . '/font-awesome';
die "failed to find $fa_path\n" unless -d $fa_path;
if ($inline) {
inline_svg();
} else {
import_svg($import);
}
sub inline_svg {
# inlines svg images into templates
# to use, create an svg element with a class name "svg-filename"
# the filename will be loaded from web/svg and replace the svg element
# only the svg-filename class will be carried over into the inline svg element,
# other classes and ids will be lost
# eg. <svg class="svg-sidebar-collapse"></svg>
my @files;
find(
sub {
my $file = $File::Find::name;
return unless -f $file && -s $file;
return unless $file =~ /\.html\.ep$/;
push @files, $file;
},
$templates_path
);
foreach my $file (sort @files) {
my $orig = Mojo::File->new($file)->slurp();
(my $tmpl = $orig) =~ s{
<svg\sclass="svg-([^"]+)".+?</svg>
}{
load_svg($1)
}gxe;
next if $orig eq $tmpl;
say substr($file, length($templates_path) + 1);
Mojo::File->new($file)->spurt($tmpl);
say 'inline-svg: updated ', substr($file, length($RealBin) + 1);
}
}
sub load_svg {
my ($name) = @_;
my $file =
-e "$fa_path/$name.svg"
? "$fa_path/$name.svg"
: "$svg_path/$name.svg";
die "failed to find $name.svg\n" unless -e $file;
my $svg = Mojo::File->new($file)->slurp();
$svg =~ s/^<svg /<svg class="svg-$name" /;
$svg =~ s/\s+$//;
return $svg;
}
sub import_svg {
my ($source_path) = @_;
# update svg icons from font-awesome pro
$source_path =~ s{/$}{};
$source_path .= '/advanced-options/raw-svg';
die "failed to find font-awesome pro icons\n" unless -d "$source_path/solid";
my $mapping = YAML::Tiny->read("$fa_path/mapping.yaml")->[0];
foreach my $output_name (sort keys %{$mapping}) {
my $input_file = "$source_path/" . $mapping->{$output_name} . '.svg';
my $output_file = "$fa_path/$output_name.svg";
my $input = Mojo::File->new($input_file)->slurp;
my $output = Mojo::File->new($output_file)->slurp;
if ($input eq $output) {
say "inline-svg: $output_name unchanged";
} else {
Mojo::File->new($output_file)->spurt($input);
say "inline-svg: $output_name updated";
}
}
}