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