Adding an input filter to automatically replace text with HTML via a script I am using Drupal 8 on Pantheon.

This is a website for an ebook.  The ebook HTML was generated using a Perl script that automatically replaces text like this:


APPLE -> HTML including an apple image
ORANGE -> HTML including an orange image


The list of these replacements is kept in a plain text file (there are about 200 replacements), and then there is a relatively short Perl script that does the actual replacing (for the ebook).

Now, I want to use these replacements in my Drupal site.

Following this lullabot tutorial, I created a custom input filter in a custom module.

But now I am wondering about how I should proceed.

In the future, the replacements for the ebook will be changed every month, so I need to make sure that it isn't too painful to keep the ebook replacements and Drupal replacements in sync (ideally, I would use the same files).

So, 


Should I try to find a way to call the Perl script from within Drupal, and share the exact same code between Drupal and the ebook?
Or should I rewrite the Perl script in PHP and incorporate it into the custom input filter?
Or should I do something else entirely?
I would rewrite the relatively short script. This shouldn't be a problem because you can re-use the Perl Regex in PHP, see http://php.net/manual/en/book.pcre.php

More Drupal specific would be caching. Add a cache tag to the filter result

  public function process($text, $langcode) {
    ...
    $result = new FilterProcessResult($text);
    $result-&gt;addCacheTags(['mymodule:filter']);
    return $result;
  }


so that you can invalidate the cached entities when the replacement text file gets updated:

\Drupal::service('cache_tags.invalidator')-&gt;invalidateTags(['mymodule:filter']);