-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfontawesome-to-latex.pl
171 lines (126 loc) · 3.71 KB
/
fontawesome-to-latex.pl
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
164
165
166
167
168
169
170
171
#!/usr/bin/perl
=head1 NAME
fontawesome-to-latex.pl
=head1 DESCRIPTION
Generate LaTeX definitions from a Font Awesome icon metadata JSON file.
=head1 USAGE
fontawesome-to-latex.pl FA_ICON_METADATA_FILE OUT_FILE
Example: fontawesome-to-latex.pl icons.json fontawesome.sty
=head2 PARAMETERS
=head3 FA_ICON_METADATA_FILE
Font Awesome icon metadata JSON file.
This should be located in the Font Awesome archive:
advanced_options/metadata/icons.json
=head3 OUT_FILE
File to output the LaTeX definitions to.
If the file exists it will be overwritten.
=cut
use strict;
use warnings;
use feature qw( say );
use FileHandle;
use File::Basename qw( basename );
use File::Slurp qw( read_file );
use JSON;
use List::Util qw( uniq );
use Pod::Usage;
my ( $fa_metadata, $out_file ) = @ARGV;
_exit_error( 'Font Awesome icon metadata JSON file required' ) unless( defined $fa_metadata and -e $fa_metadata );
_exit_error( 'Destination file required' ) unless( defined $out_file and -e $out_file );
my $data = read_file( $fa_metadata ) or _exit_error( "Failed to read file: $!" );
my $json = JSON->new();
my $metadata = $json->decode( $data ) or _exit_error( "Unable to parse '$fa_metadata': $!" );
my $fh = FileHandle->new( $out_file, 'w' );
# Determine the Font-Awesome version
my @versions;
foreach my $icon ( keys %{ $metadata } )
{
my $changes = $metadata->{ $icon }->{ 'changes' };
@versions = ( @versions, @{ $changes } );
}
my @uniq_versions = sort { _cmp_version( $a, $b ) } uniq( @versions );
my $latest_version = $uniq_versions[-1];
# Insert header so that we know which Font Awesome version this is designed for
my $cmd_name = basename( $0 );
my $head_rule = '-' x 100;
say $fh "% $head_rule";
say $fh "% Generated by $cmd_name for Font Awesome version: $latest_version";
say $fh "% $head_rule";
# Generate LaTeX definitions for each Font-Awesome icon
foreach my $icon ( sort keys %{ $metadata } )
{
my $code = uc( $metadata->{ $icon }->{ 'unicode' } );
my $name = _numbers_to_words( $icon );
$name = _title_case( '-', $name );
my $line = '\def\fa' . $name . '{\symbol{"' . $code . '}}';
say $fh $line;
}
exit( 0 );
# _cmp_version
#
# Comparison function for sorting semantic version strings.
sub _cmp_version
{
my ( $val_a, $val_b ) = @_;
my ( $a_major, $a_minor, $a_patch ) = split( /\./, $val_a, 3 );
my ( $b_major, $b_minor, $b_patch ) = split( /\./, $val_b, 3 );
return(
( $a_major // 0 ) <=> ( $b_major // 0 ) or
( $a_minor // 0 ) <=> ( $b_minor // 0 ) or
( $a_patch // 0 ) <=> ( $b_patch // 0 )
);
}
# _exit_error
#
# Output error message and exit.
#
# Command usage will also be included.
sub _exit_error
{
my ( $message ) = @_;
pod2usage({
'-exitval' => 1,
'-message' => $message,
});
}
# _numbers_to_words
#
# Given a string, replaces any digits to words e.g. 'Num 11' => 'Num OneOne'.
#
# Returns translated string.
sub _numbers_to_words
{
my ( $string ) = @_;
my $number_map = {
'0' => 'Zero',
'1' => 'One',
'2' => 'Two',
'3' => 'Three',
'4' => 'Four',
'5' => 'Five',
'6' => 'Six',
'7' => 'Seven',
'8' => 'Eight',
'9' => 'Nine',
};
foreach my $num ( 0 .. 9 )
{
my $word = $number_map->{ $num };
$string =~ s/$num/$word/g;
}
return $string;
}
# _title_case
#
# Given a word separator and string, converts words to title case
# e.g. with a separator of '-', 'a-test' is converted to 'ATest'.
#
# Returns converted string.
sub _title_case
{
my ( $separator, $string ) = @_;
my @parts = split( qr/$separator/, $string );
@parts = map { ucfirst $_ } @parts;
$string = join( '', @parts );
return $string;
}