When mixer is not available, recommend SDL2_mixer instead of SDL1.2 mixer
[freeciv.git] / doc / generate_FAQ.pl
blob82d172837e5ac790bfa9d49579ffd1498b67ecd5
1 #!/usr/bin/perl
3 $test_data = "";
4 # uncomment the following to run from named file
5 #$test_data = "sample.txt";
7 $faq_url = "http://www.freeciv.org/wiki/FAQ";
8 $faq_file = "FAQ";
10 @sections;
11 @sectStrings;
13 @content;
14 $contentLine;
15 $EOD = 0;
16 $line;
17 @graph;
19 open(FILE, ">$faq_file") || die ("Cannot open file $faq_file for writing.\n");
21 # collect input data
22 &init_data;
24 # read data and write toc
25 &processToc;
27 &processAnswers;
29 close(FILE);
31 print "File $faq_file has been written successfully\n";
33 # collect table of contents
34 sub processToc
37 # clear to FAQ
38 if ( &clearTo("FAQ") || &clearTo("Contents") ) { die "No TOC found\n"; }
40 # dump TOC
42 # header
43 print FILE <<HEND;
44 Freeciv Frequently Asked Questions (FAQ)
46 (from $faq_url)
48 Contents
50 HEND
52 for ( ; ; )
54 &readLine || die "Data runout reading TOC\n";
55 if ( $line =~ /^\s*$/ )
57 print FILE "\n";
58 return;
60 if ( $line =~ /^\s*(.) ([\.\d]+) (.+)$/ )
62 #handle section or question
63 my $type = $1;
64 my $num = $2;
65 my $name = $3;
66 my $pattern = " \\$type";
67 $line =~ s/$pattern//;
68 if ( $type eq "*" )
70 push( @sections, $name );
71 my $l = @sections;
72 if ( $l != $num ) { die "Section $l number is $num\n"; }
73 push( @sectStrings, [] );
75 else
77 my $ref = $sectStrings[-1];
78 push( @$ref, $num );
81 print FILE "$line\n";
86 sub clearTo
88 for ( ; ; )
90 &readLine || return 1;
91 if ( $line =~ /^\s*$_[0]$/ ) { last; }
93 &processBlankLine;
96 # get line, required blank
97 sub processBlankLine
99 if ( ! &readLine ) { die "Data runout in processBlankLine\n"; }
100 if ( $line!~/^\s*$/ ) { die "processBlankLine read '$line'\n"; }
103 # write answer section
104 sub processAnswers
106 my $ix = 0;
107 my $sName;
108 my @nums;
109 while ( ! $EOD ) {
110 my $type = &readGraph;
111 # handle section or question
112 if ( $type == 2 )
114 print FILE "-------\n\n";
115 if ( $graph[0] eq $sections[$ix] )
117 $sName = $sections[$ix];
118 my $nr = $sectStrings[$ix];
119 @nums = @$nr;
120 ++$ix;
121 &writeGraph( $ix );
123 else
125 my $num = shift( @nums );
126 &writeGraph( $num );
129 else
131 &writeGraph();
136 # read paragraph, return type
137 # 0: end of answers
138 # 1: other (part of answer)
139 # 2: section or question
140 sub readGraph
142 &readLine || die "Data runout in readGraph\n";
143 if ( $line eq "" ) { die "Expecting graph, read blank line\n"; }
144 @graph = ( $line );
145 if ( $line =~ /^\s*^_+$/ ) { return 0; }
146 # read rest of graph
147 for ( ; ; )
149 &readLine || die "Data runout in readGraph\n";
150 if ( $line eq "" ) { last; }
151 if ( $line !~ /^\s*_+$/ )
153 push( @graph, $line );
155 else
157 $EOD = 1;
158 last;
161 # test edit tag
162 if ( &editTag ) { return 2; }
166 # test for and remove edit tag
167 sub editTag
169 # look for on last line
170 if ( $graph[-1] =~ s/ \[[^]]*\] Edit$// ) { return 1; }
171 if ( 2 > @graph ) { return 0; }
172 if ( $graph[-1] =~ m/^\[[^]]*\] Edit$/ )
174 pop( @graph );
175 return 1;
177 # look for split
178 if ( $graph[-1]eq"Edit" && $graph[-2]=~s/ \[[^]]*\]$// )
180 pop( @graph );
181 return 1;
183 return 0;
186 # write paragraph
187 sub writeGraph
189 if ( defined $_[0] ) { print FILE "$_[0] "; }
190 while ( @graph )
192 my $data = shift( @graph );
193 print FILE "$data\n";
195 print FILE "\n";
198 # pseudo-i/o methods
200 # 'open' data 'file'
201 sub init_data
203 $contentLine = 0;
204 if ( $test_data ) {
205 print "using test data $test_data\n";
206 open ( MYDATA, $test_data );
207 @content = <MYDATA>;
208 close MYDATA;
209 } else {
210 print "using url $faq_url\n";
211 @content = `lynx -nolist -dump "$faq_url"`;
213 my $count = @content;
214 print( "$count lines of content\n" );
217 # 'read' data line
218 sub readLine
220 if ( $contentLine < @content )
222 $line = $content[$contentLine++];
223 $line =~ s/\s+$//;
226 else