Class: Kramdown::Converter::Pdf

Inherits:
Base
  • Object
show all
Includes:
Prawn::Measurements
Defined in:
lib/kramdown/converter/pdf.rb

Overview

Converts an element tree to a PDF using the prawn PDF library.

This basic version provides a nice starting point for customizations but can also be used directly.

There can be the following two methods for each element type: render_TYPE(el, opts) and TYPE_options(el, opts) where el is a kramdown element and opts an hash with rendering options.

The render_TYPE(el, opts) is used for rendering the specific element. If the element is a span element, it should return a hash or an array of hashes that can be used by the #formatted_text method of Prawn::Document. This method can then be used in block elements to actually render the span elements.

The rendering options are passed from the parent to its child elements. This allows one to define general options at the top of the tree (the root element) that can later be changed or amended.

Currently supports the conversion of all elements except those of the following types:

:html_element, :img, :footnote

Defined Under Namespace

Modules: PrawnDocumentExtension

Constant Summary collapse

VERSION =
'1.0.6'
DISPATCHER_RENDER =

:nodoc:

Hash.new {|h, k| h[k] = "render_#{k}" }
DISPATCHER_OPTIONS =

:nodoc:

Hash.new {|h, k| h[k] = "#{k}_options" }

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ Pdf

Returns a new instance of Pdf.



50
51
52
53
54
# File 'lib/kramdown/converter/pdf.rb', line 50

def initialize(root, options)
  super
  @stack = []
  @dests = {}
end

Instance Method Details

#apply_template_after?Boolean

Returns false.

Returns:

  • (Boolean)


63
64
65
# File 'lib/kramdown/converter/pdf.rb', line 63

def apply_template_after?
  false
end

#apply_template_before?Boolean

PDF templates are applied before conversion. They should contain code to augment the converter object (i.e. to override the methods).

Returns:

  • (Boolean)


58
59
60
# File 'lib/kramdown/converter/pdf.rb', line 58

def apply_template_before?
  true
end

#convert(el, opts = {}) ⇒ Object

Invoke the special rendering method for the given element el.

A PDF destination is also added at the current location if th element has an ID or if the element is of type :header and the :auto_ids option is set.



74
75
76
77
78
79
80
81
82
# File 'lib/kramdown/converter/pdf.rb', line 74

def convert(el, opts = {})
  id = el.attr['id']
  id = generate_id(el.options[:raw_text]) if !id && @options[:auto_ids] && el.type == :header
  if !id.to_s.empty? && !@dests.key?(id)
    @pdf.add_dest(id, @pdf.dest_xyz(0, @pdf.y))
    @dests[id] = @pdf.dest_xyz(0, @pdf.y)
  end
  send(DISPATCHER_RENDER[el.type], el, opts)
end