Sha256: c85af9dcdad4cc910e97538314400271b1d10901178731a6de6e474c5f91831b

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

class Shortcode::Tag

  def initialize(name, configuration, attributes=[], content="", additional_attributes=nil)
    @name = name.downcase
    @configuration = configuration
    @binding = Shortcode::TemplateBinding.new(@name,
                                              @configuration,
                                              attributes,
                                              content,
                                              additional_attributes)
  end

  def markup
    template = first_priority_template
    template = second_priority_template if template.nil?
    return template unless template.nil?
    raise Shortcode::TemplateNotFound, "No template found for #{@name} in configuration or files"
  end

  def render
    render_template
  end

  private

  attr_reader :configuration

  # rubocop:disable Metrics/AbcSize
  def render_template
    case configuration.template_parser
    when :erb
      ERB.new(markup).result(@binding.expose_binding)
    when :haml
      Haml::Engine.new(markup).render(@binding)
    when :slim
      Slim::Template.new { markup }.render(@binding)
    else
      raise Shortcode::TemplateParserNotSupported, configuration.template_parser
    end
  end
  # rubocop:enable Metrics/AbcSize

  def first_priority_template
    configuration.check_config_templates_first ? markup_from_config : markup_from_file
  end

  def second_priority_template
    configuration.check_config_templates_first ? markup_from_file : markup_from_config
  end

  def markup_from_file
    template_files.each do |path|
      return File.read(path) if File.file? path
    end
    nil
  end

  def markup_from_config
    configuration.templates[@name.to_sym]
  end

  def template_files
    template_paths.map do |filename|
      File.join configuration.template_path, filename
    end
  end

  def template_paths
    ["#{@name}.html.#{configuration.template_parser}", "#{@name}.#{configuration.template_parser}"]
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shortcode-2.0.0 lib/shortcode/tag.rb
shortcode-2.0.0.pre lib/shortcode/tag.rb