Skip to content

Instantly share code, notes, and snippets.

@remorse
Created October 12, 2016 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remorse/896cb98f070b451c9a001dcdd9b6674d to your computer and use it in GitHub Desktop.
Save remorse/896cb98f070b451c9a001dcdd9b6674d to your computer and use it in GitHub Desktop.
Create a printable nice version of XKCD comics
#!/usr/bin/env perl6
use v6;
use LWP::Simple;
use JSON::Tiny;
use Template::Anti;
my $outdir = '/Users/rem16/Pictures/comics/';
my $comic = '1053';
# get the JSON info
my $info;
{
my $url = 'https://www.xkcd.com/' ~ $comic ~ '/info.0.json';
my $lwp = LWP::Simple.new();
my $json = $lwp.get($url);
$info = from-json($json);
}
# replace the placeholders in the HTML
my $output_text;
{
my $tmpl = Template::Anti.load($=finish);
$tmpl('title').text($info<safe_title>);
$tmpl('body h1').text($info<title>);
$tmpl('body figure img').attrib(src => $info<img>);
$tmpl('body figure figcaption').text($info<alt>);
$output_text = $tmpl.render();
}
# write to a file
{
my $fname = $info<safe_title>.lc.subst(/\W/, '_', :g);
spurt $outdir ~ $fname ~ '.html', $output_text;
}
=finish
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>XXX</title>
<style type="text/css">
h1 {
text-align: center;
font-family: "Skia";
}
figure {
margin: 0 auto;
text-align: center;
}
figure img {
text-align: center;
padding-bottom: 1rem;
}
figure figcaption {
text-align: left;
font-style: italic;
font-family: "Palatino";
font-size: 0.8rem;
width: 400px;
margin: auto;
}
</style>
</head>
<body>
<h1>XXX</h1>
<figure>
<img src="XXX" />
<figcaption>XXX</figcaption>
</figure>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment