| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::GoogleFontProxy; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # ABSTRACT: a small proxy that can be useful when you embed gists in your website | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1482 | use Mojo::Base 'Mojolicious::Plugin'; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 16 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $CSS_URL_FORMAT    = 'https://fonts.googleapis.com/css?family=%s'; | 
| 10 |  |  |  |  |  |  | our $FONT_URL_FORMAT   = 'https://fonts.gstatic.com/s/%s'; | 
| 11 |  |  |  |  |  |  | our $USER_AGENT_STRING = ''; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub register { | 
| 14 | 2 |  |  | 2 | 1 | 82 | my ($self, $app, $config) = @_; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 2 | 50 |  |  |  | 11 | if ( !$config->{no_types} ) { | 
| 17 | 2 |  |  |  |  | 14 | $app->types->type( 'woff2' => 'font/woff2' ); | 
| 18 | 2 |  |  |  |  | 143 | $app->types->type( 'woff'  => 'font/woff' ); | 
| 19 | 2 |  |  |  |  | 31 | $app->types->type( 'ttf'   => 'font/ttf' ); | 
| 20 | 2 |  |  |  |  | 27 | $app->types->type( 'otf'   => 'font/opentype' ); | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | $app->hook( | 
| 24 |  |  |  |  |  |  | after_render => sub { | 
| 25 | 8 |  |  | 8 |  | 79390 | my ($c, $content, $format) = @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 8 | 50 |  |  |  | 37 | return if !$format; | 
| 28 | 8 | 100 | 100 |  |  | 64 | return if $format ne 'html' && $format ne 'css'; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 5 |  |  |  |  | 36 | $$content =~ s{ | 
| 31 |  |  |  |  |  |  | https://fonts.googleapis.com/css\?family=(.*?)['"] | 
| 32 | 2 |  |  |  |  | 11 | }{$c->url_for( 'google-proxy-css', file => $1 ) . '"';}xge; | 
| 33 |  |  |  |  |  |  | } | 
| 34 | 2 |  |  |  |  | 41 | ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | $app->routes->get( '/google/css/*file' )->to( cb => sub { | 
| 37 | 2 |  |  | 2 |  | 21479 | my $c = shift; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 2 |  |  |  |  | 10 | $c->render_later; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 2 |  |  |  |  | 62 | my $url = sprintf $CSS_URL_FORMAT, $c->param('file'); | 
| 42 | 2 |  | 33 |  |  | 126 | my $ua_string = $USER_AGENT_STRING || $c->tx->req->headers->user_agent; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | $c->ua->get( $url => { 'User-Agent' => $ua_string } => sub { | 
| 45 | 2 |  |  |  |  | 6060 | my ($ua, $tx) = @_; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 2 |  |  |  |  | 10 | my $body = $tx->res->body; | 
| 48 | 2 |  |  |  |  | 58 | $body    =~ s{ | 
| 49 |  |  |  |  |  |  | https?://fonts\.gstatic\.com/s/(.*?)\) \s* format\('(.*?)'\) | 
| 50 | 8 |  |  |  |  | 5354 | }{$c->url_for('google-proxy-font', file => $1, fformat => $2) . ") format('$2')"}xmseg; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 2 |  |  |  |  | 755 | return $c->render( data => $body, format => 'css' ); | 
| 53 | 2 |  |  |  |  | 26 | }); | 
| 54 | 2 |  |  |  |  | 47 | })->name( 'google-proxy-css' ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | $app->routes->get( '/google/font/:fformat/*file' )->to( cb => sub { | 
| 57 | 1 |  |  | 1 |  | 9342 | my $c = shift; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 1 |  |  |  |  | 5 | $c->render_later; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 1 |  |  |  |  | 22 | my $url = sprintf $FONT_URL_FORMAT, $c->param('file'); | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | $c->ua->get( $url => sub { | 
| 64 | 1 |  |  |  |  | 2632 | my ($ua, $tx) = @_; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 1 |  |  |  |  | 18 | my $body = $tx->res->body; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 1 |  |  |  |  | 26 | return $c->render( data => $body, format => $c->param('fformat') ); | 
| 69 | 1 |  |  |  |  | 36 | }); | 
| 70 | 2 |  |  |  |  | 1110 | })->name( 'google-proxy-font' ); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | 1; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | __END__ |