Class: Gimli::Converter
- Inherits:
-
Object
- Object
- Gimli::Converter
- Defined in:
- lib/gimli/converter.rb
Overview
The class that converts the files
Constant Summary collapse
- COVER_FILE_PATH =
::File.("../../../config/cover.html", __FILE__)
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
17 18 19 20 21 22 23 24 |
# File 'lib/gimli/converter.rb', line 17 def initialize(files, config) @files, @config = files, config @stylesheets = [] parameters = [@config.wkhtmltopdf_parameters] parameters << '--cover' << COVER_FILE_PATH if config.cover @wkhtmltopdf = Wkhtmltopdf.new parameters.join(' ') end |
Instance Method Details
#add_head(html) ⇒ Object
70 71 72 |
# File 'lib/gimli/converter.rb', line 70 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
82 83 84 85 86 |
# File 'lib/gimli/converter.rb', line 82 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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gimli/converter.rb', line 27 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
50 51 52 53 54 55 56 57 |
# File 'lib/gimli/converter.rb', line 50 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
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/gimli/converter.rb', line 125 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) File.open(COVER_FILE_PATH, 'w') do |f| f.write html end end |
#load_stylesheets ⇒ Object
Load the stylesheets to pdfkit loads the default and the user selected if any
75 76 77 78 79 80 |
# File 'lib/gimli/converter.rb', line 75 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 ./
100 101 102 103 104 |
# File 'lib/gimli/converter.rb', line 100 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/gimli/converter.rb', line 109 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
62 63 64 65 66 67 68 |
# File 'lib/gimli/converter.rb', line 62 def output_pdf(html, filename) html = add_head html load_stylesheets generate_cover! append_stylesheets html @wkhtmltopdf.output_pdf html, output_file(filename) end |
#style_tag_for(stylesheet) ⇒ Object
88 89 90 |
# File 'lib/gimli/converter.rb', line 88 def style_tag_for(stylesheet) "<style>#{File.read(stylesheet)}</style>" end |
#stylesheet ⇒ String
Returns the selected stylesheet. Defaults to ./gimli.css
94 95 96 |
# File 'lib/gimli/converter.rb', line 94 def stylesheet @config.stylesheet.nil? ? 'gimli.css' : @config.stylesheet end |