Class: Hyla::Commands::Generate

Inherits:
Hyla::Command show all
Defined in:
lib/hyla/commands/generate.rb

Class Method Summary collapse

Class Method Details

.add_tag_to_index_file(index_file) ⇒ Object

Modify the content of an index file if it contains include::file with extension .ad, .adoc or .asciidoc and add the tag snippet ([] –> [tag=snippet])



794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/hyla/commands/generate.rb', line 794

def self.add_tag_to_index_file(index_file)
  if File.basename(index_file) == "AllSlides.txt" then
    content = ""
    File.readlines(index_file).each do |line|
      if line =~ /^include::.*\[\]$/
        replace = line.gsub(/\[/, '[tag=' + Configuration::SNIPPET_TAG)
        content = content.to_s + replace
      else
        content = content.to_s + line
      end
    end
    replace_content(index_file, content)
  end
end

.asciidoc_to_html(source, destination, extensions, excludes, options) ⇒ Object

Call Asciidoctor.render function



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
# File 'lib/hyla/commands/generate.rb', line 182

def self.asciidoc_to_html(source, destination, extensions, excludes, options)

  # Move to Source directory & Retrieve Asciidoctor files to be processed
  source = File.expand_path source
  @destination = File.expand_path destination

  Hyla.logger2.info ">>       Source dir: #{source}"
  Hyla.logger2.info ">>  Destination dir: #{@destination}"

  # Exit if source directory does not exist
  if !Dir.exist? source
    Hyla.logger2.error ">> Source directory does not exist"
    exit(1)
  end

  # Move to source directory
  Dir.chdir(source)
  current_dir = Dir.pwd
  Hyla.logger2.info ">>       Current dir: #{current_dir}"

  #
  # Backup Asciidoctor attributes
  # Strange issue discovered
  #
  @attributes_bk = options[:attributes]

  # Delete destination directory (generated_content, ...)
  # FileUtils.rm_rf(Dir.glob(@destination))

  adoc_file_paths = []

  #
  # Search for files into the current directory using extensions parameter as filter key
  # Reject directory specified and do the rendering
  #
  files = Dir[current_dir + "/**/*.{" + extensions + "}"].reject { |f| f =~ /\/#{excludes}\// }

  #
  # Check if snippet parameter is defined
  # as we have to modify the AllSlides.txt file
  # to include within the brackets this tag --> [tag=snippet]
  #
  if options[:snippet_content] == true
    files.each do |f|
      add_tag_to_index_file(f)
    end
  end

  files.each do |f|
    path_to_source = Pathname.new(source)
    path_to_adoc_file = Pathname.new(f)
    relative_path = path_to_adoc_file.relative_path_from(path_to_source).to_s
    Hyla.logger2.debug ">>       Relative path: #{relative_path}"
    adoc_file_paths << relative_path

    # Get asciidoctor file name
    file_name_processed = path_to_adoc_file.basename

    #
    # Create destination dir relative to the path calculated
    #
    html_dir = [@destination, File.dirname(relative_path)] * '/'
    Hyla.logger2.debug ">>        Dir of html: #{html_dir}"
    FileUtils.mkdir_p html_dir

    # Copy Fonts
    # TODO : Verify if we still need to do that as the FONTS liberation have been moved
    # TODO : under local lib directory of revealjs
    # self.cp_resources_to_dir(File.dirname(html_dir), 'fonts')

    # Copy Resources for Slideshow
    case options[:backend]
      when 'deckjs'
        # Copy css, js files to destination directory
        self.cp_resources_to_dir(File.dirname(html_dir), 'deck.js')
      when 'revealjs'
        self.cp_resources_to_dir(File.dirname(html_dir), 'revealjs')
      when 'revealjs-redhat'
        self.cp_resources_to_dir(File.dirname(html_dir), 'revealjs-redhat')
    end

    #
    # Render asciidoc to HTML
    #
    Hyla.logger2.info ">> File to be rendered : #{file_name_processed}"

    #
    # Convert asciidoc file name to html file name
    #
    html_file_name = file_name_processed.to_s.gsub(/.adoc$|.ad$|.asciidoc$|.index$|.txt$/, '.html')
    options[:to_dir] = html_dir
    options[:to_file] = html_file_name
    options[:attributes] = @attributes_bk
    Asciidoctor.render_file(f, options)

    # end
  end

  #
  # Check if snippet parameter is defined
  # and remove the snippet tag from indexed files
  #
  if options[:snippet_content] == true
    files.each do |f|
      remove_tag_from_index_file(f)
    end
  end

  # No asciidoc files retrieved
  if adoc_file_paths.empty?
    Hyla.logger2.info "     >>   No asciidoc files retrieved."
    exit(1)
  end

end

.backend_dir(backend) ⇒ Object

Return backend directory containing templates (haml, slim)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hyla/commands/generate.rb', line 113

def self.backend_dir(backend)
  case backend
    when 'deckjs'
      return [Configuration::backends, 'haml', 'deckjs'] * '/'
    when 'revealjs'
      return [Configuration::backends, 'slim', 'revealjs'] * '/'
    when 'revealjs-redhat'
      return [Configuration::backends, 'slim', 'revealjs-redhat'] * '/'
    when 'html5'
      return [Configuration::backends, 'slim', 'html5'] * '/'
    else
      return [Configuration::backends, 'slim', 'html5'] * '/'
  end
end

.check_mandatory_option?(key, value) ⇒ Boolean

Check mandatory options

Returns:

  • (Boolean)


837
838
839
840
841
842
843
844
# File 'lib/hyla/commands/generate.rb', line 837

def self.check_mandatory_option?(key, value)
  if value.nil? or value.empty?
    Hyla.logger2.warn "Mandatory option missing: #{key}"
    exit(1)
  else
    true
  end
end

.check_slash_end(out_dir) ⇒ Object

Add ‘/’ at the end of the target path if the target path provided doesn’t contain it



736
737
738
739
740
741
742
743
744
# File 'lib/hyla/commands/generate.rb', line 736

def self.check_slash_end(out_dir)
  last_char = out_dir.to_s[-1, 1]
  Hyla.logger2.info '>> Last char : ' + last_char
  if !last_char.equal? '/\//'
    temp_dir = out_dir.to_s
    out_dir = temp_dir + '/'
  end
  out_dir
end

.check_style(style) ⇒ Object

CSS Style to be used Default is : asciidoctor.css



303
304
305
306
307
308
309
# File 'lib/hyla/commands/generate.rb', line 303

def self.check_style(style)
  if !style.nil?
    css_file = [style, '.css'].join()
  else
    css_file = 'asciidoctor.css'
  end
end

.cover_img(out_dir, file_name, image_name, course_name, module_name, bg_image_path, tool = imgkit) ⇒ Object

Cover Function Create a png file using the HTML generated with the Slim cover template



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
# File 'lib/hyla/commands/generate.rb', line 132

def self.cover_img(out_dir, file_name, image_name, course_name, module_name, bg_image_path, tool = imgkit)

  unless Dir.exist? out_dir
    FileUtils.mkdir_p out_dir
  end

  case tool
    when 'imgkit'
      # Configure Slim engine
      slim_file = Configuration::cover_template
      slim_tmpl = File.read(slim_file)
      template = Slim::Template.new(:pretty => true) { slim_tmpl }

      # Replace underscore with space
      course_name = course_name.gsub('_', ' ')
      # Replace underscore with space, next digits & space with nothing & Capitalize
      module_name = module_name.gsub('_', ' ').gsub(/^\d{1,2}\s/, '').capitalize

      Hyla.logger2.debug "Module name : " + module_name
      Hyla.logger2.info "Curent directory : #{Dir.pwd}"

      # Use slim template & render the HTML
      parameters = {:course_name => course_name,
                    :module_name => module_name,
                    :image_path => bg_image_path }
      res = template.render(Object.new, parameters)
      #
      # Create the cover file and do the rendering of the image
      #
      Dir.chdir(out_dir) do
        out_file = File.new(file_name, 'w')
        out_file.puts res
        out_file.puts "\n"

        # Do the Rendering Image
        kit = IMGKit.new(res, quality: 100, width: 1280, height: 800)

        kit.to_img(:png)
        kit.to_file(image_name)
      end
      
    when 'wkhtmltopdf'  
      
  end

end

.cp_resources_to_dir(path, resource) ⇒ Object

Copy resources to target dir



313
314
315
316
317
318
319
# File 'lib/hyla/commands/generate.rb', line 313

def self.cp_resources_to_dir(path, resource)
  source = [Configuration::assets, resource] * '/'
  # destination = [path, resource] * '/'
  destination = path
  Hyla.logger2.debug ">>        Copy resource from Source dir: #{source} --> destination dir: #{destination}"
  FileUtils.cp_r source, destination, :verbose => false
end

.create_asset_directory(assets = []) ⇒ Object

Create Asset Directory



713
714
715
716
717
# File 'lib/hyla/commands/generate.rb', line 713

def self.create_asset_directory(assets = [])
  assets.each do |asset|
    Dir.mkdir(asset) if !Dir.exist? asset
  end
end

.create_index_file_withoutprefix(file_name, level) ⇒ Object

Create ascidoc index file containing references to asciidoc files part of a module



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/hyla/commands/generate.rb', line 770

def self.create_index_file_withoutprefix(file_name, level)
  index_file = File.new(Configuration::INDEX_FILE, 'w')

  index_file.puts Configuration::HEADER_INDEX
  index_file.puts "\n"
  # TODO - until now we cannot use level 0 for parent/children files
  # even if doctype: book
  # This is why the level for each index file title is '=='

  rep_txt = Configuration::INDEX.gsub(/xxx/, file_name)
  index_file.puts rep_txt
  index_file.puts "\n"

#
  # index_file.puts "\n"

  index_file
end

.create_index_file_withprefix(file_name, level) ⇒ Object

Create ascidoc index file containing references to asciidoc files part of a module TODO : Not longer used -> can be removed



751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/hyla/commands/generate.rb', line 751

def self.create_index_file_withprefix(file_name, level)
  n_file_name = file_name + Configuration::INDEX_SUFFIX
  index_file = File.new(n_file_name, 'w')

  index_file.puts Configuration::HEADER_INDEX
  index_file.puts "\n"
  # TODO - until now we cannot use level 0 for parent/children files
  # even if doctype: book
  # This is why the level for each index file title is '=='
  index_file.puts '== ' + file_name
  index_file.puts "\n"

  index_file
end

.extract_file_names(file_name, destination) ⇒ Object

Extract files names from a file containing include

directive



849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'lib/hyla/commands/generate.rb', line 849

def self.extract_file_names(file_name, destination)

  result = []
  f = File.open(file_name, 'r')
  matches = f.grep(Configuration::IncludeDirectiveRx)

  if matches

    matches.each do |record|
      # Extract string after include::
      matchdata = record.match(/^include::(.+)/)

      if matchdata

        data = matchdata[1]
        # Remove []
        name = data.to_s.gsub(/[\[\]]/, '').strip

        # Rename file to .html
        name = name.gsub(/ad$/, 'html')
        file_name = [destination, name] * '/'
        file_path = File.expand_path file_name
        result << file_path
      end
    end
  end
  f.close
  return result
end

.generate_summary_pageObject

#

# Generate PDF
#
def self.html_to_pdf(source, destination, html_file_name)
  file_path = [source, html_file_name] * '/'
  html_file = File.new(file_path)
  kit = PDFKit.new(html_file,
                   :page_size => 'A4',
                   :toc => true,
                   :page_offset => 1,
                   :footer_center => 'Page [page]')

  # Create destination directory if it does not exist
  unless File.directory?(destination)
    FileUtils.mkdir_p(destination)
  end

  # Save PDF to a file
  pdf_file_name = [destination, html_file_name.sub(/html|htm/, 'pdf')] * '/'
  kit.to_file(pdf_file_name)
  Hyla.logger2.info ">> PDF file generated and saved : #{pdf_file_name} "
end


624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/hyla/commands/generate.rb', line 624

def self.generate_summary_page()
  @index += 1
  file_index = sprintf('%02d', @index)
  f_name = 'm' + @module_key + 'p' + file_index + '_summary'
  f_name = f_name + Configuration::ADOC_EXT

  # rep_txt = Configuration::SUMMARY_TXT.gsub(/xxx\.mp3/, f_name + '.mp3')

  summary_f = File.new(f_name, 'w')
  summary_f.puts Configuration::HEADER_TXT
  summary_f.puts Configuration::SUMMARY_TXT
  summary_f.close

  #
  # Include summary file to index
  #
  @index_file.puts Configuration::INCLUDE_PREFIX + f_name + Configuration::INCLUDE_SUFFIX
  @index_file.puts "\n"
end

.html_to_pdf(file_name, source, destination, footer_text, header_html_path, cover_path) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/hyla/commands/generate.rb', line 644

def self.html_to_pdf(file_name, source, destination, footer_text, header_html_path, cover_path)

  @cover_path = cover_path
  destination= File.expand_path destination
  pdf_file = [destination, "result.pdf"] * '/'
  wkhtml_cmd = "wkhtmltopdf"
  size = 'A4'

  # pdf_file_name = [destination, html_file_name.sub(/html|htm/, 'pdf')] * '/'

  list_of_files = ""

  unless File.directory?(destination)
    FileUtils.mkdir_p(destination)
  end

  if file_name.nil? || file_name.empty?
    filter = [source] * '/' + "*.html"
    files = Dir[filter]

    files.each do |file|
      file_name = File.basename file
      next if file_name.downcase.include?('assessments')
      next if file_name.downcase.include?('labinstructions')
      next if file_name.downcase.include?('title')
      next if file_name.downcase.include?('cover')
      file = File.expand_path file
      list_of_files = list_of_files + " " + file
    end
  else
    #
    # If the file passed as parameter has extension name equal to txt, then we will extract the file names
    # whenever we have a include:: directive in the file
    #
    extension_name = File.extname file_name

    case extension_name
      when '.txt'
        file_to_processed = [File.expand_path(Dir.getwd), file_name] * '/'
        result = self.extract_file_names(file_to_processed, source)

        result.each do |file_path|
          if file_path.downcase.include?('title') || file_path.downcase.include?('cover')
            @cover_path = file_path
            next
          end
          list_of_files = list_of_files + " " + file_path
        end

      else
        list_of_files = [File.expand_path(source), file_name] * '/'
    end
  end

  wkhtml_cmd.concat " #{list_of_files} #{pdf_file}"
  wkhtml_cmd.concat " --margin-top '18mm' --header-html '#{header_html_path}'" if header_html_path
  wkhtml_cmd.concat " --margin-bottom '10mm'  --footer-center '#{footer_text}'" if footer_text
  wkhtml_cmd.concat " --cover '#@cover_path'" if @cover_path
  wkhtml_cmd.concat " --page-size #{size}"
  Hyla.logger2.debug "c #{wkhtml_cmd}"

  Dir.chdir(source) do
    system "#{wkhtml_cmd}"
  end
end

.process(args, options = {}) ⇒ Object



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
# File 'lib/hyla/commands/generate.rb', line 6

def self.process(args, options = {})

  rendering = options[:rendering] if self.check_mandatory_option?('-r / --rendering', options[:rendering])

  case rendering

    when 'toc2adoc'

      Hyla.logger2.info "Rendering : Table of Content to Asciidoc"
      self.check_mandatory_option?('-t / --toc', options[:toc])
      self.check_mandatory_option?('-d / --destination', options[:destination])

      @toc_file = options[:toc]
      @out_dir = options[:destination]
      @project_name = options[:project_name] if options[:project_name]
      @project_name = 'My Project' if !options[:project_name]
      @image_path = options[:image_path] if options[:image_path]

      self.table_of_content_to_asciidoc(@toc_file, @out_dir, @project_name, @image_path)

    when 'adoc2html'

      Hyla.logger2.info "Rendering : Asciidoc to HTML"
      self.check_mandatory_option?('-s / --source', options[:source])
      self.check_mandatory_option?('-d / --destination', options[:destination])

      @destination = options[:destination]
      @source = options[:source]

      # Check Style to be used
      new_asciidoctor_option = {
          :template_dirs => [self.backend_dir(options[:backend])],
          :attributes => {
              'stylesheet' => self.check_style(options[:style])
          }
      }

      merged_options = Configuration[options].deep_merge(new_asciidoctor_option)

      extensions = 'adoc,ad,asciidoc'
      excludes = 'lab_assets|lab_assets_solution|code|snippets|templates|generated_content|generated_content_instructor|generated_content_snippet|generated_slideshow|generated_content_pdf|generated_content_students'

      self.asciidoc_to_html(@source, @destination, extensions, excludes, merged_options)

    when 'index2html'
      Hyla.logger2.info "Rendering : Asciidoctor Indexed Files to HTML"
      self.check_mandatory_option?('-s / --source', options[:source])
      self.check_mandatory_option?('-d / --destination', options[:destination])

      @destination = options[:destination]
      @source = options[:source]

      new_asciidoctor_option = {
          :template_dirs => [self.backend_dir(options[:backend])],
          :attributes => {
              'stylesheet' => self.check_style(options[:style])
          }
      }

      merged_options = Configuration[options].deep_merge(new_asciidoctor_option)

      #
      # Extension(s) of the files containing include directives
      #
      extensions = 'txt'
      excludes = 'lab_assets|lab_assets_solution|code|snippets|templates|generated_content|generated_content_instructor|generated_content_snippet|generated_slideshow|generated_content_pdf|generated_content_students'

      self.asciidoc_to_html(@source, @destination, extensions, excludes, merged_options)

    when 'html2pdf'

      Hyla.logger2.info "Rendering : Generate PDF from HTML file"

      source_dir = options[:source] if self.check_mandatory_option?('-s / --source', options[:source])
      out_dir = options[:destination] if self.check_mandatory_option?('-d / --destination', options[:destination])

      file_name = options[:file]
      cover_path ||= options[:cover_path]
      header_html_path = options[:header_html_path]
      footer_text = options[:footer_text]

      self.html_to_pdf(file_name, source_dir, out_dir, footer_text, header_html_path, cover_path)

    when 'cover2png'

      Hyla.logger2.info "Rendering : Generate Cover HTML page & picture - format png"

      out_dir = options[:destination] if self.check_mandatory_option?('-d / --destination', options[:destination])
      file_name = options[:cover_file]
      image_name = options[:cover_image]
      course_name = options[:course_name]
      module_name = options[:module_name]
      bg_image_path = options[:image_path]
      tool = options[:tool] || 'imgkit'

      self.cover_img(out_dir, file_name, image_name, course_name, module_name, bg_image_path, tool)

    else
      Hyla.logger2.error ">> Unknow rendering"
      exit(1)
  end
end

.remove_special_chars(pos, text) ⇒ Object

Remove space, dot, ampersand, hyphen, parenthesis characters from the String at a position specified



723
724
725
726
727
728
729
730
# File 'lib/hyla/commands/generate.rb', line 723

def self.remove_special_chars(pos, text)
  return text[pos, text.length].strip.gsub(/\s/, '_')
             .gsub('.', '')
             .gsub('&', '')
             .gsub('-', '')
             .gsub(/\(|\)/, '')
             .gsub('__', '_')
end

.remove_tag_from_index_file(index_file) ⇒ Object

Remove snippet tag from index file



812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/hyla/commands/generate.rb', line 812

def self.remove_tag_from_index_file(index_file)
  if File.basename(index_file) == "AllSlides.txt" then
    content = ""
    File.readlines(index_file).each do |line|
      if line =~ /^include::.*\[tag\=.*\]$/
        replace = line.gsub('[tag=' + Configuration::SNIPPET_TAG, '[')
        content = content.to_s + replace
      else
        content = content.to_s + line
      end
    end
    replace_content(index_file, content)
  end
end

.replace_content(f, content) ⇒ Object

Replace content of a File



830
831
832
# File 'lib/hyla/commands/generate.rb', line 830

def self.replace_content(f, content)
  File.open(f, "w") { |out| out << content } if !content.empty?
end

.table_of_content_to_asciidoc(toc_file, out_dir, project_name, image_path) ⇒ Object

Method parsing TOC File to generate directories and files Each Level 1 entry will become a directory and each Level 2 a file created under the directory

Parameters:

  • toc_file (File Containing the Table of Content)
  • out_dir (Directory where asciidoc files will be generated)
  • project_name (Project name used to create parent of index files)


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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/hyla/commands/generate.rb', line 330

def self.table_of_content_to_asciidoc(toc_file, out_dir, project_name, image_path)

  Hyla.logger2.info '>> Project Name : ' + project_name + ' <<'

  # Open file & parse it
  f = f = File.open(toc_file, 'r')
  f_scan_occurences = File.open(toc_file, 'r')

  # Expand File Path
  @out_dir = File.expand_path out_dir
  Hyla.logger2.info '>> Output directory : ' + out_dir + ' <<'

  #
  # Create destination directory if it does not exist
  #
  unless Dir.exist? @out_dir
    FileUtils.mkdir_p @out_dir
  end

  # Copy YAML Config file
  FileUtils.cp_r [Configuration::configs, Configuration::YAML_CONFIG_FILE_NAME] * '/', @out_dir

  #
  # Move to the directory as we will
  # create content relative to this directory
  #
  Dir.chdir @out_dir
  @out_dir = Pathname.pwd

  # Create index file of all index files
  @project_index_file = self.create_index_file_withoutprefix(project_name, Configuration::LEVEL_1)

  # Count ho many modules we have
  @modules = f_scan_occurences.read.scan(/^=\s/).size
  f_scan_occurences.close
  
  @counter = 0
  
  # File iteration
  f.each do |line|
    
    #
    # Check level 1
    # Create a directory where its name corresponds to 'Title Level 1' &
    # where we have removed the leading '=' symbol and '.' and
    # replaced ' ' by '_'
    #
    if line[/^=\s/]
      
      # Increase counter. We will use it later to add the summary 
      @counter+=1

      #
      # Create the summary.adoc file and add it to the index
      #
      if @counter > 1
        self.generate_summary_page()
      end
      
      #
      # Create the Directory name for the module and next the files
      # The special characters are removed from the string
      #
      dir_name = remove_special_chars(2, line)
      new_dir = [@out_dir, dir_name].join('/')
      FileUtils.rm_rf new_dir
      FileUtils.mkdir new_dir
      Hyla.logger2.info '>> Directory created : ' + new_dir + ' <<'

      Dir.chdir(new_dir)

      # Add image, audio, video directory
      # self.create_asset_directory(['images', 'audio', 'video'])
      self.create_asset_directory(['images'])

      #
      # Create an index file
      # It is used to include files belonging to a module and will be used for SlideShow
      # The file created contains a title (= Dir Name) and header with attributes
      #
      @index_file = create_index_file_withoutprefix(dir_name, Configuration::LEVEL_1)

      #
      # Include index file created to parent index file
      # we don't prefix the AllSlides.txt file anymore
      #
      # BEFORE @project_index_file.puts Configuration::INCLUDE_PREFIX + dir_name + '/' + dir_name + Configuration::INDEX_SUFFIX + Configuration::INCLUDE_SUFFIX
      #
      @project_index_file.puts Configuration::INCLUDE_PREFIX + dir_name + '/' + Configuration::INDEX_FILE + Configuration::INCLUDE_SUFFIX
      @project_index_file.puts "\n"

      #
      # Generate a Module key value
      # 01, 02, ...
      # that we will use as key to create the asciidoc file
      #
      dir_name = File.basename(Dir.getwd)
      @module_key = dir_name.initial.rjust(2, '0')
      Hyla.logger2.info ">> Module key : #@module_key <<"

      #
      # Reset counter value used to generate file number
      # for the file 01, 00 within this module
      #
      @index = 0

      #
      # Add the cover.adoc file
      #
      @index += 1
      file_index = sprintf('%02d', @index)
      f_name = 'm' + @module_key + 'p' + file_index + '_cover' + Configuration::ADOC_EXT
      Hyla.logger2.debug '>> Directory name : ' + dir_name.to_s.gsub('_', ' ')
      # rep_txt = Configuration::COVER_TXT.gsub(/xxx\.png/, dir_name + '.png')
      # Hyla.logger2.debug "Replaced by : " + rep_txt
      cover_f = File.new(f_name, 'w')
      cover_f.puts Configuration::COVER_TXT
      cover_f.close

      #
      # WE DON'T HAVE TO GENERATE AN IMAGE AS THE IMAGE IS CREATED BY REVEALJS
      #
      # Use the filename & generate the cover image
      #
      # out_dir = 'images'
      # file_name = dir_name + '.html'
      # image_name = dir_name + '.png'
      # course_name = @project_name
      # module_name= dir_name
      # bg_image_path = image_path
      # Hyla.logger2.debug '>> Out Directory : ' + out_dir.to_s
      # Hyla.logger2.debug '>> Image name : ' + image_name.to_s
      # Hyla.logger2.debug '>> Course Name  : ' + course_name.to_s
      # Hyla.logger2.debug '>> Module Name  : ' + module_name.to_s
      # Hyla.logger2.debug '>> Bg Image  : ' + bg_image_path.to_s
      # 
      # self.cover_img(out_dir, file_name, image_name, course_name, module_name, bg_image_path, tool = nil)
      #
      
      #
      # Include cover file to index
      #
      @index_file.puts Configuration::INCLUDE_PREFIX + f_name + Configuration::INCLUDE_SUFFIX
      @index_file.puts "\n"

      #
      # Add the objectives.adoc file
      #
      @index += 1
      file_index = sprintf('%02d', @index)
      f_name = 'm' + @module_key + 'p' + file_index + '_objectives'
      f_name = f_name + Configuration::ADOC_EXT
      
      # rep_txt = Configuration::OBJECTIVES_TXT.gsub(/xxx\.mp3/, f_name + '.mp3')

      objectives_f = File.new(f_name, 'w')
      objectives_f.puts Configuration::HEADER_TXT
      objectives_f.puts Configuration::OBJECTIVES_TXT
      objectives_f.close

      #
      # Include objectives file to index
      #
      @index_file.puts Configuration::INCLUDE_PREFIX + f_name + Configuration::INCLUDE_SUFFIX
      @index_file.puts "\n"

      #
      # Add the labinstructions.adoc file
      #
      f_name = 'labinstructions' + Configuration::ADOC_EXT
      lab_f = File.new(f_name, 'w')
      lab_f.puts Configuration::HEADER_TXT
      lab_f.puts Configuration::LABS_TXT
      lab_f.close

      #
      # Add the assessment.txt file
      #
      f_name = 'assessment.txt'
      assessment_f = File.new(f_name, 'w')
      assessment_f.puts Configuration::ASSESSMENT_TXT
      assessment_f.close

      # Move to next line record
      next
    end

    #
    # Check Level 2
    # Create a file for each Title Level 2 where name corresponds to title (without '==' symbol) &
    # where we have removed the leading '==' symbol  and '.' and replace ' ' by '_'
    #
    if line[/^==\s/]

      # Close File created previously if it exists
      if !@previous_f.nil?

        #
        # Add Footer_text to the file created
        #
        # rep_txt = Configuration::FOOTER_TXT.gsub(/xxx\.mp3/, @previous_f.to_s + '.mp3')
        @previous_f.puts Configuration::FOOTER_TXT
        @previous_f.close
      end

      #
      # Replace special characters from the title before to generate the file name
      # Convert Uppercase to lowercase
      #
      f_name = remove_special_chars(3, line).downcase

      #
      # Create the prefix for the file
      # Convention : m letter followed by module number, letter p & a number 01, 02, ..., 0n, next the title & .adoc extension
      # Example : m01p01_mytitle.adoc, m01p02_anothertitle.adoc
      #
      @index += 1
      file_index = sprintf('%02d', @index)
      f_name = 'm' + @module_key + 'p' + file_index + '_' + f_name
      f_asciidoc_name = f_name + Configuration::ADOC_EXT

      # rep_txt = Configuration::AUDIO_TXT.gsub(/xxx\.mp3/, f_name + '.mp3')

      #
      # Create File and add configuration HEADER_TXT
      #
      @new_f = File.new(f_asciidoc_name, 'w')
      @new_f.puts Configuration::HEADER_TXT

      Hyla.logger2.info '   # File created : ' + f_asciidoc_name.to_s

      @previous_f = @new_f

      # Include file to index
      @index_file.puts Configuration::INCLUDE_PREFIX + f_asciidoc_name + Configuration::INCLUDE_SUFFIX
      @index_file.puts "\n"
    end

    #
    # Add Content to file if it exists and line does not start with characters to be skipped
    #
    if !@new_f.nil? and !line.start_with?(Configuration::SKIP_CHARACTERS)
      #
      # Add audio text after the name of the title
      #
      #  ifdef::showscript[]
      #    audio::audio/m01p03_why_use_messaging[]
      #  endif::showscript[]
      #
      if line.start_with?('==')
        @new_f.puts line
        @new_f.puts "\n"
        # @new_f.puts rep_txt
      else
        @new_f.puts line
      end
    end

  end

  #
  # Add the summary.adoc file
  #
  if @counter == @modules
    self.generate_summary_page()
  end
  
end