| 1 | package GroupedSections; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use Mojo::Base 'Mojolicious'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | ▶ | sub startup { |
|
Subroutine
startup: called CC=1 SCAR=0.0
Pod
documented
| |||
| 9 | 2 | my $self = shift; | |
| 10 | |||
| 11 | 2 | $self->plugin('Buffer'); | |
| 12 | |||
| 13 | 2 | my $r = $self->routes; | |
| 14 | |||
| 15 | 2 | ▶ | $r->get('/' => sub { shift->redirect_to('/docs/intro') }); |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | # A static section: every page below this route is written by hand and | ||
| 18 | # never changes between requests, so the white space and the comments can | ||
| 19 | # go. One declaration covers the whole namespace. | ||
| 20 | 2 | my $docs = $r->under('/docs')->to(optimise_content => 1); | |
| 21 | 4 | ▶ | $docs->get('/intro' => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 22 | 4 | shift->render(template => 'docs/page', title => 'Introduction'); | |
| 23 | 2 | })->name('docs_intro'); | |
| 24 | 4 | ▶ | $docs->get('/install' => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 25 | 4 | shift->render(template => 'docs/page', title => 'Installation'); | |
| 26 | 2 | })->name('docs_install'); | |
| 27 | |||
| 28 | # These assets are built by the application rather than streamed from | ||
| 29 | # disk, so the plugin sees them. They are already minified and the | ||
| 30 | # reverse proxy in front of the app compresses them, so the application | ||
| 31 | # leaves the bytes alone: one cacheable copy per URL, not one per content | ||
| 32 | # coding. The declaration is in the controller, for the whole namespace. | ||
| 33 | 2 | $r->get('/assets/app.css')->to('assets#css'); | |
| 34 | 2 | $r->get('/assets/app.js')->to('assets#js'); | |
| 35 | |||
| 36 | 2 | return; | |
| 37 | } | ||
| 38 | |||
| 39 | package GroupedSections::Controller::Assets; | ||
| 40 | |||
| 41 | 2 | ▶ | use Mojo::Base 'Mojolicious::Controller'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | |||
| 43 | # Declared once for every action in this controller | ||
| 44 | 2 | has buffer_compress_content => 0; | |
| 45 | |||
| 46 | 2 | my $CSS = '.card{background:#fff;border:1px solid #ddd;padding:1rem}' | |
| 47 | . '.card h2{font-size:1rem;margin:0 0 .5rem}' | ||
| 48 | . '@media (prefers-color-scheme:dark){.card{background:#111;border-color:#333;color:#eee}}'; | ||
| 49 | |||
| 50 | 2 | my $JS = 'document.addEventListener("DOMContentLoaded",function(){' | |
| 51 | . 'document.querySelectorAll("[data-toggle]").forEach(function(el){' | ||
| 52 | . 'el.addEventListener("click",function(){' | ||
| 53 | . 'var target=document.getElementById(el.dataset.toggle);' | ||
| 54 | . 'if(target){target.hidden=!target.hidden}' | ||
| 55 | . '})})});'; | ||
| 56 | |||
| 57 | 2 | ▶ | sub css { |
|
Subroutine
css: called CC=1 SCAR=0.0
| |||
| 58 | 2 | my $c = shift; | |
| 59 | |||
| 60 | 2 | $c->res->headers->cache_control('public, max-age=86400'); | |
| 61 | |||
| 62 | 2 | return $c->render(data => $CSS, format => 'css'); | |
| 63 | } | ||
| 64 | |||
| 65 | 2 | ▶ | sub js { |
|
Subroutine
js: called CC=1 SCAR=0.0
| |||
| 66 | 2 | my $c = shift; | |
| 67 | |||
| 68 | 2 | $c->res->headers->cache_control('public, max-age=86400'); | |
| 69 | |||
| 70 | 2 | return $c->render(data => $JS, format => 'js'); | |
| 71 | } | ||
| 72 | |||
| 73 | 2 | 1; | |