asciidoctor / asciidoctorj

:coffee: Java bindings for Asciidoctor. Asciidoctor on the JVM!
http://asciidoctor.org
Apache License 2.0
628 stars 172 forks source link

Provide templates from the classpath / generated #742

Open SevereOverfl0w opened 5 years ago

SevereOverfl0w commented 5 years ago

It would be really useful to provide templates with a custom resolver. In circumstances where you're only deploying a jar or part of a library.

I've run into this in 2 different contexts:

  1. Dynamically altering the template based on whether the user is logged in to include navbar items like "Logout".
  2. Being consumed as a library, and not wanting to "dirty" the OS with a temporary directory to put template directories in.

I solved <1> by leaving mustache tags in the HTML and replacing those later. So that didn't turn out too bad. <2> requires a way to ship things with the library which is already on the classpath.

I think the "fully dynamic" solution of being able to provide a template-resolver class would be very neat. But practically, searching relative to the classpath solves my use-cases along with the mustache solution.

robertpanzer commented 5 years ago

Hi Dominic,

Asciidoctor core only loads templates from directories in the filesystem. That means all we can do from AsciidoctorJ is to also extract some archive to the filesystem and pass that path to Asciidoctor.

It looks to me as if a Template resolver class would be a new extension point. What do you think, @mojavelinux?

mojavelinux commented 1 year ago

It should be possible to reference templates on the classloader (in other words, that are on the classpath). The syntax is as follows:

-r ./templates.jar -T uri:classloader:templates

This assumes that the templates are in a jar named templates.jar, though this can be any jar on the classpath. The uri:classloader: prefix tells JRuby to look on the classpath. templates is a folder at the root of the classpath.

I've added a test in core that verifies this is possible.