diff --git a/generate.pl b/generate.pl index 1016eff..0dc7792 100755 --- a/generate.pl +++ b/generate.pl @@ -9,20 +9,41 @@ use LWP::UserAgent; use Storable qw(store retrieve); ## CONFIG -my $icons_per_line = 5; +my $icons_per_line = 6; # how many icons to put per line +my $debug_output = 0; # set to 1 to enable debug output ## END CONFIG +# +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"; + } +} + +debug('Reading links.json config file.'); open my $links_file, '<', 'links.json'; my $links = decode_json(join('', <$links_file>)); close $links_file; +debug('Opening newtab.html for writing.'); open my $newtab, '>', 'newtab.html'; +print_file($newtab, 'template/header.html'); -open my $header, '<', 'template/header.html' - or die "couldn't open template/header.html"; -print $newtab join('', <$header>); -close $header; - +debug('Reading template/link.html.'); open my $link_template_file, '<', 'template/link.html' or die "couldn't open template/link.html"; my $link_template = join('', <$link_template_file>); @@ -33,6 +54,12 @@ $ua->agent('Mozilla/5.0 (X11; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107 my $link_counter = 0; foreach my $link(@{$links}) { + if($link->{placeholder}) { + print_file($newtab, 'template/link_placeholder.html'); + next; + } + + debug("Processing link $link->{title}."); my $link_html = $link_template; $link_html =~ s/\{\{title\}\}/$link->{title}/g; $link_html =~ s/\{\{url\}\}/$link->{url}/g; @@ -41,6 +68,7 @@ foreach my $link(@{$links}) { mkdir 'icon-cache'; } if (! -e "icon-cache/$link->{icon}") { + debug("Cache for icon $link->{icon} not found, fetching from web."); my $icon_url = "https://icons.getbootstrap.com/icons/$link->{icon}/"; my $resp = $ua->get($icon_url); if(!$resp->is_success) { @@ -48,33 +76,31 @@ foreach my $link(@{$links}) { die "couldn't fetch icon $icon_url\n$status"; } my $icon_full_html = $resp->decoded_content; + debug("Icon $link->{icon} page fetched. Parsing html."); if($icon_full_html =~ /
]*>(.*?)<\/svg/s) { my $icon = {viewBox => $1, paths => $2}; + debug("Saving $link->{icon} icon to cache."); store($icon, "icon-cache/$link->{icon}") or die "couldn't save icon-cache/$link->{icon}"; } else { die "couldn't parse icon $link->{icon}"; } + } else { + debug("Icon $link->{icon} found in cache."); } my $icon = retrieve("icon-cache/$link->{icon}"); $link_html =~ s/\{\{viewBox\}\}/$icon->{viewBox}/g; $link_html =~ s/\{\{icon\}\}/$icon->{paths}/g; + debug("Printing $link->{title} link to newtab.html."); 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; + print_file($newtab, 'template/link_line_separator.html'); $link_counter = 0; } } -open my $footer, '<', 'template/footer.html' - or die "couldn't open template/footer.html"; -print $newtab join('', <$footer>); -close $footer; - +print_file($newtab, 'template/footer.html'); close $newtab; diff --git a/links.json.example b/links.json.example index 1df5dc0..b73d7f9 100644 --- a/links.json.example +++ b/links.json.example @@ -23,5 +23,6 @@ "title": "YouTube", "url": "https://www.youtube.com/feed/subscriptions", "icon": "youtube" - } + }, + { "placeholder": true } ] diff --git a/template/header.html b/template/header.html index b406f8d..44a2be9 100644 --- a/template/header.html +++ b/template/header.html @@ -28,6 +28,10 @@ li a:hover { color: #a6e3a1; } + li a.placeholder:hover { + color: #313244; + cursor: default; + } li a span { display: none; } diff --git a/template/link_placeholder.html b/template/link_placeholder.html new file mode 100644 index 0000000..f5b8c45 --- /dev/null +++ b/template/link_placeholder.html @@ -0,0 +1,8 @@ +
  • + + + + + + +