| 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 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::Phoebe::PageHeadings - use headings instead of file names | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This extension hides the page name from visitors, unless they start digging. | ||
| 26 | |||
| 27 | One the front page, where the last ten pages of your date pages are listed, the | ||
| 28 | name of the page is replaced with the level one heading of your page. | ||
| 29 | |||
| 30 | If you visit a page, the name of the page is similarly replaced with the level | ||
| 31 | one heading of your page. | ||
| 32 | |||
| 33 | There is no configuration. Simply add it to your F<config> file: | ||
| 34 | |||
| 35 | use App::Phoebe::PageHeadings; | ||
| 36 | |||
| 37 | Beware the consequences: | ||
| 38 | |||
| 39 | Every time somebody visits the main page, the main page itself is read, and the | ||
| 40 | ten blog pages are also read, in order to look for the headings to use; in some | ||
| 41 | high traffic situations, this could be problematic. | ||
| 42 | |||
| 43 | Every page needs to have a top level heading: the file name is no longer shown | ||
| 44 | to users. | ||
| 45 | |||
| 46 | Opening pages and looking for a top level heading doesn’t do regular parsing, | ||
| 47 | thus if your first top level heading is actually inside code fences (“```”) it | ||
| 48 | still gets used. | ||
| 49 | |||
| 50 | Beware the limitations: | ||
| 51 | |||
| 52 | The code doesn’t do the same for requests over the web. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | package App::Phoebe::PageHeadings; | ||
| 57 | use App::Phoebe qw(@extensions $server $log port host_regex space_regex success | ||
| 58 | blog_pages text print_link footer); | ||
| 59 | use Modern::Perl; | ||
| 60 | use List::Util qw(min); | ||
| 61 | use Encode qw(encode_utf8); | ||
| 62 | |||
| 63 | # Blogging where the first level headers in the page takes precedence over the | ||
| 64 | # filename. | ||
| 65 | |||
| 66 | push(@extensions, \&serve_minimal_main_menu); | ||
| 67 | |||
| 68 | # We want to serve a different main page if no page was specified. | ||
| 69 | sub serve_minimal_main_menu { | ||
| 70 | my $stream = shift; | ||
| 71 | my $url = shift; | ||
| 72 | my $hosts = host_regex(); | ||
| 73 | my $port = port($stream); | ||
| 74 | my $spaces = space_regex(); | ||
| 75 | my ($host, $space); | ||
| 76 | if (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/?$!) { | ||
| 77 | $log->info("Serving new main menu"); | ||
| 78 | success($stream); | ||
| 79 | my $page = $server->{wiki_main_page}; | ||
| 80 | if ($page) { | ||
| 81 | $stream->write(encode_utf8 text($stream, $host, $space, $page)); | ||
| 82 | } else { | ||
| 83 | $stream->write("# Welcome to Phoebe!\n"); | ||
| 84 | $stream->write("\n"); | ||
| 85 | } | ||
| 86 | blog_with_headers($stream, $host, $space, 10); | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | # The main page includes a blog with the ten most recent blog pages based on the | ||
| 93 | # filename. When linking to these pages, however, we want to display the name of | ||
| 94 | # their first header! | ||
| 95 | sub blog_with_headers { | ||
| 96 | my $stream = shift; | ||
| 97 | my $host = shift; | ||
| 98 | my $space = shift; | ||
| 99 | my $n = shift || 10; | ||
| 100 | my @blog = blog_pages($stream, $host, $space, $n); | ||
| 101 | return unless @blog; | ||
| 102 | $stream->write("Blog:\n"); | ||
| 103 | for my $id (@blog[0 .. min($#blog, $n - 1)]) { | ||
| 104 | my $text = encode_utf8 text($stream, $host, $space, $id); | ||
| 105 | next unless $text; # skipping empty pages | ||
| 106 | my ($title) = $text =~ /^# (.*)/m; | ||
| 107 | $title ||= "(untitled)"; | ||
| 108 | print_link($stream, $host, $space, $title, "page/$id"); | ||
| 109 | } | ||
| 110 | $stream->write("\n"); | ||
| 111 | } | ||
| 112 | |||
| 113 | # When serving a page, we don't want to use the filename as a first level | ||
| 114 | # heading. | ||
| 115 | no warnings 'redefine'; | ||
| 116 | *App::Phoebe::serve_page = \&serve_page; | ||
| 117 | |||
| 118 | sub serve_page { | ||
| 119 | my $stream = shift; | ||
| 120 | my $host = shift; | ||
| 121 | my $space = shift; | ||
| 122 | my $id = shift; | ||
| 123 | my $revision = shift; | ||
| 124 | $log->info("Serve Gemini page $id without a heading"); | ||
| 125 | success($stream); | ||
| 126 | $stream->write(encode_utf8 text($stream, $host, $space, $id, $revision)); | ||
| 127 | $stream->write(encode_utf8 footer($stream, $host, $space, $id, $revision)); | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; |