2e92c6c2f01710a93a54463547c273ac3a475c3c
[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 replace_wiki_xref {
65 my ($blockid, $text) = @_;
66
67 my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid};
68 my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid};
69
70 die "unable to resolve wiki link (xref:$blockid)\n"
71 if !defined($link);
72
73 $text = $reftext if !length($text);
74
75 die "xref: no text for '$blockid'\n" if !$text;
76
77 return "$link\[$text\]";
78 }
79
80 sub prepare_adoc_file {
81 my ($filename, $attributes) = @_;
82
83
84 return if $prepared_files->{$filename};
85
86 print "PREPARE $filename\n";
87
88 $prepared_files->{$filename} = 1;
89
90 my $dirname = dirname($filename);
91 my $basename = basename($filename);
92
93 my $outfilename = "$dirname/${tmpprefix}$basename";
94
95 my $fh = IO::File->new("$filename", "r") or
96 die "unable to open file '$filename' - $!\n";
97
98 my $outfh = IO::File->new("$outfilename", "w") or
99 die "unable to open temporary file '$outfilename'\n";
100
101 while (defined (my $line = <$fh>)) {
102 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
103 my ($not, $env, $text) = ($1, $2, $3);
104 die "unsuported ifdef usage - implement me" if $text;
105
106 my $skip = !exists($attributes->{$env}) ? 1 : 0;
107 $skip = ($skip ? 0 : 1 ) if $not;
108
109 push_environment($env, $skip);
110 next;
111 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
112 my ($env, $text) = ($1, $2);
113 die "unsuported ifdef usage - implement me" if $text;
114 pop_environment($env);
115 next;
116 }
117
118 next if $env_skip;
119
120 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
121 my ($fn, $rest) = ($1, $2);
122 print "INCLUDE: $fn\n";
123 my $new_fn = prepare_adoc_file($fn, $attributes);
124 $line = "include::${new_fn}$rest\n";
125 }
126
127 # fix xrefs
128 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_wiki_xref($1,$2)/ge;
129
130 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_wiki_xref($1,$2)/ge;
131
132 print $outfh $line;
133 }
134
135 return $outfilename;
136 }
137
138 sub compile_wiki {
139
140 my $verbose;
141 my $outfile;
142 my $target_env;
143
144 GetOptions ("outfile=s" => \$outfile,
145 "verbose" => \$verbose) or
146 die("Error in command line arguments\n");
147
148 my $infile = shift(@ARGV) or
149 die "no input file specified\n";
150
151 scalar(@ARGV) == 0 or
152 die "too many arguments...\n";
153
154 my $env = 'wiki';
155
156
157 my $title = $fileinfo->{titles}->{$env}->{$infile} or
158 die "unable to get title for '$infile'\n";
159
160
161 print "compile: $title\n";
162
163 my $leveloffset = 0;
164
165 my $doctype = $fileinfo->{doctype}->{$env}->{$infile} // 0;
166 $leveloffset = - $doctype;
167
168 my $date = `date`;
169 chomp $date;
170
171 my $attributes = {
172 $env => undef,
173 icons => undef,
174 'data-uri' => undef,
175 date => $date,
176 leveloffset => $leveloffset,
177 revnumber => $release,
178 };
179
180 my $cmd = ['asciidoc', '-s'];
181
182 foreach my $key (keys %$attributes) {
183 my $value = $attributes->{$key};
184 if (defined($value)) {
185 push @$cmd, '-a', "$key=$value";
186 } else {
187 push @$cmd, '-a', $key;
188 }
189 }
190
191 push @$cmd, '--verbose' if $verbose;
192
193 if (!defined($outfile)) {
194 $outfile = $infile;
195 $outfile =~ s/\.adoc$//;
196 $outfile .= ".html";
197 }
198
199 push @$cmd, '--out-file', $outfile;
200
201 my $new_infile = prepare_adoc_file($infile, $attributes);
202
203 push @$cmd, $new_infile;
204
205 print "RUN " . join(' ', @$cmd) . "\n";
206
207 system(@$cmd) == 0 or
208 die "aciidoc error";
209 }
210
211 if ($clicmd eq 'compile-wiki') {
212
213 eval { compile_wiki(); };
214 my $err = $@;
215
216 # cleanup
217
218 die $err if $err;
219
220 } else {
221
222 die "unknown command '$clicmd'\n";
223
224 }
225
226
227
228
229
230
231 exit 0;
232
233 __END__