Skip to content

Instantly share code, notes, and snippets.

@jshawl
Created May 26, 2014 23:27
Show Gist options
  • Save jshawl/5d46ad2f352edb62e8eb to your computer and use it in GitHub Desktop.
Save jshawl/5d46ad2f352edb62e8eb to your computer and use it in GitHub Desktop.
require 'pp'
require 'ostruct'
require 'yaml'
require 'jekyll'
require 'date'
require 'digest/md5'
require 'action_view'
require 'rexml/document'
require 'uri'
include ActionView::Helpers::DateHelper
# From http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html
class Hash
def stringify_keys!
keys.each do |key|
self[key.to_s] = delete(key)
end
self
end
end
#
# Parses a rss/atom feed and returns items as an array.
#
class RSSFeed
class << self
def tag()
jobs = []
data = File.open('./jobs.xml','r').read()
f = REXML::Document.new(data)
f.elements.each('result/job') do |i|
job = OpenStruct.new
job.category = i.elements['category'].text
job.title = i.elements["title"].text
job.date = i.elements["date"].text
job.url = i.elements["apply-url"].text
job.description = i.elements["description"].text
jobs << job
end
jobs
end
end
end
module Jekyll
class JobsTag < Liquid::Block
include Liquid::StandardFilters
Syntax = /(#{Liquid::QuotedFragment}+)?/
def initialize(tag_name, markup, tokens)
@variable_name = 'job'
@attributes = {}
# Parse parameters
if markup =~ Syntax
markup.scan(Liquid::TagAttributes) do |key, value|
#p key + ":" + value
@attributes[key] = value
end
else
raise SyntaxError.new("Syntax Error in 'rssfeed' - Valid syntax: rssfeed uid:x count:x]")
end
@ttl = @attributes.has_key?('ttl') ? @attributes['ttl'].to_i : nil
@url = @attributes['url']
@count = @attributes['count']
@name = 'job'
super
end
def render(context)
context.registers[:rssfeed] ||= Hash.new(0)
collection = RSSFeed.tag()
length = collection.length
result = []
# loop through found items and render results
context.stack do
collection.each_with_index do |job, index|
attrs = job.send('table')
context[@variable_name] = attrs.stringify_keys! if attrs.size > 0
context['forloop'] = {
'name' => @name,
'length' => length,
'index' => index + 1,
'index0' => index,
'rindex' => length - index,
'rindex0' => length - index -1,
'first' => (index == 0),
'last' => (index == length - 1) }
result << render_all(@nodelist, context)
end
end
result
end
end
end
Liquid::Template.register_tag('jobs', Jekyll::JobsTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment