Imported Upstream version 2.11~beta1+ds
[debian/jabref.git] / htdocs-helper / generate-htdocs-help-from-jabref-src-help.pl
1 #!/usr/bin/perl
2 #requires: perl >= 5.10
3
4 #generate-htdocs-help-form-jabref-src-help.pl
5 #(c) 2012 Kolja Brix and Oliver Kopp
6
7 #This scripts converts the help files
8 #from the source directory of JabRef (HELPDIR_JABREF)
9 #to help files for the web page (HELPDIR_WEB)
10
11 #Start it from the root directory of your git repository.
12 #  Windows: perl generate-htdocs-help-form-jabref-src-help.pl
13 #It will overwrite all help files in HELPDIR_WEB
14 #It will NOT delete files in HELPDIR_WEB which were removed in HELPDIR_JABREF
15
16 #There are NO command line parameters
17
18 #If you have newline issues at the generated files,
19 #adapt FORCE_WINDOWS_NEWLINES
20
21
22 #Error:
23 #Use of uninitialized value in concatenation (.) or string at generate-htdocs-help-from-jabref-src-help.pl line 174, <$infileH> line 138.
24 #Reason:
25 #A new language has been added to HELPDIR_JABREF, where no translation is contained in
26 #%translation_back_to_contents. Please add the language to there.
27
28 use constant HELPDIR_JABREF => "../src/main/resources/help";
29 use constant HELPDIR_WEB    => "../../htdocs/help";
30
31 #0 for normal operationrequired
32 #1 for cygwin's perl
33 use constant FORCE_WINDOWS_NEWLINES => 0;
34
35 #translations for "Back to contents"
36 our %translation_back_to_contents = (
37   "da" => "Back to contents",
38   "de" => "Zur&uuml;ck zum Inhaltsverzeichnis",
39   "en" => "Back to contents",
40   "fr" => "Retour au contenu",
41   "in" => "Kembali ke Daftar Isi",
42   "ja" => "目次に戻る"
43 );
44
45
46 #build.xml for getting string replacements @version@ and @year@
47 use constant BUILDXML       => "../build.xml";
48
49 use warnings;
50 use strict;
51
52 #enable "given/when"
53 use feature ":5.10";
54
55 sub handleDir;
56 sub handleFile;
57 sub loadPreferences;
58
59 our $jabref_version;
60 our $jabref_year;
61 our $jabref_placeholder_version;
62 our $jabref_placeholder_year;
63
64 loadPreferences();
65
66 #Debug call for a single file
67 #handleFile("../src/main/resources/help/About.html", "../../htdocs/help/About.php", "en");
68 #exit;
69
70
71 # handle English
72 handleDir(HELPDIR_JABREF, HELPDIR_WEB, "en");
73
74 #handle other languages (contained in sub directories)
75
76 my $helpdirJabRef;
77
78 opendir($helpdirJabRef, HELPDIR_JABREF) or die $!;
79
80 my $sourcedir;
81 my $targetdir;
82 my $lang;
83
84 while (my $subdir = readdir($helpdirJabRef)) {
85         $sourcedir = HELPDIR_JABREF . "/$subdir";
86         next unless (-d $sourcedir);
87         next if ($subdir =~ /\.\.?/);
88
89         $targetdir = HELPDIR_WEB . "/$subdir";
90         $lang = $subdir;
91
92         handleDir($sourcedir, $targetdir, $lang);
93 }
94 close($helpdirJabRef);
95
96 exit 0;
97
98
99
100 # Parameters:
101 #    sourcedir
102 #    targetdir
103 #    language
104 sub handleDir {
105         my $sourcedir = shift;
106         my $targetdir = shift;
107         my $lang = shift;
108
109         print("Handling $sourcedir...\n");
110
111         if (!-d $targetdir) {
112                 mkdir($targetdir);
113         }
114
115         my $dh;
116         opendir($dh, $sourcedir) or die $!;
117         while (my $infilename = readdir($dh)) {
118                 next unless ($infilename =~ /\.html$/);
119                 my $outfilename =  $infilename;
120                 $outfilename =~ s/\.html/\.php/g;
121                 my $sourcefilename = $sourcedir . "/" . $infilename;
122                 my $targetfilename = $targetdir . "/" . $outfilename;
123                 handleFile($sourcefilename, $targetfilename, $lang);
124         }
125         close($dh);
126 }
127
128 #
129 # Parameters:
130 #    infilename: source file (html)
131 #    outfile:    target file (php)
132 #    lang:       language (ISO-format)
133 #
134 sub handleFile {
135   my $infilename  =  shift;
136   my $outfilename = shift;
137   my $lang = shift;
138
139   my $replace_placeholders = ($infilename =~ /About.html$/);
140
141   #Debug output
142   #print("handleFile:\n$infilename\n$outfilename\n$lang\n$replace_placeholders\n\n");
143
144   open(my $infileH, "<", $infilename) or die "cannot open < $infilename: $!";
145   my @infile = <$infileH>;
146
147   my @outfile=();
148
149   # Determine title out of first h1 heading
150   my $title="";
151   my $line;
152   foreach $line(@infile) {
153     if ($line =~ /\<h1\>(.*)\<\/h1\>/) {
154       $title=$1;
155           if ($replace_placeholders) {
156             $title =~ s/$jabref_placeholder_version/$jabref_version/;
157             $title =~ s/$jabref_placeholder_year/$jabref_year/;
158           }
159           # title is found, go to the normal handling
160           last;
161     }
162   }
163
164   #remove html tags from title
165   #even if <em> is not allowed in h1 elements, JabRef doc uses that
166   $title =~ s#<(.|\n)*?>##g;
167
168 #Following prefix does not work at sourceforge.
169 #<?xml version=\"1.0\" encoding=\"UTF-8\"?>
170 #We use php's header statement instead
171
172   #add to the relative path to navigation|footer if help is non-english
173   my $pathaddition;
174   if ($lang eq 'en') {
175     $pathaddition = "";
176   } else {
177     $pathaddition = "../";
178   }
179
180   my $navigationlink = $pathaddition . "../navigation.php";
181   my $footerlink = $pathaddition . "../footer.php";
182
183   my $header=<<HTML;
184 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
185    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
186 <html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"$lang\" xml:lang=\"$lang\">
187 <?php
188   header('Content-type: application/xhtml+xml; charset=utf-8');
189
190   // DO NOT EDIT BY HAND
191   // This file is generated from jabref/src/help.
192   // Run generate-htdocs-help-from-jabref-src-help.pl in the root directory
193   // of the JabRef repository to regenerate the htdocs out of JabRef's help.
194 ?>
195 <head>
196   <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=UTF-8\" />
197   <title>$title</title>
198   <link href=\"/css/style.css\" rel=\"stylesheet\" type=\"text/css\" />
199 </head>
200
201 <body>
202   <div id=\"container\">
203     <?php include(\"$navigationlink\"); ?>
204     <a href=\"Contents.php\">$translation_back_to_contents{$lang}</a>
205
206 HTML
207
208   my $footer=<<HTML;
209   <?php include(\"$footerlink\"); ?>
210   </div>\n\n</body>\n</html>
211 HTML
212
213   push(@outfile, $header);
214
215   my $status=0;
216   # 0 out of html
217   # 1 in html
218   # 2 within basefont
219
220   foreach $line(@infile) {
221     #Debug states
222         #print "$status / $line";
223
224     if ($status==0 && $line =~ /\<body/) {
225       $status=1;
226     } elsif ($status==1 && $line =~ /\<\/body\>/) {
227       $status=0;
228     } elsif ($status==1) {
229       #we may not transfer a "basefont"
230           if ($line =~ /\<basefont/) {
231             if ($line !~ /\/\>/) {
232                   $status = 2;
233                 }
234           } else {
235             if ($replace_placeholders) {
236               $line =~ s/$jabref_placeholder_version/$jabref_version/;
237               $line =~ s/$jabref_placeholder_year/$jabref_year/;
238             }
239             if (!($line =~ /href=\"http:\/\//)) {
240                   #line does NOT contain a href to some http address
241                   #we assume that line is NOT a reference to an external site
242                   #replace "html" extension with "php" extension
243                   #still allow links as "...html#part".
244                   $line =~  s/href=\"([^\"]*)\.html/href=\"$1\.php/g;
245             }
246         push(@outfile, $line);
247           }
248         } elsif (($status==2) && ($line =~ /\/\>/)) {
249                 #basefont ended, reset to "inhtml"
250                 $status = 1;
251         }
252   }
253
254   push(@outfile, $footer);
255
256   open(OUTFILE,">$outfilename");
257
258   if (FORCE_WINDOWS_NEWLINES) {
259         foreach my $line (@outfile) {
260                 $line =~ s/\r?\n|\r/\r\n/g;
261         }
262   }
263
264   print OUTFILE @outfile;
265
266   close(OUTFILE);
267
268   close($infileH);
269 }
270
271 #extracts info out of build.xml
272 #       <property name="jabref.version" value="2.8b" />
273 #       <property name="jabref.year" value="2012" />
274 #       <property name="jabref.placeholder.version" value="@version@" />
275 #       <property name="jabref.placeholder.year" value="@year@" />
276 sub loadPreferences {
277   open(my $buildXML, "<", BUILDXML) or die "cannot open < " . BUILDXML . ": $!";
278   my @buildxml = <$buildXML>;
279   close($buildXML);
280   foreach my $line (@buildxml) {
281     #check for one-line property declaration name / value
282     if ($line =~ /property name="([^"]*)" value="([^"]*)"/) {
283           #copy value from value to local variable
284           #a non-hardcoded version using "eval" would also be possible
285           #the SLOC count would be equal to the count of the following (easier) given/when construct.
286           given($1) {
287                 when ("jabref.version") {
288                   $jabref_version = $2;
289                 }
290             when ("jabref.year") {
291                   $jabref_year = $2;
292                 }
293                 when ("jabref.placeholder.version") {
294                   $jabref_placeholder_version = $2;
295                 }
296                 when ("jabref.placeholder.year") {
297                   $jabref_placeholder_year = $2;
298                 }
299       }
300     }
301   }
302 }