Project

Profile

Help

HostedRedmine.com has moved to the Planio platform. All logins and passwords remained the same. All users will be able to login and use Redmine just as before. Read more...

Feature #767074 ยป m-30-26-25-fetchnews.patch

for master, S3_0, S2_6, S2_5 - Jacob Nevins, 2018-07-28 11:26 AM

View differences:

doc/fetchnews.pl
1
#!/usr/bin/perl
2
# Fetch and format data to update NEWS/NEWS-x.x from Freeciv wiki.
3
# Usage: perl fetchnews.pl URL LOCALFILE
4
# Some manual tweaks will be required afterward.
5

  
6
$test_data = "";
7
# uncomment the following to run from named file
8
#$test_data = "sample.txt";
9

  
10
@content;
11
$contentLine;
12
$EOD = 0;
13
$line;
14
@graph;
15

  
16
if (@ARGV < 2) {
17
  die "usage: $0 URL file";
18
  exit 1;
19
}
20
($url, $file) = @ARGV;
21
$hdr = $url;
22
$hdr =~ s#^.*/([^/]*)$#$1#;
23
$hdr =~ s#_# #g;
24

  
25
open(FILE, ">$file") || die ("Cannot open file $file for writing.\n");
26

  
27
# collect input data
28
&init_data;
29

  
30
if ( &clearTo($hdr) || &clearToPrefix("what's changed since ") )
31
{ die "Doesn't look like a NEWS page\n"; }
32

  
33
print FILE <<HEND;
34
CHANGES FROM x.x.x to y.y.y
35
---------------------------
36

  
37
   (from <$url>)
38

  
39
HEND
40

  
41
&processText;
42

  
43
close(FILE);
44

  
45
print "File $file has been written successfully\n";
46

  
47
sub clearTo
48
{
49
  for ( ; ; )
50
  {
51
    &readLine  || return 1;
52
    if ( $line =~ /^\s*$_[0]$/ ) { last; }
53
  }
54
  &processBlankLine;
55
}
56

  
57
# clear to line starting with arg, case-insensitive
58
sub clearToPrefix
59
{
60
  for ( ; ; )
61
  {
62
    &readLine  || return 1;
63
    if ( $line =~ /^\s*$_[0]/i ) { last; }
64
  }
65
  &processBlankLine;
66
}
67

  
68
# get line, required blank
69
# HACK: a line containing "Edit" on its own is assumed to be run-on from a
70
# heading and is discarded.
71
sub processBlankLine
72
{
73
  do {
74
    if ( ! &readLine ) { die "Data runout in processBlankLine\n"; }
75
  } while ($line eq "Edit");
76
  if ( $line!~/^\s*$/ ) { die "processBlankLine read '$line'\n"; }
77
}
78

  
79
# write answer section
80
sub processText
81
{
82
  while ( ! $EOD ) {
83
    my $type = &readGraph;
84
    # FIXME do something special for $type==2?
85
    &writeGraph();
86
  }
87
}
88

  
89
# read paragraph, return type
90
#   0: end of main section
91
#   1: other (part of body of section)
92
#   2: section
93
sub readGraph
94
{
95
  &readLine  || die "Data runout in readGraph\n";
96
  if ( $line eq "" ) { die "Expecting graph, read blank line\n"; }
97
  @graph = ( $line );
98
  if ( $line =~ /^\s*Retrieved from/ ) { $EOD = 1; return 0; }
99
  # read rest of graph
100
  for ( ; ; )
101
  {
102
    &readLine  || die "Data runout in readGraph\n";
103
    if ( $line eq "" ) { last; }
104
    if ( $line !~ /^\s*Retrieved from/ )
105
    {
106
      push( @graph, $line );
107
    }
108
    else
109
    {
110
      $EOD = 1;
111
      last;
112
    }
113
  }
114
  # test edit tag
115
  if ( &editTag ) { return 2; }
116
  1;
117
}
118

  
119
# test for and remove edit tag
120
sub editTag
121
{
122
  # look for on last line
123
  if ( $graph[-1] =~ s/ \[[^]]*\] Edit$// ) { return 1; }
124
  if ( 2 > @graph ) { return 0; }
125
  if ( $graph[-1] =~ m/^\[[^]]*\] Edit$/ )
126
  {
127
    pop( @graph );
128
    return 1;
129
  }
130
  # look for split
131
  if ( $graph[-1]eq"Edit" && $graph[-2]=~s/ \[[^]]*\]$// )
132
  {
133
    pop( @graph );
134
    return 1;
135
  }
136
  return 0;
137
}
138

  
139
# write paragraph
140
sub writeGraph
141
{
142
  if ( defined $_[0] ) { print FILE "$_[0] "; }
143
  while ( @graph )
144
  {
145
    my $data = shift( @graph );
146
    print FILE "$data\n";
147
  }
148
  print FILE "\n";
149
}
150

  
151
# pseudo-i/o methods
152

  
153
# 'open' data 'file'
154
sub init_data
155
{
156
  $contentLine = 0;
157
  if ( $test_data ) {
158
    print "using test data $test_data\n";
159
    open ( MYDATA, $test_data );
160
    @content = <MYDATA>;
161
    close MYDATA;
162
  } else {
163
    print "using url $url\n";
164
    @content = `lynx -nolist -reload -dump "$url"`;
165
  }
166
  my $count = @content;
167
  print( "$count lines of content\n" );
168
}
169

  
170
# 'read' data line
171
sub readLine
172
{
173
  if ( $contentLine < @content )
174
  {
175
    $line = $content[$contentLine++];
176
    $line =~ s/\s+$//;
177
    1;
178
  }
179
  else
180
  {
181
    0;
182
  }
183
}
    (1-1/1)