lib/hyla/commands/generate.rb in hyla-1.0.1 vs lib/hyla/commands/generate.rb in hyla-1.0.2
- old
+ new
@@ -1,38 +1,25 @@
module Hyla
module Commands
class Generate < Command
- DEFAULT_OPTIONS = {
-
- :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 = {})
rendering = options[:rendering] if self.check_mandatory_option?('--r / --rendering', options[:rendering])
case rendering
+
+ when 'html2pdf'
+
+ Hyla.logger.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] if self.check_mandatory_option?('-f / --file', options[:file])
+
+ self.html_to_pdf(source_dir, out_dir, file_name)
+
when 'toc2adoc'
Hyla.logger.info "Rendering : Table of Content to Asciidoc"
self.check_mandatory_option?('--t / --toc', options[:toc])
@toc_file = options[:toc]
@@ -44,65 +31,74 @@
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]
- extensions = 'adoc|ad|txt'
+ # Check Style to be used
+ new_asciidoctor_option = {
+ :attributes => {
+ 'stylesheet' => self.check_style(options[:style])
+ }
+ }
- self.asciidoc_to_html(@source, @destination, extensions, options)
+ merged_options = Configuration[options].deep_merge(new_asciidoctor_option)
- when 'adoc2slide'
- Hyla.logger.info "Rendering : Asciidoc to SlideShow"
+ extensions = 'adoc|ad|asciidoc'
+
+ self.asciidoc_to_html(@source, @destination, extensions, merged_options)
+
+ when 'index2slide'
+ Hyla.logger.info "Rendering : Asciidoctor Indexed Files 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'
+ @destination = options[:destination]
+ @source = options[:source]
- #
- # 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
+ 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'
+
+ self.asciidoc_to_html(@source, @destination, extensions, merged_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])
+
@destination = options[:destination]
@source = options[:source]
- options = {
- :backend => backend,
- :template_dirs => [
- self.backend_dir(options[:backend])
- ],
- :watch_ext => %w(index),
- :attributes => override_attrs
+
+ new_asciidoctor_option = {
+ :template_dirs => [self.backend_dir(options[:backend])],
+ :attributes => {
+ 'stylesheet' => self.check_style(options[:style])
+ }
}
- extensions = 'index|adoc|ad|asciidoc'
+ merged_options = Configuration[options].deep_merge(new_asciidoctor_option)
- self.asciidoc_to_html(@source, @destination, extensions, options)
+ # Extension(s) of the files to be parsed
+ extensions = 'adoc|ad|asciidoc'
+
+ self.asciidoc_to_html(@source, @destination, extensions, merged_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
# Return backend directory
# containing templates (haml, slim)
def self.backend_dir(backend)
@@ -114,21 +110,10 @@
end
end
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}"
@@ -171,12 +156,12 @@
self.cp_resources_to_dir(File.dirname(html_dir), 'revealjs')
end
# Render asciidoc to HTML
Hyla.logger.info ">> File to be rendered : #{path}"
- @options[:to_dir] = html_dir
- Asciidoctor.render_file(path, @options)
+ options[:to_dir] = html_dir
+ Asciidoctor.render_file(path, options)
end
end
# No asciidoc files retrieved
@@ -267,22 +252,23 @@
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
- Dir.mkdir('image')
+ # Add image, audio, video directory
+ self.create_asset_directory(['image', 'audio', 'video'])
#
# 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, 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
+ @project_index_file.puts "\n"
# Move to next line record
next
end
@@ -302,15 +288,17 @@
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 Configuration::HEADER
+ @new_f.puts "\n"
@previous_f = @new_f
# Include file to index
@index_file.puts Configuration::INCLUDE_PREFIX + f_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
#
@@ -321,10 +309,42 @@
end
end
#
+ # 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.logger.info ">> PDF file generated and saved : #{pdf_file_name} "
+ end
+
+ #
+ # Create Asset Directory
+ #
+ def self.create_asset_directory(assets = [])
+ assets.each do |asset|
+ Dir.mkdir(asset) if !Dir.exist? asset
+ end
+ end
+
+ #
# Remove space, dot from a String
#
def self.remove_special_chars(pos, text)
return text[pos, text.length].strip.gsub(/\s/, '_').gsub('.', '')
end
@@ -348,11 +368,17 @@
# containing references to asciidoc files part of a module
#
def self.create_index_file(file_name, level)
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 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
#
\ No newline at end of file