lib/hyla/commands/generate.rb in hyla-1.0 vs lib/hyla/commands/generate.rb in hyla-1.0.1

- old
+ new

@@ -1,102 +1,184 @@ module Hyla module Commands class Generate < Command - attr_reader :artefact - DEFAULT_OPTIONS = { - :watch_dir => '.', - :watch_ext => %w(ad adoc asc asciidoc txt index), - :run_on_start => false, - :backend => 'html5', - :eruby => 'erb', - :doctype => 'article', - :compact => false, - :attributes => {}, - :always_build_all => false, - :safe => :unsafe, - :header_footer => true + + :source => Dir.pwd, + :destination => File.join(Dir.pwd, 'generated_content'), + + :watch_dir => '.', + :watch_ext => %w(ad adoc asciidoc txt index), + :run_on_start => false, + :backend => 'html5', + :eruby => 'erb', + :doctype => 'article', + :compact => false, + :attributes => { + 'source-highlighter' => 'coderay', + 'linkcss!' => 'true', + 'data-uri' => 'true', + 'stylesheet' => 'asciidoctor.css', + 'stylesdir' => Configuration::styles + }, + :always_build_all => false, + :safe => 'unsafe', + :header_footer => true } def self.process(args, options = {}) - @config = Hyla::Configuration.new - rendering = options[:rendering] if self.check_mandatory_option?('--r / --rendering', options[:rendering]) case rendering - when 'toc2html' + when 'toc2adoc' - Hyla.logger.info "Rendering : Table of Content to HTML" + Hyla.logger.info "Rendering : Table of Content to Asciidoc" self.check_mandatory_option?('--t / --toc', options[:toc]) @toc_file = options[:toc] @out_dir = options[:destination] @project_name = options[:project_name] - self.table_of_content_to_asciidoc(@toc_file, @out_dir, @project_name) when 'adoc2html' Hyla.logger.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] - self.asciidoc_to_html(@source, @destination) + extensions = 'adoc|ad|txt' - when 'adoc2slides' - Hyla.logger.info "Rendering : Asciidoc to SlideShow - NOT YET AVAILABLE" + self.asciidoc_to_html(@source, @destination, extensions, options) + + when 'adoc2slide' + Hyla.logger.info "Rendering : Asciidoc to SlideShow" + self.check_mandatory_option?('--s / --source', options[:source]) + self.check_mandatory_option?('--d / --destination', options[:destination]) + + # Assign by default backend as HTML5 if not provided by command line + backend = options[:backend]? options[:backend] : 'html5' + + # + # Retrieve asciidoctor attributes + # Could be an Arrays of Strings key=value,key=value + # or + # Could be a Hash (DEFAULTS, CONFIG_File) + attributes = options[:attributes] + override_attrs = case attributes + when Hash then attributes + when String then + result = attributes.split(',') + attributes = Hash.new + result.each do |entry| + words = entry.split('=') + attributes[words[0]] = words[1] + end + attributes + else {} + end + + @destination = options[:destination] + @source = options[:source] + options = { + :backend => backend, + :template_dirs => [ + self.backend_dir(options[:backend]) + ], + :watch_ext => %w(index), + :attributes => override_attrs + } + + extensions = 'index|adoc|ad|asciidoc' + + self.asciidoc_to_html(@source, @destination, extensions, options) else Hyla.logger.error ">> Unknow rendering" exit(1) end # From Table of Content File to Asciidoc directories and Files # self.table_of_content_to_asciidoc(@toc_file, @out_dir, @project_name) end - def self.asciidoc_to_html(source, destination) + # Return backend directory + # containing templates (haml, slim) + def self.backend_dir(backend) + case backend + when 'deckjs' + return [Configuration::backends, 'haml', 'deckjs'] * '/' + when 'revealjs' + return [Configuration::backends, 'slim', 'revealjs'] * '/' + end + end - options = DEFAULT_OPTIONS.clone + def self.asciidoc_to_html(source, destination, extensions, options) + # CSS Style to be applied + css_style = self.check_style(options[:style]) + + override = { + :attributes => { + 'stylesheet' => css_style + } + } + + @options = Configuration[options].deep_merge(override) + # Move to Source directory & Retrieve Asciidoctor files to be processed source = File.expand_path source @destination = File.expand_path destination + Hyla.logger.info ">> Source dir: #{source}" Hyla.logger.info ">> Destination dir: #{@destination}" + # Exit if source directory does not exist + if !Dir.exist? source + Hyla.logger.error ">> Source directory does not exist" + exit(1) + end + Dir.chdir(source) current_dir = Dir.pwd Hyla.logger.info ">> Current dir: #{current_dir}" # Delete destination directory FileUtils.rm_rf(Dir.glob(@destination)) - # Search for Asciidoc files and do the rendering + # Search for files using extensions parameter and do the rendering adoc_file_paths = [] Find.find(current_dir) do |path| - if path =~ /.*\.(?:adoc|txt|index)$/ - path1 = Pathname.new(source) - path2 = Pathname.new(path) - relative_path = path2.relative_path_from(path1).to_s - Hyla.logger.debug ">> Relative path: #{relative_path}" - adoc_file_paths << relative_path + if path =~ /.*\.(?:#{extensions})$/ + path1 = Pathname.new(source) + path2 = Pathname.new(path) + relative_path = path2.relative_path_from(path1).to_s + Hyla.logger.debug ">> Relative path: #{relative_path}" + adoc_file_paths << relative_path - # Create directory in the destination directory - html_dir = @destination + '/' + File.dirname(relative_path) - Hyla.logger.info ">> Dir of html: #{html_dir}" - FileUtils.mkdir_p html_dir + # Create dir + html_dir = @destination + '/' + File.dirname(relative_path) + Hyla.logger.info ">> Dir of html: #{html_dir}" + FileUtils.mkdir_p html_dir - # Render asciidoc to HTML - Hyla.logger.info ">> File to be rendered : #{path}" - options[:to_dir] = html_dir - Asciidoctor.render_file(path, options) + # 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') + end - end + # Render asciidoc to HTML + Hyla.logger.info ">> File to be rendered : #{path}" + @options[:to_dir] = html_dir + Asciidoctor.render_file(path, @options) + + end end # No asciidoc files retrieved if adoc_file_paths.empty? Hyla.logger.info " >> No asciidoc files retrieved." @@ -104,10 +186,30 @@ end end # + # CSS Style to be used + # Default is : asciidoctor.css + # + def self.check_style(style) + if !style.nil? + css_file = [style, '.css'].join() + else + css_file = 'asciidoctor.css' + end + end + + # + # Copy resources to target dir + def self.cp_resources_to_dir(path, resource) + source = [Configuration::resources, resource] * '/' + destination = [path, resource] * '/' + FileUtils.cp_r source, destination + end + + # # 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 # # @param [File Containing the Table of Content] toc_file @@ -119,27 +221,36 @@ Hyla.logger.info '>> Project Name : ' + project_name + ' <<' # Open file & parse it f = File.open(toc_file, 'r') + # Expand File Path + @out_dir = File.expand_path out_dir + # Re Create Directory of generated content - if Dir.exist? out_dir - FileUtils.rm_rf out_dir - FileUtils.mkdir_p out_dir + if Dir.exist? @out_dir + FileUtils.rm_rf @out_dir + FileUtils.mkdir_p @out_dir else - FileUtils.mkdir_p 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 + # # Move to 'generated' directory as we will # create content relative to this directory # - Dir.chdir out_dir - out_dir = Pathname.pwd + Dir.chdir @out_dir + @out_dir = Pathname.pwd # Create index file of all index files - @project_index_file = self.create_index_file(project_name, @config.LEVEL_1) + @project_index_file = self.create_index_file(project_name, Configuration::LEVEL_1) # File iteration f.each do |line| @@ -151,11 +262,11 @@ # if line[/^=\s/] # Create File dir_name = remove_special_chars(2, line) - new_dir = [out_dir, dir_name].join('/') + new_dir = [@out_dir, dir_name].join('/') Hyla.logger.info '>> Directory created : ' + new_dir + ' <<' FileUtils.mkdir_p new_dir Dir.chdir(new_dir) # Add image directory @@ -164,14 +275,14 @@ # # Create an index file # It is used to include files belonging to a module and will be used for SlideShows # The file created contains a title (= Dir Name) and header with attributes # - @index_file = create_index_file(dir_name, @config.LEVEL_2) + @index_file = create_index_file(dir_name, Configuration::LEVEL_1) # Include index file created to parent index file - @project_index_file.puts @config.INCLUDE_PREFIX + dir_name + '/' + dir_name + @config.INDEX_SUFFIX + @config.INCLUDE_SUFFIX + @project_index_file.puts Configuration::INCLUDE_PREFIX + dir_name + '/' + dir_name + Configuration::INDEX_SUFFIX + Configuration::INCLUDE_SUFFIX # Move to next line record next end @@ -190,31 +301,29 @@ # Create File f_name = remove_special_chars(3, line) Hyla.logger.info ' # File created : ' + f_name.to_s f_name += '.adoc' @new_f = File.new(f_name, 'w') - @new_f.puts @config.HEADER + @new_f.puts Configuration::HEADER @previous_f = @new_f # Include file to index - @index_file.puts @config.INCLUDE_PREFIX + f_name + @config.INCLUDE_SUFFIX + @index_file.puts Configuration::INCLUDE_PREFIX + f_name + Configuration::INCLUDE_SUFFIX 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?(@config.SKIP_CHARACTERS) + if !@new_f.nil? and !line.start_with?(Configuration::SKIP_CHARACTERS) @new_f.puts line end end end - # method parse_file(f) - # # Remove space, dot from a String # def self.remove_special_chars(pos, text) return text[pos, text.length].strip.gsub(/\s/, '_').gsub('.', '') @@ -237,13 +346,13 @@ # # Create ascidoc index file # containing references to asciidoc files part of a module # def self.create_index_file(file_name, level) - n_file_name = file_name + @config.INDEX_SUFFIX + n_file_name = file_name + Configuration::INDEX_SUFFIX index_file = File.new(n_file_name, 'w') index_file.puts level + file_name - index_file.puts @config.HEADER_INDEX + index_file.puts Configuration::HEADER_INDEX index_file end # \ No newline at end of file