forked from SteffenBauer/PocketPuzzles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeinstructions.pl
executable file
·359 lines (333 loc) · 8.95 KB
/
makeinstructions.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
use IO::Compress::Zip qw($ZipError :constants);
# GLOBALS
# uuid generated with uuidgen on 2023-09-18
my $UUID = 'C06B058C-7B81-41C8-9CAD-2487C884B7BA';
# New creation date each time the script is run
my $DATE = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime time);
# How to name the eBook
my $TITLE = 'Pocket Puzzles';
# Who is the (main) author
my $AUTHOR = 'Steffen Bauer';
my $AUTHOR_SORT = 'Bauer, Steffen';
# Short description
my $DESCRIPTION = 'Rules for the Pocket Puzzles.';
# %games will be a map of game title to game rules
# Additionally the introduction will be in the map
my %games;
# Parse all the games
opendir(my $gamesdir, 'games');
while (my $filename = readdir($gamesdir)) {
# Ignore all non .c files
next unless $filename =~ /\.c$/;
open my $game, '<', "games/$filename";
my $raw_rules;
my $name;
# Get the name and the rules out of the source code
while (<$game>) {
# Found the raw rules
if ( s/^static const char rules\[\]\s*=\s*// .. /\x22;\s*$/) {
$raw_rules .= eval sprintf($_);
next;
}
# Found the name
if (/^(const |struct game )+thegame/) {
$_ = <$game>;
/^\s*"(.*?)"/;
$name = $1;
next;
}
}
# Just in case the name wasn't found…
if ($name eq "") {
print $filename,$/;
die 1;
}
# Convert the rules to html
my $paragraph;
my $rules;
foreach (split /(?<=\n)/, $raw_rules) {
if (/^$/) {
$rules .= para($paragraph);
$paragraph = '';
next;
}
$paragraph .= $_;
}
$rules .= para($paragraph);
$games{$name} = $rules;
close $game;
}
closedir($gamesdir);
# Convert the README to an introductory text in HTML
# NOTE: This is just a cheap parser not capable of all
# markdown possibilities.
open my $readme, '<', 'README.md';
my $intro;
my $paragraph;
my @images;
while (<$readme>) {
# Handle the code blocks
if (my $hit = /^```/ .. /^```\s*$/) {
if ($hit == 1) {
$intro .= para($paragraph);
$paragraph = '';
next;
}
if ($hit ne 0+$hit) {
$intro .= "<pre><code>\n$paragraph</code></pre>";
$paragraph = '';
next;
}
$paragraph .= $_;
next;
}
# `inline code`
s:`(.*?)`:<code>$1</code>:g; # NOTE! Cheaply done! Will interpret MD inside code!
# ''bold**
s:\*\*(.*?)\*\*:<b>$1</b>:g;
# *italics*
s:\*(.*?)\*:<i>$1</i>:g;
# [link](url)
s:\[(.*?)\]\((.*?)\):<a href="$2">$1</a>:g;
# <img…>
if (s#(?<=<img src=")https://raw.githubusercontent.com/SteffenBauer/PocketPuzzles/master/##g) { #"
push @images, $_ foreach (/<img src="(.*?)"/g);
s#<img[^>]+[^/]\K>#/>#g;
}
# Headlines
if (s:^(#+)\s*(.*?)\s*$:"<h".(length $1).">$2</h".(length $1).">":e or /^$/) {
$intro .= para($paragraph);
$paragraph = '';
$intro .= $_;
next;
}
$paragraph .= $_;
}
$intro .= para($paragraph);
# Define the playoreder/sequnce.
# Introduction first, then the puzzles in alphabetical order
my @content = ($TITLE, sort keys %games);
$games{$TITLE} = $intro;
# Write the mimetype file
my $z = IO::Compress::Zip->new( "build/$TITLE.epub",
AutoClose => 1,
Name => 'mimetype',
Method => ZIP_CM_STORE,
-Level => Z_NO_COMPRESSION,
) or die "zip failed: $ZipError\n";
$z->print('application/epub+zip');
$z->newStream(
Name => 'META-INF/container.xml',
Method => ZIP_CM_DEFLATE,
-Level => Z_BEST_COMPRESSION,
);
$z->print(container_xml());
$z->newStream(
Name => 'content.opf',
Method => ZIP_CM_DEFLATE,
-Level => Z_BEST_COMPRESSION,
);
$z->print(content_opf(\@content, \@images));
$z->newStream(
Name => 'content.ncx',
Method => ZIP_CM_DEFLATE,
-Level => Z_BEST_COMPRESSION,
);
$z->print(content_ncx(\@content));
$z->newStream(
Name => 'nav.xhtml',
Method => ZIP_CM_DEFLATE,
-Level => Z_BEST_COMPRESSION,
);
$z->print(nav_xhtml(\@content));
while (my($title, $text) = each %games) {
(my $fname = $title) =~ tr/ /_/;
$z->newStream(
Name => "$fname.xhtml",
Method => ZIP_CM_DEFLATE,
-Level => Z_BEST_COMPRESSION,
);
$z->print(chapter_xhtml($title, $text));
}
foreach (@images) {
open my $input, '<', $_;
$z->newStream(
Name => $_,
Method => ZIP_CM_STORE,
-Level => Z_NO_COMPRESSION,
);
$z->print(<$input>);
close $input
}
$z->close();
sub container_xml {
return <<CONTAINERXML;
<?xml version="1.0" encoding="UTF-8"?>
<container
xmlns="urn:oasis:names:tc:opendocument:xmlns:container"
version="1.0">
<rootfiles>
<rootfile
full-path="content.opf"
media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
CONTAINERXML
}
sub content_opf {
my ($games, $images) = @_;
my $items;
my $itemrefs;
my $sep = '';
foreach (@$games) {
(my $fname = $_) =~ tr/ /_/;
$items .= qq#$sep<item id="$fname" href="$fname.xhtml" media-type="application/xhtml+xml"/>#;
$itemrefs .= qq#$sep<itemref idref="$fname"/>#;
$sep = "\n ";
}
foreach (@$images) {
(my $fname = $_) =~ s#^.*/##;
$fname =~ tr/ ./__/;
my $mimetype = "application/text";
my $extension = lc($1) if /\.(.+)$/;
SWITCH: for ($extension) {
/^png$/ && do { $mimetype = "image/png"; last SWITCH; };
/^gif$/ && do { $mimetype = "image/gif"; last SWITCH; };
/^jpe?g$/ && do { $mimetype = "image/jpeg"; last SWITCH; };
}
$items .= qq#$sep<item id="$fname" href="$_" media-type="$mimetype"/>#;
$sep = "\n ";
}
return <<CONTENTOPF;
<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:opf="http://www.idpf.org/2007/opf"
xmlns="http://www.idpf.org/2007/opf"
unique-identifier="Id">
<metadata>
<dc:identifier id="Id">$UUID</dc:identifier>
<meta property="dcterms:modified">$DATE</meta>
<dc:language>en</dc:language>
<dc:title xml:lang="de">$TITLE</dc:title>
<dc:description xml:lang="de">$DESCRIPTION</dc:description>
<dc:creator id="author_pocket_puzzles" xml:lang="de">$AUTHOR</dc:creator>
<meta refines="#author_pocket_puzzles" property="file-as">$AUTHOR_SORT</meta>
<meta refines="#author_pocket_puzzles" property="role" scheme="marc:relators">aut</meta>
<meta property="identifier-type" refines="#Id">UUID</meta>
<meta property="dcterms:created">$DATE</meta>
<meta property="dcterms:issued">$DATE</meta>
</metadata>
<manifest>
<item id="ncx" href="content.ncx" media-type="application/x-dtbncx+xml"/>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
$items
</manifest>
<spine toc="ncx">
<itemref idref="nav"/>
$itemrefs
</spine>
</package>
CONTENTOPF
}
sub content_ncx {
my ($games) = @_;
my $navpoints;
my $playorder = 0;
foreach (@$games) {
(my $fname = $_) =~ tr/ /_/;
++$playorder;
$navpoints .= <<NAVPOINT;
<navPoint playOrder="$playorder" id="id_$playorder">
<navLabel>
<text>$_</text>
</navLabel>
<content src="$fname.xhtml"/>
</navPoint>
NAVPOINT
}
chomp($navpoints);
return<<CONTENTNCX;
<?xml version="1.0" encoding="UTF-8"?>
<ncx
xmlns="http://www.daisy.org/z3986/2005/ncx/"
version="2005-1"
xml:lang="de">
<head>
<meta name="dtb:uid" content="$UUID"/>
</head>
<docTitle>
<text>$TITLE</text>
</docTitle>
<docAuthor>
<text>$AUTHOR</text>
</docAuthor>
<navMap>
$navpoints
</navMap>
</ncx>
CONTENTNCX
}
sub nav_xhtml {
my ($games) = @_;
my $elements;
foreach (@$games) {
(my $fname = $_) =~ tr/ /_/;
$elements .= <<ELEMENT;
<li><a href="$fname.xhtml">$_</a></li>
ELEMENT
}
chomp($elements);
return <<NAVXHTML;
<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ops="http://www.idpf.org/2007/ops"
xml:lang="de">
<head>
<title>Table of content</title>
</head>
<body>
<nav ops:type="toc">
<h1>Table of content</h1>
<ol>
$elements
</ol>
</nav>
</body>
</html>
NAVXHTML
}
sub chapter_xhtml {
my ($title, $text) = @_;
return <<CONTENTXHTML;
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ops="http://www.idpf.org/2007/ops"
xml:lang="de">
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
$text
</body>
</html>
CONTENTXHTML
}
sub para {
local($_) = @_;
return '' unless $_;
if (/\A(^[-*].*$)+\Z/s) {
s:^[-*]\s*(.*?)\s*$:<li>$1</li>:gm;
return "<ul>\n$_\n</ul>\n";
}
if (/\A(^\d+\..*$)+\Z/s) {
s:^\d+\.\s*(.*?)\s*$:<li>$1</li>:gm;
return "<ol>\n$_\n</ol>\n";
}
return "<p>\n$_</p>\n";
}