k0kubun / hamlit

High Performance Haml Implementation
https://rubygems.org/gems/hamlit
Other
981 stars 59 forks source link

Register a custom filter? #114

Closed swrobel closed 7 years ago

swrobel commented 7 years ago

How do I register a custom filter, say babel-transpiler for es6?

k0kubun commented 7 years ago

Like this:

module Hamlit
  class Filters
    class Es6 < TiltBase
      def compile(node)
        # branch with `@format` here if you want
        compile_html(node)
      end

      private

      def compile_html(node)
        temple = [:multi]
        temple << [:static, "<script>\n"]
        temple << compile_with_tilt(node, 'es6', indent_width: 2)
        temple << [:static, "\n</script>"]
        temple
      end
    end
    register :es6, Es6 # <-- this part
  end
end

Unfortunately Hamlit doesn't have the compatible interface with register_tilt_filter (it generates a filter for Tilt and inherits existing one if necessary, and it depends on Haml's internal).

I hope the above code works for your use case.

swrobel commented 7 years ago

Thank you so much for the quick reply. That worked perfectly. 😁