Sha256: a7bba7b5794255918b569248d25b66a3778f5d8c7ad7ccf1b2d628d891e8b073

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require "tilt/erb"
require "active_support/core_ext/hash/keys"

module Bridgetown
  class ERBView < RubyTemplateView
    include ERB::Util

    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("/")

      Tilt::ERBTemplate.new(
        site.in_source_dir(site.config[:partials_dir], "#{partial_name}.erb"),
        trim: "<>-",
        outvar: "@_erbout"
      ).render(self, options)
    end

    def markdownify
      previous_buffer_state = @_erbout
      @_erbout = +""
      result = yield
      @_erbout = previous_buffer_state

      content = Bridgetown::Utils.reindent_for_markdown(result)
      converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
      md_output = converter.convert(content).strip
      @_erbout << md_output
    end
  end

  module Converters
    class ERBTemplates < Converter
      input :erb

      # Logic to do the content conversion.
      #
      # content - String content of file (without front matter).
      #
      # Returns a String of the converted content.
      def convert(content, convertible)
        erb_view = Bridgetown::ERBView.new(convertible)

        erb_renderer = Tilt::ERBTemplate.new(trim: "<>-", outvar: "@_erbout") { content }

        if convertible.is_a?(Bridgetown::Layout)
          erb_renderer.render(erb_view) do
            convertible.current_document_output
          end
        else
          erb_renderer.render(erb_view)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bridgetown-core-0.16.0.beta1 lib/bridgetown-core/converters/erb_templates.rb