File: pcd2html_create_index

package info (click to toggle)
pcd2html 0.5.1.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 384 kB
  • ctags: 56
  • sloc: perl: 1,405; makefile: 120; sh: 69
file content (376 lines) | stat: -rwxr-xr-x 11,778 bytes parent folder | download | duplicates (3)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/perl -w
# pcd2html modul pcd2html_create_index
# Copyright Andreas Tille <tille@debian.org>
#
# This modul creates a main index to all subdirectories and
# an index of each subdirectory
## use strict ;
use pcd2html_uti ;
use strict ;

my $debug = 0;

my ( $title, 
     $textfile,
     %item
   ) ;
# Copy CSS file
system "cp -a ${datadir}/" . GetCSS() . ' ' . GetCSS() ;
# Copy needed icons
system "cp -a ${datadir}/*.png ." ;

if ( -f "missing" ) { unlink "missing" ; } ;

$title="PCD created on `date +%Y/%m/%d` using <a href=\"" . GetPcd2HtmlSourceUrl() .
       "\">pcd2html</a>" ;

%item = GetTranslationItems("eng") ;
CreateFooterString ( $item{"lang"}, "index".$item{"text"} ) ;
($title, $textfile) = GetTitle("index", $title, $item{"text"}) ;

my ( $do_create_index_icons, @IndexIcons ) ;
$do_create_index_icons = 1;
if ( CreateHtmlIndex( $title, $textfile ) ) { exit ; }
$do_create_index_icons = 0;

$title="PCD erstellt am " . `date +%d.%m.%Y` . " mittels <a href=\"" . GetPcd2HtmlSourceUrl() .
       "\">pcd2html</a>" ;

%item = GetTranslationItems("deu") ;
CreateFooterString ( $item{"lang"}, "index".$item{"text"} ) ;
($title, $textfile) = GetTitle("index", $title, $item{"text"}) ;

CreateHtmlIndex( $title, $textfile );

sub GetNumImg {
  # count subdirectories mentioned in main rules and number of icons in one line

  my ( $i, $icons ) ;

  $i     = 0 ;
  $icons = 3 ;
  open(RULES, "rules" ) || die "Unable to open rules file\n" ;
  while ( <RULES> ) {
    unless ( /^#/ ) {
      if ( /^\w/ ) { $i++ ; }
    }
    if ( /^#*\s*icons\s*=\s*([0-9]*)/ ) {
      $icons = $1 ;
    }
  }
  close RULES ;

  return ( $i, $icons ) ;
}

sub PrintEntry {
  ## print a single entry in the table of icons
  ## $_[0]  entry to print
  ## $_[1]  number of current entry
  ## $_[2]  number of icons per row
  ## $_[3]  number of entries left
  ##
  ## returns ( $i, $nimg) 

  my ( $entry,    # 
       $i,        # 
       $numicons, # 
       $nimg      # 
     );

  $entry    = $_[0];
  $i        = $_[1];
  $numicons = $_[2];
  $nimg     = $_[3];

#        print "<td width=\"100\" height=\"100\" class=\"icon\" " ;
  print "  <td class=\"icon\">$entry</td>\n" ;
  if ( $i == $numicons ) { 
    print " </tr>\n" ;
    if ( $nimg > 1 ) { print " <tr>\n" ; }
    $i = 0;
  }
  $i++ ;
  $nimg--;

  return ($i, $nimg) ;
}

##############################################################################################
#  create main index and index in subdirectories
#
sub CreateHtmlIndex  {
  my ( $keywords, $title, $textfile, $back, $home, $nums, $size, $html, $jpg, $nimg, $entry, $i,
       $sub, @sortimg, %subtitle, %images, $numicons, $w, $h, $numsubdirs,
       $previndex, $previndextext, $nextindex, $nextindextext, 
       @Indizees, @NextIndizees, @PrevIndizees );

  $title    = $_[0];
  $textfile = $_[1];
  
  File2String( "keywords." . $item{"lang"} ) ;
  File2String( "content." . $item{"lang"} ) ;

  # open main index file for writing
  $html = "index" . $item{"hext"} ;
  unless ( open(HTML, ">$html" ) ) { die "Unable to open $html\n" ; }
  select HTML ;
  MyHtmlStart($title, $keywords, undef, GetCSS(), undef ) ;

  ($back, $home) = GetTextHome($textfile) ;
  MyNavigation($item{"home"}, $item{"top"}, $item{"other"},
               $item{"oicon"}, $item{"otitle"}, $back, $home, '');

  print "<h1>$title</h1>\n<p class=\"justify\">\n" ;
  if ( -f $textfile ) {
    File2HTML( $textfile, 0, $title ) ;
  }
  print "</p>\n<table class=\"index\">\n <tr>\n" ;

  ($nimg, $numicons) = GetNumImg();

  # create table of links to the subdriectories with icon image table
  open(RULES, "rules" ) || die "Unable to open rules file\n" ;

  $i = 1;
  $numsubdirs = 0;
  @IndexIcons = () ;
  @Indizees   = () ;
  while ( <RULES> ) {
    unless ( /^#/ ) {
      if ( /^\w/ ) {
        /([^ ]*) *{(.*)} *\[(.*)\]/ ;
        $sub = $1 ;
        $numsubdirs++;
        if ( $item{"lang"} eq "en" ) {
	    $subtitle{$sub} = $2 ;
        } else {
	    $subtitle{$sub} = $3 ;
        }
        my $tmp_icon = undef ;
        open(SUBRULES, "$sub/rules") || die "Unable to open $sub/rules\n" ;
        while ( <SUBRULES> ) {
          unless ( /^#/ ) {
            s/\s*#.*// ;
            if ( /(\w*):(\w*)\s.*\s\*/ ) {
              $tmp_icon = ExistingPicWithName ( "$sub/${1}_$2" ) ;
              last ;
            }
            unless ( $tmp_icon ) {
              /(\w*):(\w*)\s/ ;
              $tmp_icon = ExistingPicWithName ( "$sub/${1}_$2" ) ;
            }
	  }
        }
        close SUBRULES ;

        @Indizees = ( @Indizees, $sub ) ;
        $entry = "<a href=\"$sub\/" . $item{"index"} . "\">";
        if ( $tmp_icon ) {
          ($w, $h) = ImageSize($tmp_icon);
          $entry = "<a href=\"$sub\/" . $item{"index"} . "\"><img src=\"$tmp_icon\" width=\"$w\" height=\"$h\" ";
          $entry = "$entry alt=\"$sub\" /></a><br />\n   $subtitle{$sub}" ;
        } else {
          $entry = "$entry$subtitle{$sub}</a>" ;
        }
        @IndexIcons   = ( @IndexIcons, $tmp_icon ) ;

        ($i, $nimg) = PrintEntry ( $entry, $i, $numicons, $nimg ) ;
      }
    }
  }
  close RULES ;
  if  ( $do_create_index_icons == 1 ) { CreateIndexIcon(4); }

  if ( $i != 1 ) { print " </tr>\n" ; }
  print "</table>\n" ;

  MyHtmlEnd ( ) ;
  close HTML ;
  select STDOUT ;

  @NextIndizees  = ( ) ;
  @PrevIndizees  = ( "", "" ) ;
  $i             = 0;
  my $subdir ;
  foreach $subdir ( @Indizees ) { 
    @NextIndizees = ( @NextIndizees, $subdir ); 
    if ( $i < $numsubdirs ) { @PrevIndizees = ( @PrevIndizees, $subdir ); }
    $i++ ;
  }
  @NextIndizees = ( @NextIndizees, "" );
  shift( @PrevIndizees ) ;
  shift( @NextIndizees ) ;

  $_ = $back ;
  if ( defined ( $_ ) && /^\.\.\/?/ ) {
      $back = '../' . $back ;
  }
  $_ = $home ;
  if ( defined ( $_ ) && /^\.\.\/?/ ) {
      $home = '../' . $home ;
  }

  foreach $subdir ( @Indizees ) { 
    ### Prevent double '//' in case of first/last index!!!
    $previndextext = shift( @PrevIndizees ) ;
    if ( $previndextext ) { $previndex     = "../" . $previndextext; }
    else                  { $previndex     = ".." ; }
    $nextindextext = shift( @NextIndizees ) ;
    if ( $nextindextext ) { $nextindex     = "../" . $nextindextext; }
    else                  { $nextindex     = ".." ; }
    chdir $subdir ;
    print "Working in $subdir ...\n" ;
    unless ( open(HTML, ">$html" ) ) { die "Unable to open $html\n" ; }
    select HTML ;
    MyHtmlStart( $subtitle{$subdir}, undef, undef, "../".GetCSS(), undef );

    MyNavigation($item{"home"}, $item{"top"}, $item{"other"},
                 $item{"oicon"}, $item{"otitle"}, $back, $home, '../');

    print "<h1>$subtitle{$subdir}</h1>\n" ;

    %images  = () ;
    @sortimg = () ;

    ($nimg, $numicons) = GetNumImg();

    open(RULES, "rules" ) || die "Unable to open rules file\n" ;
    while ( <RULES> ) {
      unless ( /^#/ ) {
        if ( /(\w*):(\w*)\s\s*(\w*)/ ) {
	  $images{"$1_$2"} = $3 ;
          @sortimg         = (@sortimg, "$1_$2") ;
        } 
      }
    }

    @IndexIcons = ( ) ;

    print "<table width=\"100%\">\n <tr>\n  <th class=\"prev\">\n" ;
    # navigation images to the other directories
    print "   <a href=\"$previndex/" . $item{"index"} . '" class="prev">' . 
          "<img src=\"$previndex/index.jpg\" alt=\"$previndextext\" /></a>\n" ;
    print "  </th>\n   <th rowspan=\"2\">\n" ;
    print "   <table class=\"index\">\n <tr>\n" ;
    my $i = 1;
    my $num;
    foreach $num ( @sortimg ) {
      ($title, $textfile) = GetTitle($num, $images{$num}, $item{"text"});

      unless ( defined($title) ) {
	$title = $images{$num} ;
      }

      $jpg = ExistingPicWithName ( $num . $images{$num} ) ;

      if ( defined ($jpg) ) {
        my ( $tmp_icon ) ;
        $size = int ((( stat( $jpg ) )[7]  + 500) / 1000) ;
        $tmp_icon = ExistingPicWithName ( $num ) ;
        ($w, $h) = ImageSize($tmp_icon);
        $entry = '<a href="' . $num . $item{"hext"} . "\"><img src=\"$tmp_icon\" width=\"$w\" height=\"$h\" ";
        $entry = "$entry alt=\"$title\" /></a><br />\n   $title <span class=\"iconsize\">(${size}k)</span>" ;
        @IndexIcons = ( @IndexIcons, $tmp_icon ) ;
      }
      else {
        if ( $item{"lang"} eq "en" ) {
          $_ = $num ;
	  s/_.*// ;
          print STDOUT "To complete insert CD $_ and rerun pcd2html to build $images{$num}.\n" ;
          unless ( open(MISSING, ">>../missing" ) ) { die "Unable to open $html\n" ; }
	  print MISSING "$subdir/$num$images{$num}\n" ;
          close MISSING ;
          $entry = "Image $images{$num} is missing" ;
        } else {
          $entry = "Bild $images{$num} fehlt" ;
        }
      }

      ($i, $nimg) = PrintEntry ( $entry, $i, $numicons, $nimg ) ;
    }
    if  ( $do_create_index_icons == 1 ) { CreateIndexIcon(9); }
    if ( $i != 1 ) { print "    </tr>\n"; }
    print "   </table>\n  </th><th class=\"next\">\n" ;
    print "   <a href=\"$nextindex/" . $item{"index"} . '" class="next">' . 
          "<img src=\"$nextindex/index.jpg\" alt=\"$nextindextext\" /></a>\n" ;
    print "   </th>\n  </tr>\n  <tr>\n" ;
    print "  <th class=\"indexl\">\n" ;
    print IndexLink($item{"index"}, "index", 0);
    print "  </th><th class=\"indexr\">\n" ;
    print IndexLink($item{"index"}, "index", 0);
    print "  </td>\n </tr>\n" ; 
    if ( -f "index" . $item{"text"} ) {
      print "  <tr>\n    <th></th>\n    <th class=\"description\">" ;
      File2HTML( "index" . $item{"text"}, 1, $title ) ;
      print "  </th>\n    <th></th>\n  </tr>\n" ;
    }
    print "</table>\n" ;
    MyHtmlEnd ( '../' ) ;
    close HTML ;
    select STDOUT ;
    chdir ".." ;
  }
  return 0 ;
}

sub CreateIndexIcon {
  my ( $icon, $nicons, $i, @imgs, $minisize, $iconsize, $tile, $cropsize );

  $nicons = $_[0];
  if ( $nicons == 4 ) {
    $minisize = "32x24" ;
    $iconsize = "80x60" ;
    $tile     = "2x2" ;
  } else {
    $minisize = "24x18" ;
    $iconsize = "80x64" ;
    $tile     = "3x3" ;
  }
  $cropsize = GetCropSize($nicons);

  $i    = 0;
  @imgs = () ;

  my $cmdstring ;
  # build mini icons for building directory icon
  foreach $icon ( @IndexIcons ) {
    # Since ImageMagick 6.0.3 convert uses a new syntax
    # system GetConvert() . "-colors 64 -dither -geometry $minisize! $icon $icon.mini 2>> /dev/null" ;
    $cmdstring = GetConvert($icon) . " -colors 64 -dither -geometry $minisize! $icon.mini 2>> /dev/null" ;
    if ($debug) { print STDERR "DEBUG (index/339): $cmdstring\n" ; }
    system "$cmdstring" ;
    @imgs = ( @imgs, "$icon.mini" );
    $i++ ;
    if ( $i == $nicons ) {
      last ;
    }
  }
  # fill with empty icons if necessary
  while ( $i < $nicons ) {
    my $empty = $i . "empty.mini" ;
    # Since ImageMagick 6.0.3 convert uses a new syntax 
    # system "$convert -colors 64 -dither -geometry $minisize! ${datadir}/empty.xpm $empty 2>> /dev/null" ;
    $cmdstring = GetConvert("${datadir}/empty.xpm") . " -colors 64 -dither -geometry $minisize! $empty 2>> /dev/null" ;
    if ($debug) { print STDERR "DEBUG (index/355): $cmdstring ($i)\n" ; }
    system "$cmdstring" ;
    @imgs = ( @imgs, $empty );
    $i++ ;
  }
  my $zwindex = "zwindex.jpg" ;
  $cmdstring = GetMontage() . " -tile $tile -geometry $minisize -size $iconsize @imgs $zwindex" ;
  if ($debug) { print STDERR "DEBUG (index/358): $cmdstring\n" ; }
  system "$cmdstring" ;
  if ( ! -f $zwindex ) {
      if ( -f "$zwindex.0" ) {
	  rename("$zwindex.0", $zwindex) ;
      } else {
	  print STDERR "Problem to create page index\n";
	  return(-1) ;
      }
  }
  # Since ImageMagick 6.0.3 convert uses a new syntax 
  # system "convert -crop $cropsize+1+1 $zwindex index.jpg; rm -f $zwindex* @imgs" ;
  system GetConvert($zwindex) . " -crop $cropsize+1+1 index.jpg; rm -f $zwindex* @imgs" ;
}