class Jekyll::Amazon::AmazonTag
Constants
- DEFAULT_COUNTRY
- DEFAULT_LOCALE
Public Class Methods
new(tag_name, markup, tokens)
click to toggle source
Calls superclass method
# File lib/jekyll-amazon/amazon_tag.rb, line 103 def initialize(tag_name, markup, tokens) super parse_options(markup) error "No ASIN given in #{tag_name} tag" if @asin.nil? || @asin.empty? end
Public Instance Methods
render(context)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 109 def render(context) setup(context) setup_i18n item = AmazonResultCache.instance.item_lookup(@asin.to_s) return unless item render_from_file(@template_type, item) end
Private Instance Methods
br2nl(text)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 150 def br2nl(text) text.gsub(%r{<br\s*/?>}, "\n") unless text.nil? end
error(message)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 154 def error(message) raise SyntaxError, message end
parse_options(markup)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 135 def parse_options(markup) options = (markup || '').split(' ').map(&:strip) @asin = options.shift @template_type = options.shift || :title end
render_from_file(type, item)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 141 def render_from_file(type, item) if @template_dir template_file = File.expand_path("#{type}.erb", @template_dir) file = template_file if File.exist? template_file end file ||= File.expand_path("../../templates/#{type}.erb", __dir__) ERB.new(open(file).read).result(binding) end
setup(context)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 119 def setup(context) @site = context.registers[:site] @config = @site.config['jekyll-amazon'] || {} country = @config['country'] || DEFAULT_COUNTRY @template_dir = @config['template_dir'] AmazonResultCache.instance.setup(country) end
setup_i18n()
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 127 def setup_i18n locale = @config['locale'] || DEFAULT_LOCALE I18n.enforce_available_locales = false I18n.locale = locale.to_sym file = File.expand_path("../../locales/#{locale}.yml", __dir__) I18n.load_path = [file] end