Browse Source

HTML sitemap, XML to come soon

George S. Baugh 5 years ago
parent
commit
d20dd4b2bc
3 changed files with 117 additions and 7 deletions
  1. 11 7
      lib/Trog/Data/DUMMY.pm
  2. 73 0
      lib/Trog/Routes/HTML.pm
  3. 33 0
      www/templates/sitemap.tx

+ 11 - 7
lib/Trog/Data/DUMMY.pm

@@ -41,7 +41,7 @@ my $example_posts = [
         title        => 'Example Post',
         user         => 'Nobody',
         id           => 665,
-        tags         => ['news'],
+        tags         => ['news', 'public'],
         created      => time(),
         version      => 0,
     },
@@ -53,7 +53,7 @@ my $example_posts = [
         title        => 'Muh Blog',
         user         => 'Nobody',
         id           => 666,
-        tags         => ['blog'],
+        tags         => ['blog', 'public'],
         created      => time(),
         version      => 0,
     },
@@ -65,7 +65,7 @@ my $example_posts = [
         title        => 'Nobody',
         user         => 'Nobody',
         id           => 669,
-        tags         => ['about', 'profile'],
+        tags         => ['about', 'profile', 'public'],
         created      => time(),
         version      => 0,
     },
@@ -77,7 +77,7 @@ my $example_posts = [
         title        => "humm.gif",
         user         => 'Nobody',
         id           => 420,
-        tags         => ['image', 'files', 'profile-image'],
+        tags         => ['image', 'files', 'profile-image', 'public'],
         created      => time(),
         version      => 0,
         preview      => '/img/avatar/humm.gif',
@@ -90,7 +90,7 @@ my $example_posts = [
         title        => "testpattern.jpg",
         user         => 'Nobody',
         id           => 90210,
-        tags         => ['image', 'files'],
+        tags         => ['image', 'files', 'public'],
         created      => time(),
         version      => 0,
         preview      => '/img/sys/testpattern.jpg',
@@ -103,7 +103,7 @@ my $example_posts = [
         title        => "test.mp3",
         user         => "Nobody",
         id           => 111,
-        tags         => ["audio", "files"],
+        tags         => ["audio", "files", 'public'],
         created      => time(),
         version      => 0,
         preview      => '/img/sys/testpattern.jpg',
@@ -116,7 +116,7 @@ my $example_posts = [
         title        => "test.ogv",
         user         => "Nobody",
         id           => "222",
-        tags         => ["video", "files"],
+        tags         => ["video", "files", 'public'],
         created      => time(),
         version      => 0,
         preview      => '/img/sys/testpattern.jpg',
@@ -166,6 +166,10 @@ sub get ($self, %request) {
     return \@filtered;
 }
 
+sub total_posts {
+    return scalar(@$example_posts);
+}
+
 sub _add_post_type (@posts) {
     return map {
         my $post = $_;

+ 73 - 0
lib/Trog/Routes/HTML.pm

@@ -88,6 +88,14 @@ our %routes = (
         auth     => 1,
         callback => \&Trog::Routes::HTML::themeclone,
     },
+    '/series', => {
+        method   => 'GET',
+        callback => \&Trog::Routes::HTML::series,
+    },
+    '/sitemap', => {
+        method   => 'GET',
+        callback => \&Trog::Routes::HTML::sitemap,
+    },
 );
 
 # Build aliases for /post with extra data
@@ -454,6 +462,71 @@ sub _post_helper ($query, $tags) {
     );
 }
 
+#TODO actually do stuff
+sub series ($query, $input, $render_cb) {
+    return Trog::Routes::HTML::index($query,$input,$render_cb);
+}
+
+=head2 sitemap
+
+Return the sitemap index unless the static or a set of dynamic routes is requested.
+We have a maximum of 99,990,000 posts we can make under this model
+As we have 10,000 * 10,000 posts which are indexable via the sitemap format.
+1 top level index slot (10k posts) is taken by our static routes, the rest will be /posts.
+
+=cut
+
+sub sitemap ($query, $input, $render_cb) {
+
+    my (@to_map, $is_index, $route_type);
+    my $warning = '';
+    $query->{map} //= '';
+    if ($query->{map} eq 'static') {
+        # Return the map of static routes
+        $route_type = 'Static Routes';
+        @to_map = grep { !defined $routes{$_}->{captures} && $_ ne 'default' } keys(%routes);
+    } elsif ( !$query->{map} ) {
+        # Return the index instead
+        @to_map = ('static');
+        my $data = Trog::Data->new($conf);
+        my $tot = $data->total_posts();
+        my $size = 50000;
+        my $pages = int($tot / $size) + (($tot % $size) ? 1 : 0);
+
+        # Truncate pages at 10k due to standard
+        my $clamped = $pages > 49999 ? 49999 : $pages;
+        $warning = "More posts than possible to represent in sitemaps & index!  Old posts have been truncated." if $pages > 49999;
+
+        foreach my $page ($clamped..1) {
+            push(@to_map, "$page");
+        }
+        $is_index = 1;
+    } else {
+        $route_type = "Posts: Page $query->{map}";
+        # Return the map of the particular range of dynamic posts
+        $query->{limit} = 50000;
+        $query->{page} = $query->{map};
+        @to_map = @{_post_helper($query, ['public'])};
+    }
+
+    @to_map = sort @to_map unless $is_index;
+    my $processor = Text::Xslate->new(
+        path   => _dir_for_resource('sitemap.tx'),
+    );
+
+    my $styles = _build_themed_styles('sitemap.css');
+
+    my $content = $processor->render('sitemap.tx', {
+        title      => "Site Map",
+        to_map     => \@to_map,
+        is_index   => $is_index,
+        route_type => $route_type,
+        route      => $query->{route},
+    });
+
+    return Trog::Routes::HTML::index($query,$input,$render_cb,$content,$styles);
+}
+
 sub _rss ($posts) {
     return [200, ["Content-type: text/plain\n"], ["TODO"]];
 }

+ 33 - 0
www/templates/sitemap.tx

@@ -0,0 +1,33 @@
+: if ( $is_index ) {
+    <h3>Sitemap Index</h3>
+    <p>
+    Each page of posts is at maximum 50,000 entries due to the <a href="https://www.sitemaps.org/faq.html#faq_sitemap_size">limitations of the sitemap format</a>.
+    This means this map will only show the most recent 2,499,950 posts.
+    </p>
+    : if ( $warning ) {
+        <: $warning :><br /><br />
+    : }
+    : for $to_map -> $map {
+        <a href="?map=<: $map :>">
+        : if ( $map != 'static' ) {
+        Posts Page
+        : }
+        <: $map :>
+        </a><br />
+    : }
+: } else {
+    <h3>Sitemap of <: $route_type :></h3>
+    <p>
+        <a href="/sitemap">Back</a>
+    </p>
+    : for $to_map -> $map {
+        : if ( $route_type == 'static' ) {
+            <a href="<: $map :>">
+            <: $map :>
+        : } else {
+            <a href="/posts/<: $map.id :>">
+            <: $map.title :>
+        : }
+        </a><br />
+    : }
+: }