Re: Registering a transform that always runs, without attaching it to a custom component

Guenter Milde via Docutils-users <[email protected]> Fri, 15 Jan 2021 11:18:16 -0000 (UTC)
Newsgroups gmane.text.docutils.user
Message-ID <[email protected]>
On 2021-01-14, Clément Pit-Claudel wrote:
> Hi there,

> I have a custom directive FooDirective.  It creates a custom pending
> node to run FooTransform.  FooTransform gathers all pending foo nodes
> and applies a transformation. The results are cached in a file, because
> the transformation runs costly computations.

> At the moment, users of the directive just call
> docutils.directives.register_directive('foo', FooDirective). 

Where is this done? A custom front-end?

> There's one problem: I don't have any 'foo's in the document, the cache
> should be cleared.  But, with no 'foo's, no pending node is added and
> the transform isn't executed; hence, the cache isn't cleared.

> How can I register a directive that always runs?

> I'm aware of the get_transform method on components, but I don't have a
> custom component; I'm just writing a directive.

I have never done this, but I'd try to get inspiration from the function
that processes the list returned by component.get_transform(). This is
transforms.Transformer.populate_from_components() where you will find::

        for component in components:
            if component is None:
                continue
            self.add_transforms(component.get_transforms())
            self.components[component.component_type] = component
 
So, maybe calling ::

  document.transformer.add_transform(FooTransform)
  
will do the trick (this is not tested and most probably needs adaption).



> In Sphinx I'd use app.add_transform; is there a Docutils equivalent?

> Or is there a better way to do this?

You may also consider subclassing a suitable component that adds the
transform. This can be done in the same module/script that also registers
the directive.

Günter



_______________________________________________
Docutils-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.