Class: Gimli::Converter
- Inherits:
-
Object
- Object
- Gimli::Converter
- Defined in:
- lib/gimli/converter.rb
Overview
The class that converts the files
Instance Method Summary collapse
- #add_head(html) ⇒ Object
- #append_stylesheets(html) ⇒ Object
-
#convert! ⇒ Object
Convert the file and save it as a PDF file.
-
#convert_image_urls(html, filename) ⇒ String
Rewrite relative image urls to absolute.
-
#initialize(files, config) ⇒ Converter
constructor
Initialize the converter with a File.
-
#load_stylesheets ⇒ Object
Load the stylesheets to pdfkit loads the default and the user selected if any.
-
#output_dir ⇒ String
Returns the directory where to save the output.
-
#output_file(file = nil) ⇒ String
Generate the name of the output file.
-
#output_pdf(html, filename) ⇒ Object
Create the pdf.
- #style_tag_for(stylesheet) ⇒ Object
-
#stylesheet ⇒ String
Returns the selected stylesheet.
Constructor Details
#initialize(files, config) ⇒ Converter
Initialize the converter with a File
15 16 17 18 19 20 |
# File 'lib/gimli/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/gimli/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/gimli/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/gimli/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
46 47 48 49 50 51 52 53 |
# File 'lib/gimli/converter.rb', line 46 def convert_image_urls(html, filename) dir_string = ::File.dirname(::File.(filename)) html.scan(/<img[^>]+src="([^"]+)"/).each do |url| html.gsub!(url[0], ::File.(url[0], dir_string)) unless url[0] =~ /^https?/ end html end |
#load_stylesheets ⇒ Object
Load the stylesheets to pdfkit loads the default and the user selected if any
70 71 72 73 74 75 |
# File 'lib/gimli/converter.rb', line 70 def load_stylesheets # Load standard stylesheet style = ::File.("../../../config/style.css", __FILE__) @stylesheets << style @stylesheets << stylesheet if ::File.exists?(stylesheet) end |
#output_dir ⇒ String
Returns the directory where to save the output. Defaults to ./
95 96 97 98 99 |
# File 'lib/gimli/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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/gimli/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
58 59 60 61 62 63 |
# File 'lib/gimli/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/gimli/converter.rb', line 83 def style_tag_for(stylesheet) "<style>#{File.read(stylesheet)}</style>" end |
#stylesheet ⇒ String
Returns the selected stylesheet. Defaults to ./gimli.css
89 90 91 |
# File 'lib/gimli/converter.rb', line 89 def stylesheet @config.stylesheet.nil? ? 'gimli.css' : @config.stylesheet end |