summaryrefslogtreecommitdiff
path: root/docs/modules/extend/examples/pdf-converter-numbered-paragraphs.rb
blob: decd5574c6d2f9d25d720ef285503b225a0bab99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class PDFConverterNumberedParagraphs < (Asciidoctor::Converter.for 'pdf')
  register_for 'pdf'

  def init_pdf doc
    doc
      .find_by(context: :paragraph) {|candidate| [:document, :section].include? candidate.parent.context }
      .each_with_index {|paragraph, idx| paragraph.set_attr 'number', idx + 1 }
    super
  end

  def convert_paragraph node
    if (paragraph_number = node.attr 'number')
      float do
        label = %(#{paragraph_number}.#{::Prawn::Text::NBSP})
        label_width = rendered_width_of_string label
        bounding_box [-label_width, cursor], width: label_width do
          ink_prose label, color: 'CCCCCC', align: :right, margin: 0, single_line: true
        end
      end
    end
    super
  end
end