frontman icon indicating copy to clipboard operation
frontman copied to clipboard

Register custom renderers

Open DevinCodes opened this issue 5 years ago • 3 comments

Description

Frontman supports HAML, ERB and Markdown out of the box. However, it could be nice to extend on this and give users the possibility to implement and add custom renderers, so that they can use even more templating languages.

Next steps

  • Write an RFC for the addition of custom renderers.

DevinCodes avatar Jul 01 '20 16:07 DevinCodes

Proposed syntax

A user could add custom renderers (or community-created renderers) from their config.rb, with the following approach:

require 'frontman-community-renderer' # Renderer from e.g. external package
require './lib/custom_renderer' # custom renderer in own project

Frontman::Config.set :custom_renderers, { comm: CommunityRenderer.instance, cust: CustomRenderer.instance }

In lib/frontman/renderers/renderer_resolver.rb, we can then merge the default renderers, and the custom renderers, making them all available to use, like this:

def get_renderer(extension)
      renderers = { 
        'erb': Frontman::ErbRenderer.instance,
        'md': Frontman::MarkdownRenderer.instance,
        'haml': Frontman::HamlRenderer.instance,
      }.merge(Frontman::Config.get(:custom_renderers, fallback: {}))

    renderers[extension.to_sym]
end

DevinCodes avatar Sep 02 '20 09:09 DevinCodes

How about we abstract away .instance part

While writing this out I also noticed, theres likely a chance to improve performance via caching of the renderers hash

Frontman::Config.set :custom_renderers, { comm: CommunityRenderer, cust: CustomRenderer }

def get_renderer(extension)
  @renderers ||= begin
    renderers = { 
      'erb': Frontman::ErbRenderer.instance,
      'md': Frontman::MarkdownRenderer.instance,
      'haml': Frontman::HamlRenderer.instance,
    }.merge(Frontman::Config.get(:custom_renderers, fallback: {}))

    custom_renderers = Frontman::Config.get(:custom_renderers, fallback: {})

    custom_renderers.each do |k,v|
      custom_renderers[k] = v.instance
    end

    renderers
  end

  @renderers[extension.to_sym]
end

westonganger avatar Oct 01 '20 00:10 westonganger

Great idea, let's try this out 😄

DevinCodes avatar Oct 08 '20 12:10 DevinCodes