0e17b7a39cdb4737ffc5a6cd83a2190f04a47722
[pve-docs.git] / asciidoc-pve.in
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Getopt::Long;
6 use File::Path;
7 use File::Basename;
8 use IO::File;
9
10 use JSON;
11
12 my $release = '@RELEASE@';
13
14 my $clicmd = shift or
15 die "no command specified\n";
16
17 my $data_str = "";
18 while (<main::DATA>) { $data_str .= $_; }
19
20 my $fileinfo = decode_json($data_str);
21
22 my $tmpprefix = ".asciidoc-pve-tmp_";
23
24 my $adoc_source_dir = "/usr/share/pve-doc-generator";
25
26 # inside pve-docs source dir?
27 if (-f "attributes.txt" && -f "pve-admin-guide.adoc") {
28 $adoc_source_dir = "."
29 }
30
31 my $prepared_files = {};
32
33 my $env_stack = [];
34 my $env_skip = 0;
35
36 sub push_environment {
37 my ($env, $skip) = @_;
38
39 $skip = 1 if $env_skip;
40 $skip = 0 if !defined($skip);
41
42 push @$env_stack, [$env, $skip];
43
44 $env_skip = $skip;
45 }
46
47 sub pop_environment {
48 my ($env) = @_;
49
50 my $last_stack_entry = pop @$env_stack;
51 die "unable to pop env '$env'" if !defined($last_stack_entry);
52
53 my ($last_env, $skip) = @$last_stack_entry;
54 die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
55
56 if (!scalar(@$env_stack)) {
57 $env_skip = 0;
58 } else {
59 my (undef, $skip) = @{$env_stack->[-1]};
60 $env_skip = $skip;
61 }
62 }
63
64 sub cleanup {
65
66 # TODO: anything ?
67 }
68
69 sub replace_wiki_xref {
70 my ($blockid, $text) = @_;
71
72 my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid};
73 my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid};
74
75 die "unable to resolve wiki link (xref:$blockid)\n"
76 if !defined($link);
77
78 $text = $reftext if !length($text);
79
80 die "xref: no text for '$blockid'\n" if !$text;
81
82 return "$link\[$text\]";
83 }
84
85 sub prepare_adoc_file {
86 my ($filename, $attributes) = @_;
87
88
89 return if $prepared_files->{$filename};
90
91 print "PREPARE $filename\n";
92
93 $prepared_files->{$filename} = 1;
94
95 my $dirname = dirname($filename);
96 my $basename = basename($filename);
97
98 my $outfilename = "$dirname/${tmpprefix}$basename";
99
100 my $fh = IO::File->new("$filename", "r") or
101 die "unable to open file '$filename' - $!\n";
102
103 my $outfh = IO::File->new("$outfilename", "w") or
104 die "unable to open temporary file '$outfilename'\n";
105
106 while (defined (my $line = <$fh>)) {
107 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
108 my ($not, $env, $text) = ($1, $2, $3);
109 die "unsuported ifdef usage - implement me" if $text;
110
111 my $skip = !exists($attributes->{$env}) ? 1 : 0;
112 $skip = ($skip ? 0 : 1 ) if $not;
113
114 push_environment($env, $skip);
115 next;
116 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
117 my ($env, $text) = ($1, $2);
118 die "unsuported ifdef usage - implement me" if $text;
119 pop_environment($env);
120 next;
121 }
122
123 next if $env_skip;
124
125 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
126 my ($fn, $rest) = ($1, $2);
127 print "INCLUDE: $fn\n";
128 my $new_fn = prepare_adoc_file($fn, $attributes);
129 $line = "include::${new_fn}$rest\n";
130 }
131
132 # fix xrefs
133 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_wiki_xref($1,$2)/ge;
134
135 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_wiki_xref($1,$2)/ge;
136
137 print $outfh $line;
138 }
139
140 return $outfilename;
141 }
142
143 sub compile_wiki {
144
145 my $verbose;
146 my $outfile;
147 my $target_env;
148
149 GetOptions ("outfile=s" => \$outfile,
150 "verbose" => \$verbose) or
151 die("Error in command line arguments\n");
152
153 my $infile = shift(@ARGV) or
154 die "no input file specified\n";
155
156 scalar(@ARGV) == 0 or
157 die "too many arguments...\n";
158
159 my $env = 'wiki';
160
161
162 my $title = $fileinfo->{titles}->{$env}->{$infile} or
163 die "unable to get title for '$infile'\n";
164
165
166 print "compile: $title\n";
167
168 my $leveloffset = 0;
169
170 my $doctype = $fileinfo->{doctype}->{$env}->{$infile} // 0;
171 $leveloffset = - $doctype;
172
173 my $date = `date`;
174 chomp $date;
175
176 my $attributes = {
177 $env => undef,
178 icons => undef,
179 'data-uri' => undef,
180 date => $date,
181 leveloffset => $leveloffset,
182 revnumber => $release,
183 };
184
185 my $cmd = ['asciidoc', '-s'];
186
187 foreach my $key (keys %$attributes) {
188 my $value = $attributes->{$key};
189 if (defined($value)) {
190 push @$cmd, '-a', "$key=$value";
191 } else {
192 push @$cmd, '-a', $key;
193 }
194 }
195
196 push @$cmd, '--verbose' if $verbose;
197
198 if (!defined($outfile)) {
199 $outfile = $infile;
200 $outfile =~ s/\.adoc$//;
201 $outfile .= ".html";
202 }
203
204 push @$cmd, '--out-file', $outfile;
205
206 my $new_infile = prepare_adoc_file($infile, $attributes);
207
208 push @$cmd, $new_infile;
209
210 print "RUN " . join(' ', @$cmd) . "\n";
211
212 system(@$cmd) == 0 or
213 die "aciidoc error";
214 }
215
216 if ($clicmd eq 'compile-wiki') {
217
218 eval { compile_wiki(); };
219 my $err = $@;
220
221 cleanup();
222
223 die $err if $err;
224
225 } else {
226
227 die "unknown command '$clicmd'\n";
228
229 }
230
231
232
233
234
235
236 exit 0;
237
238 __END__