#!/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 my $icons_per_line = 5; ## END CONFIG open my $links_file, '<', 'links.json'; my $links = decode_json(join('', <$links_file>)); close $links_file; open my $newtab, '>', 'newtab.html'; open my $header, '<', 'template/header.html' or die "couldn't open template/header.html"; print $newtab join('', <$header>); close $header; 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}) { 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}") { 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; if($icon_full_html =~ /
]*>(.*?)<\/svg/s) { my $icon = {viewBox => $1, paths => $2}; store($icon, "icon-cache/$link->{icon}") or die "couldn't save icon-cache/$link->{icon}"; } else { die "couldn't parse icon $link->{icon}"; } } my $icon = retrieve("icon-cache/$link->{icon}"); $link_html =~ s/\{\{viewBox\}\}/$icon->{viewBox}/g; $link_html =~ s/\{\{icon\}\}/$icon->{paths}/g; print $newtab $link_html; if(++$link_counter == $icons_per_line) { open my $line_separator, '<', 'template/link_line_separator.html' or die "couldn't open template/link_line_separator.html"; print $newtab join('', <$line_separator>); close $line_separator; $link_counter = 0; } } open my $footer, '<', 'template/footer.html' or die "couldn't open template/footer.html"; print $newtab join('', <$footer>); close $footer; close $newtab;