mirror of
https://github.com/moparisthebest/android.moparisthebest.org
synced 2024-10-31 23:35:00 -04:00
Merge branch '2.1' into site
This commit is contained in:
commit
b3a5d2bf48
@ -5,35 +5,28 @@ module BacktickCodeBlock
|
|||||||
AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+)\s*(.+)?/i
|
AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+)\s*(.+)?/i
|
||||||
LangCaption = /([^\s]+)\s*(.+)?/i
|
LangCaption = /([^\s]+)\s*(.+)?/i
|
||||||
def render_code_block(input)
|
def render_code_block(input)
|
||||||
@options = nil
|
|
||||||
@caption = nil
|
|
||||||
@lang = nil
|
|
||||||
@url = nil
|
|
||||||
@title = nil
|
|
||||||
input.encode!("UTF-8")
|
input.encode!("UTF-8")
|
||||||
input.gsub(/^`{3} *([^\n]+)?\n([\S\s]+?)\n`{3}/m) do
|
input.gsub /^`{3}(.+?)`{3}/m do
|
||||||
@options = $1 || ''
|
str = $1.to_s
|
||||||
str = $2
|
linenos = true
|
||||||
|
start = 1
|
||||||
if @options =~ AllOptions
|
str.gsub /([^\n]+)?\n(.+?)\Z/m do
|
||||||
@lang = $1
|
@options = $1 || ''
|
||||||
@caption = "<figcaption><span>#{$2}</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>"
|
code = $2.to_s
|
||||||
elsif @options =~ LangCaption
|
if @options =~ /\s*linenos:false/i
|
||||||
@lang = $1
|
linenos = false
|
||||||
@caption = "<figcaption><span>#{$2}</span></figcaption>"
|
@options = @options.sub(/\s*linenos:false/i,'')
|
||||||
end
|
end
|
||||||
|
if @options =~ /\s*start:(\d+)/i
|
||||||
if @lang.nil? || @lang == 'plain'
|
start = $1.to_i
|
||||||
code = tableize_code(str.gsub('<','<').gsub('>','>'))
|
@options = @options.sub(/\s*start:\d+/i,'')
|
||||||
"<figure class='code'>#{@caption}#{code}</figure>"
|
end
|
||||||
else
|
if @options =~ AllOptions
|
||||||
if @lang.include? "-raw"
|
highlight(code, $1, {caption: $2, url: $3, anchor: $4 || 'Link', linenos: linenos, start: start})
|
||||||
raw = "``` #{@options.sub('-raw', '')}\n"
|
elsif @options =~ LangCaption
|
||||||
raw += str
|
highlight(code, $1, {caption: $2 || '', linenos: linenos, start: start})
|
||||||
raw += "\n```\n"
|
|
||||||
else
|
else
|
||||||
code = highlight(str, @lang)
|
highlight(code, 'plain', {linenos: linenos, start: start})
|
||||||
"<figure class='code'>#{@caption}#{code}</figure>"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -48,48 +48,44 @@ module Jekyll
|
|||||||
|
|
||||||
class CodeBlock < Liquid::Block
|
class CodeBlock < Liquid::Block
|
||||||
include HighlightCode
|
include HighlightCode
|
||||||
include TemplateWrapper
|
|
||||||
CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i
|
CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i
|
||||||
CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i
|
CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i
|
||||||
Caption = /(\S[\S\s]*)/
|
Caption = /(\S[\S\s]*)/
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
@title = nil
|
|
||||||
@caption = nil
|
@caption = nil
|
||||||
@filetype = nil
|
@url = nil
|
||||||
@highlight = true
|
@lang = nil
|
||||||
|
@start = 1
|
||||||
if markup =~ /\s*lang:(\w+)/i
|
if markup =~ /\s*lang:(\w+)/i
|
||||||
@filetype = $1
|
@lang = $1
|
||||||
markup = markup.sub(/lang:\w+/i,'')
|
markup = markup.sub(/lang:\w+/i,'')
|
||||||
end
|
end
|
||||||
if markup =~ CaptionUrlTitle
|
if markup =~ /\s*start:(\d+)/i
|
||||||
@file = $1
|
@start = $1.to_i
|
||||||
@caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>#{$4}</a></figcaption>"
|
markup = markup.sub(/\s*start:\d+/i,'')
|
||||||
elsif markup =~ CaptionUrl
|
|
||||||
@file = $1
|
|
||||||
@caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>link</a></figcaption>"
|
|
||||||
elsif markup =~ Caption
|
|
||||||
@file = $1
|
|
||||||
@caption = "<figcaption><span>#{$1}</span></figcaption>\n"
|
|
||||||
end
|
end
|
||||||
if @file =~ /\S[\S\s]*\w+\.(\w+)/ && @filetype.nil?
|
if markup =~ CaptionUrlTitle
|
||||||
@filetype = $1
|
@caption = $1
|
||||||
|
@url = $2 + $3
|
||||||
|
@anchor = $4
|
||||||
|
elsif markup =~ CaptionUrl
|
||||||
|
@caption = $1
|
||||||
|
@url = $2 + $3
|
||||||
|
elsif markup =~ Caption
|
||||||
|
@caption = $1
|
||||||
|
end
|
||||||
|
if @caption =~ /\S[\S\s]*\w+\.(\w+)/ && @lang.nil?
|
||||||
|
@lang = $1
|
||||||
end
|
end
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
code = super
|
code = super.strip
|
||||||
source = "<figure class='code'>"
|
code = highlight(code, @lang, {caption: @caption, url: @url, anchor: @anchor, start: @start})
|
||||||
source += @caption if @caption
|
code = context['pygments_prefix'] + code if context['pygments_prefix']
|
||||||
if @filetype
|
code = code + context['pygments_suffix'] if context['pygments_suffix']
|
||||||
source += " #{highlight(code, @filetype)}</figure>"
|
code
|
||||||
else
|
|
||||||
source += "#{tableize_code(code.lstrip.rstrip.gsub(/</,'<'))}</figure>"
|
|
||||||
end
|
|
||||||
source = safe_wrap(source)
|
|
||||||
source = context['pygments_prefix'] + source if context['pygments_prefix']
|
|
||||||
source = source + context['pygments_suffix'] if context['pygments_suffix']
|
|
||||||
source
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -21,21 +21,34 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
require './plugins/pygments_code'
|
require './plugins/pygments_code'
|
||||||
require './plugins/raw'
|
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
|
|
||||||
class IncludeCodeTag < Liquid::Tag
|
class IncludeCodeTag < Liquid::Tag
|
||||||
include HighlightCode
|
include HighlightCode
|
||||||
include TemplateWrapper
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
@title = nil
|
@title = nil
|
||||||
@file = nil
|
@file = nil
|
||||||
|
@start = 1
|
||||||
|
@end = nil
|
||||||
if markup.strip =~ /\s*lang:(\w+)/i
|
if markup.strip =~ /\s*lang:(\w+)/i
|
||||||
@filetype = $1
|
@filetype = $1
|
||||||
markup = markup.strip.sub(/lang:\w+/i,'')
|
markup = markup.strip.sub(/lang:\w+/i,'')
|
||||||
end
|
end
|
||||||
|
if markup =~ /\s*start:(\d+)/i
|
||||||
|
@start = $1.to_i
|
||||||
|
markup = markup.sub(/\s*start:\d+/i,'')
|
||||||
|
end
|
||||||
|
if markup =~ /\s*end:(\d+)/i
|
||||||
|
@end = $1.to_i
|
||||||
|
markup = markup.sub(/\s*end:\d+/i,'')
|
||||||
|
end
|
||||||
|
if markup =~ /\s*range:(\d+),(\d+)/i
|
||||||
|
@start = $1.to_i
|
||||||
|
@end = $2.to_i
|
||||||
|
markup = markup.sub(/\s*range:\d+,\d+/i,'')
|
||||||
|
end
|
||||||
if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i
|
if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i
|
||||||
@title = $1 || nil
|
@title = $1 || nil
|
||||||
@file = $3
|
@file = $3
|
||||||
@ -58,12 +71,17 @@ module Jekyll
|
|||||||
|
|
||||||
Dir.chdir(code_path) do
|
Dir.chdir(code_path) do
|
||||||
code = file.read
|
code = file.read
|
||||||
|
length = code.lines.count
|
||||||
|
@end ||= 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
|
||||||
|
if @start > 1 or @end < length
|
||||||
|
code = code.split(/\n/).slice(@start -1, @end + 1 - @start).join("\n")
|
||||||
|
end
|
||||||
@filetype = file.extname.sub('.','') if @filetype.nil?
|
@filetype = file.extname.sub('.','') if @filetype.nil?
|
||||||
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"
|
highlight(code, @filetype, {caption: title, url: url, anchor: 'download', start: @start})
|
||||||
source += " #{highlight(code, @filetype)}</figure>"
|
|
||||||
safe_wrap(source)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -9,10 +9,10 @@ module OctopressFilters
|
|||||||
include BacktickCodeBlock
|
include BacktickCodeBlock
|
||||||
include TemplateWrapper
|
include TemplateWrapper
|
||||||
def pre_filter(input)
|
def pre_filter(input)
|
||||||
input = render_code_block(input)
|
|
||||||
input.gsub /(<figure.+?>.+?<\/figure>)/m do
|
input.gsub /(<figure.+?>.+?<\/figure>)/m do
|
||||||
safe_wrap($1)
|
safe_wrap($1)
|
||||||
end
|
end
|
||||||
|
input = render_code_block(input)
|
||||||
end
|
end
|
||||||
def post_filter(input)
|
def post_filter(input)
|
||||||
input = unwrap(input)
|
input = unwrap(input)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#require 'albino'
|
||||||
|
require './plugins/raw'
|
||||||
require 'pygments'
|
require 'pygments'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
require 'digest/md5'
|
require 'digest/md5'
|
||||||
@ -6,33 +8,77 @@ 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)
|
include TemplateWrapper
|
||||||
lang = 'ruby' if lang == 'ru'
|
|
||||||
lang = 'objc' if lang == 'm'
|
|
||||||
lang = 'perl' if lang == 'pl'
|
|
||||||
lang = 'yaml' if lang == 'yml'
|
|
||||||
str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #strip out divs <div class="highlight">
|
|
||||||
tableize_code(str, lang)
|
|
||||||
end
|
|
||||||
|
|
||||||
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)
|
path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html") if defined?(PYGMENTS_CACHE_DIR)
|
||||||
if File.exist?(path)
|
if File.exist?(path)
|
||||||
highlighted_code = File.read(path)
|
highlighted_code = File.read(path)
|
||||||
else
|
else
|
||||||
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
#highlighted_code = Albino.new(code, lang, :html)
|
||||||
|
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
||||||
File.open(path, 'w') {|f| f.print(highlighted_code) } if path
|
File.open(path, 'w') {|f| f.print(highlighted_code) } if path
|
||||||
end
|
end
|
||||||
highlighted_code
|
highlighted_code.to_s
|
||||||
|
rescue
|
||||||
|
puts $!,$@
|
||||||
end
|
end
|
||||||
|
|
||||||
def tableize_code (str, lang = '')
|
def highlight(code, lang, options = {})
|
||||||
table = '<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers">'
|
lang = 'ruby' if lang == 'ru'
|
||||||
code = ''
|
lang = 'objc' if lang == 'm'
|
||||||
str.lines.each_with_index do |line,index|
|
lang = 'perl' if lang == 'pl'
|
||||||
table += "<span class='line-number'>#{index+1}</span>\n"
|
lang = 'yaml' if lang == 'yml'
|
||||||
code += "<span class='line'>#{line}</span>"
|
lang = 'coffeescript' if lang == 'coffee'
|
||||||
|
lang = 'plain' if lang == '' or lang.nil?
|
||||||
|
|
||||||
|
caption = options[:caption] || nil
|
||||||
|
url = options[:url] || nil
|
||||||
|
anchor = options[:anchor] || nil
|
||||||
|
wrap = options[:wrap] || true
|
||||||
|
linenos = options[:linenos]
|
||||||
|
start = options[:start]
|
||||||
|
|
||||||
|
if lang == 'plain'
|
||||||
|
# Escape html tags
|
||||||
|
code = code.gsub('<','<').gsub('>','>')
|
||||||
|
elsif lang.include? "-raw"
|
||||||
|
output = "``` #{$1.sub('-raw', '')}\n"
|
||||||
|
output += code
|
||||||
|
output += "\n```\n"
|
||||||
|
else
|
||||||
|
code = pygments(code, lang).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
|
||||||
end
|
end
|
||||||
table += "</pre></td><td class='code'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"
|
|
||||||
|
code = tableize_code(code, lang, { linenos: linenos, start: start })
|
||||||
|
caption = captionize(caption, url, anchor) if caption
|
||||||
|
|
||||||
|
figure = "<figure class='code'>#{caption}#{code}</figure>"
|
||||||
|
figure = safe_wrap(figure) if wrap
|
||||||
|
figure
|
||||||
|
end
|
||||||
|
|
||||||
|
def captionize (caption, url, anchor)
|
||||||
|
figcaption = "<figcaption><span>#{caption}</span>"
|
||||||
|
figcaption += "<a href='#{url}' title='Download code'> #{anchor || 'link'}</a>" if url
|
||||||
|
figcaption += "</figcaption>"
|
||||||
|
end
|
||||||
|
|
||||||
|
def tableize_code (code, lang, options = {})
|
||||||
|
start = options[:start]
|
||||||
|
lines = options[:linenos] || true
|
||||||
|
table = "<div class='highlight'><table>"
|
||||||
|
table += number_lines(start, code.lines.count) if lines
|
||||||
|
table += "<td class='code'><pre><code class='#{lang}'>"
|
||||||
|
table += code.gsub /^((.+)?(\n?))/, '<span class=\'line\'>\1</span>'
|
||||||
|
table +="</code></pre></td></tr></table></div>"
|
||||||
|
end
|
||||||
|
|
||||||
|
def number_lines (start, count)
|
||||||
|
start ||= 1
|
||||||
|
lines = "<td class='gutter'><pre class='line-numbers'>"
|
||||||
|
count.times do |index|
|
||||||
|
lines += "<span class='line-number'>#{index + start}</span>\n"
|
||||||
|
end
|
||||||
|
lines += "</pre></td>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user