| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | App::Phoebe::TokiPona - serve a linja pona via the web | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This extension adds rendering of Toki Pona glyphs to the web output of your | ||
| 24 | site. For this to work, you need to download the WOFF file from the Linja Pona | ||
| 25 | 4.2 repository and put it into your wiki directory. | ||
| 26 | |||
| 27 | L<https://github.com/janSame/linja-pona/> | ||
| 28 | |||
| 29 | No further configuration is necessary. Simply add it to your F<config> file: | ||
| 30 | |||
| 31 | use App::Phoebe::TokiPona; | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | package App::Phoebe::TokiPona; | ||
| 36 | use App::Phoebe::Web; | ||
| 37 | use App::Phoebe qw(@extensions $server $log); | ||
| 38 | use File::Slurper qw(read_binary); | ||
| 39 | use Modern::Perl; | ||
| 40 | |||
| 41 | push(@extensions, \&toki_pona_font); | ||
| 42 | |||
| 43 | sub toki_pona_font { | ||
| 44 | my $stream = shift; | ||
| 45 | my $request = shift; | ||
| 46 | if ($request =~ m!^GET /linja-pona-4.2.woff HTTP/1\.[01]$!) { | ||
| 47 | serve_font_via_http($stream); | ||
| 48 | return 1; | ||
| 49 | } | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub serve_font_via_http { | ||
| 54 | my $stream = shift; | ||
| 55 | $log->info("Serving font via HTTP"); | ||
| 56 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 57 | $stream->write("Content-Type: font/woff\r\n"); | ||
| 58 | $stream->write("Cache-Control: public, max-age=86400, immutable\r\n"); # 24h | ||
| 59 | $stream->write("\r\n"); | ||
| 60 | my $dir = $server->{wiki_dir}; | ||
| 61 | $stream->write(read_binary("$dir/linja-pona-4.2.woff")); | ||
| 62 | } | ||
| 63 | |||
| 64 | # CSS | ||
| 65 | |||
| 66 | no warnings qw(redefine); | ||
| 67 | *old_serve_css_via_http = \&App::Phoebe::Web::serve_css_via_http; | ||
| 68 | *App::Phoebe::Web::serve_css_via_http = \&serve_css_via_http; | ||
| 69 | |||
| 70 | sub serve_css_via_http { | ||
| 71 | my $stream = shift; | ||
| 72 | old_serve_css_via_http($stream); | ||
| 73 | $log->info("Adding more CSS via HTTP (for toki pona)"); | ||
| 74 | $stream->write(<<'EOT'); | ||
| 75 | @font-face { | ||
| 76 | font-family: linja-pona; | ||
| 77 | src: url('/linja-pona-4.2.woff') format('woff'); | ||
| 78 | font-weight: normal; | ||
| 79 | font-style: normal; | ||
| 80 | } | ||
| 81 | pre.toki { | ||
| 82 | font-family: linja-pona; | ||
| 83 | font-feature-settings: "liga" 1, "clig" 1, "calt" 1, "kern" 1, "mark" 1; | ||
| 84 | text-rendering: optimizeLegibility; | ||
| 85 | font-size: 150%; | ||
| 86 | } | ||
| 87 | EOT | ||
| 88 | } |