-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.pl
142 lines (126 loc) · 3.79 KB
/
app.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env perl
use utf8;
use Mojolicious::Lite;
use Mojo::ByteStream qw(b);
use Path::Class qw(file);
use Text::Markdown qw( markdown );
use List::Util qw(min max);
app->secret(b(__FILE__)->sha1_sum);
helper parse_markdown => sub {
my ($self, $md) = @_;
$md =~ s!public/image/!image/!g;
my %opts = (
width => 1024,
height => 768,
max_column => 5,
);
my ($content, $min_x, $min_y, $max_x, $max_y);
my @sections;
my $SectionRe =
qr{(^#+.*?)((?=^#+)|\z)}ms;
while ($md =~ /$SectionRe/g) {
push @sections, $1;
}
$opts{max_column} = int( @sections ** (1/2) + 1 );
my $bored = 1;
my $x = 0;
my $y = 0;
my $current_column = 0;
$min_x = $max_x = 0;
$min_y = $max_y = 0;
for my $section (@sections) {
my %attrs;
$attrs{class} = 'step'; # default
while ($section =~ /^<!\-{2,}\s*([^\s]+)\s*\-{2,}>/gm) {
my $attr = $1;
if ($attr =~ /(.+)="?([^"]+)?"?/) {
$attrs{$1} = $attrs{$1} ? [$attrs{$1}, $2] : $2;
}
}
if (!defined $attrs{id} && $x == 0 && $y == 0) {
$attrs{id} = 'title'; # for first presentation
}
unless (defined $attrs{'data-x'}) {
$attrs{'data-x'} = $x;
$x += $opts{width};
}
unless (defined $attrs{'data-y'}) {
$attrs{'data-y'} = $y;
$current_column++;
if ($current_column >= $opts{max_column}) {
$x = 0;
$y += $opts{height};
$current_column = 0;
}
}
my $attrs = join ' ', map {
if (ref $attrs{$_} eq 'ARRAY')
{
sprintf '%s="%s"', $_, join ' ', @{$attrs{$_}};
}
else {
sprintf '%s="%s"', $_, $attrs{$_};
}
} keys %attrs;
my $markdown = markdown($section);
$markdown =~ s/<pre>/<pre class="lang-perl prettyprint linenums">/msg;
$content .= sprintf '<div %s>%s</div>', $attrs, $markdown;
$bored = undef;
$min_x = min($min_x, $x);
$min_y = min($min_y, $y);
$max_x = max($max_x, $x);
$max_y = max($max_y, $y);
}
return {
content => $content,
cx => ($min_x + $max_x) / 2,
cy => ($min_y + $max_y) / 2,
};
};
get '/' => sub {
my ($self) = @_;
my $pm = $self->parse_markdown(b(file($ARGV[0] || 'slide.md')->slurp)->decode);
$self->render(
template => 'index',
presentation => $pm->{content},
cx => $pm->{cx},
cy => $pm->{cy},
);
};
app->start;
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html lang="ja-JP">
<head>
<meta charset="<%= app->renderer->encoding %>">
<meta name="viewport" content="width=1024" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title><%= title %></title>
<link href="http://fonts.googleapis.com/css?family=Open+Sans:regular,semibold,italic,italicsemibold|PT+Sans:400,700,400italic,700italic|PT+Serif:400,700,400italic,700italic" rel="stylesheet" />
<%= stylesheet '/impress/css/impress-demo.css' %>
<%= stylesheet '/gcp/prettify.css' %>
<%= stylesheet '/css/app.css' %>
</head>
<body class="impress-not-supported">
<div class="fallback-message">
<p>Your browser <b>doesn't support the features required</b> by impress.js, so you are presented with a simplified version of this presentation.</p>
<p>For the best experience please use the latest <b>Chrome</b>, <b>Safari</b> or <b>Firefox</b> browser.</p>
</div>
<div id="impress">
<%== $presentation %>
<!-- <div id="overview" class="step" data-x="<%= $cx %>" data-y="<%= $cy %>" data-scale="15"></div> -->
</div>
<div class="hint">
<p>Use a spacebar or arrow keys to navigate</p>
</div>
<script>
if ("ontouchstart" in document.documentElement) {
document.querySelector(".hint").innerHTML = "<p>Tap on the left or right to navigate</p>";
}
</script>
<%= javascript '/impress/js/impress.js' %>
<%= javascript '/gcp/prettify.js' %>
<%= javascript '/js/app.js' %>
</body>
</html>