forked from optionalg/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_docs.pl
More file actions
executable file
·316 lines (255 loc) · 8.29 KB
/
build_docs.pl
File metadata and controls
executable file
·316 lines (255 loc) · 8.29 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
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
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
use FindBin;
use lib "$FindBin::RealBin/lib";
use ES::Util
qw(run $Opts build_chunked build_single sha_for timestamp write_html_redirect);
use Getopt::Long;
use YAML qw(LoadFile);
use Path::Class qw(dir file);
use Browser::Open qw(open_browser);
use ES::Repo();
use ES::Book();
use ES::Toc();
use ES::LinkCheck();
use Proc::PID::File;
die "$0 already running\n"
if Proc::PID::File->running( dir => $FindBin::RealBin . '/.run' );
our $Old_Pwd = dir()->absolute;
init_env();
our $Conf = LoadFile('conf.yaml');
GetOptions(
$Opts, #
'all', 'push', #
'single', 'doc=s', 'out=s', 'toc', 'chunk=i', 'toc_level=i', 'comments',
'open', 'web',
'lenient', 'verbose'
);
$Opts->{doc} ? build_local( $Opts->{doc} )
: $Opts->{all} ? build_all()
: usage();
#===================================
sub build_local {
#===================================
my $doc = shift;
my $index = file($doc)->absolute($Old_Pwd);
die "File <$doc> doesn't exist" unless -f $index;
say "Building HTML from $doc";
my $dir = dir( $Opts->{out} || 'html_docs' )->absolute($Old_Pwd);
my $html;
if ( $Opts->{single} ) {
$dir->rmtree;
$dir->mkpath;
build_single( $index, $dir, %$Opts );
$html = $index->basename;
$html =~ s/\.[^.]+$/.html/;
}
else {
build_chunked( $index, $dir, %$Opts );
$html = 'index.html';
}
$html = $dir->file($html);
say "Done";
if ( $Opts->{web} ) {
if ( my $pid = fork ) {
# parent
$SIG{CHLD} = sub {
kill -9, $pid;
};
if ( $Opts->{open} ) {
sleep 1;
open_browser( 'http://localhost:8000/' . $html->basename );
}
wait;
print "\nExiting\n";
exit;
}
else {
my $http = dir( 'resources', 'http.py' )->absolute;
close STDIN;
open( STDIN, "</dev/null" );
chdir $dir;
exec( $http '8000' );
}
}
elsif ( $Opts->{open} ) {
say "Opening: $html";
open_browser($html);
}
else {
say "See: $html";
}
}
#===================================
sub build_all {
#===================================
init_repos();
my $build_dir = $Conf->{paths}{build}
or die "Missing <paths.build> in config";
$build_dir = dir($build_dir);
$build_dir->mkpath;
my $contents = $Conf->{contents}
or die "Missing <contents> configuration section";
my $toc = ES::Toc->new( $Conf->{contents_title} || 'Guide' );
build_entries( $build_dir, $toc, @$contents );
say "Writing main TOC";
$toc->write($build_dir);
say "Writing extra HTML redirects";
for ( @{ $Conf->{redirects} } ) {
write_html_redirect( $build_dir->subdir( $_->{prefix} ),
$_->{redirect} );
}
my $links = ES::LinkCheck->new($build_dir);
for ( @{ $Conf->{extra_links} } ) {
my $repo = ES::Repo->get_repo( $_->{repo} );
my $file = $repo->dir->file( $_->{file} );
say "Checking links in: $file";
$links->check_file($file);
}
if ( $links->check ) {
say $links->report;
}
else {
die $links->report;
}
push_changes($build_dir)
if $Opts->{push};
}
#===================================
sub build_entries {
#===================================
my ( $build, $toc, @entries ) = @_;
while ( my $entry = shift @entries ) {
my $title = $entry->{title}
or die "Missing title for entry: " . Dumper($entry);
if ( my $sections = $entry->{sections} ) {
my $section_toc = ES::Toc->new($title);
$toc->add_entry($section_toc);
build_entries( $build, $section_toc, @$sections );
next;
}
my $book = ES::Book->new( dir => $build, %$entry );
$toc->add_entry( $book->build );
}
return $toc;
}
#===================================
sub build_sitemap {
#===================================
my ($dir) = @_;
my $sitemap = $dir->file('sitemap.xml');
say "Building sitemap: $sitemap";
my $date = timestamp();
open my $fh, '>', $sitemap or die "Couldn't create $sitemap: $!";
say $fh <<SITEMAP_START;
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="http://www.elasticsearch.org/main-sitemap.xsl"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
SITEMAP_START
$dir->recurse(
callback => sub {
my $item = shift;
if ( $item->is_dir ) {
return $item->PRUNE
if $item->basename eq 'images';
return;
}
return unless $item->basename =~ /\.html$/;
return $item->PRUNE unless $item->parent->basename eq 'current';
my $url
= 'http://www.elasticsearch.org/guide/' . $item->relative($dir);
say $fh <<ENTRY;
<url>
<loc>$url</loc>
<lastmod>$date</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
ENTRY
}
);
say $fh "</urlset>";
close $fh or die "Couldn't close $sitemap: $!"
}
#===================================
sub init_repos {
#===================================
say "Updating repositories";
my $repos_dir = $Conf->{paths}{repos}
or die "Missing <paths.repos> in config";
$repos_dir = dir($repos_dir);
$repos_dir->mkpath;
my $conf = $Conf->{repos}
or die "Missing <repos> in config";
for my $name ( sort keys %$conf ) {
my $repo = ES::Repo->new(
name => $name,
dir => $repos_dir,
%{ $conf->{$name} }
);
$repo->update_from_remote();
}
}
#===================================
sub push_changes {
#===================================
my $build_dir = shift;
run qw( git add -A), $build_dir;
if ( run qw(git status -s -- ), $build_dir ) {
build_sitemap($build_dir);
run qw( git add -A), $build_dir;
say "Commiting changes";
run qw(git commit -m), 'Updated docs';
}
my $remote_sha = eval {
my $remote = run qw(git rev-parse --symbolic-full-name @{u});
chomp $remote;
return sha_for($remote);
} || '';
if ( sha_for('HEAD') ne $remote_sha ) {
say "Pushing changes";
run qw(git push origin HEAD );
}
else {
say "No changes to push";
}
}
#===================================
sub init_env {
#===================================
chdir($FindBin::RealBin) or die $!;
$ENV{SGML_CATALOG_FILES} = $ENV{XML_CATALOG_FILES} = join ' ',
file('resources/docbook-xsl-1.78.1/catalog.xml')->absolute,
file('resources/docbook-xml-4.5/catalog.xml')->absolute;
$ENV{PATH}
= dir('resources/asciidoc-8.6.8/')->absolute . ':' . $ENV{PATH};
eval { run( 'xsltproc', '--version' ) }
or die "Please install <xsltproc>";
}
#===================================
sub usage {
#===================================
say <<USAGE;
Build local docs:
$0 --doc path/to/index.asciidoc [opts]
Opts:
--single Generate a single HTML page, instead of
a chunking into a file per chapter
--toc Include a TOC at the beginning of the page.
--out dest/dir/ Defaults to ./html_docs.
--chunk=1 Also chunk sections into separate files
--toc_level=1 How many sections deep should the main ToC display
--comments Make // comments visible
--open Open the docs in a browser once built.
--web Serve the docs via a webserver once built.
--lenient Ignore linking errors
--verbose
WARNING: Anything in the `out` dir will be deleted!
Build docs from all repos in conf.yaml:
$0 --all [opts]
Opts:
--push Commit the updated docs and push to origin
--verbose
USAGE
}