Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing all registered file extensions #331

Open
lilith opened this issue Aug 17, 2018 · 3 comments
Open

Accessing all registered file extensions #331

lilith opened this issue Aug 17, 2018 · 3 comments

Comments

@lilith
Copy link
Contributor

lilith commented Aug 17, 2018

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?

@judofyr
Copy link
Collaborator

judofyr commented Aug 17, 2018

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] }

@lilith
Copy link
Contributor Author

lilith commented Aug 17, 2018 via email

@jeremyevans
Copy link
Contributor

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants