-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbuild_docs.pl
More file actions
executable file
·233 lines (183 loc) · 5.66 KB
/
build_docs.pl
File metadata and controls
executable file
·233 lines (183 loc) · 5.66 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
#!/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);
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();
our $Old_Pwd = dir()->absolute;
init_env();
our $Conf = LoadFile('conf.yaml');
GetOptions(
$Opts, #
'all', 'push', #
'single', 'doc=s', 'out=s', 'toc', 'open', 'chunk=i',
'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->{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);
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 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 ) {
say "Commiting changes";
run qw(git commit -m), 'Updated docs';
say "Rebasing changes";
run qw(git pull --rebase );
}
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
--open Open the docs in a browser 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
}