Class: Markover::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/markover/converter.rb

Overview

The class that converts the files

Instance Method Summary collapse

Constructor Details

#initialize(files, config) ⇒ Converter

Initialize the converter with a File

Parameters:

  • files (Array)

    The list of Markover::MarkupFile to convert (passing a single file will still work)

  • config (Markover::Config)


15
16
17
18
19
20
# File 'lib/markover/converter.rb', line 15

def initialize(files, config)
  @files, @config = files, config

  @stylesheets = []
  @wkhtmltopdf = Wkhtmltopdf.new @config.wkhtmltopdf_parameters
end

Instance Method Details

#add_head(html) ⇒ Object



65
66
67
# File 'lib/markover/converter.rb', line 65

def add_head(html)
    html.insert(0, "\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n</head>\n")
end

#append_stylesheets(html) ⇒ Object



77
78
79
80
81
# File 'lib/markover/converter.rb', line 77

def append_stylesheets(html)
  @stylesheets.each do |stylesheet|
    html.insert(0, style_tag_for(stylesheet))
  end
end

#convert!Object

Convert the file and save it as a PDF file



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/markover/converter.rb', line 23

def convert!
  merged_contents = []
  @files.each do |file|
    markup = Markup::Renderer.new file, @config.remove_front_matter
    html = convert_image_urls markup.render, file.filename
    if @config.merge
      html = "<div class=\"page-break\"></div>#{html}" unless merged_contents.empty?
      merged_contents << html
    else
      output_pdf(html, file)
    end
    puts html if @config.debug
  end

  unless merged_contents.empty?
    html = merged_contents.join
    output_pdf(html, nil)
  end
end

#convert_image_urls(html, filename) ⇒ String

Rewrite relative image urls to absolute

Parameters:

  • html (String)

    some html to parse

Returns:

  • (String)

    the html with all image urls replaced to absolute



46
47
48
49
50
51
52
53
# File 'lib/markover/converter.rb', line 46

def convert_image_urls(html, filename)
  dir_string = ::File.dirname(::File.expand_path(filename))
  html.scan(/<img[^>]+src="([^"]+)"/).each do |url|
    html.gsub!(url[0], ::File.expand_path(url[0], dir_string)) unless url[0] =~ /^https?/
  end

  html
end

#load_stylesheetsObject

Load the stylesheets to pdfkit loads the default and the user selected if any



70
71
72
73
74
75
# File 'lib/markover/converter.rb', line 70

def load_stylesheets
  # Load standard stylesheet
  style = ::File.expand_path("../../../config/style.css", __FILE__)
  @stylesheets << style
  @stylesheets << stylesheet if ::File.exists?(stylesheet)
end

#output_dirString

Returns the directory where to save the output. Defaults to ./

Returns:

  • (String)


95
96
97
98
99
# File 'lib/markover/converter.rb', line 95

def output_dir
  output_dir = @config.output_dir.nil? ? Dir.getwd : @config.output_dir
  FileUtils.mkdir_p(output_dir) unless ::File.directory?(output_dir)
  output_dir
end

#output_file(file = nil) ⇒ String

Generate the name of the output file

Parameters:

  • file (Markover::MarkupFile) (defaults to: nil)

    optionally, specify a file, otherwise use output filename

Returns:

  • (String)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/markover/converter.rb', line 104

def output_file(file = nil)
  if file
    output_filename = file.name
    if !@config.output_filename.nil? && @files.length == 1
      output_filename = @config.output_filename
    end
  else
    output_filename = Time.now.to_s.split(' ').join('_')
    output_filename = @files.last.name if @files.length == 1 || @config.merge
    output_filename = @config.output_filename unless @config.output_filename.nil?
  end

  ::File.join(output_dir, "#{output_filename}.pdf")
end

#output_pdf(html, filename) ⇒ Object

Create the pdf

Parameters:

  • html (String)

    the html input

  • filename (String)

    the name of the output file



58
59
60
61
62
63
# File 'lib/markover/converter.rb', line 58

def output_pdf(html, filename)
  load_stylesheets
  append_stylesheets html
  add_head html
  @wkhtmltopdf.output_pdf html, output_file(filename)
end

#style_tag_for(stylesheet) ⇒ Object



83
84
85
# File 'lib/markover/converter.rb', line 83

def style_tag_for(stylesheet)
  "<style>#{File.read(stylesheet)}</style>"
end

#stylesheetString

Returns the selected stylesheet. Defaults to ./markover.css

Returns:

  • (String)


89
90
91
# File 'lib/markover/converter.rb', line 89

def stylesheet
  @config.stylesheet.nil? ? 'markover.css' : @config.stylesheet
end