Sha256: d920ea92a271d620b2bc58c5dc4b3cfb976bd7656ea91f0917d7ba4c9b75d4f5

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require "haml"

module Bridgetown
  class HamlView < RubyTemplateView
    def partial(partial_name, options = {})
      options.merge!(options[:locals]) if options[:locals]

      partial_segments = partial_name.split("/")
      partial_segments.last.sub!(%r!^!, "_")
      partial_name = partial_segments.join("/")

      Haml::Template.new(
        site.in_source_dir(site.config[:partials_dir], "#{partial_name}.haml")
      ).render(self, options).html_safe
    end
  end

  module Converters
    class HamlTemplates < Converter
      priority :highest
      input :haml

      # Logic to do the Haml content conversion.
      #
      # @param content [String] Content of the file (without front matter).
      # @params convertible [Bridgetown::Page, Bridgetown::Document, Bridgetown::Layout]
      #   The instantiated object which is processing the file.
      #
      # @return [String] The converted content.
      def convert(content, convertible)
        return content if convertible.data[:template_engine] != "haml"

        haml_view = Bridgetown::HamlView.new(convertible)

        haml_renderer = Haml::Template.new(convertible.relative_path) { content }

        if convertible.is_a?(Bridgetown::Layout)
          haml_renderer.render(haml_view) do
            convertible.current_document_output
          end
        else
          haml_renderer.render(haml_view)
        end
      end

      def matches(ext, convertible)
        return true if convertible.data[:template_engine] == "haml"

        super(ext).tap do |ext_matches|
          convertible.data[:template_engine] = "haml" if ext_matches
        end
      end

      def output_ext(ext)
        ext == ".haml" ? ".html" : ext
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bridgetown-haml-2.0.0 lib/bridgetown-haml/haml_templates.rb