Login  Register

Re: Exploring extensions

Posted by 1marc1 on Dec 29, 2018; 1:51pm
URL: https://discuss.asciidoctor.org/Exploring-extensions-tp6659p6661.html

Hi team,

After a lot of puzzling, I learned a lot and I think I can answer my own questions.

@Dan Allen, please correct me if I am wrong.

First of all, for some reason I was under the impression that the extensions were somehow in addition to the functions inside converter.rb. They are not. They replace these functions. However, you can write an extension that makes a logical decision to either run the original code from converter.rb or the code from your extension. Your extension will then look something like:

if condition
  super
else
  custom code
end

The above example essentially says: if a certain condition is true, then run the original code (super), in all other cases, run the custom code. There is no interaction between the original code and the code in the extension. Hence why I was getting the errors.

In order to solve the example task I gave myself (adding the keyword "green" to every PDF file that is created), I have created an extension that contains the same code of the "build_pdf_info" function inside converter.rb. I then changed the "if" statement for the "keywords" to look like this:

if doc.attr? 'keywords'
  info[:Keywords] = ('green ').as_pdf
  info[:Keywords] += (doc.attr 'keywords').as_pdf
end

Doing so will set the first keyword to "green" and will then append all keywords that are defined inside the document via the "keywords" attribute.

I hope this is helpful for someone along the way.

Marc.