summaryrefslogtreecommitdiff
blob: 208ff70d68773d0a5e63b36d20f1b3913e10f93e (plain)
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
#!/usr/bin/perl

use List::MoreUtils qw(uniq nsort_by);
use LaTeX::Encode qw(latex_encode);

# four files: 
# - glr: input file, unsorted list of glep numbers, may contain duplicates, 
#        generated by LaTeX
# - gld: database of gleps, format
#        * glep number
#        * glep title
#        * glep status
#        * index keywords separated by ";"
# - glg: output file, LaTeX fragment that can be sourced



# Fetch the title of a bug from Bugzilla
# URL example: https://wiki.gentoo.org/index.php?title=GLEP:31&action=raw
# Result contains
# {{GLEP
# |Number=31
# |Title=Character Sets for Portage Tree Items
# |Type=Standards Track
# |Status=Final
# |Author=Ciaran McCreesh <ciaranm@gentoo.org>
# }}


sub getglepdata {
  my $number=shift;

  open (my $web, '-|:encoding(UTF-8)', 
   'wget -O - https://wiki.gentoo.org/index.php?title=GLEP:'.$number.'\&action=raw');

  my @glep=<$web>;
  close $web;
  
  my $title, $status;
  
  foreach (@glep) {
    $line=$_; chomp $line; print "line is $line\n";
    if ($_=~ /^\|Title=/) {
      $title=$_;
      $title=~s/^\|Title=//;
      chomp $title;
    };
    if ($_=~ /^\|Status=/) {
      $status=$_;
      $status=~s/^\|Status=//;
      chomp $status;
    };
  };
  
  return ($title, $status);
}


# Main code start

# Read the list of gleps referenced in the LaTeX file.
open my $glr, '<', "decisions.glr";
my @glrlist = <$glr>;
close $glr;
chomp @glrlist;

my @glepnumbers=uniq nsort_by { $_ } @glrlist;

# Read the database
open my $gld, '<', "decisions.gld";

my $gleptitle={};
my $glepstatus={};
my $glepkeywords={};

while (1) {
  defined ( my $number=<$gld> ) or last;
  defined ( my $title=<$gld> ) or last;
  defined ( my $status=<$gld> ) or last;
  defined ( my $keywords=<$gld> ) or last;
  chomp $number; 
  chomp $title;
  chomp $status;
  chomp $keywords;
  $gleptitle{$number}=$title;
  $glepstatus{$number}=$status;
  $glepkeywords{$number}=$keywords;  
};

close $gld;


# Loop through the referenced glep numbers, check if we already have the title,
# and if not fetch and add it.
foreach(@glepnumbers) {
  my $cur=$_;
  print "GLEP number $cur\n";
  if ($gleptitle{$cur}) {
    print "  Title is \"$gleptitle{$cur}\"\n";
  } else {
    print "  Title not yet available, fetching all the data\n";
    ($gleptitle{$cur},$glepstatus{$cur})=getglepdata($cur);
    $glepkeywords{$cur}="";
  };
};

# Write out the bug title cache again.
my @gleptitlenew= map { $_ => $gleptitle{$_} } sort keys %gleptitle;

open my $gld, '>', "decisions.gld";
foreach(@glepnumbers) {
  my $cur=$_;
  print $gld "$cur\n";
  print $gld "$gleptitle{$cur}\n";
  print $gld "$glepstatus{$cur}\n";
  print $gld "$glepkeywords{$cur}\n";
};
close $gld;

# Write out the TeX input file
open my $glg, '>', "decisions.glg";
print $glg '\renewcommand{\gleptitle}[1]{%'."\n";

foreach(@glepnumbers) {
  my $cur=$_;
  my $title=latex_encode($gleptitle{$cur});

  my @keywords=split /;/, $glepkeywords{$cur};
  foreach(@keywords) {
    $title=$title.'\index{'.$_.'}';
  }
  print $glg '\ifthenelse{\equal{#1}{'.$cur.'}}{'.$title.'}{}%'."\n";
};

print $glg '}'."\n";
close $glg;