Last active
July 1, 2022 15:12
-
-
Save agarie/3816637 to your computer and use it in GitHub Desktop.
A jekyll plugin to generate a quotes index page from a quotes.json file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------ | |
# QuotesGenerator | |
# A Jekyll plugin to generate a quotes/index.html page based on a Tumblr's | |
# account. | |
# | |
# You need a quotes.json file inside a SITE_ROOT/_data/ directory to use this | |
# plugin. It must use the same format as Tumblr's API v2. | |
# | |
# Available _config.yml settings: | |
# - text_size_limit: Maximum size of a used quote. Default to 1000. | |
# - tumblr_account: Used to link to your account. | |
# - tumblr_quotes_layout: Layout used. | |
# | |
#------------------------------------------------------------------------------ | |
require 'json' | |
module Jekyll | |
class QuotesPage < Page | |
attr_accessor :content, :data | |
def initialize(site, base, dir, name) | |
@site = site | |
@base = base | |
@dir = dir | |
@name = name | |
self.process(name) | |
self.content = '' | |
self.data = {} | |
end | |
end | |
class Site | |
def create_quotes_page | |
# Get the configurations from _config.yml. | |
self.config['text_size_limit'] ||= 1000 | |
self.config['tumblr_account'] ||= 'turing-machine' | |
self.config['tumblr_quotes_layout'] ||= 'default' | |
quotes_page = QuotesPage.new(self, self.source, 'quotes_page', 'index.html') | |
# Get the quotes from the JSON. | |
quotes = get_quotes | |
# Remove noise from the quotes. | |
quotes = remove_noise(quotes) | |
# Generate a list of quotes. It isn't worth to require Nokogiri for such | |
# a tiny string. | |
string = '' | |
string << "<p>These quotes are automatically fetched from my Tumblr account " | |
string << "<a href='http://#{self.config['tumblr_account']}.tumblr.com'>" | |
string << "#{self.config['tumblr_account']}</a>.</p>" | |
quotes.each do |q| | |
string << "<blockquote>" | |
string << "#{q['text']}" | |
string << "<small>#{q['source']}</small>" | |
string << "</blockquote>" | |
end | |
quotes_page.content = string | |
quotes_page.data["layout"] = self.config['tumblr_quotes_layout'] | |
self.pages << quotes_page | |
end | |
def get_quotes | |
response = JSON.parse(File.open("./_data/quotes.json", 'r').readlines.first)["response"] | |
posts = response["posts"] | |
posts | |
end | |
def remove_noise(quotes) | |
quotes.select do |quote| | |
quote["text"].size < self.config['text_size_limit'] | |
end.delete_if do |quote| | |
# Remove quotes with images. | |
quote["text"] =~ /<img/ || quote["source"] =~ /<img/ | |
end | |
end | |
end | |
class QuoteGenerator < Generator | |
safe true | |
priority :low | |
def generate(site) | |
site.create_quotes_page | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Am I going insane or have you got the source of your plugin twice in this file? Thanks for the writeup on your blog and putting the code up!