tilt icon indicating copy to clipboard operation
tilt copied to clipboard

Accessing all registered file extensions

Open lilith opened this issue 7 years ago • 3 comments

In 1.x, I could glob for all tilt-registered file extensions like this: "*.{#{Tilt.mappings.keys.join(',')}}"

Is there any way in 2.X to enumerate the file extensions supported by Tilt at runtime?

lilith avatar Aug 17 '18 07:08 lilith

There is currently no API for this. I could provide one, but I'd like to know the use case first. For instance:

  • Do you want all file extensions that we know can be rendered? (e.g: only explicitly loaded template classes are counted)
  • Do you want all file extensions that there are template classes (possibly lazy loaded) for?

In general I'd recommend to not depend on registered template classes since it can change in point releases and cause problems for end users. Instead you could:

  • Add an extension blacklist, and otherwise pass all files to Tilt
  • Require that files that are processed by Tilt are in a specific directory
  • Require that files that are processed by Tilt have a common prefix/postfix

All of these approaches are less "magic" and thus less fragile.

You can also implementing the glob filtering in Ruby: Fetch all files and then check if it's possible to render it:

fiiles = Dir["*"].select { |x| Tilt[x] }

judofyr avatar Aug 17 '18 13:08 judofyr

Either would be fine for my cms - only supported or all potential extensions.

I don't mind breaking with Tilt point releases; Hardwired is really just a Tilt wrapper with dynamic file contents indexing.

On Fri, Aug 17, 2018, 7:07 AM Magnus Holm [email protected] wrote:

There is currently no API for this. I could provide one, but I'd like to know the use case first. For instance:

  • Do you want all file extensions that we know can be rendered? (e.g: only explicitly loaded template classes are counted)
  • Do you want all file extensions that there are template classes (possibly lazy loaded) for?

In general I'd recommend to not depend on registered template classes since it can change in point releases and cause problems for end users. Instead you could:

  • Add an extension blacklist, and otherwise pass all files to Tilt
  • Require that files that are processed by Tilt are in a specific directory
  • Require that files that are processed by Tilt have a common prefix/postfix

All of these approaches are less "magic" and thus less fragile.

You can also implementing the glob filtering in Ruby: Fetch all files and then check if it's possible to render it:

fiiles = Dir["*"].select { |x| Tilt[x] }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rtomayko/tilt/issues/331#issuecomment-413859422, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGlnwn8rwHCINLFO9ObkDmbrfig6bjfks5uRr_0gaJpZM4WBF17 .

lilith avatar Aug 17 '18 18:08 lilith

I don't think we should add this as a specific method. Using lazy_map leads to likely incorrect results, unless you have every possible template engine Tilt could use installed. By default, template_map is empty, so the results would not be useful.

Using both is already possible via the public API:

Tilt.default_mapping.template_map.keys
# => []

Tilt.default_mapping.lazy_map.keys[0..10].join(',')
# => "erb,rhtml,erubis,erubi,markdown,mkd,md,ad,adoc,asciidoc,es6"

If you wanted to be accurate and didn't care about memory, you could do:

Tilt.default_mapping.lazy_map.keys.map{|k| begin Tilt[k]; rescue LoadError; else; k end}.compact.join(',')
=> "erb,rhtml,erubi,markdown,mkd,md,ad,adoc,asciidoc,builder,rcsv,etn,etanni,haml,nokogiri,html,rdoc,sass,scss,sigil,str"

jeremyevans avatar Aug 03 '22 20:08 jeremyevans