htdebeer / paru

Control pandoc with Ruby and write pandoc filters in Ruby
https://heerdebeer.org/Software/markdown/paru/
GNU General Public License v3.0
38 stars 12 forks source link

Inline filter possible? #52

Closed evaneykelen closed 4 years ago

evaneykelen commented 4 years ago

I'm looking for a way to embed filters "inline", for example:

 converter = Paru::Pandoc.new do
  from "textile"
  to "html"
  filter do
    ...
  end
end

Is this possible? Or is there another way?

htdebeer commented 4 years ago

This is not possible. You can, however, configure the converter to use any external filter you want. For example, the following should work:

convert = Paru::Pandoc.new do
    from "textile"
    to "html"
    filter "./my_internal_filter.rb"
end

Do also note that filters are called in order, so

converter = Paru::Pandoc.new do
    from "textile"
    to "html
    filter "./first_filter.rb"
    filter "./second_filter.rb"
    filter "./etc_filter.rb"
end

should work as well. It would first run the first filter on the whole document, then the second on the output of the first, and so on.

evaneykelen commented 4 years ago

@htdebeer: thank you for confirming that run-time execution of "external" Ruby files is the only way to invoke filters.

I love the unix-like piping of filter inputs & outputs btw!

htdebeer commented 4 years ago

Do note that you can run any script / program as a filter via paru, not only those written in Ruby. The pandoc wrapper part of paru is just a very thin shell around the command-line of pandoc, so anything you can do with pandoc on the command-line you can also do in a paru converter.

evaneykelen commented 4 years ago

you can run any script / program as a filter via paru

This is exactly the reason for my initial hesitation since this is potentially a security issue. I like to avoid run time invocations of shell commands as much as possible. However since the filter is under my control, and not permitting user input, it's safe enough.