diff --git a/plugins/backtick_code_block.rb b/plugins/backtick_code_block.rb index fcf4e83..9ee80c9 100644 --- a/plugins/backtick_code_block.rb +++ b/plugins/backtick_code_block.rb @@ -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 diff --git a/plugins/code_block.rb b/plugins/code_block.rb index 09024ab..464b6c2 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -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 diff --git a/plugins/gist_tag.rb b/plugins/gist_tag.rb index 4bbd79f..32ae9c8 100644 --- a/plugins/gist_tag.rb +++ b/plugins/gist_tag.rb @@ -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 diff --git a/plugins/include_code.rb b/plugins/include_code.rb index 46593ac..21c3a41 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -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) diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index ff33a23..ebebb18 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -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