2 #requires: perl >= 5.10
4 #generate-htdocs-help-form-jabref-src-help.pl
5 #(c) 2012 Kolja Brix and Oliver Kopp
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)
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
16 #There are NO command line parameters
18 #If you have newline issues at the generated files,
19 #adapt FORCE_WINDOWS_NEWLINES
23 #Use of uninitialized value in concatenation (.) or string at generate-htdocs-help-from-jabref-src-help.pl line 174, <$infileH> line 138.
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.
28 use constant HELPDIR_JABREF => "../src/main/resources/help";
29 use constant HELPDIR_WEB => "../../htdocs/help";
31 #0 for normal operationrequired
33 use constant FORCE_WINDOWS_NEWLINES => 0;
35 #translations for "Back to contents"
36 our %translation_back_to_contents = (
37 "da" => "Back to contents",
38 "de" => "Zurück zum Inhaltsverzeichnis",
39 "en" => "Back to contents",
40 "fr" => "Retour au contenu",
41 "in" => "Kembali ke Daftar Isi",
46 #build.xml for getting string replacements @version@ and @year@
47 use constant BUILDXML => "../build.xml";
61 our $jabref_placeholder_version;
62 our $jabref_placeholder_year;
66 #Debug call for a single file
67 #handleFile("../src/main/resources/help/About.html", "../../htdocs/help/About.php", "en");
72 handleDir(HELPDIR_JABREF, HELPDIR_WEB, "en");
74 #handle other languages (contained in sub directories)
78 opendir($helpdirJabRef, HELPDIR_JABREF) or die $!;
84 while (my $subdir = readdir($helpdirJabRef)) {
85 $sourcedir = HELPDIR_JABREF . "/$subdir";
86 next unless (-d $sourcedir);
87 next if ($subdir =~ /\.\.?/);
89 $targetdir = HELPDIR_WEB . "/$subdir";
92 handleDir($sourcedir, $targetdir, $lang);
94 close($helpdirJabRef);
105 my $sourcedir = shift;
106 my $targetdir = shift;
109 print("Handling $sourcedir...\n");
111 if (!-d $targetdir) {
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);
130 # infilename: source file (html)
131 # outfile: target file (php)
132 # lang: language (ISO-format)
135 my $infilename = shift;
136 my $outfilename = shift;
139 my $replace_placeholders = ($infilename =~ /About.html$/);
142 #print("handleFile:\n$infilename\n$outfilename\n$lang\n$replace_placeholders\n\n");
144 open(my $infileH, "<", $infilename) or die "cannot open < $infilename: $!";
145 my @infile = <$infileH>;
149 # Determine title out of first h1 heading
152 foreach $line(@infile) {
153 if ($line =~ /\<h1\>(.*)\<\/h1\>/) {
155 if ($replace_placeholders) {
156 $title =~ s/$jabref_placeholder_version/$jabref_version/;
157 $title =~ s/$jabref_placeholder_year/$jabref_year/;
159 # title is found, go to the normal handling
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;
168 #Following prefix does not work at sourceforge.
169 #<?xml version=\"1.0\" encoding=\"UTF-8\"?>
170 #We use php's header statement instead
172 #add to the relative path to navigation|footer if help is non-english
177 $pathaddition = "../";
180 my $navigationlink = $pathaddition . "../navigation.php";
181 my $footerlink = $pathaddition . "../footer.php";
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\">
188 header('Content-type: application/xhtml+xml; charset=utf-8');
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.
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\" />
202 <div id=\"container\">
203 <?php include(\"$navigationlink\"); ?>
204 <a href=\"Contents.php\">$translation_back_to_contents{$lang}</a>
209 <?php include(\"$footerlink\"); ?>
210 </div>\n\n</body>\n</html>
213 push(@outfile, $header);
220 foreach $line(@infile) {
222 #print "$status / $line";
224 if ($status==0 && $line =~ /\<body/) {
226 } elsif ($status==1 && $line =~ /\<\/body\>/) {
228 } elsif ($status==1) {
229 #we may not transfer a "basefont"
230 if ($line =~ /\<basefont/) {
231 if ($line !~ /\/\>/) {
235 if ($replace_placeholders) {
236 $line =~ s/$jabref_placeholder_version/$jabref_version/;
237 $line =~ s/$jabref_placeholder_year/$jabref_year/;
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;
246 push(@outfile, $line);
248 } elsif (($status==2) && ($line =~ /\/\>/)) {
249 #basefont ended, reset to "inhtml"
254 push(@outfile, $footer);
256 open(OUTFILE,">$outfilename");
258 if (FORCE_WINDOWS_NEWLINES) {
259 foreach my $line (@outfile) {
260 $line =~ s/\r?\n|\r/\r\n/g;
264 print OUTFILE @outfile;
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>;
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.
287 when ("jabref.version") {
288 $jabref_version = $2;
290 when ("jabref.year") {
293 when ("jabref.placeholder.version") {
294 $jabref_placeholder_version = $2;
296 when ("jabref.placeholder.year") {
297 $jabref_placeholder_year = $2;