Fixes for Redcarpet

- Removed unnecessary content filtering for escaping markdown
- Escaping codeblocks defaults to false and can be enabled by passing escape:true
- Backtick codeblocks are automatically escaped when using Textile
- Scoped styling of .highlight divs to figure elements
This commit is contained in:
Brandon Mathis 2013-01-03 11:36:51 -06:00
parent a3eea15ac5
commit a25787a204
7 changed files with 27 additions and 41 deletions

View File

@ -118,7 +118,7 @@ figure.code {
} }
} }
.highlight { figure .highlight {
border: 1px solid $pre-border; border: 1px solid $pre-border;
background: $base03; background: $base03;
overflow-y: hidden; overflow-y: hidden;
@ -228,7 +228,7 @@ $solar-scroll-thumb: rgba(#fff, .2);
$solar-scroll-thumb: rgba(#000, .15); $solar-scroll-thumb: rgba(#000, .15);
} }
pre, .highlight { pre, figure .highlight {
&::-webkit-scrollbar { height: .5em; background: $solar-scroll-bg; } &::-webkit-scrollbar { height: .5em; background: $solar-scroll-bg; }
&::-webkit-scrollbar-thumb:horizontal { background: $solar-scroll-thumb; -webkit-border-radius: 4px; border-radius: 4px } &::-webkit-scrollbar-thumb:horizontal { background: $solar-scroll-thumb; -webkit-border-radius: 4px; border-radius: 4px }
} }

View File

@ -4,7 +4,8 @@ module BacktickCodeBlock
include HighlightCode 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 render_code_block(input, ext)
escape = ext ? ext.match(/textile/) != nil : false
input.encode!("UTF-8") input.encode!("UTF-8")
input.gsub /^`{3}(.+?)`{3}/m do input.gsub /^`{3}(.+?)`{3}/m do
str = $1.to_s str = $1.to_s
@ -21,6 +22,7 @@ module BacktickCodeBlock
url: opts[:url], url: opts[:url],
link_text: opts[:link_text] || 'link', link_text: opts[:link_text] || 'link',
start: opts[:start] || 1, start: opts[:start] || 1,
escape: opts[:escape] || escape
} }
markup = clean_markup(markup) markup = clean_markup(markup)

View File

@ -55,7 +55,7 @@ module Jekyll
code = get_range(code, @options[:start], @options[:end]) code = get_range(code, @options[:start], @options[:end])
code = highlight(code, @options) code = highlight(code, @options)
end end
code || safe_wrap(cache) code || cache
else else
"Gist formatting error, format should be {% gist gist_id [filename] %}" "Gist formatting error, format should be {% gist gist_id [filename] %}"
end end

View File

@ -8,15 +8,8 @@ require 'rubypants'
module OctopressFilters module OctopressFilters
include BacktickCodeBlock include BacktickCodeBlock
include TemplateWrapper include TemplateWrapper
def pre_filter(input) def pre_filter(input, ext)
input.gsub /(<figure.+?>.+?<\/figure>)/m do input = render_code_block(input, ext)
safe_wrap($1)
end
input = render_code_block(input)
end
def post_filter(input)
input = unwrap(input)
RubyPants.new(input).to_html
end end
end end
@ -25,12 +18,7 @@ module Jekyll
include OctopressFilters include OctopressFilters
def pre_render(post) def pre_render(post)
if post.ext.match('html|textile|markdown|haml|slim|xml') if post.ext.match('html|textile|markdown|haml|slim|xml')
post.content = pre_filter(post.content) post.content = pre_filter(post.content, post.ext)
end
end
def post_render(post)
if post.ext.match('html|textile|markdown|haml|slim|xml')
post.content = post_filter(post.content)
end end
end end
end end

View File

@ -31,7 +31,7 @@ module HighlightCode
url = options[:url] || nil url = options[:url] || nil
title = options[:title] || (url ? ' ' : nil) title = options[:title] || (url ? ' ' : nil)
link_text = options[:link_text] || nil link_text = options[:link_text] || nil
wrap = options[:wrap] || true escape = options[:escape] || false
marks = options[:marks] marks = options[:marks]
linenos = options[:linenos] linenos = options[:linenos]
start = options[:start] || 1 start = options[:start] || 1
@ -55,14 +55,14 @@ module HighlightCode
code = tableize_code(code, lang, {linenos: linenos, start: start, marks: marks }) code = tableize_code(code, lang, {linenos: linenos, start: start, marks: marks })
title = captionize(title, url, link_text) if title title = captionize(title, url, link_text) if title
code = "<figure class='code'>#{title}#{code}</figure>" code = "<figure class='code'>#{title}#{code}</figure>"
code = safe_wrap(code) if escape
File.open(path, 'w') {|f| f.print(code) } unless no_cache File.open(path, 'w') {|f| f.print(code) } unless no_cache
end end
code = safe_wrap(cache || code) if wrap cache || code
code
end end
def read_cache (path) def read_cache (path)
code = File.exist?(path) ? File.read(path) : nil unless path.nil? File.exist?(path) ? File.read(path) : nil unless path.nil?
end end
def get_cache_path (dir, name, str) def get_cache_path (dir, name, str)
@ -114,6 +114,7 @@ module HighlightCode
lang = input.match(/\s*lang:(\w+)/i) lang = input.match(/\s*lang:(\w+)/i)
title = input.match(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i) title = input.match(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
linenos = input.match(/\s*linenos:(\w+)/i) linenos = input.match(/\s*linenos:(\w+)/i)
escape = input.match(/\s*escape:(\w+)/i)
marks = get_marks(input) marks = get_marks(input)
url = input.match(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i) url = input.match(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
link_text = input.match(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i) link_text = input.match(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
@ -124,6 +125,7 @@ module HighlightCode
lang: (lang.nil? ? nil : lang[1]), lang: (lang.nil? ? nil : lang[1]),
title: (title.nil? ? nil : title[3] || title[5] || title[6]), title: (title.nil? ? nil : title[3] || title[5] || title[6]),
linenos: (linenos.nil? ? nil : linenos[1]), linenos: (linenos.nil? ? nil : linenos[1]),
escape: (escape.nil? ? nil : escape[1]),
marks: marks, marks: marks,
url: (url.nil? ? nil : url[3] || url[5] || url[6]), url: (url.nil? ? nil : url[3] || url[5] || url[6]),
start: (start.nil? ? nil : start[1].to_i), start: (start.nil? ? nil : start[1].to_i),
@ -134,15 +136,15 @@ module HighlightCode
end end
def clean_markup (input) def clean_markup (input)
input.sub(/\s*lang:\w+/i, '' input.sub(/\s*lang:\s*\w+/i, ''
).sub(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i, '' ).sub(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i, ''
).sub(/\s*url:(\S+)/i, '' ).sub(/\s*url:\s*(\S+)/i, ''
).sub(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i, '' ).sub(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i, ''
).sub(/\s*mark:\d\S*/i,'' ).sub(/\s*mark:\d\S*/i,''
).sub(/\s*linenos:false/i,'' ).sub(/\s*linenos:\s*\w+/i,''
).sub(/\s*start:\d+/i,'' ).sub(/\s*start:\s*\d+/i,''
).sub(/\s*end:\d+/i,'' ).sub(/\s*end:\s*\d+/i,''
).sub(/\s*range:\d+-\d+/i,'') ).sub(/\s*range:\s*\d+-\d+/i,'')
end end
def get_marks (input) def get_marks (input)

View File

@ -1,16 +1,10 @@
# Author: Brandon Mathis # Author: Brandon Mathis
# Description: Provides plugins with a method for wrapping and unwrapping input to prevent Markdown and Textile from parsing it. # Description: Provides plugins with a method for wrapping and unwrapping input to prevent Textile from parsing it.
# 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 Textile from being too aggressive and incorrectly parsing in-line HTML.
module TemplateWrapper module TemplateWrapper
# Wrap input with a <div> # Escape content for Textile with <notextile>
def safe_wrap(input) def safe_wrap(input)
"<!--escape--><div class='escape-wrapper'><notextile><!--content-->#{input}<!--end-content--></notextile></div><!--end-escape-->" "<notextile>#{input}</notextile>"
end
# This must be applied after the
def unwrap(input)
input.gsub /<!--escape-->([\s\S]*?)<!--content-->(.+?)<!--end-content-->([\s\S]*?)<!--end-escape-->/m do
$2
end
end end
end end

View File

@ -42,6 +42,7 @@ module Jekyll
file_dir = (context.registers[:site].source || 'source') file_dir = (context.registers[:site].source || 'source')
file_path = Pathname.new(file_dir).expand_path file_path = Pathname.new(file_dir).expand_path
file = file_path + @file file = file_path + @file
ext = File.extname(@file)
unless file.file? unless file.file?
return "File #{file} could not be found" return "File #{file} could not be found"
@ -52,7 +53,7 @@ module Jekyll
if contents =~ /\A-{3}.+[^\A]-{3}\n(.+)/m if contents =~ /\A-{3}.+[^\A]-{3}\n(.+)/m
contents = $1.lstrip contents = $1.lstrip
end end
contents = pre_filter(contents) contents = pre_filter(contents, ext)
if @raw if @raw
contents contents
else else
@ -61,7 +62,6 @@ module Jekyll
contents = partial.render(context) contents = partial.render(context)
site = context.registers[:site] site = context.registers[:site]
ext = File.extname(@file)
converter = site.converters.find { |c| c.matches(ext) } converter = site.converters.find { |c| c.matches(ext) }
if converter.nil? if converter.nil?