Class: ThumbnailPageGenerator

Inherits:
HtmlGenerator show all
Defined in:
lib/piggy-core/thumbnail_page_generator.rb

Overview

ThumbnailPageGenerator generates html-pages for an internet picture album. It does no image processing at all, this has to happen outside.

Generation without frames:

output_path
 |
 +----subDir
          |
          +----style
          |      |
          |      ...(stylesheet and background images)
          |
          +----index.htm
          |
          ...(3 files per image)

The 3 files per image are:

  • thumbnail image

  • image

  • image embedded in html with some basic navigation

Generation with frames:

output_path
 |
 +----subDir
 |        |
 |        +----index.htm
 |        |
 |        ...(3 files per image)
 |
 +----style
 |      |
 |      ...(stylesheet and background images)
 |
 +----index.htm
 |
 +----"#{subDir}.htm"
 |
 +----navigation.htm (updated)
 |
 +----kopf.htm
 |
 +----fuss.htm

You may replace kopf.htm and fuss.htm by your own files.

Constant Summary collapse

HtmlExt =
'.htm'
DownloadName =
'download.zip'
SlideshowName =
'slideshow'
JavaFxName =
'animation'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HtmlGenerator

#a, #add, #add_html, #add_item, #begin_tag, #body, #br, #center, #channel, #cr, #dd, #dec_depth, #description, #div, #dl, #dt, #end_tag, #escape_html, #format_tag, #frame, #frameset, #get_page, #h1, #h2, #h3, #head, #html, #img, #inc_depth, #language, #link, #meta, #new_frame, #new_rss, #new_transitional, #new_xml, #noframes, #norobots, #p, #quote, #reset, #span, #stylesheet, #table, #tag, #td, #title, #tr, #utf8

Constructor Details

#initialize(options) ⇒ ThumbnailPageGenerator

Returns a new instance of ThumbnailPageGenerator.



66
67
68
69
70
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 66

def initialize(options)
  @client = self
  @options = options
  @indexName = 'index' + HtmlExt
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



58
59
60
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 58

def client
  @client
end

#imageOutputPathObject (readonly)

Returns the value of attribute imageOutputPath.



58
59
60
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 58

def imageOutputPath
  @imageOutputPath
end

#indexNameObject (readonly)

Returns the value of attribute indexName.



58
59
60
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 58

def indexName
  @indexName
end

#subDirObject (readonly)

Returns the value of attribute subDir.



58
59
60
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 58

def subDir
  @subDir
end

Instance Method Details

#add_preview(info) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 388

def add_preview(info)
  div({'class' => 'preview'}){
    div({'class' => 'thumb'}){
      p {
        name = escape_html(info.name_without_extension)
        a({
            "href" => name + HtmlExt,
            'target' => '_top'}
        ) {
          img(image_name(info, 't_'), name)
        }
      }
    }
  }
end

#backObject



101
102
103
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 101

def back
  return "..#{sep}"
end

#build_frame_pageObject



450
451
452
453
454
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 450

def build_frame_page()
  content = frame_text
  write_file(File.join(output_path, subDir + HtmlExt), content)
  write_file(File.join(output_path, indexName), content)
end

#build_html_pages_for_images(progress) ⇒ Object



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
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 156

def build_html_pages_for_images(progress)
  progress.set_task("Generate html", @inputFilenames.size)
  max_ind = @inputFilenames.size - 1
  backlink = get_backlink_string
  (0..max_ind).each { | i |
    currentInfo = @inputFilenames[i]
    predInfo = @inputFilenames[i == 0 ? max_ind : i - 1]
    succInfo = @inputFilenames[i == max_ind ? 0 : i + 1]
    current = currentInfo.name_without_extension
    comment = currentInfo.comment
    pred = predInfo.name_without_extension
    succ = succInfo.name_without_extension
    progress.set_progress(current, 1)
    new_transitional
    html {
      head {
        title() { escape_html(current) }
        common_head
        stylesheet sub_stylesheet_url
      }
      body({"class"=>"image_page"}) {
        div({'class' => 'content'}){
          div({'class' => 'navigation', 'id'=>'toprevnext'}){
            div {
              div {
                p {
                  span {
                    a({"HREF" => escape_html(pred) + HtmlExt}){
                      add(i == 0 ? 'last' : 'previous')
                    }
                  }
                  span {
                    a({"HREF" => escape_html(backlink) }) {
                      add('overview')
                    }
                  }
                  if(@options.addSlideshow?)
                    span {
                      a({"HREF" => escape_html(SlideshowName + HtmlExt + "?" + image_name(currentInfo)) }) {
                        add('slideshow')
                      }
                    }
                  end
                  span {
                    a({"HREF" => escape_html(succ) + HtmlExt}) {
                      add(i == max_ind ? 'first' : 'next')
                    }
                  }
                }
              }
            }
          }
          div({'class' => 'image'}){
            div {
              div {
                p {
                  img(image_name(currentInfo), escape_html(current))
                }
              }
            }
          }
          div({'class' => 'comment'}){
            div {
              div {
                p { add_html(comment) }
              }
            }
          }
        }
      }
    }
    File.open(File.join(imageOutputPath, current + HtmlExt), 'w') { | fp |
      fp.print(get_page)
    }
  }
end

#build_javafx_configObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 233

def build_javafx_config()
  new_xml do
    tag('root', {}) do
        add("<title>#{escape_html(@title)}</title>")
        cr
        tag('images', {}){
          cr
          maxInd = @inputFilenames.size - 1
          (0..maxInd).each { | i |
            current = image_name(@inputFilenames[i])
            add("<image src=\"#{string_as_utf8(current)}\"/>")
            cr
          }
        }
        tag('layout', {}) {
          add("<imageSize width=\"#{@options.imageWidth}\" height=\"#{@options.imageHeight}\"/>")
        }
    end
  end
  write_file(File.join(imageOutputPath, 'config.xml'), get_page)
end

#build_slideshow_listObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 255

def build_slideshow_list
  File.open(File.join(imageOutputPath, 'images.js'), 'w') { | fp |
    fp.print("var images = new Array(\n")
    maxInd = @inputFilenames.size - 1
    (0..maxInd).each { | i |
      current = slideshow_image(@inputFilenames[i])
      fp.print("#{current}")
      fp.print(i == maxInd ? "\n" : ",\n")
    }
    fp.print(")\n")
  }
  unless @inputFilenames.empty?
    update_slideshow_template(@inputFilenames[1])
  end
end

#build_thumbnail_page(size_of_zipfile = -1)) ⇒ Object



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
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 300

def build_thumbnail_page(size_of_zipfile = -1)
  num_col = 5
  matrix = Array.new(@inputFilenames.size() / num_col)
  row = -1
  col = 0
  @inputFilenames.each { |info|
    if col % num_col == 0
      col = 0
      row += 1
      matrix[row] = Array.new(num_col)
    end
    matrix[row][col] = info
    col += 1
  }
  new_transitional
  html() {
    head() {
      title() { @title }
      common_head()
      stylesheet sub_stylesheet_url
    }
    body({'class'=>'thumbs'}){
      h3({}){ add(escape_html(@title)) }
      div({'class'=>'navigation', 'id'=>'toslides' }) do
        if(size_of_zipfile > 0)
          span {
            a({"href" => DownloadName }) { add_html(DownloadName) }
            add_html("(#{bytes_as_string(size_of_zipfile)})")
          }
        end
        if(@options.addSlideshow?)
          span {
            a({"href" => File.join('slideshow.htm'), "target" => "_top"}) {
              add_html(SlideshowName)
            }
          }
        end
        if(@options.addJavaFxSlideshow?)
          span {
            a({"href" => File.join('javafx.htm'), "target" => "_top"}) {
              add_html(JavaFxName)
            }
          }
        end
      end
      image_table({'class'=>'table'}) {
        matrix.each do |m_row|
          image_row {
            m_row.each do |info|
              if info
                image_column {
                  add_preview(info)
                }
              end
            end
          }
        end
      }
    }
  }
  File.open(File.join(imageOutputPath, indexName), 'w') { | fp |
    fp.print(get_page) }
end

#build_zip_file(nameOfZipFile, progress) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 461

def build_zip_file(nameOfZipFile, progress)
  require 'zip/zip'
  progress.set_task("Creating zipfile", @inputFilenames.size)
  Zip::ZipFile.open(nameOfZipFile, Zip::ZipFile::CREATE) {
    |zipfile|
    zipfile.mkdir(subDir)
    @inputFilenames.each do
      |info|
      progress.set_progress("Zip #{info.name}", 1)
      entry = File.join(subDir, info.name)
      src = info.name_with_path
      zipfile.add(entry, src) 
    end
  }
end

#bytes_as_string(bytes) ⇒ Object



291
292
293
294
295
296
297
298
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 291

def bytes_as_string(bytes)
  kb = bytes/1024
  return "#{kb} KB" if kb < 1024
  mb = kb/1024
  return "#{mb} MB" if mb < 1024
  gb = mb/1024
  return  "#{gb} GB"
end

#common_headObject

Add important meta tags



134
135
136
137
138
139
140
141
142
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 134

def common_head
  norobots()
  # the next tag will tell Piggy that this file may be
  # overwritten by Piggy
  meta({
      "name" => "generator",
      "content" =>"Piggy"})
  utf8()
end

#conversion_type_for(type) ⇒ Object

Overwrite this, if you convert arbitrary image types into output formats suitable for web pages, e. g. jpg or png



79
80
81
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 79

def conversion_type_for(type)
  type
end

#copy_style(targetPath) ⇒ Object



508
509
510
511
512
513
514
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 508

def copy_style(targetPath)
  target_dir = File.join(targetPath, 'style')
  File.makedirs(target_dir)
  Dir[File.join(style_source_dir, '*.*')].each { |fName|
    File.copy(fName, target_dir) unless File.exists?(File.join(target_dir, fName))
  }
end

#copy_template_filesObject



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 477

def copy_template_files()
  if @options.generateFramePage
    copy_templates(['navigation.htm', 'kopf.htm', 'fuss.htm'],
    output_path)
    copy_style(output_path)
  else
    copy_style(imageOutputPath)
  end
  if @options.addSlideshow?
    copy_templates(['slideshow.htm', 'slideshow.js'], imageOutputPath)
    link = "<link href=\"#{sub_stylesheet_url}\" rel=\"stylesheet\" type=\"text/css\">"
    insert_in_file(File.join(imageOutputPath, 'slideshow.htm'), /<!--STYLE-->/, link)
  end
  if @options.addJavaFxSlideshow?
    fx_files = ['javafx.htm', 'PiggyFX.jnlp', 'PiggyFX_browser.jnlp']
    copy_templates(fx_files + ['PiggyFX.jar'], imageOutputPath)
    fx_files.each { |file_name|
      # magic numbers from javafx code
      w = @options.imageWidth + 205
      h = @options.imageHeight + 140
      h = 700 if h < 700 # need this height for image bar with 5 images
      replace_in_file(File.join(imageOutputPath, file_name), /@WIDTH@/, w.to_s)
      replace_in_file(File.join(imageOutputPath, file_name), /@HEIGHT@/, h.to_s)
    }
  end
end

#copy_templates(files, targetPath) ⇒ Object



516
517
518
519
520
521
522
523
524
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 516

def copy_templates(files, targetPath)
  files.each do
    |fName|
    unless File.exists?(File.join(targetPath, fName))
      templateFile = File.join(PIGGY_PATH, 'templates', fName)
      File.copy(templateFile, targetPath) 
    end
  end
end

#create_subdirObject

Will create the directory for all the ‘3 files per image’ unless directory exits



568
569
570
571
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 568

def create_subdir
  sub_dir_with_path = File.join(output_path, subDir)
  Dir.mkdir(sub_dir_with_path) unless  File.directory?(sub_dir_with_path)
end

#error(msg) ⇒ Object



560
561
562
563
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 560

def error msg
  puts "Error: #{msg}"
  return false
end

#frame_textObject



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 404

def frame_text()
  new_frame
  html(){
    head(){
      title { 'Bildindex' }
      common_head
      stylesheet main_stylesheet_url
    }
    frameset({'rows'=>'14%,65%,21%'}){
      frame({
          'src'=>"kopf.htm",
          'name'=>"Kopf",
          'scrolling'=>"no",
          'frameborder'=>"0"
        })
      frameset({'cols'=>"15%,85%"}){
        frame({
            'src'=>"navigation.htm",
            'name'=>"Navigation",
            'scrolling'=>"yes",
            'frameborder'=>"0"
          })
        frame({
            'src'=>"#{subDir + sep + indexName}",
            'name'=>"Daten",
            'frameborder'=>"0"
          })
      }
      frame({
          'src'=>"fuss.htm",
          'name'=>"Fuss",
          'scrolling'=>"no",
          'frameborder'=>"0"
        })
      noframes(){
        cr
        add('Ohne Frames siehe: ')
        a({'href'=>"navigation.htm"}){
          add('Navigationsleiste')
        }
      }
    }
  }
  return get_page
end

#generate(progress, overwrite = false, add_zipfile = false) ⇒ Object

Central bublic doit function



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 575

def generate(progress, overwrite=false, add_zipfile=false)
  unless File.directory?(output_path)
    return error("#{output_path} ist no directory}")
  end
  if File.directory?(File.join(output_path, subDir))
    return error("#{subDir} allready exists") unless overwrite
  else
    progress.set_task("Creating directory")
    create_subdir
  end
  size_of_zipfile = -1
  if add_zipfile
    name_of_zip_file = File.join(output_path, subDir, DownloadName)
    build_zip_file(name_of_zip_file, progress)
    size_of_zipfile = File.size(name_of_zip_file) if File.exists?(name_of_zip_file)
  end
  build_html_pages_for_images(progress)
  build_thumbnail_page(size_of_zipfile)
  copy_template_files
  build_slideshow_list if @options.addSlideshow?
  build_javafx_config if @options.addJavaFxSlideshow?
  if @options.generateFramePage?
    build_frame_page
    update_navigation_frame
  end
  image_processing(progress) unless @options.skipImageProcessing
  return true
end


117
118
119
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 117

def get_backlink_string
  @options.generateFramePage? ? back + subDir + HtmlExt : indexName
end

#image_column(&block) ⇒ Object



380
381
382
383
384
385
386
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 380

def image_column(&block)
  if @options.useTable?
    td({}, &block)
  else
    dd({}, &block)
  end
end

#image_name(info, pre = '') ⇒ Object



83
84
85
86
87
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 83

def image_name(info, pre = '')
  offset = @options.stylesSample? ? "../basic/#{pre}" : pre
  name = "#{info.name_without_extension}.#{conversion_type_for(info.extension)}"
  offset + escape_html(name)
end

#image_row(&block) ⇒ Object



372
373
374
375
376
377
378
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 372

def image_row(&block)
  if @options.useTable?
    tr({}, &block)
  else
    block.call
  end
end

#image_table(params, &block) ⇒ Object



364
365
366
367
368
369
370
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 364

def image_table(params, &block)
  if @options.useTable?
    table(params, &block)
  else
    dl(params, &block)
  end
end

#insert_in_file(file, pattern, insertion) ⇒ Object

Insert insertion into file after any match of pattern



527
528
529
530
531
532
533
534
535
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 527

def insert_in_file(file, pattern, insertion)
  lines = File.readlines(file)
  File.open(file, 'w') { |fp|
    lines.each { |line|
      fp.print(line)
      fp.print(insertion) if line =~ pattern
    }
  }
end

#is_piggy_file?(file) ⇒ Boolean

Check if this file has been written by Piggy

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 145

def is_piggy_file?(file)
  ret = false
  File.open(file, 'r') do |fp|
    while !ret && (line = fp.gets)
      ret = /<meta.*name="generator"/.match(line) &&
        /<meta.*content="Piggy"/.match(line)
    end
  end
  return ret
end

#main_index_pathObject



109
110
111
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 109

def main_index_path
  return @options.generateFramePage ? output_path : imageOutputPath
end

#main_index_with_pathObject



113
114
115
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 113

def main_index_with_path
  return main_index_path + sep + indexName
end

#main_stylesheet_urlObject



129
130
131
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 129

def main_stylesheet_url
  'style/style.css'
end

#output_pathObject



105
106
107
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 105

def output_path
  return @options.localDestinationPath
end

#replace_in_file(file, pattern, replacement) ⇒ Object



537
538
539
540
541
542
543
544
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 537

def replace_in_file(file, pattern, replacement)
  lines = File.readlines(file)
  File.open(file, 'w') { |fp|
    lines.each { |line|
      fp.print(line.gsub(pattern, replacement))
    }
  }
end

#sepObject



97
98
99
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 97

def sep
  return "#{File::SEPARATOR}"
end

#set_files(fileInfos) ⇒ Object



89
90
91
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 89

def set_files(fileInfos)
  @inputFilenames = fileInfos  
end

#set_output_directory(outputDir) ⇒ Object



72
73
74
75
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 72

def set_output_directory(outputDir)
  @subDir = outputDir
  @imageOutputPath = File.join(output_path, outputDir)
end

#set_title(title) ⇒ Object



93
94
95
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 93

def set_title(title)
  @title = title
end

#slideshow_image(fileInfo) ⇒ Object



271
272
273
274
275
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 271

def slideshow_image(fileInfo)
  src = image_name(fileInfo)
  txt = escape_html(fileInfo.comment)
  "new slide(\"#{src}\", \"\", \"#{txt}\")"
end

#style_source_dirObject



504
505
506
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 504

def style_source_dir
  File.join(PIGGY_PATH, 'templates/styles', @options.style)
end

#sub_stylesheet_urlObject



121
122
123
124
125
126
127
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 121

def sub_stylesheet_url
  if @options.generateFramePage?
    back + main_stylesheet_url
  else
    main_stylesheet_url
  end
end

#update_navigation_frameObject



546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 546

def update_navigation_frame()
  reset
  nav_file = File.join(output_path, 'navigation.htm')
  a({
      'href' => escape_html(subDir) + '/index.htm',
      'target' => 'Daten'}) {
    add_html(@title)
  }
  br
  add_html("\n")
  link = get_page
  insert_in_file(nav_file, /<!--NAV-->/, link)
end

#update_slideshow_template(imageFile) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 277

def update_slideshow_template(imageFile)
  reset
  file = File.join(imageOutputPath, 'slideshow.htm')
  lines = File.readlines(file)
  backlink = "href=\"#{get_backlink_string}\""
  File.open(file, 'w') { |fp|
    lines.each { |line|
      line = line.gsub('PLACEHOLDER_FOR_IMAGE_NAME', image_name(imageFile))
      line = line.gsub('href="index.htm"', backlink)
      fp.print(line)
    }
  }
end

#write_file(file, content) ⇒ Object



456
457
458
459
# File 'lib/piggy-core/thumbnail_page_generator.rb', line 456

def write_file(file, content)
  return if File.exists?(file) && !is_piggy_file?(file)
  File.open(file, 'w') { |fp| fp.print(content) }
end