Class: Resumer::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/resumer/export.rb

Overview

Export a YAML CV to an HTML or PDF file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExport

Returns a new instance of Export.



14
15
16
17
18
19
20
21
22
23
# File 'lib/resumer/export.rb', line 14

def initialize
  @formats = %i[html pdf]
  @defaults = {
    format: @formats.first,
    theme: File.expand_path(
      "#{File.dirname(__FILE__)}/../../themes/default"
    ),
    pdf: pdf_defaults
  }
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



11
12
13
# File 'lib/resumer/export.rb', line 11

def defaults
  @defaults
end

#formatsObject (readonly)

Returns the value of attribute formats.



12
13
14
# File 'lib/resumer/export.rb', line 12

def formats
  @formats
end

Instance Method Details

#create_html(data, theme = , with_style = false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/resumer/export.rb', line 70

def create_html(data, theme = @defaults[:theme], with_style = false)
  # Load the theme
  erb = ERB.new(
    File.read(File.absolute_path("#{theme}/index.html"))
  )
  if with_style
    style = File.read(
      File.absolute_path("#{theme}/css/styles.css")
    )
  end
  data = normalize(data)
  # Generate the HTML
  erb.result(binding)
end

#default_formatObject



36
37
38
# File 'lib/resumer/export.rb', line 36

def default_format
  @defaults[:format]
end

#markdown(text) ⇒ Object

Markdown helper for tempaltes



66
67
68
# File 'lib/resumer/export.rb', line 66

def markdown(text)
  Kramdown::Document.new(text).to_html
end

#normalize(data) ⇒ Object

Transform the YAML object in an OpenStruct with symbol keys



41
42
43
44
45
46
47
# File 'lib/resumer/export.rb', line 41

def normalize(data)
  return normalize_hash(data) if data.is_a? Hash

  return normalize_array(data) if data.is_a? Array

  data
end

#normalize_array(data) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/resumer/export.rb', line 57

def normalize_array(data)
  result = []
  data.each do |i|
    result.push normalize(i)
  end
  result
end

#normalize_hash(data) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/resumer/export.rb', line 49

def normalize_hash(data)
  result = OpenStruct.new data
  data.each do |k, v|
    result[k] = normalize(v)
  end
  result
end

#pdf_defaultsObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/resumer/export.rb', line 25

def pdf_defaults
  {
    page_size: 'A4',
    margin_top: '1.5cm',
    margin_bottom: '1.4cm',
    margin_left: '1.5cm',
    margin_right: '1.5cm',
    print_media_type: true
  }
end

#run(data, dest, settings = @defaults) ⇒ Object

Raises:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/resumer/export.rb', line 106

def run(data, dest, settings = @defaults)
  raise Error, 'Invalid or empty source data' if data.nil?

  html = create_html(data, settings[:theme], (settings[:format] == :html))
  case settings[:format]
  # Process HTML export
  when :html
    save_html(html, dest)
  # Process PDF export
  when :pdf
    save_pdf(html, dest, settings[:theme])
  else
    raise Error, "Invalid format #{settings[:format].to_s.upcase}"
  end
end

#save_html(html, dest) ⇒ Object



85
86
87
88
89
# File 'lib/resumer/export.rb', line 85

def save_html(html, dest)
  File.open(dest, 'w+') do |file|
    file.puts html
  end
end

#save_pdf(html, dest, theme = ) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/resumer/export.rb', line 91

def save_pdf(html, dest, theme = @defaults[:theme])
  pdf_config
  kit = PDFKit.new(html)

  # Load generic styles
  kit.stylesheets << "#{theme}/css/styles.css"

  # Load PDF-specific styles
  pdfstyles = "#{theme}/css/pdf.styles.css"
  kit.stylesheets << pdfstyles if File.exist? pdfstyles

  # Compile and save
  kit.to_file(dest)
end