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