Swap out Text::Template for Text::Xslate #5

This commit is contained in:
gg 2023-09-04 03:24:51 -07:00
parent 3c1961031b
commit 4d0336ef5d
8 changed files with 55 additions and 98 deletions

View File

@ -1,4 +1,6 @@
use Text::Template;
use strict;
use warnings;
use Text::Xslate;
use Path::Tiny 'path';
# Return two closures to make SQL interaction easier.
# $sql is for SELECT queries.
@ -56,34 +58,24 @@ sub aupay_th {
"https://image.check.tv/lives/$id/vertical.jpg";
}
sub tlink {
my $title = shift;
my $escaped = $title; $escaped =~ s/ /+/;
return qq{<a href="segment/?n=$escaped">$title</a>};
}
sub clink {
my $caster = shift;
return $caster
? qq{<a href="caster/?n=$caster">$caster</a>}
: ""
}
## Text::Template
my $_dir = path(__FILE__)->parent;
my $_header = Text::Template->new(TYPE => 'FILE', SOURCE => "$_dir/tmpl/_header.html");
my $_footer = Text::Template->new(TYPE => 'FILE', SOURCE => "$_dir/tmpl/_footer.html");
# render a template from tmpl/$name and wrap it with header and footer
my $tx = Text::Xslate->new(
path => [ $_dir ],
header => [ "./xslate/_header.html" ],
footer => [ "./xslate/_footer.html" ],
function => {
yt => \&yt,
th => \&thumb,
aupay_vid => \&aupay_vid,
aupay_m3u8 => \&aupay_m3u8,
aupay_th => \&aupay_th,
}
);
sub page {
my $name = shift;
my $vars = shift;
my $main = Text::Template->new(TYPE => 'FILE', SOURCE => "$_dir/tmpl/$name.html");
return
$_header->fill_in(HASH => $vars, PACKAGE => 'main') .
$main->fill_in(HASH => $vars, PACKAGE => 'main') .
$_footer->fill_in(HASH => $vars, PACKAGE => 'main');
my $template = "./xslate/$name.html";
return $tx->render($template, $vars);
}
1;

View File

@ -1,26 +0,0 @@
<section class="container" id="schedule">
<table>
<thead>
<tr>
<th>title</th>
<th>caster</th>
<th>links</th>
<th>time</th>
</tr>
</thead>
<tbody>
{ $indent = ""; foreach (@r) {
if ($_->{video_id}) {
$yt = sprintf('<a href="%s">yt</a> | <a href="%s">th</a>',
yt($_->{video_id}),
thumb($_->{video_id})
);
} else {
$yt = "";
}
$OUT .= "$indent<tr><td>$_->{title}</td><td>$_->{caster}</td><td>$yt</td><td>$_->{jst}</td></tr>\n";
$indent = " ";
} }
</tbody>
</table>
</section>

View File

@ -1,37 +0,0 @@
<section class="container" id="schedule">
<table>
<thead>
<tr>
<th>title</th>
<th>caster</th>
<th>links</th>
<th>time</th>
</tr>
</thead>
<tbody>
{ $indent = ""; foreach (@r) {
if ($_->{video_id}) {
if ($_->{title} eq 'au PAY') {
$links = sprintf('<a href="%s">vid</a> | <a href="%s">m3u8</a> | <a href="%s">th</a>',
aupay_vid($_->{video_id}),
aupay_m3u8($_->{video_id}),
aupay_th($_->{video_id})
);
} else {
$links = sprintf('<a href="%s">yt</a> | <a href="%s">th</a>',
yt($_->{video_id}),
thumb($_->{video_id})
);
}
} else {
$links = "";
}
$t = tlink($_->{title});
$c = clink($_->{caster});
$mscale = $_->{mscale} ? "m$_->{mscale}" : "m";
$OUT .= "$indent<tr class=\"$mscale\"><td>$t</td><td>$c</td><td>$links</td><td>$_->{jst}</td></tr>\n";
$indent = " ";
} }
</tbody>
</table>
</section>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<base href="{$base}" />
<base href="<: $base :>" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
@ -9,7 +9,6 @@
content="This is an archive of the WeatherNews Live schedule."
/>
<title>WeatherNews</title>
<!--
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"
@ -18,7 +17,6 @@
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css"
/>
-->
<link rel="stylesheet" href="css/milligram.min.css" />
<!--
<link rel="stylesheet" href="https://milligram.io/styles/main.css" />
@ -27,6 +25,6 @@
<body>
<main class="wrapper">
<section class="container">
<h1><a href="{$base}">WeatherNews</a></h1>
<h1><a href="<: $base :>">WeatherNews</a></h1>
<h6>segment | caster | <a href="ranking/">ranking</a></h6>
</section>

7
www/xslate/_links.html Normal file
View File

@ -0,0 +1,7 @@
: if $i.video_id {
: if $i.title == "au PAY" {
<a href="<: aupay_vid($i.video_id) :>">vid</a> | <a href="<: aupay_m3u8($i.video_id) :>">m3u8</a> | <a href="<: aupay_th($i.video_id) :>">th</a><: "" -:>
: } else {
<a href="<: yt($i.video_id) :>">yt</a> | <a href="<: th($i.video_id) :>">th</a><: "" -:>
: }
: }

View File

@ -7,11 +7,12 @@
</tr>
</thead>
<tbody>
{ $indent = ""; foreach (@r) {
$c = clink($_->{caster});
$OUT .= "$indent<tr><td>$c</td><td class=\"numeric\">$_->{appearances}</td></tr>\n";
$indent = " ";
} }
: for $r -> $i {
<tr>
<td><a href="caster/?n=<: $i.caster | uri :>"><: $i.caster :></a></td>
<td><: $i.appearances :></td>
</tr>
: }
</tbody>
</table>
<div class="row">
@ -30,7 +31,7 @@
Later, when we have monthly breakdowns, you'll be able to see more fairly counted rankings.
</p>
<p>
<b>Date Range</b>: {$min_jst} &dash; {$max_jst}
<b>Date Range</b>: <: $min_jst :> - <: $max_jst :>
</p>
</div>
</div>

22
www/xslate/schedule.html Normal file
View File

@ -0,0 +1,22 @@
<section class="container" id="schedule">
<table>
<thead>
<tr>
<th>title</th>
<th>caster</th>
<th>links</th>
<th>time</th>
</tr>
</thead>
<tbody>
: for $r -> $i {
<tr>
<td class="segment"><a href="segment/?n=<: $i.title | uri :>"><: $i.title :></a></td>
<td class="caster"><a href="caster/?n=<: $i.caster | uri :>"><: $i.caster :></a></td>
<td class="links"><: include "xslate/_links.html" { i => $i } :></td>
<td class="datetime"><: $i.jst :></td>
</tr>
: }
</tbody>
</table>
</section>