k0kubun / hamlit

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

Is it possible to create "include" plugin? #127

Closed kikonen closed 6 years ago

kikonen commented 6 years ago

I tried to create "include" filter to allow utilizing splitting files into more manageable chunks, but avoid overhead of calling partial 1000+ times.

I tried following kind of structure

  module Hamlit
    class Filters
      class IncludeFile < TiltBase
        def compile(node)
          include_file node
        end

        private

        def include_file(node)
          temple = [:multi]

          path = node.value[:text].strip.split('/')
          path[path.length - 1] = "_#{path.last}"
          partial_name = path.join('/')

          data = File.read "#{Rails.root}/app/views/#{partial_name}.haml"
          include_node = Hamlit::HamlParser::ParseNode.new(:tag, 1, {text: data})
          temple << compile_with_tilt(include_node, 'haml')
          temple
        end
      end

      register :include_file, IncludeFile
    end
  end

However, it fails to work. Fails with first local variable defined inside include filed

Example, this fails _included_file.haml

- some_var = ...
...

Q: Is it possible to make such structure work, or is this effort futile?

k0kubun commented 6 years ago

Please follow things described in https://github.com/k0kubun/hamlit#reporting-an-issue when you report an issue, or just create a reproductive repository if you're not sure how to report issues. Otherwise people who receive your issue report can't reproduce issues correctly and so can't help you.

I tried to prepare the reproductive environment for you https://github.com/k0kubun/misc/tree/hamlit-include, and include_file directive worked fine and at least it wasn't broken with the information you provided. What you did or something you didn't report should be wrong.

At least this should not be an issue inside Hamlit, so let me close this.


In addition to that, hamlit.gem does not intend to provide Haml parsing interface and the class you used Hamlit::HamlParser::ParserNode is internal interface which is not intended to be directly used, until we make a separated Haml parser gem which is shared among Haml and Hamlit. You're free to use it, but it's out of support for now and please help you on your own if you want hack on internal interface.