3 # Script to convert Exim FAQ into wiki markup - moin flavour
15 foreach my $str (@in) {
16 $str =~ tr/0-9A-Za-z _//cd;
19 return join('/', @bits);
24 sub mkwikifilename ($) {
27 $wn =~ s/([^A-Za-z0-9])/sprintf("_%02x",ord($1))/eg;
34 sub format_wiki_question_header ($) {
39 for($i =0; ($i <= $#lines); $i++) {
40 unless (defined($lines[$i])) {
44 $lines[$i] =~ s/^\s+//;
46 return('= ' . join(' ', @lines) . ' =');
55 my $qtag = $meta->qtags->{$qref};
57 warn "Unknown qtag $qref\n";
61 return join(':', '[', $qref->{wikiname}, $qref . ']');
66 sub wiki_markup ($$) {
70 $s =~ s/@\\/\@\@backslash\@\@/g; # @\ temporarily hidden
72 $s =~ s/\\#/ /g; # \# is a hard space
74 $s =~ s/\\\*\*([^*]*)\*\*\\/'''$1'''/g; # \**...**\ => bold
75 $s =~ s/\\\*([^*]*)\*\\/''$1''/g; # \*.....*\ => italic
76 $s =~ s/\\"([^"]*)"\\/`$1`/g; # \"....."\ => fixed pitch
77 $s =~ s/\\\$([^\$]*)\$\\/''\$$1''/g; # \$.....$\ => $italic
78 $s =~ s/\\\\([^\\]*)\\\\/<small>$1<\/small>/g; # \\.....\\ => small
79 $s =~ s/\\\(([^)]*)\)\\/''$1''/g; # \(.....)\ => italic
80 $s =~ s/\\-([^\\]*)-\\/'''-$1'''/g; # \-.....-\ => -bold
81 $s =~ s/\\\[([^]]*)\]\\/''$1''/gx; # \[.....]\ => <italic>
82 $s =~ s/\\\?(.*?)\?\\/$1/g; # \?.....?\ => URL
83 $s =~ s/\\\^\^([^^]*)\^\^\\/''$1''/g; # \^^...^^\ => italic
84 $s =~ s/\\\^([^^]*)\^\\/''$1''/g; # \^.....^\ => italic
85 $s =~ s/\\%([^%]*)%\\/'''$1'''/g; # \%.....%\ => bold
86 $s =~ s/\\\/([^\/]*)\/\\/''$1''/g; # \/...../\ => italic
87 $s =~ s/\\([^\\]+)\\/`$1`/g; # \.......\ => fixed pitch
89 $s =~ s"//([^/\"]*)//"''$1</i>"g; # //.....// => italic
90 $s =~ s/::([^:]*)::/''$1:''/g; # ::.....:: => italic:
92 $s =~ s/``(.*?)''/“$1”/g; # ``.....'' => quoted text
94 #$s =~ s/\s*\[\[br\]\]\s*/<br>/g; # [[br]] => <br>
96 $s =~ s/\@\@backslash\@\@/\\/g; # Put back single backslash
98 $s =~ s/^(\s*\(\d\)\s)/$1 /; # Extra space after (1), etc.
100 # Cross references within paragraphs
102 $s =~ s/Q(\d{4})(?!:)/wikiref($meta, $1)/xg;
104 # References to configuration samples
106 ##$s =~ s/\b([CFLS]\d\d\d)\b/<a href="$1.txt">$1<\/a>/g;
108 # Remove white space preceding a newline in the middle of paragraphs,
109 # to keep the file smaller (and for human reading when debugging).
118 sub clip_paragraph ($) {
125 # split off and throw initial para breaks
126 while (($#{$lines} >= 0) && (!defined($lines->[0]))) {
130 # if nothing else return
132 unless ($#{$lines} >= 0);
134 # deal with example chunks
135 if ($lines->[0] =~ /^(\=\=\>\s+)\S/) {
136 $offlen = length($1);
137 while (($#{$lines} >= 0) && (defined($lines->[0]))) {
138 my $txt = substr(shift @{$lines}, $offlen);
139 $ret .= (defined($ret)) ? "\n$txt" : $txt;
141 return ($ret, 'code');
145 # deal with rest - numeric lines first
146 if ($lines->[0] =~ /^(\s+\(\d+\)\s*)/) {
147 $offlen = length($1);
150 } elsif ($lines->[0] =~ /^(\s+)\S/) {
151 $offlen = length($1);
160 while (($#{$lines} >= 0) && (defined($lines->[0]))) {
163 substr(shift @{$lines}, $offlen);
168 return ($ret, $flags);
173 sub format_wiki_text ($$) {
180 while ($#lines >= 0) {
181 my($para, $flags) = clip_paragraph(\@lines);
182 if ($flags eq 'code') {
183 $out .= "{{{\n" . $para . "\n}}}\n";
184 } elsif ($flags eq 'numlist') {
185 $out .= ' 1. ' . wiki_markup($meta, $para) . "\n";
186 } elsif ($flags eq 'empty') {
188 $out .= wiki_markup($meta, $para) . "\n";
196 sub output_wiki_header ($$$) {
201 $fh->print(join("\n",
203 '#pragma section-numbers off',
204 '## Autogenerated by f2wiki',
205 join('', '["FAQ"] / [:',
206 $qset->{section}->{wikiname},
208 $qset->{section}->{title},
212 '[[Navigation(siblings)]]',
219 sub output_wiki_question ($$$$) {
225 $fh->print(join("\n",
226 ('= ' . $qset->{qtag} . ' ='),
230 format_wiki_text($meta, $lines),
237 sub output_wiki_answer ($$$$) {
243 $fh->print(join("\n",
245 format_wiki_text($meta, $lines),
251 sub output_wiki_trailer ($$$) {
256 $fh->print(join("\n",
258 '[[Navigation(siblings)]]',
260 join('', '["FAQ"] / [:',
261 $qset->{section}->{wikiname},
263 $qset->{section}->{title},
267 'CategoryFrequentlyAskedQuestions',
276 my $tfh = IO::File->new('FAQ', 'w');
277 my @sections = sort { $a->{num} <=> $b->{num} }
278 values %{$meta->{sections}};
279 foreach my $sect (@sections) {
280 my $fh = IO::File->new($sect->{wikifile}, 'w');
281 $fh->print(join("\n",
283 '#pragma section-numbers off',
284 '## Autogenerated by f2wiki',
285 join('', '["FAQ"] / [:',
291 '[[Navigation(siblings,1)]]',
293 '[[Navigation(children)]]',
297 '= ' . $sect->{title} . ' =',
302 '/.*,,2,from="##qstart",to="##qend")]]'),
305 '[[Navigation(siblings,1)]]',
307 '[[Navigation(children)]]',
309 join('', '["FAQ"] / [:',
315 'CategoryFrequentlyAskedQuestions',
318 $tfh->print(' * [:', $sect->{wikiname}, ':', $sect->{title}, "]\n");
324 sub process_qset ($$$$) {
330 unless ($qset->{wikifile}) {
338 my $fh = IO::File->new($qset->{wikifile}, 'w') ||
339 die "$qset->{wikifile} OUT $!";
340 output_wiki_header($fh, $meta, $qset);
341 output_wiki_question($fh, $meta, $qset, $qlines);
342 output_wiki_answer($fh, $meta, $qset, $alines);
343 output_wiki_trailer($fh, $meta, $qset);
348 sub parse_faqsrc ($$) {
357 unless(defined($section)) {
358 unless (/^\d+\.\s/) {
362 $sectitle) = split(/\s+/, $_, 3);
363 $secnum =~ tr/0-9//cd;
364 my $wikiname = mkwikiname('FAQ', $sectitle);
365 my $wikifile = mkwikifilename($wikiname);
366 $meta->{sections}->{$secnum} =
369 wikiname => $wikiname,
370 wikifile => $wikifile,
378 $sect = $meta->{sections}->{$section};
380 } elsif (/^(Q\d+):/) {
382 my $wikiname = mkwikiname('FAQ', $sect->{title}, $qtag);
383 my $wikifile = mkwikifilename($wikiname);
384 my $qset = {section => $sect,
386 wikiname => $wikiname,
387 wikifile => $wikifile};
388 $meta->{qtags}->{$qtag} = $qset;
389 push(@{$sect->{qtags}}, $qset);
396 sub process_faqsrc ($$) {
403 my $clines = $qlines;
408 # skip preceding stuff....
409 unless(defined($qset)) {
410 next unless (/^Q\d+/);
414 # just skip section boundaries - we have done those before
416 } elsif (/^([QA]\d+):\s+(.+)$/) {
419 if (substr($1, 0, 1) eq 'Q') {
420 process_qset($meta, $qset, $qlines, $alines);
424 $qset = $meta->{qtags}->{$qtag};
428 push(@{$clines}, $line);
430 push(@{$clines}, undef);
432 push(@{$clines}, $_);
436 process_qset($meta, $qset, $qlines, $alines);
448 my $fh = IO::File->new(shift, 'r') || die $!;
450 parse_faqsrc($fh, $state);
452 # print Dumper($state);
453 process_faqsrc($fh, $state);