class Jekyll::WebringTag

Public Class Methods

new(tag_name, text, tokens) click to toggle source
Calls superclass method
# File lib/jekyll-webring.rb, line 119
def initialize (tag_name, text, tokens)
        super
        @text = text
end

Public Instance Methods

get_items_from_feeds(param) click to toggle source
# File lib/jekyll-webring.rb, line 138
def get_items_from_feeds (param)
        items = []

        feeds = JekyllWebring.feeds
        case param
                when 'random'
                        feeds.each do |feed_items|
                                items << feed_items.sample
                        end
                when Time, '', nil 
                        date = param || Time.now
                        feeds.each do |feed_items|
                                item_to_add = nil

                                feed_items.each do |item|
                                        if item['_date'] < date
                                                item_to_add = item
                                                break
                                        end
                                end

                                if item_to_add
                                        items << item_to_add
                                        next
                                end

                                case JekyllWebring::config['no_item_at_date_behaviour']
                                        when 'use_oldest'
                                                items << feed_items.last
                                        when 'use_latest'
                                                items << feed_items.first
                                        when 'random'
                                                items << feed_items.sample
                                        when 'ignore', ''
                                                next
                                end
                        end
        end

        items = items.sort_by { |item| item['_date'] }

        items
end
get_value(context, expression) click to toggle source
# File lib/jekyll-webring.rb, line 124
def get_value (context, expression)
        result = nil

        unless expression.empty?
                lookup_path = expression.split('.')
                result = context
                lookup_path.each do |variable|
                        result = result[variable] if result
                end
        end

        result
end
render(context) click to toggle source
# File lib/jekyll-webring.rb, line 182
def render (context)
        JekyllWebring::set_config(context)

        site = context.registers[:site]
        param = get_value(context, @text.strip)

        webring_data = JekyllWebring.get_data(site)

        if webring_data[param]
                items = webring_data[param]
        else
                items = get_items_from_feeds(param)
                webring_data[param] = items if param

                if JekyllWebring::config['data_file']
                        filename = JekyllWebring::config['data_file']
                        dirname = File.dirname filename
                        unless File.directory? dirname
                                FileUtils.mkdir_p dirname
                        end

                        File.open(filename, 'w') do |file|
                                file.write(webring_data.to_yaml)
                        end
                end
        end

        liquid_opts = site.config['liquid']

        content = JekyllWebring::TEMPLATE
        payload = context

        # stuff beyond this point mainly hacked together from jekyll internals
        filename = JekyllWebring::config['layout_file']
        if File.file? filename
                begin
                        content = File.read filename
                        if content =~ Document::YAML_FRONT_MATTER_REGEXP
                                content = $POSTMATCH
                                payload = payload.merge SafeYAML.load(Regexp.last_match(1))
                        end
                rescue Psych::SyntaxError => e
                        Jekyll.logger.warn "YAML Exception reading #{filename}: #{e.message}"
                        raise e if site.config["strict_front_matter"]
                rescue StandardError => e
                        Jekyll.logger.warn "Error reading file #{filename}: #{e.message}"
                        raise e if site.config["strict_front_matter"]
                end
        end

        template = site.liquid_renderer.file((File.file? filename) ? filename : '').parse(content)

        info = {
                :registers        => { :site => site, :page => context['page'] },
                :strict_filters   => liquid_opts['strict_filters'],
                :strict_variables => liquid_opts['strict_variables'],
        }

        webring_items = items.take(JekyllWebring::config['num_items'])
        webring_items.each { |item| item['date'] = item['_date'].strftime(JekyllWebring::config['date_format']) }

        payload['webring'] = webring_items

        template.render!(payload, info)
end