-
Notifications
You must be signed in to change notification settings - Fork 0
/
manopod2html
executable file
·320 lines (264 loc) · 9.08 KB
/
manopod2html
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
#!/usr/bin/env perl
# vim: se ft=perl:
################################################################################
# manopod2html: Prettify html generated from .pod file. #
# #
# Author: #
# Albert Mendes #
# #
# Description: #
# manopod2html uses Pod::Html to generate the html source. #
# It also inserts the manochrome Javascript and an initialization #
# script before the body closing tag. #
# pod2html inserts the manochrome CSS theme depending on the -t flag #
# if -f is not set, otherwise manopod2html inserts the css in a #
# <style> tag before </head>. #
# #
# Usage: #
# manopod2html -t <theme> [-i <input_file>] -o <output_file> [-f] #
# #
# -f build single file with CSS and Javascript included #
# #
# Available themes #
# #
# - light #
# - dark #
# - monk #
# #
################################################################################
use strict;
use warnings;
use Getopt::Std;
use File::Copy;
use Term::ANSIColor;
use Pod::Html;
use Data::Dumper;
my %opts;
getopts('ht:i:o:f', \%opts);
help(0) if $opts{h};
# Check if all necessary options were passed.
help(1) if ! $opts{t} || ! $opts{o};
my $package = "manochrome";
my $datadir = "/usr/local/share/$package";
my $config_file = "$ENV{HOME}/.manopod2html.rc";
my $config;
my $cssdir = "css";
my $jsdir = "js";
my $builddir = "./${package}_build";
my $local_css_dir = "${builddir}/css";
my $local_js_dir = "${builddir}/js";
my $js_src = "$datadir/$jsdir/$package.js";
my $init_js_file = "init.js";
my $init_js_src = "$datadir/$jsdir/$init_js_file";
my $css_src_prefix = "$datadir/$cssdir";
my %css_theme_src = (
"dark" => $css_src_prefix . "/${package}_dark.css",
"monk" => $css_src_prefix . "/${package}_monk.css",
"light" => $css_src_prefix . "/${package}_light.css"
);
my $html_edit_file;
my $temp_file = "manochrome.temp";
my $input_file = ($opts{i}) ? $opts{i} : $temp_file;
my $doctype = "<!doctype html>\n";
my $viewport = '<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />' . "\n";
# Read configuration.
if (-e $config_file) {
unless ($config = do $config_file) {
warn "Couldn't parse $config_file.\n";
}
}
if(!$opts{i}) {
print "Reading from STDIN ...\n";
open(TEMP, ">$temp_file") or die $!;
print TEMP $_ while (<>);
close(TEMP);
}
check_sources(\$js_src, \$init_js_src, \$css_src_prefix, \%css_theme_src);
my @options = (
"--infile=" . $input_file,
"--outfile=" . $opts{o},
);
if (!$opts{f}) {
$html_edit_file = "$builddir/$opts{o}";
# If output is not a single file
# put everything in $builddir.
# Create directories.
foreach my $d (
$builddir,
$local_css_dir,
$local_js_dir
) {
mkdir($d);
}
# Set options.
$options[1] = "--outfile=$builddir/$opts{o}";
push(@options, "--css=" . "css/${package}_$opts{t}.css");
# Copy files.
copy($css_theme_src{$opts{t}}, $local_css_dir);
copy($js_src, $local_js_dir);
}
else {
$html_edit_file = $opts{o};
open(CSS, "<$css_theme_src{$opts{t}}") or die "$!";
my @css = <CSS>;
close(CSS);
$css_theme_src{$opts{t}} = "<style>\n";
$css_theme_src{$opts{t}} .= $_ for (@css);
$css_theme_src{$opts{t}} .= "</style>\n";
}
my $script_src = (!$opts{f}) ? "<script src=\"js/$package.js\"></script>\n" : "";
if ($opts{f}) {
open(JS, "<$js_src") or die "$!";
my @js = <JS>;
close(JS);
$script_src = "<script>\n";
$script_src .= $_ for (@js);
$script_src .= "</script>\n";
}
my $init_manochrome = "<script>\n";
open(IM, "<$init_js_src");
my @im = <IM>;
close(IM);
$init_manochrome .= $_ for @im;
$init_manochrome .= "</script>\n";
# Generate html using pod2html.
pod2html(@options);
# Edit the generated html.
open(HTML, "<$html_edit_file") or die $!;
my @html = <HTML>;
close(HTML);
open(EDIT, ">$html_edit_file") or die "$!";
for (@html) {
if (/^<\/head>/) {
if ($opts{f}) {
print EDIT $css_theme_src{$opts{t}};
}
print EDIT $_ for (return_head_lines($config, "last"));
print EDIT $_ for (return_css_from_config($config));
}
if (/^<\/body>/) {
print EDIT $script_src;
if (scalar(return_js_from_config($config)) == 0) {
print EDIT $init_manochrome;
}
print EDIT for (return_js_from_config($config));
}
# Remove xml version and change doctype.
next if /^<\?xml version/;
if (/^<!DOCTYPE/) {
print EDIT $doctype;
next;
}
if (/^<html>/) {
print EDIT "<html>\n";
next;
}
if (/^<title/) {
print EDIT $_;
print EDIT $viewport;
# Insert lines like <meta ...>, <link ...>
print EDIT $_ for (return_head_lines($config, "first"));
next;
}
print EDIT $_;
}
close(EDIT);
unlink($temp_file) if -e $temp_file;
# Remove pod2htmd.tmp generated by pod2html
unlink("pod2htmd.tmp") if -e "pod2htmd.tmp";
################################################################################
# SUBROUTINES
sub return_head_lines {
my ($config, $position) = @_;
if ($position ne "first" && $position ne "last") {
warn "Position unknown.\n";
exit(1);
}
my $key = "head_lines_$position";
my @result = ();
if ($config->{$key}) {
for (@{$config->{$key}}) {
push @result, $_ . "\n";
}
}
return @result;
}
sub return_js_from_config {
my ($config) = @_;
my @result = ();
if ($config->{js_files}) {
for (@{$config->{js_files}}) {
if (!$opts{f}) {
push @result, '<script src="' .
$_ . '"' . "></script>\n"
} else {
push @result, "<script>\n";
open(TMP, "<$_") or die $!;
my @tmp = <TMP>;
close(TMP);
for my $l(@tmp) { push @result, $l; }
push @result, "</script>\n";
}
}
}
return @result;
}
sub return_css_from_config {
my ($config) = @_;
my @result = ();
if (@{$config->{css_files}}) {
for (@{$config->{css_files}}) {
if (!$opts{f}) {
push @result, '<link rel="stylesheet" href="' .
$_ . '">' . "\n";
} else {
push @result, "<style>\n";
open(TMP, "<$_") or die "Can't open $_, you probably need to change your root directory. $!";
my @tmp = <TMP>;
close(TMP);
for my $l(@tmp) { push @result, $l; }
push @result, "</style>\n";
}
}
}
return @result;
}
sub check_sources {
my ($js_src, $init_js_src, $css_src_prefix, $css_theme_src) = @_;
# /usr/local/share is prefered.
$datadir = (! -e $datadir) ? "." : $datadir;
${$js_src} = "$datadir/$jsdir/$package.js";
${$init_js_src} = "$datadir/$jsdir/$init_js_file";
${$css_src_prefix} = "$datadir/$cssdir";
# Make sure sources are correct in case $datadir changed.
foreach my $k (keys %{$css_theme_src}) {
$css_theme_src{$k} = ${$css_src_prefix} . "/${package}_$k.css";
}
foreach (
$datadir,
${$js_src},
${$css_src_prefix},
) {
if (! -e $_) {
print "File $_ does not exist.\n";
exit(1);
}
}
if (!defined ${$css_theme_src}{$opts{t}}) {
print "Theme $opts{t} does not exist.";
exit(1);
}
}
sub help {
my $exit_code = shift;
print "Usage: $0 -t <theme> [-i <input_file>] -o <output_file> [-f]\n\n";
print color('bold'); print "\t-f\t\t\t"; print color('reset');
print "build single .html file with CSS and Javascript included\n\n";
print color('bold');
print "\tAvailable themes\n";
print color('reset');
print "\t\t\t\t- light\n";
print "\t\t\t\t- dark\n";
print "\t\t\t\t- monk\n";
exit($exit_code);
}