-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbookbuild.pl
More file actions
executable file
·136 lines (122 loc) · 4.09 KB
/
bookbuild.pl
File metadata and controls
executable file
·136 lines (122 loc) · 4.09 KB
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
#!/usr/bin/perl
use warnings;
use strict;
use utf8;
use XML::Parser;
use Tie::IxHash;
use File::Basename;
# take a Manifest file and construct the HTML data for a liturgical book
# use the cumd processor as a backend
my $input = $ARGV[0];
die "Manifest file not found" unless (-e $input);
############# BEGIN PARSING SUBS ###############
my %metadata = ();
tie my %chapters, "Tie::IxHash"; # a hash for storing the chapters in sequential order
sub default {
return;
}
sub text {
return;
}
sub startElement {
my( $parseinst, $element, %attrs ) = @_;
SWITCH: {
if ($element eq "meta") {
# this is a meta information tag
# push its contents into the meta hash
$metadata{$attrs{name}} = $attrs{content};
last SWITCH;
}
if ($element eq "chapter") {
# this gives us the chapter name of the appropriate file
$chapters{$attrs{file}} = $attrs{name};
last SWITCH;
}
};
}
sub endElement {
my ($parseinst, $elem) = @_;
return;
}
####### END OF PARSING SUBS #####################
my $outputdir = "/home/sasha/Documents/design/ponomar/maktabah";
my $parser = new XML::Parser(ErrorContext => 2);
$parser->setHandlers( Start => \&startElement,
End => \&endElement,
Char => \&text,
Default => \&default);
$parser->parsefile($input);
# set up parameters
my $title = $metadata{title};
my $path = $metadata{latin} . $metadata{year};
my($filename, $dirs, $suffix) = fileparse($input);
mkdir "$outputdir/$path" || die "Output directory $path already exists!";
# create Index file
open (OUTPUT, ">:encoding(UTF-8)", "$outputdir/$path/index.html") || die "Cannot write to output file: $!";
print OUTPUT qq(<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://www.ponomar.net/css/book.css">
</head>
<body>
);
print OUTPUT qq(<p text_align="center">$title</p>\n);
print OUTPUT qq(<p text_align="center">$metadata{publisher}, $metadata{year}</p>\n);
print OUTPUT qq(<p text_align="center">Contents</p>\n);
print OUTPUT qq(<ul>\n);
foreach my $chapter (keys %chapters) {
print OUTPUT qq(<li><a href="$chapter.html" name="$chapter">$chapters{$chapter}</a></li>\n);
}
print OUTPUT qq(
</ul>
</body>
</html>);
close (OUTPUT);
my $j = 0;
foreach my $chapter (keys %chapters) {
system ("cumd -e footnotes $dirs/chapters/$chapter.md toomp.html");
# now create the HTML file
open (OUTPUT, ">:encoding(UTF-8)", "$outputdir/$path/$chapter.html") || die "Cannot write to output file: $!";
print OUTPUT qq(<!DOCTYPE html>
<html>
<head>
<title>$chapters{$chapter}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://sci.ponomar.net/css/fonts.css">
<link rel="stylesheet" href="https://www.ponomar.net/css/book.css">
</head>
<body>
);
# navigation panel
print OUTPUT qq(
<div class="nav"><table align="center"><tr>);
print OUTPUT $j == 0 ? qq(<td><< Previous</td>) : qq(<td><a href=") . (keys(%chapters))[$j - 1] . qq(.html"><< Previous</a></td>);
print OUTPUT qq(<td><a href="index.html#$chapter">Contents</a></td>);
print OUTPUT $j == scalar keys(%chapters) ? qq(<td>Next >></td>) : qq(<td><a href=") . (keys(%chapters))[$j + 1] . qq(.html">Next >></a></td>);
print OUTPUT qq(</tr></table></div>\n);
print OUTPUT qq(<cu>\n);
# here goes the file
open (TOOMP, "<:encoding(UTF-8)", "toomp.html") || die "Cannot read from toomp: $!";
while (<TOOMP>) {
print OUTPUT $_;
}
close (TOOMP);
unlink "toomp.html";
# bottom navigation panel
print OUTPUT qq(
</cu>
<div class="nav"><table align="center"><tr>);
print OUTPUT $j == 0 ? qq(<td><< Previous</td>) : qq(<td><a href=") . (keys(%chapters))[$j - 1] . qq(.html"><< Previous</a></td>);
print OUTPUT qq(<td><a href="index.html#$chapter">Contents</a></td>);
print OUTPUT $j == scalar keys(%chapters) ? qq(<td>Next >></td>) : qq(<td><a href=") . (keys(%chapters))[$j + 1] . qq(.html">Next >></a></td>);
print OUTPUT qq(</tr></table></div>\n);
print OUTPUT qq(
</body>
</html>);
close (OUTPUT);
$j++;
}