Support octopress-hooks and octopress-date-format #1622

This commit is contained in:
Brandon Mathis 2014-08-12 10:55:23 -05:00
parent 18784a4cb0
commit 73e540409c
11 changed files with 47 additions and 58 deletions

View File

@ -1,15 +1,5 @@
{% capture date %}{{ page.date }}{{ post.date }}{% endcapture %} {% if page.date %}{% capture time %}{{ page.date_time_html }}{% endcapture %}{% endif %}
{% capture date_formatted %}{{ page.date_formatted }}{{ post.date_formatted }}{% endcapture %} {% if post.date %}{% capture time %}{{ post.date_time_html }}{% endcapture %}{% endif %}
{% capture has_date %}{{ date | size }}{% endcapture %}
{% capture updated %}{{ page.updated }}{{ post.updated }}{% endcapture %} {% if page.updated %}{% capture updated %}{{ page.date_time_updated_html }}{% endcapture %}{% endif %}
{% capture updated_formatted %}{{ page.updated_formatted }}{{ post.updated_formatted }}{% endcapture %} {% if post.updated %}{% capture updated %}{{ post.date_time_updated_html }}{% endcapture %}{% endif %}
{% capture was_updated %}{{ updated | size }}{% endcapture %}
{% if has_date != '0' %}
{% capture time %}<time datetime="{{ date | datetime | date_to_xmlschema }}" pubdate{% if updated %} data-updated="true"{% endif %}>{{ date_formatted }}</time>{% endcapture %}
{% endif %}
{% if was_updated != '0' %}
{% capture updated %}<time datetime="{{ updated | datetime | date_to_xmlschema }}" class="updated">Updated {{ updated_formatted }}</time>{% endcapture %}
{% else %}{% assign updated = false %}{% endif %}

View File

@ -9,7 +9,7 @@ single: true
<footer> <footer>
<p class="meta"> <p class="meta">
{% include post/author.html %} {% include post/author.html %}
{% include post/date.html %}{% if updated %}{{ updated }}{% else %}{{ time }}{% endif %} {% include post/date.html %}{{ time }}{% if updated %} {{ updated }}{% endif %}
{% include post/categories.html %} {% include post/categories.html %}
</p> </p>
{% unless page.sharing == false %} {% unless page.sharing == false %}

View File

@ -1,5 +1,4 @@
--- ---
layout: nil
--- ---
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"> <feed xmlns="http://www.w3.org/2005/Atom">

View File

@ -1,7 +1,6 @@
--- ---
layout: nil
--- ---
User-agent: * User-agent: *
Disallow: Disallow:
Sitemap: {{ site.url }}/sitemap.xml Sitemap: {{ site.url }}/sitemap.xml

View File

@ -3,8 +3,8 @@ source "https://rubygems.org"
group :development do group :development do
gem 'rake', '~> 10.0' gem 'rake', '~> 10.0'
gem 'jekyll', '~> 2.0' gem 'jekyll', '~> 2.0'
gem 'jekyll-page-hooks', '~> 1.2' gem 'octopress-hooks', '~> 2.2'
gem 'jekyll-date-format', '~> 1.0' gem 'octopress-date-format', '~> 2.0'
gem 'jekyll-sitemap' gem 'jekyll-sitemap'
gem 'rdiscount', '~> 2.0' gem 'rdiscount', '~> 2.0'
gem 'RedCloth', '~> 4.2.9' gem 'RedCloth', '~> 4.2.9'

View File

@ -1,10 +1,9 @@
require './plugins/pygments_code' require './plugins/pygments_code'
module BacktickCodeBlock module BacktickCodeBlock
include HighlightCode
AllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i AllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
LangCaption = /([^\s]+)\s*(.+)?/i LangCaption = /([^\s]+)\s*(.+)?/i
def render_code_block(input) def self.render_code_block(input)
@options = nil @options = nil
@caption = nil @caption = nil
@lang = nil @lang = nil
@ -26,7 +25,7 @@ module BacktickCodeBlock
str = str.gsub(/^( {4}|\t)/, '') str = str.gsub(/^( {4}|\t)/, '')
end end
if @lang.nil? || @lang == 'plain' if @lang.nil? || @lang == 'plain'
code = tableize_code(str.gsub('<','&lt;').gsub('>','&gt;')) code = HighlightCode::tableize_code(str.gsub('<','&lt;').gsub('>','&gt;'))
"<figure class='code'>#{@caption}#{code}</figure>" "<figure class='code'>#{@caption}#{code}</figure>"
else else
if @lang.include? "-raw" if @lang.include? "-raw"
@ -34,7 +33,7 @@ module BacktickCodeBlock
raw += str raw += str
raw += "\n```\n" raw += "\n```\n"
else else
code = highlight(str, @lang) code = HighlightCode::highlight(str, @lang)
"<figure class='code'>#{@caption}#{code}</figure>" "<figure class='code'>#{@caption}#{code}</figure>"
end end
end end

View File

@ -47,8 +47,6 @@ require './plugins/raw'
module Jekyll module Jekyll
class CodeBlock < Liquid::Block class CodeBlock < Liquid::Block
include HighlightCode
include TemplateWrapper
CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
Caption = /(\S[\S\s]*)/ Caption = /(\S[\S\s]*)/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@ -79,11 +77,11 @@ module Jekyll
source = "<figure class='code'>" source = "<figure class='code'>"
source += @caption if @caption source += @caption if @caption
if @filetype if @filetype
source += "#{highlight(code, @filetype)}</figure>" source += "#{HighlightCode::highlight(code, @filetype)}</figure>"
else else
source += "#{tableize_code(code.lstrip.rstrip.gsub(/</,'&lt;'))}</figure>" source += "#{HighlightCode::tableize_code(code.lstrip.rstrip.gsub(/</,'&lt;'))}</figure>"
end end
source = safe_wrap(source) source = TemplateWrapper::safe_wrap(source)
source = context['pygments_prefix'] + source if context['pygments_prefix'] source = context['pygments_prefix'] + source if context['pygments_prefix']
source = source + context['pygments_suffix'] if context['pygments_suffix'] source = source + context['pygments_suffix'] if context['pygments_suffix']
source source

View File

@ -27,8 +27,6 @@ require 'pathname'
module Jekyll module Jekyll
class IncludeCodeTag < Liquid::Tag class IncludeCodeTag < Liquid::Tag
include HighlightCode
include TemplateWrapper
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@title = nil @title = nil
@file = nil @file = nil
@ -62,8 +60,8 @@ module Jekyll
title = @title ? "#{@title} (#{file.basename})" : file.basename title = @title ? "#{@title} (#{file.basename})" : file.basename
url = "/#{code_dir}/#{@file}" url = "/#{code_dir}/#{@file}"
source = "<figure class='code'><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n" source = "<figure class='code'><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n"
source += "#{highlight(code, @filetype)}</figure>" source += "#{HighlightCode::highlight(code, @filetype)}</figure>"
safe_wrap(source) TemplateWrapper::safe_wrap(source)
end end
end end
end end

View File

@ -7,32 +7,38 @@ require './plugins/raw'
require 'rubypants' require 'rubypants'
module OctopressFilters module OctopressFilters
include BacktickCodeBlock def self.pre_filter(page)
include TemplateWrapper if page.ext.match('html|textile|markdown|md|haml|slim|xml')
def pre_filter(input) input = BacktickCodeBlock::render_code_block(page.content)
input = render_code_block(input) page.content = input.gsub /(<figure.+?>.+?<\/figure>)/m do
input.gsub /(<figure.+?>.+?<\/figure>)/m do TemplateWrapper::safe_wrap($1)
safe_wrap($1) end
end end
end end
def post_filter(input) def self.post_filter(page)
input = unwrap(input) if page.ext.match('html|textile|markdown|md|haml|slim|xml')
RubyPants.new(input).to_html input = TemplateWrapper::unwrap(page.content)
page.content = RubyPants.new(input).to_html
end
end end
end
module Jekyll class PageFilters < Octopress::Hooks::Page
class ContentFilters < PageHooks def pre_render(page)
include OctopressFilters OctopressFilters::pre_filter(page)
def pre_render(post)
if post.ext.match('html|textile|markdown|md|haml|slim|xml')
post.content = pre_filter(post.content)
end
end end
def post_render(page)
OctopressFilters::post_filter(page)
end
end
class PostFilters < Octopress::Hooks::Post
def pre_render(post)
OctopressFilters::pre_filter(post)
end
def post_render(post) def post_render(post)
if post.ext.match('html|textile|markdown|md|haml|slim|xml') OctopressFilters::post_filter(post)
post.content = post_filter(post.content)
end
end end
end end
end end

View File

@ -6,7 +6,7 @@ PYGMENTS_CACHE_DIR = File.expand_path('../../.pygments-cache', __FILE__)
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR) FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
module HighlightCode module HighlightCode
def highlight(str, lang) def self.highlight(str, lang)
lang = 'ruby' if lang == 'ru' lang = 'ruby' if lang == 'ru'
lang = 'objc' if lang == 'm' lang = 'objc' if lang == 'm'
lang = 'perl' if lang == 'pl' lang = 'perl' if lang == 'pl'
@ -15,7 +15,7 @@ module HighlightCode
tableize_code(str, lang) tableize_code(str, lang)
end end
def pygments(code, lang) def self.pygments(code, lang)
if defined?(PYGMENTS_CACHE_DIR) if defined?(PYGMENTS_CACHE_DIR)
path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html") path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html")
if File.exist?(path) if File.exist?(path)
@ -33,7 +33,7 @@ module HighlightCode
end end
highlighted_code highlighted_code
end end
def tableize_code (str, lang = '') def self.tableize_code (str, lang = '')
table = '<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers">' table = '<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers">'
code = '' code = ''
str.lines.each_with_index do |line,index| str.lines.each_with_index do |line,index|

View File

@ -3,11 +3,11 @@
# Purpose: This is useful for preventing Markdown and Textile from being too aggressive and incorrectly parsing in-line HTML. # Purpose: This is useful for preventing Markdown and Textile from being too aggressive and incorrectly parsing in-line HTML.
module TemplateWrapper module TemplateWrapper
# Wrap input with a <div> # Wrap input with a <div>
def safe_wrap(input) def self.safe_wrap(input)
"<div class='bogus-wrapper'><notextile>#{input}</notextile></div>" "<div class='bogus-wrapper'><notextile>#{input}</notextile></div>"
end end
# This must be applied after the # This must be applied after the
def unwrap(input) def self.unwrap(input)
input.gsub /<div class='bogus-wrapper'><notextile>(.+?)<\/notextile><\/div>/m do input.gsub /<div class='bogus-wrapper'><notextile>(.+?)<\/notextile><\/div>/m do
$1 $1
end end