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.
-
#generate_cover! ⇒ Object
Generate cover file if optional cover was given.
-
#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
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gimli/converter.rb', line 16 def initialize(files, config) @files, @config = files, config @stylesheets = [] parameters = [@config.wkhtmltopdf_parameters] if config.cover @coverfile = Tempfile.new(['coverfile', '.html']) parameters << '--cover' << @coverfile.path end @wkhtmltopdf = Wkhtmltopdf.new parameters.join(' ') end |
Instance Method Details
#add_head(html) ⇒ Object
73 74 75 |
# File 'lib/gimli/converter.rb', line 73 def add_head(html) html = "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n</head><body>\n#{html}</body></html>" end |
#append_stylesheets(html) ⇒ Object
85 86 87 88 89 |
# File 'lib/gimli/converter.rb', line 85 def append_stylesheets(html) @stylesheets.each do |stylesheet| html.gsub!(/<\/head>/, style_tag_for(stylesheet) + '</head>') end end |
#convert! ⇒ Object
Convert the file and save it as a PDF file
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gimli/converter.rb', line 29 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
52 53 54 55 56 57 58 59 |
# File 'lib/gimli/converter.rb', line 52 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 |
#generate_cover! ⇒ Object
Generate cover file if optional cover was given
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/gimli/converter.rb', line 128 def generate_cover! return unless @config.cover cover_file = MarkupFile.new @config.cover markup = Markup::Renderer.new cover_file html = "<div class=\"cover\">\n#{markup.render}\n</div>" append_stylesheets(html) html = add_head(html) @coverfile.write(html) @coverfile.close end |
#load_stylesheets ⇒ Object
Load the stylesheets to pdfkit loads the default and the user selected if any
78 79 80 81 82 83 |
# File 'lib/gimli/converter.rb', line 78 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 ./
103 104 105 106 107 |
# File 'lib/gimli/converter.rb', line 103 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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/gimli/converter.rb', line 112 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
64 65 66 67 68 69 70 71 |
# File 'lib/gimli/converter.rb', line 64 def output_pdf(html, filename) html = add_head html load_stylesheets generate_cover! append_stylesheets html puts @wkhtmltopdf.command(output_file(filename)).join(' ') if @config.debug @wkhtmltopdf.output_pdf html, output_file(filename) end |
#style_tag_for(stylesheet) ⇒ Object
91 92 93 |
# File 'lib/gimli/converter.rb', line 91 def style_tag_for(stylesheet) "<style>#{File.read(stylesheet)}</style>" end |
#stylesheet ⇒ String
Returns the selected stylesheet. Defaults to ./gimli.css
97 98 99 |
# File 'lib/gimli/converter.rb', line 97 def stylesheet @config.stylesheet.nil? ? 'gimli.css' : @config.stylesheet end |