lib/hyla/commands/generate.rb in hyla-1.0.6 vs lib/hyla/commands/generate.rb in hyla-1.0.7.pre.1

- old
+ new

@@ -17,12 +17,13 @@ @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) + self.table_of_content_to_asciidoc(@toc_file, @out_dir, @project_name, @image_path) when 'adoc2html' Hyla.logger.info "Rendering : Asciidoc to HTML" self.check_mandatory_option?('-s / --source', options[:source]) @@ -90,40 +91,16 @@ Hyla.logger.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] - # Configure Slim engine - slim_file = Configuration::cover_template - slim_tmpl = File.read(slim_file) - template = Slim::Template.new(:pretty => true) { slim_tmpl } + self.cover_img(out_dir, file_name, image_name, course_name, module_name, bg_image_path) - # Do the Rendering HTML - parameters = {:course_name => options[:course_name], - :module_name => options[:module_name], - :image_path => options[:image_path]} - res = template.render(Object.new, parameters) - - unless Dir.exist? out_dir - FileUtils.mkdir_p out_dir - end - - 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: 90, width: 950, height: 750) - kit.to_img(:png) - kit.to_file(image_name) - - # Convert HTML to Image - # system ("wkhtmltoimage -f 'png' #{file_name} #{image_name}") - end - else Hyla.logger.error ">> Unknow rendering" exit(1) end end @@ -144,10 +121,54 @@ return [Configuration::backends, 'slim', 'html5'] * '/' end end # + # Cover Function + # Create a png file using the HTML generated with the Slim cover template + # + def self.cover_img(out_dir, file_name, image_name, course_name, module_name, bg_image_path) + + unless Dir.exist? out_dir + FileUtils.mkdir_p out_dir + end + + # 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.logger.debug "Module name : " + module_name + + # Do the HTML Rendering + 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: 90, width: 950, height: 750) + kit.to_img(:png) + kit.to_file(image_name) + end + + end + + # # Call Asciidoctor.render function # def self.asciidoc_to_html(source, destination, extensions, excludes, options) # Move to Source directory & Retrieve Asciidoctor files to be processed @@ -184,13 +205,13 @@ # Reject directory specified and do the rendering # files = Dir[current_dir + "/**/*.{" + extensions + "}"].reject { |f| f =~ /\/#{excludes}\// } # - # Check if companion parameter is defined + # Check if snippet parameter is defined # as we have to modify the AllSlides.txt file - # containing as tag name this value [tag=snippet] + # 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 @@ -243,12 +264,12 @@ # end end # - # Check if companion parameter is defined - # and remove the companion tag from indexed files + # 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 @@ -290,77 +311,102 @@ # # @param [File Containing the Table of Content] toc_file # @param [Directory where asciidoc files will be generated] out_dir # @param [Project name used to create parent of index files] project_name # - def self.table_of_content_to_asciidoc(toc_file, out_dir, project_name) + def self.table_of_content_to_asciidoc(toc_file, out_dir, project_name, image_path) Hyla.logger.info '>> Project Name : ' + project_name + ' <<' # Open file & parse it - f = File.open(toc_file, 'r') + 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.logger.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::templates, Configuration::YAML_CONFIG_FILE_NAME] * '/', @out_dir # Copy styles - FileUtils.cp_r Configuration::styles, @out_dir + # FileUtils.cp_r Configuration::styles, @out_dir # - # Move to 'generated' directory as we will + # 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(project_name, Configuration::LEVEL_1) + @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 Directory of the module and next the File + # Add the summary.adoc file # + 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.logger.info '>> Directory created : ' + new_dir + ' <<' Dir.chdir(new_dir) # Add image, audio, video directory - self.create_asset_directory(['image', 'audio', 'video']) + # self.create_asset_directory(['image', 'audio', 'video']) + self.create_asset_directory(['image']) # # Create an index file - # It is used to include files belonging to a module and will be used for SlideShows + # 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(dir_name, Configuration::LEVEL_1) + @index_file = create_index_file_withoutprefix(dir_name, Configuration::LEVEL_1) + # # Include index file created to parent index file - @project_index_file.puts Configuration::INCLUDE_PREFIX + dir_name + '/' + dir_name + Configuration::INDEX_SUFFIX + Configuration::INCLUDE_SUFFIX + # 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, ... @@ -370,14 +416,89 @@ @module_key = dir_name.initial.rjust(2, '0') Hyla.logger.info ">> Module key : #@module_key <<" # # Reset counter value used to generate file number - # for the file 01, 00 + # 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.logger.debug '>> Directory name : ' + dir_name.to_s.gsub('_', ' ') + rep_txt = Configuration::COVER_TXT.gsub(/xxx\.png/, dir_name + '.png') + Hyla.logger.debug "Replaced by : " + rep_txt + cover_f = File.new(f_name, 'w') + cover_f.puts rep_txt + cover_f.close + + # + # Use the filename & generate the cover image + # + out_dir = 'image' + file_name = dir_name + '.html' + image_name = dir_name + '.png' + course_name = @project_name + module_name= dir_name + bg_image_path = image_path + Hyla.logger.debug '>> Out Directory : ' + out_dir.to_s + Hyla.logger.debug '>> Image name : ' + image_name.to_s + Hyla.logger.debug '>> Course Name : ' + course_name.to_s + Hyla.logger.debug '>> Module Name : ' + module_name.to_s + Hyla.logger.debug '>> Bg Image : ' + bg_image_path.to_s + + self.cover_img(out_dir, file_name, image_name, course_name, module_name, bg_image_path) + + # + # 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' + + rep_txt = Configuration::OBJECTIVES_TXT.gsub(/xxx\.mp3/, f_name + '.mp3') + + f_name = f_name + Configuration::ADOC_EXT + + objectives_f = File.new(f_name, 'w') + objectives_f.puts Configuration::HEADER_TXT + objectives_f.puts rep_txt + objectives_f.close + + # + # Include cover 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 # @@ -387,49 +508,83 @@ # 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 rep_txt @previous_f.close end # - # Replace special characters form the file and - # add the module key followed by the index value for the file - # Example : m01p01_MyTitle.ad, m01p02_Another_Title.ad + # Replace special characters from the title before to generate the file name + # Convert Uppercase to lowercase # - f_name = remove_special_chars(3, line) + 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 = @index.to_s.initial.rjust(2, '0') file_index = sprintf('%02d', @index) - f_name = 'm' + @module_key + 'p' + file_index + '_' + f_name + '.ad' + f_name = 'm' + @module_key + 'p' + file_index + '_' + f_name - Hyla.logger.info ' # File created : ' + f_name.to_s + rep_txt = Configuration::AUDIO_TXT.gsub(/xxx\.mp3/, f_name + '.mp3') + f_asciidoc_name = f_name + Configuration::ADOC_EXT + # - # Create File and add configuration HEADER + # Create File and add configuration HEADER_TXT # - @new_f = File.new(f_name, 'w') - @new_f.puts Configuration::HEADER + @new_f = File.new(f_asciidoc_name, 'w') + @new_f.puts Configuration::HEADER_TXT @new_f.puts "\n" + Hyla.logger.info ' # File created : ' + f_asciidoc_name.to_s + @previous_f = @new_f # Include file to index - @index_file.puts Configuration::INCLUDE_PREFIX + f_name + Configuration::INCLUDE_SUFFIX + @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) - @new_f.puts line + # + # Add audio text after the name of the title + # + # ifdef::audioscript[] + # audio::audio/m01p03_why_use_messaging[] + # endif::[] + # + 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 =begin # # Generate PDF @@ -453,10 +608,25 @@ kit.to_file(pdf_file_name) Hyla.logger.info ">> PDF file generated and saved : #{pdf_file_name} " end =end + def self.generate_summary_page() + @index += 1 + file_index = sprintf('%02d', @index) + f_name = 'm' + @module_key + 'p' + file_index + '_summary' + + rep_txt = Configuration::SUMMARY_TXT.gsub(/xxx\.mp3/, f_name + '.mp3') + + f_name = f_name + Configuration::ADOC_EXT + + summary_f = File.new(f_name, 'w') + summary_f.puts Configuration::HEADER_TXT + summary_f.puts rep_txt + summary_f.close + end + 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"] * '/' @@ -529,16 +699,20 @@ Dir.mkdir(asset) if !Dir.exist? asset end end # - # Remove space, dot from a String + # Remove space, dot, ampersand, hyphen, parenthesis characters from the String + # at a position specified # def self.remove_special_chars(pos, text) return text[pos, text.length].strip.gsub(/\s/, '_') - .gsub('.', '') - .gsub('&', '') + .gsub('.', '') + .gsub('&', '') + .gsub('-', '') + .gsub(/\(|\)/, '') + .gsub('__', '_') end # # Add '/' at the end of the target path # if the target path provided doesn't contain it @@ -554,12 +728,13 @@ end # # Create ascidoc index file # containing references to asciidoc files part of a module + # TODO : Not longer used -> can be removed # - def self.create_index_file(file_name, level) + 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" @@ -571,47 +746,77 @@ index_file end # - # Add snippet tag to index file with extension .ad[] - # as this is not yet the case + # Create ascidoc index file + # containing references to asciidoc files part of a module # + 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 + + # + # 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]) + # def self.add_tag_to_index_file(index_file) - content = File.read(index_file) - # - # Modify the content of an index file if - # it contains include::file with extension .ad, .adoc or .asciidoc - # - if content =~ /(\.ad)|(\.adoc)|(\.asciidoc)/ - replace = content.gsub(/\[/, '[tag=' + Configuration::SNIPPET_TAG) - replace_content(index_file, replace) + 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 # # Remove snippet tag from index file # def self.remove_tag_from_index_file(index_file) - content = File.read(index_file) - # - # Modify the content of an index file if - # it contains include::file with extension .ad, .adoc or .asciidoc - # - if content =~ /(\.ad)|(\.adoc)|(\.asciidoc)/ - replace = content.gsub('[tag=' + Configuration::SNIPPET_TAG, '[') - replace_content(index_file, replace) + 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 of a File # def self.replace_content(f, content) - File.open(f, "w") { |f| f.puts content } if !content.empty? + File.open(f, "w") { |out| out << content } if !content.empty? end - # + # Check mandatory options # def self.check_mandatory_option?(key, value) if value.nil? or value.empty? Hyla.logger.warn "Mandatory option missing: #{key}"