]>
Commit | Line | Data |
---|---|---|
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 | return "$link\[$text\]"; | |
76 | } | |
77 | ||
78 | sub prepare_adoc_file { | |
79 | my ($filename, $attributes) = @_; | |
80 | ||
81 | return if $prepared_files->{$filename}; | |
82 | ||
83 | print "PREPARE $filename\n"; | |
84 | ||
85 | $prepared_files->{$filename} = 1; | |
86 | ||
87 | my $dirname = dirname($filename); | |
88 | my $basename = basename($filename); | |
89 | ||
90 | my $outfilename = "$dirname/${tmpprefix}$basename"; | |
91 | ||
92 | my $fh = IO::File->new("$filename", "r") or | |
93 | die "unable to open file '$filename' - $!\n"; | |
94 | ||
95 | my $outfh = IO::File->new("$outfilename", "w") or | |
96 | die "unable to open temporary file '$outfilename'\n"; | |
97 | ||
98 | while (defined (my $line = <$fh>)) { | |
99 | if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) { | |
100 | my ($not, $env, $text) = ($1, $2, $3); | |
101 | die "unsuported ifdef usage - implement me" if $text; | |
102 | ||
103 | my $skip = !exists($attributes->{$env}) ? 1 : 0; | |
104 | $skip = ($skip ? 0 : 1 ) if $not; | |
105 | ||
106 | push_environment($env, $skip); | |
107 | next; | |
108 | } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) { | |
109 | my ($env, $text) = ($1, $2); | |
110 | die "unsuported ifdef usage - implement me" if $text; | |
111 | pop_environment($env); | |
112 | next; | |
113 | } | |
114 | ||
115 | next if $env_skip; | |
116 | ||
117 | if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) { | |
118 | my ($fn, $rest) = ($1, $2); | |
119 | print "INCLUDE: $fn\n"; | |
120 | my $new_fn = prepare_adoc_file($fn, $attributes); | |
121 | $line = "include::${new_fn}$rest\n"; | |
122 | } | |
123 | ||
124 | # fix xrefs | |
125 | $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_wiki_xref($1,$2)/ge; | |
126 | ||
127 | $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_wiki_xref($1,$2)/ge; | |
128 | ||
129 | print $outfh $line; | |
130 | } | |
131 | ||
132 | return $outfilename; | |
133 | } | |
134 | ||
135 | sub compile_wiki { | |
136 | ||
137 | my $verbose; | |
138 | my $outfile; | |
139 | my $target_env; | |
140 | ||
141 | GetOptions ("outfile=s" => \$outfile, | |
142 | "verbose" => \$verbose) or | |
143 | die("Error in command line arguments\n"); | |
144 | ||
145 | my $infile = shift(@ARGV) or | |
146 | die "no input file specified\n"; | |
147 | ||
148 | scalar(@ARGV) == 0 or | |
149 | die "too many arguments...\n"; | |
150 | ||
151 | my $env = 'wiki'; | |
152 | ||
153 | ||
154 | my $title = $fileinfo->{titles}->{$env}->{$infile} or | |
155 | die "unable to get title for '$infile'\n"; | |
156 | ||
157 | ||
158 | print "compile: $title\n"; | |
159 | ||
160 | my $leveloffset = 0; | |
161 | ||
162 | if ($clicmd eq 'compile-wiki-chapter') { | |
163 | $leveloffset = 0; | |
164 | } elsif ($clicmd eq 'compile-wiki-section') { | |
165 | $leveloffset = -1; | |
166 | } | |
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-chapter' || | |
212 | $clicmd eq 'compile-wiki-section') { | |
213 | ||
214 | eval { compile_wiki(); }; | |
215 | my $err = $@; | |
216 | ||
217 | # cleanup | |
218 | ||
219 | die $err if $err; | |
220 | ||
221 | } else { | |
222 | ||
223 | die "unknown command '$clicmd'\n"; | |
224 | ||
225 | } | |
226 | ||
227 | ||
228 | ||
229 | ||
230 | ||
231 | ||
232 | exit 0; | |
233 | ||
234 | __END__ |