Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created April 20, 2013 13:50
Show Gist options
  • Save ttscoff/5426048 to your computer and use it in GitHub Desktop.
Save ttscoff/5426048 to your computer and use it in GitHub Desktop.
Brett's custom Jekyll filters
# encoding: utf-8
#
# Brett's misc filters
require './plugins/post_filters'
require 'fileutils'
module Jekyll
module Filters
def keyword_string(keywords)
keywords.join(" ")
end
def og_tags(tags)
tags.map {|tag|
%Q{<meta property="article:tag" content="#{tag}">}
}.join("\n")
end
def first_img(input)
imgurl = ""
if input =~ /img.*?src="(.*?)\.(jpg|png)"/m
imgurl = "#{$1}_lg.#{$2}"
elsif input =~ /\{% img (\w+ )?(\d+ )*((\S+?)\.(jpg|png|gif|svg)) %\}/m
imgurl = "#{$4}_lg.#{$5}"
end
imgurl = "http://brettterpstra.com/#{imgurl.gsub(/^\//,'')}" unless imgurl =~ /^http/ or imgurl == ""
imgurl == "" ? "http://brettterpstra.com/images/mepic_lg.jpg" : imgurl
end
def og_image(input)
if input.strip =~ /^http:\/\/brettterpstra.com(\/.*?\.(jpg|png))$/
filename = File.expand_path(File.join(%x{git rev-parse --show-toplevel}.strip,"source"+$1.strip))
filename.gsub!(/_lg(\.(jpg|png))$/,"\\1") unless File.exists?(filename)
width = %x{sips -g pixelWidth "#{filename}"|awk '{print $2}'}.strip
height = %x{sips -g pixelHeight "#{filename}"|awk '{print $2}'}.strip
imgtype = File.extname(filename) =~ /.jpe?g/ ? "jpeg" : "png"
out = %Q{<meta property="og:image" content="#{input.strip}">\n}
out += %Q{<meta property="og:image:type" content="image/#{imgtype}" />\n}
out += %Q{<meta property="og:image:width" content="#{width}" />\n}
out += %Q{<meta property="og:image:height" content="#{height}" />\n}
end
end
def largest_img(input)
src_images = input.scan(/<img(.*?((src|width|height)="(.*?)"))+[^>]+>/m)
end
end
# class ContentFilters < PostFilter
# def pre_write(post)
# if post.respond_to?(:url)
# file_path = File.expand_path(File.join("public",post.url + "index.html"))
# file_bak = File.expand_path(File.join("public",post.url + "index_o.html"))
# if File.exists?(file_path)
# $stderr.puts "Compressing #{file_path}"
# FileUtils.mv(file_path,file_bak)
# system %Q{/usr/local/bin/htmlcompressor --type=html -o "#{file_path}" #{file_bak}}
# end
# end
# end
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment