#!/usr/bin/env perl
use Mojolicious::Lite;

use if -e 'lib/App/remarkpl', qw(lib lib);
use App::remarkpl;
use Mojo::File qw(path);

# Read in the presentation when running the script from command line,
# show the manual or an error message in case of an invalid file.
my $presentation = shift @ARGV || $ENV{REMARK_PRESENTATION} || 'example.markdown';
unless ($ENV{MOJO_APP_LOADER}) {
  exec perldoc => -tT => $INC{'App/remarkpl.pm'} if $presentation =~ m!^-*(h|help|man)$!;
  die qq(Cannot read presentation file "$presentation".\n)
    unless $presentation eq 'example.markdown' or -r $presentation;
  unshift @ARGV, 'daemon';
}

$presentation = path $presentation;

# Include static assets and templates
app->renderer->paths(
  [split /:/, ($ENV{REMARK_TEMPLATES} // $presentation->sibling('templates')->to_abs)]);
app->static->paths([split /:/, ($ENV{REMARK_STATIC} // $presentation->dirname)]);
push @{app->static->paths},
  path(path($INC{'App/remarkpl.pm'})->dirname, qw(remarkpl public))->to_abs;

# Custom switch to show example presentation file
exit(print(read_presentation()) ? 0 : $!) if grep {/^--print/} @ARGV;

app->log->info(qq(Looking for static files in ) . join ', ', @{app->static->paths});
app->log->info(qq(Looking for templates in ) . join ', ',    @{app->renderer->paths});
app->log->info(qq(Serving presentation "$presentation"));

get '/' => sub { shift->render(template => 'remarkpl', presentation => read_presentation()) };

app->defaults(remarkjs => $ENV{REMARK_JS} || '/remark.min.js');
app->defaults(title    => $presentation->basename);
app->start;

sub read_presentation {
  Mojo::Util::decode('UTF-8',
    -e $presentation
    ? $presentation->slurp
    : Mojo::Loader::data_section(__PACKAGE__, $presentation));
}

__DATA__
@@ remarkpl.html.ep
<!DOCTYPE html>
<html>
<head>
  <title><%= title %> - remarkpl</title>
  <meta charset="utf-8">
  %= stylesheet '/fonts.css'
  %= stylesheet '/basic.css'
  %= stylesheet '/custom.css'
</head>
<body>
  <textarea id="source"><%= $presentation %></textarea>
  %= javascript $remarkjs
  %= javascript '/custom.js'
  <script>const slideshow = remark.create();</script>
</body>
</html>
@@ example.markdown
class: center, middle, inverse

# remark presentation example

"class" can be center, middle, bottom, left, center, right, inverse.

---

# Agenda

1. Introduction
2. Deep-dive

<img src="example.jpg" class="h400">

---

# Code

```perl
my $foo = "Code formatting";
```

```
.left[Left-aligned text]
.center[Centered text]
.right[Right-aligned text]
```

---
class: contrast, inverse
background-image: url(example.jpg)

# Lists

- Item 1

- Item 2

  With a paragraph

---
class: center, middle
# The end

[jhthorsen@cpan.org](mailto:jhthorsen@cpan.org)
