mirror of
https://github.com/moparisthebest/android.moparisthebest.org
synced 2024-11-16 06:05:00 -05:00
More code snippet refactoring
- Each code snippet's html is now cached (before only pygments output was cached and html was being processed each time) - Caching MD5 is based on code snippets as well as options. - Fixed an issue where gist tags weren't specifying a default start line Note: The caching change shaved 800 milliseconds off of the render time for the docs. For code heavy sites this should be a nice change.
This commit is contained in:
parent
e686f8544e
commit
ed186b18a2
@ -40,6 +40,7 @@ module Jekyll
|
|||||||
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
|
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
|
||||||
|
|
||||||
length = code.lines.count
|
length = code.lines.count
|
||||||
|
@start ||= 1
|
||||||
@end ||= length
|
@end ||= length
|
||||||
return "#{file} is #{length} lines long, cannot begin at line #{@start}" if @start > length
|
return "#{file} is #{length} lines long, cannot begin at line #{@start}" if @start > length
|
||||||
return "#{file} is #{length} lines long, cannot read beyond line #{@end}" if @end > length
|
return "#{file} is #{length} lines long, cannot read beyond line #{@end}" if @end > length
|
||||||
|
@ -12,18 +12,12 @@ module HighlightCode
|
|||||||
include TemplateWrapper
|
include TemplateWrapper
|
||||||
include SiteConfig
|
include SiteConfig
|
||||||
def pygments(code, lang)
|
def pygments(code, lang)
|
||||||
path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html") if defined?(PYGMENTS_CACHE_DIR)
|
|
||||||
if File.exist?(path)
|
|
||||||
highlighted_code = File.read(path)
|
|
||||||
else
|
|
||||||
if get_config('pygments')
|
if get_config('pygments')
|
||||||
highlighted_code = Albino.new(code, lang, :html)
|
highlighted_code = Albino.new(code, lang, :html)
|
||||||
else
|
else
|
||||||
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
||||||
end
|
end
|
||||||
highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
|
highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
|
||||||
File.open(path, 'w') {|f| f.print(highlighted_code) } if path
|
|
||||||
end
|
|
||||||
highlighted_code.to_s
|
highlighted_code.to_s
|
||||||
rescue
|
rescue
|
||||||
puts $!,$@
|
puts $!,$@
|
||||||
@ -46,23 +40,24 @@ module HighlightCode
|
|||||||
linenos = options[:linenos]
|
linenos = options[:linenos]
|
||||||
start = options[:start] || 1
|
start = options[:start] || 1
|
||||||
|
|
||||||
|
path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(options.to_s + code)}.html") if defined?(PYGMENTS_CACHE_DIR)
|
||||||
|
|
||||||
|
if File.exist?(path)
|
||||||
|
code = File.read(path)
|
||||||
|
else
|
||||||
if lang == 'plain'
|
if lang == 'plain'
|
||||||
# Escape html tags
|
# Escape html tags
|
||||||
code = code.gsub('<','<')
|
code = code.gsub('<','<')
|
||||||
elsif lang.include? "-raw"
|
|
||||||
output = "``` #{$1.sub('-raw', '')}\n"
|
|
||||||
output += code
|
|
||||||
output += "\n```\n"
|
|
||||||
else
|
else
|
||||||
code = pygments(code, lang).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
|
code = pygments(code, lang).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
|
||||||
end
|
end
|
||||||
|
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>"
|
||||||
figure = "<figure class='code'>#{title}#{code}</figure>"
|
File.open(path, 'w') {|f| f.print(code) } if path
|
||||||
figure = safe_wrap(figure) if wrap
|
end
|
||||||
figure
|
code = safe_wrap(code) if wrap
|
||||||
|
code
|
||||||
end
|
end
|
||||||
|
|
||||||
def captionize (caption, url, link_text)
|
def captionize (caption, url, link_text)
|
||||||
@ -75,7 +70,7 @@ module HighlightCode
|
|||||||
start = options[:start] || 1
|
start = options[:start] || 1
|
||||||
lines = options[:linenos] || true
|
lines = options[:linenos] || true
|
||||||
marks = options[:marks] || []
|
marks = options[:marks] || []
|
||||||
table = "<table class='highlight'>"
|
table = "<div class='highlight'><table>"
|
||||||
table += number_lines(start, code.lines.count, marks) if lines
|
table += number_lines(start, code.lines.count, marks) if lines
|
||||||
table += "<td class='main #{'unnumbered' unless lines} #{lang}'><pre>"
|
table += "<td class='main #{'unnumbered' unless lines} #{lang}'><pre>"
|
||||||
code.lines.each_with_index do |line,index|
|
code.lines.each_with_index do |line,index|
|
||||||
@ -88,7 +83,7 @@ module HighlightCode
|
|||||||
line = line.strip.empty? ? ' ' : line
|
line = line.strip.empty? ? ' ' : line
|
||||||
table += "<div class='#{classes}'>#{line}</div>"
|
table += "<div class='#{classes}'>#{line}</div>"
|
||||||
end
|
end
|
||||||
table +="</pre></td></tr></table>"
|
table +="</pre></td></tr></table></div>"
|
||||||
end
|
end
|
||||||
|
|
||||||
def number_lines (start, count, marks)
|
def number_lines (start, count, marks)
|
||||||
@ -112,7 +107,7 @@ module HighlightCode
|
|||||||
linenos = input.match(/\s*linenos:(\w+)/i)
|
linenos = input.match(/\s*linenos:(\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)
|
||||||
start = input.match(/\s*start:(\d+)/i)
|
start = input.match(/\s*start:(\d+)/i)
|
||||||
endline = input.match(/\s*end:(\d+)/i)
|
endline = input.match(/\s*end:(\d+)/i)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user