2022-11-23 12:00:14 -06:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
use utf8;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use open qw(:std :utf8);
|
|
|
|
use JSON qw(decode_json);
|
|
|
|
use LWP::UserAgent;
|
|
|
|
use Storable qw(store retrieve);
|
|
|
|
|
|
|
|
## CONFIG
|
2022-11-23 13:37:23 -06:00
|
|
|
my $icons_per_line = 6; # how many icons to put per line
|
|
|
|
my $debug_output = 0; # set to 1 to enable debug output
|
2022-11-23 12:00:14 -06:00
|
|
|
## END CONFIG
|
2022-11-23 13:37:23 -06:00
|
|
|
#
|
|
|
|
sub print_file($$) {
|
|
|
|
my $destination_handle = shift;
|
|
|
|
my $file_name = shift;
|
|
|
|
debug("Writing $file_name to file.");
|
|
|
|
open my $file, '<', $file_name
|
|
|
|
or die "couldn't open file $file_name";
|
|
|
|
my $content = join('', <$file>);
|
|
|
|
close $file;
|
|
|
|
print $destination_handle $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub debug($) {
|
|
|
|
my $msg = shift;
|
|
|
|
if($debug_output) {
|
|
|
|
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
|
|
|
|
my $timestamp = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
|
|
|
|
$year + 1900, $mon + 1, $mday, $hour, $min, $sec);
|
|
|
|
print "[$timestamp] $msg\n";
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 12:00:14 -06:00
|
|
|
|
2022-11-23 13:37:23 -06:00
|
|
|
debug('Reading links.json config file.');
|
2022-11-23 12:00:14 -06:00
|
|
|
open my $links_file, '<', 'links.json';
|
|
|
|
my $links = decode_json(join('', <$links_file>));
|
|
|
|
close $links_file;
|
|
|
|
|
2022-11-23 13:37:23 -06:00
|
|
|
debug('Opening newtab.html for writing.');
|
2022-11-23 12:00:14 -06:00
|
|
|
open my $newtab, '>', 'newtab.html';
|
2022-11-23 13:37:23 -06:00
|
|
|
print_file($newtab, 'template/header.html');
|
2022-11-23 12:00:14 -06:00
|
|
|
|
2022-11-23 13:37:23 -06:00
|
|
|
debug('Reading template/link.html.');
|
2022-11-23 12:00:14 -06:00
|
|
|
open my $link_template_file, '<', 'template/link.html'
|
|
|
|
or die "couldn't open template/link.html";
|
|
|
|
my $link_template = join('', <$link_template_file>);
|
|
|
|
close $link_template_file;
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new();
|
|
|
|
$ua->agent('Mozilla/5.0 (X11; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107.0');
|
|
|
|
|
|
|
|
my $link_counter = 0;
|
|
|
|
foreach my $link(@{$links}) {
|
2022-11-23 13:37:23 -06:00
|
|
|
if($link->{placeholder}) {
|
|
|
|
print_file($newtab, 'template/link_placeholder.html');
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Processing link $link->{title}.");
|
2022-11-23 12:00:14 -06:00
|
|
|
my $link_html = $link_template;
|
|
|
|
$link_html =~ s/\{\{title\}\}/$link->{title}/g;
|
|
|
|
$link_html =~ s/\{\{url\}\}/$link->{url}/g;
|
|
|
|
|
|
|
|
if(! -d 'icon-cache') {
|
|
|
|
mkdir 'icon-cache';
|
|
|
|
}
|
|
|
|
if (! -e "icon-cache/$link->{icon}") {
|
2022-11-23 13:37:23 -06:00
|
|
|
debug("Cache for icon $link->{icon} not found, fetching from web.");
|
2022-11-23 12:00:14 -06:00
|
|
|
my $icon_url = "https://icons.getbootstrap.com/icons/$link->{icon}/";
|
|
|
|
my $resp = $ua->get($icon_url);
|
|
|
|
if(!$resp->is_success) {
|
|
|
|
my $status = $resp->status_line;
|
|
|
|
die "couldn't fetch icon $icon_url\n$status";
|
|
|
|
}
|
|
|
|
my $icon_full_html = $resp->decoded_content;
|
2022-11-23 13:37:23 -06:00
|
|
|
debug("Icon $link->{icon} page fetched. Parsing html.");
|
2022-11-23 12:00:14 -06:00
|
|
|
if($icon_full_html =~ /<div class="icon-demo [^>]+>[^<]*<svg [^>]+viewBox="([^"]+)"[^>]*>(.*?)<\/svg/s) {
|
|
|
|
my $icon = {viewBox => $1, paths => $2};
|
2022-11-23 13:37:23 -06:00
|
|
|
debug("Saving $link->{icon} icon to cache.");
|
2022-11-23 12:00:14 -06:00
|
|
|
store($icon, "icon-cache/$link->{icon}")
|
|
|
|
or die "couldn't save icon-cache/$link->{icon}";
|
|
|
|
} else {
|
|
|
|
die "couldn't parse icon $link->{icon}";
|
|
|
|
}
|
2022-11-23 13:37:23 -06:00
|
|
|
} else {
|
|
|
|
debug("Icon $link->{icon} found in cache.");
|
2022-11-23 12:00:14 -06:00
|
|
|
}
|
|
|
|
my $icon = retrieve("icon-cache/$link->{icon}");
|
|
|
|
|
|
|
|
$link_html =~ s/\{\{viewBox\}\}/$icon->{viewBox}/g;
|
|
|
|
$link_html =~ s/\{\{icon\}\}/$icon->{paths}/g;
|
|
|
|
|
2022-11-23 13:37:23 -06:00
|
|
|
debug("Printing $link->{title} link to newtab.html.");
|
2022-11-23 12:00:14 -06:00
|
|
|
print $newtab $link_html;
|
|
|
|
|
|
|
|
if(++$link_counter == $icons_per_line) {
|
2022-11-23 13:37:23 -06:00
|
|
|
print_file($newtab, 'template/link_line_separator.html');
|
2022-11-23 12:00:14 -06:00
|
|
|
$link_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:37:23 -06:00
|
|
|
print_file($newtab, 'template/footer.html');
|
2022-11-23 12:00:14 -06:00
|
|
|
close $newtab;
|