Sha256: 1094e212889b3ec9c7b403f04e24ae5256d9556cff1ec79d908c3e3c2a41f0fa

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 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

  def render_template
    case configuration.template_parser
    when :erb
      ERB.new(markup).result(@binding.get_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

  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.to_s}", "#{@name}.#{configuration.template_parser.to_s}" ]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shortcode-1.2.1 lib/shortcode/tag.rb
shortcode-1.2.0 lib/shortcode/tag.rb