Class: Zlide::Redcarpet::Renderers::PDF

Inherits:
Redcarpet::Render::Base
  • Object
show all
Defined in:
lib/zlide/redcarpet/renderers/pdf.rb

Defined Under Namespace

Classes: ListData, TableData

Instance Method Summary collapse

Constructor Details

#initializePDF

Returns a new instance of PDF.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 14

def initialize
  @pdf = Prawn::Document.new(
    :page_size => "A4", 
    :page_layout => :landscape, 
    :info => {:Title => CONFIG['title']}
  )
  @pdf.define_grid(:rows => 2, :columns => 2, :gutter => 1.cm)
  @current_row = 0
  @current_column = 0
  @command_queue = []
  @current_box = @pdf.grid(0,0)
  @slide_open = false
  @list_items = []
  @table_cells = []
  @table_rows = []
  @table_cache = []
  super
end

Instance Method Details

#block_code(code, language) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 140

def block_code(code, language)
  code.gsub!(/^(\s+)/m) { |m| "\xC2\xA0" * m.size }
  code_block = Proc.new do
    font = @pdf.font.name
    @pdf.bounding_box([5, @pdf.cursor], :width => @pdf.bounds.width - 10) do
      @pdf.move_down 5
      @pdf.font "Courier"
      @pdf.indent(5) { @pdf.text code, :size => 9 }
      @pdf.line_width 0.1
      @pdf.join_style :round
      @pdf.stroke_bounds
    end
    @pdf.font font
  end
  @command_queue << code_block 
  ""
end

#double_emphasis(text) ⇒ Object



108
109
110
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 108

def double_emphasis(text)
  tag :b, text 
end

#emphasis(text) ⇒ Object



104
105
106
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 104

def emphasis(text)
  tag :i, text
end

#header(text, level) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 33

def header(text, level)
  text_size = 17 - level
  @command_queue << Proc.new do 
    @pdf.text text, :size => text_size, :style => :bold, :inline_format => true
    if level <= 2
      @pdf.line_width 0.5 
      @pdf.horizontal_rule
      @pdf.stroke
    end
    @pdf.move_down text_size
  end
  ""
end

#image(href, title, alt_text) ⇒ Object



120
121
122
123
124
125
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 120

def image(href, title, alt_text)
  @command_queue << Proc.new do
    @pdf.image File.join('public', href), :fit => [@pdf.bounds.width, @pdf.cursor - 5]
  end
  ""
end


116
117
118
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 116

def link(href, title, content)
  "<link href='#{href}'>#{content}</link>"
end

#list(content, type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 47

def list(content, type)
  data = ListData.new 
  counter = 1
  number_of_items = content.count('.') 
  @list_items.pop(number_of_items).each do |item|
    if item.is_a? String
      case type
      when :unordered
        data << ["", item]
      when :ordered
        data << ["#{counter}.", item]
        counter += 1
      end
    else
      data << [nil, item]
      @command_queue.delete(item)
    end
  end
  index = @table_cache.size
  @command_queue << data 
  @table_cache << data
  "__table_#{index}"
end

#list_item(text, type) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 71

def list_item(text, type)
  table = nil
  matches = text.scan(/\n__table_\d+/)
  if matches.any?
    table_marker = matches.first
    table = @table_cache[table_marker.sub("\n__table_", '').to_i]
    text.sub!(table_marker, '')
  end
  @list_items << text
  @list_items << table if table
  table ? ".." : "."
end

#paragraph(text) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 127

def paragraph(text)
  if text =~ /^!SLIDE/
    if @slide_open
      next_box
    else
      @slide_open = true
    end
  else
    @command_queue << Proc.new { @pdf.text(text, :inline_format => true) }
  end
  "" 
end

#strikethrough(text) ⇒ Object



112
113
114
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 112

def strikethrough(text)
  tag :strikethrough, text
end

#table(header, body) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 84

def table(header, body)
  data = TableData.new
  body_rows = @table_rows.pop(body.count('.'))
  header_rows = @table_rows.pop(body.count('.'))
  data.concat header_rows.map{|row| row.map{|column| tag(:b, column)}}
  data.concat body_rows
  @command_queue << data
  ""
end

#table_cell(content, alignment) ⇒ Object



99
100
101
102
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 99

def table_cell(content, alignment)
  @table_cells << content
  "."
end

#table_row(content) ⇒ Object



94
95
96
97
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 94

def table_row(content)
  @table_rows << @table_cells.pop(content.count('.'))
  "."
end

#write_fileObject



158
159
160
# File 'lib/zlide/redcarpet/renderers/pdf.rb', line 158

def write_file
  @pdf.render_file("#{CONFIG['title']}.pdf")
end