forked from freeciv/freeciv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_FAQ.pl
230 lines (202 loc) · 3.91 KB
/
generate_FAQ.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/perl
$test_data = "";
# uncomment the following to run from named file
#$test_data = "sample.txt";
$faq_url = "https://www.freeciv.org/wiki/FAQ";
$faq_file = "FAQ";
@sections;
@sectStrings;
@content;
$contentLine;
$EOD = 0;
$line;
@graph;
open(FILE, ">$faq_file") || die ("Cannot open file $faq_file for writing.\n");
# collect input data
&init_data;
# read data and write toc
&processToc;
&processAnswers;
close(FILE);
print "File $faq_file has been written successfully\n";
# collect table of contents
sub processToc
{
# clear to FAQ
if ( &clearTo("FAQ") || &clearTo("Contents") ) { die "No TOC found\n"; }
# dump TOC
# header
print FILE <<HEND;
Freeciv Frequently Asked Questions (FAQ)
(from $faq_url)
Contents
HEND
for ( ; ; )
{
&readLine || die "Data runout reading TOC\n";
if ( $line =~ /^\s*$/ )
{
print FILE "\n";
return;
}
if ( $line =~ /^\s*(.) ([\.\d]+) (.+)$/ )
{
#handle section or question
my $type = $1;
my $num = $2;
my $name = $3;
my $pattern = " \\$type";
$line =~ s/$pattern//;
if ( $type eq "*" )
{
push( @sections, $name );
my $l = @sections;
if ( $l != $num ) { die "Section $l number is $num\n"; }
push( @sectStrings, [] );
}
else
{
my $ref = $sectStrings[-1];
push( @$ref, $num );
}
}
print FILE "$line\n";
}
}
sub clearTo
{
for ( ; ; )
{
&readLine || return 1;
if ( $line =~ /^\s*$_[0]$/ ) { last; }
}
&processBlankLine;
}
# get line, required blank
sub processBlankLine
{
if ( ! &readLine ) { die "Data runout in processBlankLine\n"; }
if ( $line!~/^\s*$/ ) { die "processBlankLine read '$line'\n"; }
}
# write answer section
sub processAnswers
{
my $ix = 0;
my $sName;
my @nums;
while ( ! $EOD ) {
my $type = &readGraph;
# handle section or question
if ( $type == 2 )
{
print FILE "-------\n\n";
if ( $graph[0] eq $sections[$ix] )
{
$sName = $sections[$ix];
my $nr = $sectStrings[$ix];
@nums = @$nr;
++$ix;
&writeGraph( $ix );
}
else
{
my $num = shift( @nums );
&writeGraph( $num );
}
}
else
{
&writeGraph();
}
}
}
# read paragraph, return type
# 0: end of answers
# 1: other (part of answer)
# 2: section or question
sub readGraph
{
&readLine || die "Data runout in readGraph\n";
if ( $line eq "" ) { die "Expecting graph, read blank line\n"; }
@graph = ( $line );
if ( $line =~ /^\s*^_+$/ ) { return 0; }
# read rest of graph
for ( ; ; )
{
&readLine || die "Data runout in readGraph\n";
if ( $line eq "" ) { last; }
if ( $line !~ /^\s*_+$/ )
{
push( @graph, $line );
}
else
{
$EOD = 1;
last;
}
}
# test edit tag
if ( &editTag ) { return 2; }
1;
}
# test for and remove edit tag
sub editTag
{
# look for on last line
if ( $graph[-1] =~ s/ \[[^]]*\] Edit$// ) { return 1; }
if ( 2 > @graph ) { return 0; }
if ( $graph[-1] =~ m/^\[[^]]*\] Edit$/ )
{
pop( @graph );
return 1;
}
# look for split
if ( $graph[-1]eq"Edit" && $graph[-2]=~s/ \[[^]]*\]$// )
{
pop( @graph );
return 1;
}
return 0;
}
# write paragraph
sub writeGraph
{
if ( defined $_[0] ) { print FILE "$_[0] "; }
while ( @graph )
{
my $data = shift( @graph );
print FILE "$data\n";
}
print FILE "\n";
}
# pseudo-i/o methods
# 'open' data 'file'
sub init_data
{
$contentLine = 0;
if ( $test_data ) {
print "using test data $test_data\n";
open ( MYDATA, $test_data );
@content = <MYDATA>;
close MYDATA;
} else {
print "using url $faq_url\n";
@content = `lynx -nolist -dump "$faq_url"`;
}
my $count = @content;
print( "$count lines of content\n" );
}
# 'read' data line
sub readLine
{
if ( $contentLine < @content )
{
$line = $content[$contentLine++];
$line =~ s/\s+$//;
1;
}
else
{
0;
}
}