This file is indexed.

/usr/lib/ruby/vendor_ruby/kramdown/converter/pdf.rb is in ruby-kramdown 1.4.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# -*- coding: utf-8 -*-
#
#--
# Copyright (C) 2009-2014 Thomas Leitner <t_leitner@gmx.at>
#
# This file is part of kramdown which is licensed under the MIT.
#++
#

require 'prawn'
require 'prawn/table'
require 'kramdown/utils/entities'
require 'open-uri'

module Kramdown

  module Converter

    # 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
    #
    #
    class Pdf < Base

      include Prawn::Measurements

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

      # PDF templates are applied before conversion. They should contain code to augment the
      # converter object (i.e. to override the methods).
      def apply_template_before?
        true
      end

      # Returns +false+.
      def apply_template_after?
        false
      end

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

      # 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.
      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.has_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

      protected

      # Render the children of this element with the given options and return the results as array.
      #
      # Each time a child is rendered, the +TYPE_options+ method is invoked (if it exists) to get
      # the specific options for the element with which the given options are updated.
      def inner(el, opts)
        @stack.push([el, opts])
        result = el.children.map do |inner_el|
          options = opts.dup
          options.update(send(DISPATCHER_OPTIONS[inner_el.type], inner_el, options))
          convert(inner_el, options)
        end.flatten.compact
        @stack.pop
        result
      end


      # ----------------------------
      # :section: Element rendering methods
      # ----------------------------


      def root_options(root, opts)
        {:font => 'Times-Roman', :size => 12, :leading => 2}
      end

      def render_root(root, opts)
        @pdf = setup_document(root)
        inner(root, root_options(root, opts))
        create_outline(root)
        finish_document(root)
        @pdf.render
      end

      def header_options(el, opts)
        size = opts[:size] * 1.15**(6 - el.options[:level])
        {
          :font => "Helvetica", :styles => (opts[:styles] || []) + [:bold],
          :size => size, :bottom_padding => opts[:size], :top_padding => opts[:size]
        }
      end

      def render_header(el, opts)
        render_padded_and_formatted_text(el, opts)
      end

      def p_options(el, opts)
        bpad = (el.options[:transparent] ? opts[:leading] : opts[:size])
        {:align => :justify, :bottom_padding => bpad}
      end

      def render_p(el, opts)
        if el.children.size == 1 && el.children.first.type == :img
          render_standalone_image(el, opts)
        else
          render_padded_and_formatted_text(el, opts)
        end
      end

      def render_standalone_image(el, opts)
        img = el.children.first
        line = img.options[:location]

        if img.attr['src'].empty?
          warning("Rendering an image without a source is not possible#{line ? " (line #{line})" : ''}")
          return nil
        elsif img.attr['src'] !~ /\.jpe?g$|\.png$/
          warning("Cannot render images other than JPEG or PNG, got #{img.attr['src']}#{line ? " on line #{line}" : ''}")
          return nil
        end

        img_dirs = (@options[:image_directories] || ['.']).dup
        begin
          img_path = File.join(img_dirs.shift, img.attr['src'])
          image_obj, image_info = @pdf.build_image_object(open(img_path))
        rescue
          img_dirs.empty? ? raise : retry
        end

        options = {:position => :center}
        if img.attr['height'] && img.attr['height'] =~ /px$/
          options[:height] = img.attr['height'].to_i / (@options[:image_dpi] || 150.0) * 72
        elsif img.attr['width'] && img.attr['width'] =~ /px$/
          options[:width] = img.attr['width'].to_i / (@options[:image_dpi] || 150.0) * 72
        else
          options[:scale] =[(@pdf.bounds.width - mm2pt(20)) / image_info.width.to_f, 1].min
        end

        if img.attr['class'] =~ /\bright\b/
          options[:position] = :right
          @pdf.float { @pdf.embed_image(image_obj, image_info, options) }
        else
          with_block_padding(el, opts) do
            @pdf.embed_image(image_obj, image_info, options)
          end
        end
      end

      def blockquote_options(el, opts)
        {:styles => [:italic]}
      end

      def render_blockquote(el, opts)
        @pdf.indent(mm2pt(10), mm2pt(10)) { inner(el, opts) }
      end

      def ul_options(el, opts)
        {:bottom_padding => opts[:size]}
      end

      def render_ul(el, opts)
        with_block_padding(el, opts) do
          el.children.each do |li|
            @pdf.float { @pdf.formatted_text([text_hash("•", opts)]) }
            @pdf.indent(mm2pt(6)) { convert(li, opts) }
          end
        end
      end

      def ol_options(el, opts)
        {:bottom_padding => opts[:size]}
      end

      def render_ol(el, opts)
        with_block_padding(el, opts) do
          el.children.each_with_index do |li, index|
            @pdf.float { @pdf.formatted_text([text_hash("#{index+1}.", opts)]) }
            @pdf.indent(mm2pt(6)) { convert(li, opts) }
          end
        end
      end

      def li_options(el, opts)
        {}
      end

      def render_li(el, opts)
        inner(el, opts)
      end

      def dl_options(el, opts)
        {}
      end

      def render_dl(el, opts)
        inner(el, opts)
      end

      def dt_options(el, opts)
        {:styles => (opts[:styles] || []) + [:bold], :bottom_padding => 0}
      end

      def render_dt(el, opts)
        render_padded_and_formatted_text(el, opts)
      end

      def dd_options(el, opts)
        {}
      end

      def render_dd(el, opts)
        @pdf.indent(mm2pt(10)) { inner(el, opts) }
      end

      def math_options(el, opts)
        {}
      end

      def render_math(el, opts)
        if el.options[:category] == :block
          @pdf.formatted_text([{:text => el.value}], block_hash(opts))
        else
          {:text => el.value}
        end
      end

      def hr_options(el, opts)
        {:top_padding => opts[:size], :bottom_padding => opts[:size]}
      end

      def render_hr(el, opts)
        with_block_padding(el, opts) do
          @pdf.stroke_horizontal_line(@pdf.bounds.left + mm2pt(5), @pdf.bounds.right - mm2pt(5))
        end
      end

      def codeblock_options(el, opts)
        {
          :font => 'Courier', :color => '880000',
          :bottom_padding => opts[:size]
        }
      end

      def render_codeblock(el, opts)
        with_block_padding(el, opts) do
          @pdf.formatted_text([text_hash(el.value, opts, false)], block_hash(opts))
        end
      end

      def table_options(el, opts)
        {:bottom_padding => opts[:size]}
      end

      def render_table(el, opts)
        data = []
        el.children.each do |container|
          container.children.each do |row|
            data << []
            row.children.each do |cell|
              if cell.children.any? {|child| child.options[:category] == :block}
                line = el.options[:location]
                warning("Can't render tables with cells containing block elements#{line ? " (line #{line})" : ''}")
                return
              end
              cell_data = inner(cell, opts)
              data.last << cell_data.map {|c| c[:text]}.join('')
            end
          end
        end
        with_block_padding(el, opts) do
          @pdf.table(data, :width => @pdf.bounds.right) do
            el.options[:alignment].each_with_index do |alignment, index|
              columns(index).align = alignment unless alignment == :default
            end
          end
        end
      end



      def text_options(el, opts)
        {}
      end

      def render_text(el, opts)
        text_hash(el.value.to_s, opts)
      end

      def em_options(el, opts)
        if opts[:styles] && opts[:styles].include?(:italic)
          {:styles => opts[:styles].reject {|i| i == :italic}}
        else
          {:styles => (opts[:styles] || []) << :italic}
        end
      end

      def strong_options(el, opts)
        {:styles => (opts[:styles] || []) + [:bold]}
      end

      def a_options(el, opts)
        hash = {:color => '000088'}
        if el.attr['href'].start_with?('#')
          hash[:anchor] = el.attr['href'].sub(/\A#/, '')
        else
          hash[:link] = el.attr['href']
        end
        hash
      end

      def render_em(el, opts)
        inner(el, opts)
      end
      alias_method :render_strong, :render_em
      alias_method :render_a, :render_em

      def codespan_options(el, opts)
        {:font => 'Courier', :color => '880000'}
      end

      def render_codespan(el, opts)
        text_hash(el.value, opts)
      end

      def br_options(el, opts)
        {}
      end

      def render_br(el, opts)
        text_hash("\n", opts, false)
      end

      def smart_quote_options(el, opts)
        {}
      end

      def render_smart_quote(el, opts)
        text_hash(smart_quote_entity(el).char, opts)
      end

      def typographic_sym_options(el, opts)
        {}
      end

      def render_typographic_sym(el, opts)
        str = if el.value == :laquo_space
                ::Kramdown::Utils::Entities.entity('laquo').char +
                  ::Kramdown::Utils::Entities.entity('nbsp').char
              elsif el.value == :raquo_space
                ::Kramdown::Utils::Entities.entity('raquo').char +
                  ::Kramdown::Utils::Entities.entity('nbsp').char
              else
                ::Kramdown::Utils::Entities.entity(el.value.to_s).char
              end
        text_hash(str, opts)
      end

      def entity_options(el, opts)
        {}
      end

      def render_entity(el, opts)
        text_hash(el.value.char, opts)
      end

      def abbreviation_options(el, opts)
        {}
      end

      def render_abbreviation(el, opts)
        text_hash(el.value, opts)
      end

      def img_options(el, opts)
        {}
      end

      def render_img(el, *args) #:nodoc:
        line = el.options[:location]
        warning("Rendering span images is not supported for PDF converter#{line ? " (line #{line})" : ''}")
        nil
      end



      def xml_comment_options(el, opts) #:nodoc:
        {}
      end
      alias_method :xml_pi_options, :xml_comment_options
      alias_method :comment_options, :xml_comment_options
      alias_method :blank_options, :xml_comment_options
      alias_method :footnote_options, :xml_comment_options
      alias_method :raw_options, :xml_comment_options
      alias_method :html_element_options, :xml_comment_options

      def render_xml_comment(el, opts) #:nodoc:
        # noop
      end
      alias_method :render_xml_pi, :render_xml_comment
      alias_method :render_comment, :render_xml_comment
      alias_method :render_blank, :render_xml_comment

      def render_footnote(el, *args) #:nodoc:
        line = el.options[:location]
        warning("Rendering #{el.type} not supported for PDF converter#{line ? " (line #{line})" : ''}")
        nil
      end
      alias_method :render_raw, :render_footnote
      alias_method :render_html_element, :render_footnote


      # ----------------------------
      # :section: Organizational methods
      #
      # These methods are used, for example, to up the needed Prawn::Document instance or to create
      # a PDF outline.
      # ----------------------------


      # This module gets mixed into the Prawn::Document instance.
      module PrawnDocumentExtension

        # Extension for the formatted box class to recognize images and move text around them.
        module CustomBox

          def available_width
            return super unless @document.respond_to?(:converter) && @document.converter

            @document.image_floats.each do |pn, x, y, w, h|
              next if @document.page_number != pn
              if @at[1] + @baseline_y <= y - @document.bounds.absolute_bottom &&
                  (@at[1] + @baseline_y + @arranger.max_line_height + @leading >= y - h - @document.bounds.absolute_bottom)
                return @width - w
              end
            end

            return super
          end

        end

        Prawn::Text::Formatted::Box.extensions << CustomBox

        # Access the converter instance from within Prawn
        attr_accessor :converter

        def image_floats
          @image_floats ||= []
        end

        # Override image embedding method for adding image positions to #image_floats.
        def embed_image(pdf_obj, info, options)
          # find where the image will be placed and how big it will be
          w,h = info.calc_image_dimensions(options)

          if options[:at]
            x,y = map_to_absolute(options[:at])
          else
            x,y = image_position(w,h,options)
            move_text_position h
          end

          #--> This part is new
          if options[:position] == :right
            image_floats << [page_number, x - 15, y, w + 15, h + 15]
          end

          # add a reference to the image object to the current page
          # resource list and give it a label
          label = "I#{next_image_id}"
          state.page.xobjects.merge!(label => pdf_obj)

          # add the image to the current page
          instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
          add_content instruct % [ w, h, x, y - h, label ]
        end

      end


      # Return a hash with options that are suitable for Prawn::Document.new.
      #
      # Used in #setup_document.
      def document_options(root)
        {
          :page_size => 'A4', :page_layout => :portrait, :margin => mm2pt(20),
          :info => {
            :Creator => 'kramdown PDF converter',
            :CreationDate => Time.now
          },
          :compress => true, :optimize_objects => true
        }
      end

      # Create a Prawn::Document object and return it.
      #
      # Can be used to define repeatable content or register fonts.
      #
      # Used in #render_root.
      def setup_document(root)
        doc = Prawn::Document.new(document_options(root))
        doc.extend(PrawnDocumentExtension)
        doc.converter = self
        doc
      end

      #
      #
      # Used in #render_root.
      def finish_document(root)
        # no op
      end

      # Create the PDF outline from the header elements in the TOC.
      def create_outline(root)
        toc = ::Kramdown::Converter::Toc.convert(root).first

        text_of_header = lambda do |el|
          if el.type == :text
            el.value
          else
            el.children.map {|c| text_of_header.call(c)}.join('')
          end
        end

        add_section = lambda do |item, parent|
          text = text_of_header.call(item.value)
          destination = @dests[item.attr[:id]]
          if !parent
            @pdf.outline.page(:title => text, :destination => destination)
          else
            @pdf.outline.add_subsection_to(parent) do
              @pdf.outline.page(:title => text, :destination => destination)
            end
          end
          item.children.each {|c| add_section.call(c, text)}
        end

        toc.children.each do |item|
          add_section.call(item, nil)
        end
      end


      # ----------------------------
      # :section: Helper methods
      # ----------------------------


      # Move the prawn document cursor down before and/or after yielding the given block.
      #
      # The :top_padding and :bottom_padding options are used for determinig the padding amount.
      def with_block_padding(el, opts)
        @pdf.move_down(opts[:top_padding]) if opts.has_key?(:top_padding)
        yield
        @pdf.move_down(opts[:bottom_padding]) if opts.has_key?(:bottom_padding)
      end

      # Render the children of the given element as formatted text and respect the top/bottom
      # padding (see #with_block_padding).
      def render_padded_and_formatted_text(el, opts)
        with_block_padding(el, opts) { @pdf.formatted_text(inner(el, opts), block_hash(opts)) }
      end

      # Helper function that returns a hash with valid "formatted text" options.
      #
      # The +text+ parameter is used as value for the :text key and if +squeeze_whitespace+ is
      # +true+, all whitespace is converted into spaces.
      def text_hash(text, opts, squeeze_whitespace = true)
        text = text.gsub(/\s+/, ' ') if squeeze_whitespace
        hash = {:text => text}
        [:styles, :size, :character_spacing, :font, :color, :link,
         :anchor, :draw_text_callback, :callback].each do |key|
          hash[key] = opts[key] if opts.has_key?(key)
        end
        hash
      end

      # Helper function that returns a hash with valid options for the prawn #text_box extracted
      # from the given options.
      def block_hash(opts)
        hash = {}
        [:align, :valign, :mode, :final_gap, :leading, :fallback_fonts,
         :direction, :indent_paragraphs].each do |key|
          hash[key] = opts[key] if opts.has_key?(key)
        end
        hash
      end

    end

  end
end