mirror of
https://github.com/moparisthebest/android.moparisthebest.org
synced 2025-01-14 07:08:01 -05:00
Further simplified code plugins
- The highlight function now accepts only to variables: code, options. - Extracting part of a code snippet is now a method of pygments_code.rb. - Options assignment has been simplified for all code plugins.
This commit is contained in:
parent
66a883f2af
commit
8eac9cf471
@ -12,28 +12,30 @@ module BacktickCodeBlock
|
||||
markup = $1 || ''
|
||||
code = $2.to_s
|
||||
|
||||
options = parse_markup(markup)
|
||||
@lang = options[:lang]
|
||||
@title = options[:title]
|
||||
@lineos = options[:lineos]
|
||||
@marks = options[:marks]
|
||||
@url = options[:url]
|
||||
@link_text = options[:link_text]
|
||||
@start = options[:start]
|
||||
opts = parse_markup(markup)
|
||||
@options = {
|
||||
lang: opts[:lang],
|
||||
title: opts[:title],
|
||||
lineos: opts[:lineos],
|
||||
marks: opts[:marks],
|
||||
url: opts[:url],
|
||||
link_text: opts[:link_text] || 'link',
|
||||
start: opts[:start] || 1,
|
||||
}
|
||||
markup = clean_markup(markup)
|
||||
|
||||
if markup =~ AllOptions
|
||||
@lang ||= $1
|
||||
@title ||= $2
|
||||
@url ||= $3
|
||||
@link_text ||= $4
|
||||
@options[:lang] ||= $1
|
||||
@options[:title] ||= $2
|
||||
@options[:url] ||= $3
|
||||
@options[:link_text] ||= $4
|
||||
elsif markup =~ LangCaption
|
||||
@lang ||= $1
|
||||
@title ||= $2
|
||||
@options[:lang] ||= $1
|
||||
@options[:title] ||= $2
|
||||
else
|
||||
@lang = 'plain'
|
||||
@options[:lang] ||= 'plain'
|
||||
end
|
||||
highlight(code, @lang, {title: @title, url: @url, link_text: @link_text, linenos: @linenos, marks: @marks, start: @start })
|
||||
highlight(code, @options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -52,38 +52,38 @@ module Jekyll
|
||||
TitleUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i
|
||||
Title = /(\S[\S\s]*)/
|
||||
def initialize(tag_name, markup, tokens)
|
||||
@title = nil
|
||||
|
||||
options = parse_markup(markup)
|
||||
@lang = options[:lang]
|
||||
@title = options[:title]
|
||||
@lineos = options[:lineos]
|
||||
@marks = options[:marks]
|
||||
@url = options[:url]
|
||||
@link_text = options[:link_text]
|
||||
@start = options[:start]
|
||||
opts = parse_markup(markup)
|
||||
@options = {
|
||||
lang: opts[:lang],
|
||||
title: opts[:title],
|
||||
lineos: opts[:lineos],
|
||||
marks: opts[:marks],
|
||||
url: opts[:url],
|
||||
link_text: opts[:link_text] || 'link',
|
||||
start: opts[:start] || 1,
|
||||
}
|
||||
markup = clean_markup(markup)
|
||||
|
||||
if markup =~ TitleUrlLinkText
|
||||
@title ||= $1
|
||||
@url ||= $2 + $3
|
||||
@link_text ||= $4
|
||||
@options[:title] ||= $1
|
||||
@options[:url] ||= $2 + $3
|
||||
@options[:link_text] ||= $4
|
||||
elsif markup =~ TitleUrl
|
||||
@title ||= $1
|
||||
@url ||= $2 + $3
|
||||
@options[:title] ||= $1
|
||||
@options[:url] ||= $2 + $3
|
||||
elsif markup =~ Title
|
||||
@title ||= $1
|
||||
@options[:title] ||= $1
|
||||
end
|
||||
# grab lang from filename in title
|
||||
if @title =~ /\S[\S\s]*\w+\.(\w+)/ && @lang.nil?
|
||||
@lang ||= $1
|
||||
if @options[:title] =~ /\S[\S\s]*\w+\.(\w+)/ && @options[:lang].nil?
|
||||
@options[:lang] ||= $1
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
def render(context)
|
||||
code = super.strip
|
||||
code = highlight(code, @lang, {title: @title, url: @url, link_text: @link_text, start: @start, marks: @marks, linenos: @linenos})
|
||||
code = highlight(code, @options)
|
||||
code = context['pygments_prefix'] + code if context['pygments_prefix']
|
||||
code = code + context['pygments_suffix'] if context['pygments_suffix']
|
||||
code
|
||||
|
@ -30,7 +30,7 @@ module Jekyll
|
||||
lineos: opts[:lineos],
|
||||
marks: opts[:marks],
|
||||
url: opts[:url],
|
||||
link_text: opts[:link_text],
|
||||
link_text: opts[:link_text] || 'Gist page',
|
||||
start: opts[:start],
|
||||
end: opts[:end]
|
||||
}
|
||||
@ -45,23 +45,15 @@ module Jekyll
|
||||
@options[:title] ||= file.empty? ? "Gist: #{gist}" : file
|
||||
@options[:url] ||= "https://gist.github.com/#{gist}"
|
||||
@options[:lang] ||= file.empty? ? @options[:lang] || '' : file.split('.')[-1]
|
||||
@options[:link_text] ||= "Gist page"
|
||||
@options[:no_cache] = @cache_disabled
|
||||
@options[:cache_path] = @cache_disabled ? nil : get_cache_path(@cache_folder, get_cache_file(gist, file), @markup + @options.to_s)
|
||||
|
||||
cache = read_cache(@options[:cache_path])
|
||||
|
||||
unless cache
|
||||
code = get_gist_from_web(gist, file)
|
||||
length = code.lines.count
|
||||
@start ||= 1
|
||||
@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
|
||||
code = highlight(code, @options[:lang], @options)
|
||||
code = get_gist_from_web(gist, file)
|
||||
code = get_range(code, @options[:start], @options[:end])
|
||||
code = highlight(code, @options)
|
||||
end
|
||||
code
|
||||
else
|
||||
|
@ -29,23 +29,24 @@ module Jekyll
|
||||
include HighlightCode
|
||||
def initialize(tag_name, markup, tokens)
|
||||
@file = nil
|
||||
@title = nil
|
||||
@title_old = nil
|
||||
|
||||
options = parse_markup(markup)
|
||||
@lang = options[:lang]
|
||||
@title = options[:title]
|
||||
@lineos = options[:lineos]
|
||||
@marks = options[:marks]
|
||||
@url = options[:url]
|
||||
@link_text = options[:link_text]
|
||||
@start = options[:start]
|
||||
@end = options[:end]
|
||||
opts = parse_markup(markup)
|
||||
@options = {
|
||||
lang: opts[:lang],
|
||||
title: opts[:title],
|
||||
lineos: opts[:lineos],
|
||||
marks: opts[:marks],
|
||||
url: opts[:url],
|
||||
link_text: opts[:link_text] || 'view raw',
|
||||
start: opts[:start] || 1,
|
||||
end: opts[:end]
|
||||
}
|
||||
markup = clean_markup(markup)
|
||||
|
||||
if markup.strip =~ /(^\S*\.\S+) *(.+)?/i
|
||||
@file = $1
|
||||
@title ||= $2
|
||||
@options[:title] ||= $2
|
||||
elsif markup.strip =~ /(.*?)(\S*\.\S+)\Z/i # Title before file is deprecated in 2.1
|
||||
@title_old = $1
|
||||
@file = $2
|
||||
@ -56,14 +57,14 @@ module Jekyll
|
||||
def render(context)
|
||||
code_dir = (context.registers[:site].config['code_dir'].sub(/^\//,'') || 'downloads/code')
|
||||
code_path = (Pathname.new(context.registers[:site].source) + code_dir).expand_path
|
||||
file = code_path + @file
|
||||
filepath = code_path + @file
|
||||
|
||||
unless @title_old.nil?
|
||||
@title ||= @title_old
|
||||
@options[:title] ||= @title_old
|
||||
puts "### ------------ WARNING ------------ ###"
|
||||
puts "This include_code syntax is deprecated "
|
||||
puts "Correct syntax: path/to/file.ext [title]"
|
||||
puts "Update include for #{file}"
|
||||
puts "Update include for #{filepath}"
|
||||
puts "### --------------------------------- ###"
|
||||
end
|
||||
|
||||
@ -72,29 +73,22 @@ module Jekyll
|
||||
return "Code directory '#{code_path}' cannot be a symlink"
|
||||
end
|
||||
|
||||
unless file.file?
|
||||
puts "File #{file} could not be found"
|
||||
return "File #{file} could not be found"
|
||||
unless filepath.file?
|
||||
puts "File #{filepath} could not be found"
|
||||
return "File #{filepath} could not be found"
|
||||
end
|
||||
|
||||
Dir.chdir(code_path) do
|
||||
code = file.read
|
||||
length = code.lines.count
|
||||
@end ||= length
|
||||
@start ||= 1
|
||||
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
|
||||
@lang = file.extname.sub('.','') unless @lang
|
||||
title = @title ? "#{@title} (#{file.basename})" : file.basename
|
||||
url = "/#{code_dir}/#{@file}"
|
||||
highlight(code, @lang, {title: title, url: url, link_text: @link_text || 'view raw', start: @start, marks: @marks, linenos: @linenos })
|
||||
@options[:lang] ||= filepath.extname.sub('.','')
|
||||
@options[:title] = @options[:title] ? "#{@options[:title]} (#{filepath.basename})" : filepath.basename
|
||||
@options[:url] ||= "/#{code_dir}/#{@file}"
|
||||
|
||||
code = filepath.read
|
||||
code = get_range(code, @options[:start], @options[:end])
|
||||
highlight(code, @options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Liquid::Template.register_tag('include_code', Jekyll::IncludeCodeTag)
|
||||
|
@ -23,7 +23,8 @@ module HighlightCode
|
||||
puts $!,$@
|
||||
end
|
||||
|
||||
def highlight(code, lang, options = {})
|
||||
def highlight(code, options = {})
|
||||
lang = options[:lang]
|
||||
lang = 'ruby' if lang == 'ru'
|
||||
lang = 'objc' if lang == 'm'
|
||||
lang = 'perl' if lang == 'pl'
|
||||
@ -134,7 +135,7 @@ module HighlightCode
|
||||
end: (endline.nil? ? nil : endline[1].to_i),
|
||||
link_text: (link_text.nil? ? nil : link_text[3] || link_text[5] || link_text[6])
|
||||
}
|
||||
opts.merge(get_range(input, opts[:start], opts[:end]))
|
||||
opts.merge(parse_range(input, opts[:start], opts[:end]))
|
||||
end
|
||||
|
||||
def clean_markup (input)
|
||||
@ -163,11 +164,24 @@ module HighlightCode
|
||||
marks
|
||||
end
|
||||
|
||||
def get_range (input, start, endline)
|
||||
def parse_range (input, start, endline)
|
||||
if input =~ / *range:(\d+)-(\d+)/i
|
||||
start = $1.to_i
|
||||
endline = $2.to_i
|
||||
end
|
||||
{start: start, end: endline}
|
||||
end
|
||||
|
||||
def get_range (code, start, endline)
|
||||
length = code.lines.count
|
||||
start ||= 1
|
||||
endline ||= length
|
||||
if start > 1 or endline < length
|
||||
raise "#{filepath} is #{length} lines long, cannot begin at line #{start}" if start > length
|
||||
raise "#{filepath} is #{length} lines long, cannot read beyond line #{endline}" if endline > length
|
||||
code = code.split(/\n/).slice(start - 1, endline + 1 - start).join("\n")
|
||||
end
|
||||
code
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user