class Source2Pdf::Exporter
rubocop:disable ClassLength, MethodLength
Attributes
Public Class Methods
The initializer for Exporter
@param [String] url the input URL like
https://github.com/opal/opal.git or just the immediate folder name
@param [Hash<Symbol,Object>] opts the option hash
@option opts [Array<String>] :exts the list of file extension to be used @option opts [Array<String>] :non_exts the list of file without extension to be used @option opts [String] :theme the colorscheme to use with `vim_printer` @option opts [String] :output_name the output filename if any
# File lib/source2pdf/exporter.rb, line 25 def initialize(url, opts = {}) @url = url @base_dir = Dir.pwd @exts = opts[:exts] || [] @non_exts = opts[:non_exts] || [] @theme = opts[:theme] || "default" @repo_name = project_name(url) @output_path = File.expand_path([base_dir, repo_name].join(File::SEPARATOR)) @output_name = pdf_filename(opts[:output_name]) || "#{@repo_name}.pdf" end
Public Instance Methods
Print and export the source from a given URL to a pdf
# File lib/source2pdf/exporter.rb, line 37 def export clone puts "FYI: list of extensions: #{all_extensions}" puts "FYI: list of all files : #{all_files}" files2htmls htmls2pdfs pdfs2pdf copy_output cleanup end
# File lib/source2pdf/exporter.rb, line 48 def to_s <<-EOT url : #{url} base_dir : #{base_dir} exts : #{exts} non_exts : #{non_exts} repo_name : #{repo_name} theme : #{theme} output_path : #{output_path} output_name : #{output_name} EOT end
Private Instance Methods
List all extensions
# File lib/source2pdf/exporter.rb, line 72 def all_extensions all_exts = Source2Pdf.list_extensions(output_path) # Strip off the '.' in the output if any. all_exts.map! { |e| e.gsub(/^\./, "") } all_exts end
List all files base on simple criteria
# File lib/source2pdf/exporter.rb, line 80 def all_files files = [] if input_available? files = Source2Pdf.list_files base_dir: output_path, exts: exts, non_exts: non_exts, recursive: true end files end
# File lib/source2pdf/exporter.rb, line 122 def cleanup FileUtils.rm_rf File.expand_path(File.dirname(output_dir) + "../../#{Source2Pdf::TMP_DIR}") FileUtils.rm_rf File.expand_path(File.dirname(output_dir) + "../../#{repo_name}/vim_printer_#{repo_name}.tar.gz") end
# File lib/source2pdf/exporter.rb, line 63 def clone if File.exist?(output_path) puts "The project #{output_path} already exist, no git clone needed!" return end Source2Pdf.clone_repository(url, repo_name, base_dir) end
# File lib/source2pdf/exporter.rb, line 115 def copy_output generated_file = "#{output_dir}/pdfs2pdf_#{repo_name}.pdf" destination_file = File.expand_path(File.dirname(output_dir) + "../../#{output_name}") FileUtils.mv generated_file, destination_file if File.exist?(generated_file) puts "Your final output is #{File.expand_path(destination_file)}" end
Convert files to htmls
# File lib/source2pdf/exporter.rb, line 92 def files2htmls Source2Pdf.files_to_htmls base_dir: output_path, exts: exts, non_exts: non_exts, theme: theme if input_available? end
Convert list of html to list of pdf files
# File lib/source2pdf/exporter.rb, line 100 def htmls2pdfs input_file = File.expand_path("#{output_path}/vim_printer_#{repo_name}.tar.gz") FileUtils.mkdir_p output_dir AgileUtils::FileUtil.gunzip input_file, output_dir if File.exist?(input_file) Source2Pdf.htmls_to_pdfs base_dir: output_dir end
# File lib/source2pdf/exporter.rb, line 131 def input_available? (exts && !exts.empty?) || (non_exts && !non_exts.empty?) end
# File lib/source2pdf/exporter.rb, line 127 def output_dir File.expand_path("#{base_dir}/#{Source2Pdf::TMP_DIR}/#{repo_name}") end
Add/rename the file extension to pdf
@param [String] filename input file @return [String,NilClass] output file with .pdf as extension or nil
# File lib/source2pdf/exporter.rb, line 151 def pdf_filename(filename) return nil unless filename extname = File.extname(filename) basename = File.basename(filename, ".*") if extname == "" "#{filename}.pdf" elsif extname != ".pdf" "#{basename}.pdf" else filename end end
Merge/join multiple pdf files into single pdf
# File lib/source2pdf/exporter.rb, line 108 def pdfs2pdf input_file = File.expand_path("#{output_dir}/html2pdf_#{repo_name}.tar.gz") AgileUtils::FileUtil.gunzip input_file, output_dir if File.exist?(input_file) Source2Pdf.pdfs_to_pdf base_dir: output_dir, recursive: true end
Extract project name from a given URL
@param [String] uri input uri @example
project_name('https://github.com/erikhuda/thor.git') #=> 'thor' project_name('https://github.com/erikhuda/thor') #=> 'thor'
# File lib/source2pdf/exporter.rb, line 142 def project_name(uri) name = URI(uri).path.split(File::SEPARATOR).last if uri File.basename(name, ".*") if name end