Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Octopress code highlighter #1590

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.sass-cache
.gist-cache
.pygments-cache
.code-highlighter-cache
_deploy
public
sass.old
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ group :development do
gem 'jekyll-page-hooks', '~> 1.2'
gem 'jekyll-date-format', '~> 1.0'
gem 'jekyll-sitemap'
gem 'octopress-codefence'
gem 'octopress-gist'
gem 'rdiscount', '~> 2.0'
gem 'RedCloth', '~> 4.2.9'
gem 'haml', '~> 4.0'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ GEM
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
mercenary (0.3.3)
octopress-code-highlighter (4.0.0)
colorator (~> 0.1.0)
octopress-codefence (1.4.2)
jekyll-page-hooks (>= 1.0.2)
octopress-code-highlighter (~> 4.0.0)
octopress-gist (1.2.0)
octopress-code-highlighter (~> 4.0.0)
parslet (1.5.0)
blankslate (~> 2.0)
posix-spawn (0.3.8)
Expand Down Expand Up @@ -95,6 +102,8 @@ DEPENDENCIES
jekyll-date-format (~> 1.0)
jekyll-page-hooks (~> 1.2)
jekyll-sitemap
octopress-codefence
octopress-gist
rake (~> 10.0)
rb-fsevent (~> 0.9)
rdiscount (~> 2.0)
Expand Down
43 changes: 0 additions & 43 deletions plugins/backtick_code_block.rb

This file was deleted.

246 changes: 123 additions & 123 deletions plugins/gist_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,126 +5,126 @@
#
# Example usage: {% gist 1027674 gist_tag.rb %} //embeds a gist for this plugin

require 'cgi'
require 'digest/md5'
require 'net/https'
require 'uri'

module Jekyll
class GistTag < Liquid::Tag
def initialize(tag_name, text, token)
super
@text = text
@cache_disabled = false
@cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__)
FileUtils.mkdir_p @cache_folder
end

def render(context)
if parts = @text.match(/([a-zA-Z\d]*) (.*)/)
gist, file = parts[1].strip, parts[2].strip
else
gist, file = @text.strip, ""
end
if gist.empty?
""
else
script_url = script_url_for gist, file
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
html_output_for script_url, code
end
end

def html_output_for(script_url, code)
code = CGI.escapeHTML code
<<-HTML
<div><script src='#{script_url}'></script>
<noscript><pre><code>#{code}</code></pre></noscript></div>
HTML
end

def script_url_for(gist_id, filename)
url = "https://gist.github.com/#{gist_id}.js"
url = "#{url}?file=#{filename}" unless filename.nil? or filename.empty?
url
end

def get_gist_url_for(gist, file)
"https://gist.githubusercontent.com/raw/#{gist}/#{file}"
end

def cache(gist, file, data)
cache_file = get_cache_file_for gist, file
File.open(cache_file, "w") do |io|
io.write data
end
end

def get_cached_gist(gist, file)
return nil if @cache_disabled
cache_file = get_cache_file_for gist, file
File.read cache_file if File.exist? cache_file
end

def get_cache_file_for(gist, file)
bad_chars = /[^a-zA-Z0-9\-_.]/
gist = gist.gsub bad_chars, ''
file = file.gsub bad_chars, ''
md5 = Digest::MD5.hexdigest "#{gist}-#{file}"
File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache"
end

def get_gist_from_web(gist, file)
gist_url = get_gist_url_for(gist, file)
data = get_web_content(gist_url)

locations = Array.new
while (data.code.to_i == 301 || data.code.to_i == 302)
data = handle_gist_redirecting(data)
break if locations.include? data.header['Location']
locations << data.header['Location']
end

if data.code.to_i != 200
raise RuntimeError, "Gist replied with #{data.code} for #{gist_url}"
end

cache(gist, file, data.body) unless @cache_disabled
data.body
end

def handle_gist_redirecting(data)
redirected_url = data.header['Location']
if redirected_url.nil? || redirected_url.empty?
raise ArgumentError, "GitHub replied with a 302 but didn't provide a location in the response headers."
end

get_web_content(redirected_url)
end

def get_web_content(url)
raw_uri = URI.parse url
proxy = ENV['http_proxy']
if proxy
proxy_uri = URI.parse(proxy)
https = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new raw_uri.host, raw_uri.port
else
https = Net::HTTP.new raw_uri.host, raw_uri.port
end
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new raw_uri.request_uri
data = https.request request
end
end

class GistTagNoCache < GistTag
def initialize(tag_name, text, token)
super
@cache_disabled = true
end
end
end

Liquid::Template.register_tag('gist', Jekyll::GistTag)
Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)
#require 'cgi'
#require 'digest/md5'
#require 'net/https'
#require 'uri'

#module Jekyll
#class GistTag < Liquid::Tag
#def initialize(tag_name, text, token)
#super
#@text = text
#@cache_disabled = false
#@cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__)
#FileUtils.mkdir_p @cache_folder
#end

#def render(context)
#if parts = @text.match(/([a-zA-Z\d]*) (.*)/)
#gist, file = parts[1].strip, parts[2].strip
#else
#gist, file = @text.strip, ""
#end
#if gist.empty?
#""
#else
#script_url = script_url_for gist, file
#code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
#html_output_for script_url, code
#end
#end

#def html_output_for(script_url, code)
#code = CGI.escapeHTML code
#<<-HTML
#<div><script src='#{script_url}'></script>
#<noscript><pre><code>#{code}</code></pre></noscript></div>
#HTML
#end

#def script_url_for(gist_id, filename)
#url = "https://gist.github.com/#{gist_id}.js"
#url = "#{url}?file=#{filename}" unless filename.nil? or filename.empty?
#url
#end

#def get_gist_url_for(gist, file)
#"https://gist.githubusercontent.com/raw/#{gist}/#{file}"
#end

#def cache(gist, file, data)
#cache_file = get_cache_file_for gist, file
#File.open(cache_file, "w") do |io|
#io.write data
#end
#end

#def get_cached_gist(gist, file)
#return nil if @cache_disabled
#cache_file = get_cache_file_for gist, file
#File.read cache_file if File.exist? cache_file
#end

#def get_cache_file_for(gist, file)
#bad_chars = /[^a-zA-Z0-9\-_.]/
#gist = gist.gsub bad_chars, ''
#file = file.gsub bad_chars, ''
#md5 = Digest::MD5.hexdigest "#{gist}-#{file}"
#File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache"
#end

#def get_gist_from_web(gist, file)
#gist_url = get_gist_url_for(gist, file)
#data = get_web_content(gist_url)

#locations = Array.new
#while (data.code.to_i == 301 || data.code.to_i == 302)
#data = handle_gist_redirecting(data)
#break if locations.include? data.header['Location']
#locations << data.header['Location']
#end

#if data.code.to_i != 200
#raise RuntimeError, "Gist replied with #{data.code} for #{gist_url}"
#end

#cache(gist, file, data.body) unless @cache_disabled
#data.body
#end

#def handle_gist_redirecting(data)
#redirected_url = data.header['Location']
#if redirected_url.nil? || redirected_url.empty?
#raise ArgumentError, "GitHub replied with a 302 but didn't provide a location in the response headers."
#end

#get_web_content(redirected_url)
#end

#def get_web_content(url)
#raw_uri = URI.parse url
#proxy = ENV['http_proxy']
#if proxy
#proxy_uri = URI.parse(proxy)
#https = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new raw_uri.host, raw_uri.port
#else
#https = Net::HTTP.new raw_uri.host, raw_uri.port
#end
#https.use_ssl = true
#https.verify_mode = OpenSSL::SSL::VERIFY_NONE
#request = Net::HTTP::Get.new raw_uri.request_uri
#data = https.request request
#end
#end

#class GistTagNoCache < GistTag
#def initialize(tag_name, text, token)
#super
#@cache_disabled = true
#end
#end
#end

#Liquid::Template.register_tag('gist', Jekyll::GistTag)
#Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)
38 changes: 4 additions & 34 deletions plugins/octopress_filters.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
#custom filters for Octopress
require './plugins/backtick_code_block'
require 'jekyll-page-hooks'
#require './plugins/backtick_code_block'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing soon. Thanks for jumping on this so fast! You're the 💣.

require 'octopress-codefence'
require 'octopress-gist'
require 'jekyll-sitemap'
require 'jekyll-date-format'
require './plugins/raw'
require 'rubypants'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of RubyPants in this file, too, right?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think people should be using the features of Rdiscount or whatever if they want fancy quotes now. This isn't how it should be done.


module OctopressFilters
include BacktickCodeBlock
include TemplateWrapper
def pre_filter(input)
input = render_code_block(input)
input.gsub /(<figure.+?>.+?<\/figure>)/m do
safe_wrap($1)
end
end
def post_filter(input)
input = unwrap(input)
RubyPants.new(input).to_html
end
end

module Jekyll
class ContentFilters < PageHooks
include OctopressFilters
def pre_render(post)
if post.ext.match('html|textile|markdown|md|haml|slim|xml')
post.content = pre_filter(post.content)
end
end
def post_render(post)
if post.ext.match('html|textile|markdown|md|haml|slim|xml')
post.content = post_filter(post.content)
end
end
end
end


module OctopressLiquidFilters

# Used on the blog index to split posts on the <!--more--> marker
Expand Down Expand Up @@ -131,5 +100,6 @@ def titlecase(input)
end

end

Liquid::Template.register_filter OctopressLiquidFilters

Loading