class GithubExporter::Exporter

rubocop:disable ClassLength, MethodLength

Attributes

base_dir[R]
exts[R]
non_exts[R]
output_name[R]
output_path[R]
repo_name[R]
theme[R]
url[R]

Public Class Methods

new(url, opts = {}) click to toggle source

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/github_exporter/exporter.rb, line 29
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

export() click to toggle source

Print and export the source from a given URL to a pdf

# File lib/github_exporter/exporter.rb, line 41
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
to_s() click to toggle source
# File lib/github_exporter/exporter.rb, line 52
    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

all_extensions() click to toggle source

List all extensions

# File lib/github_exporter/exporter.rb, line 76
def all_extensions
  all_exts = GithubExporter.list_extensions(output_path)
  # Strip off the '.' in the output if any.
  all_exts.map! { |e| e.gsub(/^\./, "") }
  all_exts
end
all_files() click to toggle source

List all files base on simple criteria

# File lib/github_exporter/exporter.rb, line 84
def all_files
  files = []
  if input_available?
    files = GithubExporter.list_files base_dir:  output_path,
                                      exts:      exts,
                                      non_exts:  non_exts,
                                      recursive: true

  end
  files
end
cleanup() click to toggle source
# File lib/github_exporter/exporter.rb, line 127
def cleanup
  FileUtils.rm_rf File.expand_path(File.dirname(output_dir) + "../../#{GithubExporter::TMP_DIR}")
  FileUtils.rm_rf File.expand_path(File.dirname(output_dir) + "../../#{repo_name}/vim_printer_#{repo_name}.tar.gz")
end
clone() click to toggle source
# File lib/github_exporter/exporter.rb, line 67
def clone
  if File.exist?(output_path)
    puts "The project #{output_path} already exist, no git clone needed!"
    return
  end
  GithubExporter.clone_repository(url, repo_name, base_dir)
end
copy_output() click to toggle source
# File lib/github_exporter/exporter.rb, line 120
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
files2htmls() click to toggle source

Convert files to htmls

# File lib/github_exporter/exporter.rb, line 97
def files2htmls
  GithubExporter.files_to_htmls base_dir: output_path,
                                exts:     exts,
                                non_exts: non_exts,
                                theme:    theme if input_available?
end
htmls2pdfs() click to toggle source

Convert list of html to list of pdf files

# File lib/github_exporter/exporter.rb, line 105
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)
  GithubExporter.htmls_to_pdfs base_dir: output_dir
end
input_available?() click to toggle source
# File lib/github_exporter/exporter.rb, line 136
def input_available?
  (exts && !exts.empty?) || (non_exts && !non_exts.empty?)
end
output_dir() click to toggle source
# File lib/github_exporter/exporter.rb, line 132
def output_dir
  File.expand_path("#{base_dir}/#{GithubExporter::TMP_DIR}/#{repo_name}")
end
pdf_filename(filename) click to toggle source

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/github_exporter/exporter.rb, line 157
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
pdfs2pdf() click to toggle source

Merge/join multiple pdf files into single pdf

# File lib/github_exporter/exporter.rb, line 113
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)
  GithubExporter.pdfs_to_pdf base_dir:  output_dir,
                             recursive: true
end
project_name(uri) click to toggle source

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/github_exporter/exporter.rb, line 148
def project_name(uri)
  name = URI(uri).path.split(File::SEPARATOR).last if uri
  File.basename(name, ".*") if name
end