module LucaSupport::View

File rendering functionality like HTML, PDF.

Public Instance Methods

erb2pdf(path) click to toggle source
# File lib/luca_support/view.rb, line 18
def erb2pdf(path)
  html2pdf(render_erb(path))
end
html2pdf(html_dat) click to toggle source

Requires wkhtmltopdf command

# File lib/luca_support/view.rb, line 31
def html2pdf(html_dat)
  out, err, stat = Open3.capture3('wkhtmltopdf - -', stdin_data: html_dat)
  puts err
  out
end
nushell(yml) click to toggle source
# File lib/luca_support/view.rb, line 50
def nushell(yml)
  require 'open3'
  Open3.pipeline_w(%(nu -c 'cat - | from yaml')) { |stdin| stdin.puts yml }
end
render_erb(path) click to toggle source
# File lib/luca_support/view.rb, line 22
def render_erb(path)
  @template_dir = File.dirname(path)
  erb = ERB.new(File.read(path.to_s), trim_mode: '-')
  erb.result(binding)
end
save_pdf(html_dat, path) click to toggle source
# File lib/luca_support/view.rb, line 14
def save_pdf(html_dat, path)
  File.write(path, html2pdf(html_dat))
end
search_template(file, dir = 'templates') click to toggle source

Search existing file and return path under:

  1. 'templates/' in Project directory that data resides

  2. 'templates/' in Library directory that calls LucaSupport::View#search_template

# File lib/luca_support/view.rb, line 41
def search_template(file, dir = 'templates')
  # TODO: load config
  [LucaSupport::PJDIR, lib_path].each do |base|
    path = (Pathname(base) / dir / file)
    return path.to_path if path.file?
  end
  nil
end