diff --git a/.themes/classic/source/_layouts/default.html b/.themes/classic/source/_layouts/default.html index 5523ac1..629468d 100644 --- a/.themes/classic/source/_layouts/default.html +++ b/.themes/classic/source/_layouts/default.html @@ -5,7 +5,7 @@
- {{ content | expand_urls: root_url | backtick_codeblock | smart_quotes }} + {{ content | expand_urls: root_url }}
diff --git a/plugins/backtick_code_block.rb b/plugins/backtick_code_block.rb new file mode 100644 index 0000000..c7a8343 --- /dev/null +++ b/plugins/backtick_code_block.rb @@ -0,0 +1,42 @@ +require './plugins/pygments_code' + +module BacktickCodeBlock + include HighlightCode + AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+)\s*(.+)?/i + LangCaption = /([^\s]+)\s*(.+)?/i + def render_code_block(input) + @caption = nil + @lang = nil + @url = nil + @title = nil + input.gsub /^`{3} *([^\n]+)?\n(.+?)\n`{3}/m do + options = $1 + str = $2 + + if options =~ AllOptions + @lang = $1 + @caption = "
#{$2}#{$4 || 'link'}
" + elsif options =~ LangCaption + @lang = $1 + @caption = "
#{$2}
" + end + + if str.match(/\A {4}/) + str = str.gsub /^ {4}/, '' + end + if @lang.nil? || @lang == 'plain' + code = tableize_code(str.gsub('<','<').gsub('>','>')) + "
#{@caption}#{code}
" + else + if @lang.include? "-raw" + raw = "``` #{@lang.sub('-raw', '')}\n" + raw += str + raw += "\n```\n" + else + code = highlight(str, @lang) + "
#{@caption}#{code}
" + end + end + end + end +end diff --git a/plugins/code_block.rb b/plugins/code_block.rb index 9c971bf..00b0b43 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -42,11 +42,13 @@ # # require './plugins/pygments_code' +require './plugins/raw' module Jekyll class CodeBlock < Liquid::Block include HighlightCode + include TemplateWrapper CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i Caption = /(\S[\S\s]*)/ @@ -78,14 +80,15 @@ module Jekyll def render(context) output = super code = super.join - source = "
" + source = "
" source += @caption if @caption - source = context['pygments_prefix'] + source if context['pygments_prefix'] if @filetype - source += " #{highlight(code, @filetype)}
" + source += " #{highlight(code, @filetype)}" else - source += "#{tableize_code(code.lstrip.rstrip.gsub(/" + source += "#{tableize_code(code.lstrip.rstrip.gsub(/" end + source = safe_wrap(source) + source = context['pygments_prefix'] + source if context['pygments_prefix'] source = source + context['pygments_suffix'] if context['pygments_suffix'] end end diff --git a/plugins/include_code.rb b/plugins/include_code.rb index 70d5f13..80951cb 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -21,12 +21,14 @@ # require './plugins/pygments_code' +require './plugins/raw' require 'pathname' module Jekyll class IncludeCodeTag < Liquid::Tag include HighlightCode + include TemplateWrapper def initialize(tag_name, markup, tokens) @title = nil @file = nil @@ -59,8 +61,9 @@ module Jekyll @filetype = file.extname.sub('.','') if @filetype.nil? title = @title ? "#{@title} (#{file.basename})" : file.basename url = "/#{code_dir}/#{@file}" - source = "
#{title} download
\n" - source += " #{highlight(code, @filetype)}
" + source = "
#{title} download
\n" + source += " #{highlight(code, @filetype)}
" + safe_wrap(source) end end end diff --git a/plugins/octopress_filters.rb b/plugins/octopress_filters.rb index a63c43a..1a95989 100644 --- a/plugins/octopress_filters.rb +++ b/plugins/octopress_filters.rb @@ -1,8 +1,38 @@ #custom filters for Octopress -require './plugins/pygments_code' +require './plugins/backtick_code_block' +require './plugins/post_filters' +require './plugins/raw' +require 'rubypants' module OctopressFilters - include HighlightCode + include BacktickCodeBlock + include TemplateWrapper + def pre_filter(input) + input = render_code_block(input) + input.gsub /(.+?<\/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 < PostFilter + include OctopressFilters + def pre_render(post) + post.content = pre_filter(post.content) + end + def post_render(post) + post.content = post_filter(post.content) + end + end +end + + +module OctopressLiquidFilters # Used on the blog index to split posts on the marker def excerpt(input) if input.index(//i) @@ -26,45 +56,6 @@ module OctopressFilters end end - # for Github style codeblocks eg. - # ``` ruby - # code snippet - # ``` - def backtick_codeblock(input) - code = nil - # Markdown support - input = input.gsub /

`{3}\s*(\w+)?<\/p>\s*

\s*(.+?)\s*<\/code><\/pre>\s*

`{3}<\/p>/m do - lang = $1 - if lang != '' - str = $2.gsub('<','<').gsub('>','>').gsub('&','&') - code = highlight(str, lang) - "

#{code}
" - else - code = tableize_code($2) - "
#{code}
" - end - end - - # Textile warning - input = input.gsub /

`{3}\s*(\w+)?\n(.+?)`{3}<\/p>/m do - lang = $1 - "

Back tick code blocks are not supported for Textile.\nTry HTML or Markdown instead or use the codeblock tag.\n\n{% codeblock #{lang} %}\nYour code snippet\n{% endcodeblock %}
" - end - - # Regular HTML support - input.gsub /^`{3}\s*(\w+)?\n(.+?)\n`{3}/m do - lang = $1 - str = $2.gsub(/^\s{4}/, '') - if lang != '' - code = highlight(str, lang) - "
#{code}
" - else - code = tableize_code($2.gsub('<','<').gsub('>','>')) - "
#{code}
" - end - end - end - # Replaces relative urls with full urls def expand_urls(input, url='') url ||= '/' @@ -88,12 +79,6 @@ module OctopressFilters end end - # replaces primes with smartquotes using RubyPants - def smart_quotes(input) - require 'rubypants' - RubyPants.new(input).to_html - end - # Returns a title cased string based on John Gruber's title case http://daringfireball.net/2008/08/title_case_update def titlecase(input) input.titlecase @@ -127,5 +112,5 @@ module OctopressFilters end end end -Liquid::Template.register_filter OctopressFilters +Liquid::Template.register_filter OctopressLiquidFilters diff --git a/plugins/raw.rb b/plugins/raw.rb index 2deb5d1..4b00262 100644 --- a/plugins/raw.rb +++ b/plugins/raw.rb @@ -1,3 +1,19 @@ +# Author: Brandon Mathis +# Description: Provides plugins with a method for wrapping and unwrapping input to prevent Markdown and Textile from parsing it. +# Purpose: This is useful for preventing Markdown and Textile from being too aggressive and incorrectly parsing in-line HTML. +module TemplateWrapper + # Wrap input with a
+ def safe_wrap(input) + "
#{input}
" + end + # This must be applied after the + def unwrap(input) + input.gsub /
(.+?)<\/notextile><\/div>/m do + $1 + end + end +end + # Author: phaer, https://github.com/phaer # Source: https://gist.github.com/1020852 # Description: Raw tag for jekyll. Keeps liquid from parsing text betweeen {% raw %} and {% endraw %} diff --git a/plugins/render_partial.rb b/plugins/render_partial.rb index 9f66564..b6ebfe8 100644 --- a/plugins/render_partial.rb +++ b/plugins/render_partial.rb @@ -22,10 +22,12 @@ # require 'pathname' +require './plugins/octopress_filters' module Jekyll class RenderPartialTag < Liquid::Tag + include OctopressFilters def initialize(tag_name, markup, tokens) @file = nil @raw = false @@ -50,6 +52,7 @@ module Jekyll if contents =~ /\A-{3}.+[^\A]-{3}\n(.+)/m contents = $1.lstrip end + contents = pre_filter(contents) if @raw contents else