From 41a8b10780e7f41e05f8b2ef24a20dae29a979cc Mon Sep 17 00:00:00 2001 From: Anatoliy Yastreb Date: Tue, 24 May 2016 17:03:06 +0300 Subject: [PATCH 1/7] rubocop: fix highlight tag code style --- .rubocop.yml | 1 - lib/jekyll/tags/highlight.rb | 82 +++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 4189be7bb14..50e89d45ef0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -56,7 +56,6 @@ AllCops: - lib/jekyll/site.rb - lib/jekyll/static_file.rb - lib/jekyll/stevenson.rb - - lib/jekyll/tags/highlight.rb - lib/jekyll/tags/include.rb - lib/jekyll/tags/link.rb - lib/jekyll/tags/post_url.rb diff --git a/lib/jekyll/tags/highlight.rb b/lib/jekyll/tags/highlight.rb index a7dbd5193bc..550b3522e26 100644 --- a/lib/jekyll/tags/highlight.rb +++ b/lib/jekyll/tags/highlight.rb @@ -14,22 +14,9 @@ def initialize(tag_name, markup, tokens) super if markup.strip =~ SYNTAX @lang = Regexp.last_match(1).downcase - @highlight_options = {} - if defined?(Regexp.last_match(2)) && Regexp.last_match(2) != '' - # Split along 3 possible forms -- key="", key=value, or key - Regexp.last_match(2).scan(/(?:\w="[^"]*"|\w=\w|\w)+/) do |opt| - key, value = opt.split('=') - # If a quoted list, convert to array - if value && value.include?("\"") - value.delete!('"') - value = value.split - end - @highlight_options[key.to_sym] = value || true - end - end - @highlight_options[:linenos] = "inline" if @highlight_options.key?(:linenos) && @highlight_options[:linenos] == true + @highlight_options = parse_options(Regexp.last_match(2)) else - raise SyntaxError.new <<-eos + raise SyntaxError, <<-eos Syntax Error in tag 'highlight' while parsing the following markup: #{markup} @@ -42,15 +29,15 @@ def initialize(tag_name, markup, tokens) def render(context) prefix = context["highlighter_prefix"] || "" suffix = context["highlighter_suffix"] || "" - code = super.to_s.gsub(/\A(\n|\r)+|(\n|\r)+\z/, '') + code = super.to_s.gsub(/\A(\n|\r)+|(\n|\r)+\z/, "") is_safe = !!context.registers[:site].safe output = case context.registers[:site].highlighter - when 'pygments' + when "pygments" render_pygments(code, is_safe) - when 'rouge' + when "rouge" render_rouge(code) else render_codehighlighter(code) @@ -66,7 +53,7 @@ def sanitized_opts(opts, is_safe) [:startinline, opts.fetch(:startinline, nil)], [:hl_lines, opts.fetch(:hl_lines, nil)], [:linenos, opts.fetch(:linenos, nil)], - [:encoding, opts.fetch(:encoding, 'utf-8')], + [:encoding, opts.fetch(:encoding, "utf-8")], [:cssclass, opts.fetch(:cssclass, nil)] ].reject { |f| f.last.nil? }] else @@ -74,8 +61,30 @@ def sanitized_opts(opts, is_safe) end end + private + + def parse_options(input) + options = {} + if defined?(input) && input != "" + # Split along 3 possible forms -- key="", key=value, or key + input.scan(/(?:\w="[^"]*"|\w=\w|\w)+/) do |opt| + key, value = opt.split("=") + # If a quoted list, convert to array + if value && value.include?("\"") + value.delete!('"') + value = value.split + end + options[key.to_sym] = value || true + end + end + if options.key?(:linenos) && options[:linenos] == true + options[:linenos] = "inline" + end + options + end + def render_pygments(code, is_safe) - Jekyll::External.require_with_graceful_fail('pygments') + Jekyll::External.require_with_graceful_fail("pygments") highlighted_code = Pygments.highlight( code, @@ -84,22 +93,26 @@ def render_pygments(code, is_safe) ) if highlighted_code.nil? - Jekyll.logger.error "There was an error highlighting your code:" - puts - Jekyll.logger.error code - puts - Jekyll.logger.error "While attempting to convert the above code, Pygments.rb" \ - " returned an unacceptable value." - Jekyll.logger.error "This is usually a timeout problem solved by running `jekyll build` again." - raise ArgumentError.new("Pygments.rb returned an unacceptable value when attempting to highlight some code.") + Jekyll.logger.error <<-eos +There was an error highlighting your code: + +#{code} + +While attempting to convert the above code, Pygments.rb returned an unacceptable value. +This is usually a timeout problem solved by running `jekyll build` again. +eos + raise ArgumentError, "Pygments.rb returned an unacceptable value "\ + "when attempting to highlight some code." end - highlighted_code.sub('
', '').sub('
', '') + highlighted_code.sub('
', "").sub("
", "") end def render_rouge(code) - Jekyll::External.require_with_graceful_fail('rouge') - formatter = Rouge::Formatters::HTML.new(:line_numbers => @highlight_options[:linenos], :wrap => false) + Jekyll::External.require_with_graceful_fail("rouge") + formatter = Rouge::Formatters::HTML.new( + :line_numbers => @highlight_options[:linenos], :wrap => false + ) lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText formatter.format(lexer.lex(code)) end @@ -110,13 +123,14 @@ def render_codehighlighter(code) def add_code_tag(code) code_attributes = [ - "class=\"language-#{@lang.to_s.tr('+', '-')}\"", + "class=\"language-#{@lang.to_s.tr("+", "-")}\"", "data-lang=\"#{@lang}\"" ].join(" ") - "
#{code.chomp}
" + "
"\
+        "#{code.chomp}
" end end end end -Liquid::Template.register_tag('highlight', Jekyll::Tags::HighlightBlock) +Liquid::Template.register_tag("highlight", Jekyll::Tags::HighlightBlock) From 0d1151191437e0da82a6a2cae7282cb8e137945a Mon Sep 17 00:00:00 2001 From: Anatoliy Yastreb Date: Tue, 24 May 2016 17:13:54 +0300 Subject: [PATCH 2/7] rubocop: fix link tag code style --- .rubocop.yml | 1 - lib/jekyll/tags/link.rb | 12 ++++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 50e89d45ef0..2dd1e114ff9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -57,7 +57,6 @@ AllCops: - lib/jekyll/static_file.rb - lib/jekyll/stevenson.rb - lib/jekyll/tags/include.rb - - lib/jekyll/tags/link.rb - lib/jekyll/tags/post_url.rb - lib/jekyll/theme.rb - lib/jekyll/url.rb diff --git a/lib/jekyll/tags/link.rb b/lib/jekyll/tags/link.rb index f81e43b4fd1..b2be119942b 100644 --- a/lib/jekyll/tags/link.rb +++ b/lib/jekyll/tags/link.rb @@ -1,7 +1,7 @@ module Jekyll module Tags class Link < Liquid::Tag - TagName = 'link' + TAG_NAME = "link".freeze def initialize(tag_name, relative_path, tokens) super @@ -16,11 +16,15 @@ def render(context) return document.url if document.relative_path == @relative_path end - raise ArgumentError, "Could not find document '#{@relative_path}' in tag '#{TagName}'.\n\n" \ - "Make sure the document exists and the path is correct." + raise ArgumentError, < Date: Tue, 24 May 2016 17:23:25 +0300 Subject: [PATCH 3/7] rubocop: fix post URL tag code style --- .rubocop.yml | 1 - lib/jekyll/tags/post_url.rb | 13 +++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 2dd1e114ff9..f5dcc93aa73 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -57,7 +57,6 @@ AllCops: - lib/jekyll/static_file.rb - lib/jekyll/stevenson.rb - lib/jekyll/tags/include.rb - - lib/jekyll/tags/post_url.rb - lib/jekyll/theme.rb - lib/jekyll/url.rb - lib/jekyll/utils.rb diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb index b05d056b848..2154c0cd961 100644 --- a/lib/jekyll/tags/post_url.rb +++ b/lib/jekyll/tags/post_url.rb @@ -1,14 +1,14 @@ module Jekyll module Tags class PostComparer - MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/ + MATCHER = %r!^(.+/)*(\d+-\d+-\d+)-(.*)$! attr_reader :path, :date, :slug, :name def initialize(name) @name = name - all, @path, @date, @slug = *name.sub(/^\//, "").match(MATCHER) + all, @path, @date, @slug = *name.sub(%r!^/!, "").match(MATCHER) unless all raise Jekyll::Errors::InvalidPostNameError, "'#{name}' does not contain valid date and/or title." @@ -42,9 +42,9 @@ def deprecated_equality(other) def post_slug(other) path = other.basename.split("/")[0...-1].join("/") if path.nil? || path == "" - other.data['slug'] + other.data["slug"] else - path + '/' + other.data['slug'] + path + "/" + other.data["slug"] end end end @@ -78,7 +78,8 @@ def render(context) site.posts.docs.each do |p| next unless @post.deprecated_equality p - Jekyll::Deprecator.deprecation_message "A call to '{{ post_url #{@post.name} }}' did not match " \ + Jekyll::Deprecator.deprecation_message "A call to "\ + "'{{ post_url #{@post.name} }}' did not match " \ "a post using the new matching method of checking name " \ "(path-date-slug) equality. Please make sure that you " \ "change this tag to match the post's name exactly." @@ -95,4 +96,4 @@ def render(context) end end -Liquid::Template.register_tag('post_url', Jekyll::Tags::PostUrl) +Liquid::Template.register_tag("post_url", Jekyll::Tags::PostUrl) From 2ca80920dcf0ae72cec37c714cc158c568402989 Mon Sep 17 00:00:00 2001 From: Anatoliy Yastreb Date: Wed, 25 May 2016 13:15:04 +0300 Subject: [PATCH 4/7] rubocop: fix include tag code style --- .rubocop.yml | 1 - lib/jekyll/tags/include.rb | 69 ++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f5dcc93aa73..d57fefd1920 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -56,7 +56,6 @@ AllCops: - lib/jekyll/site.rb - lib/jekyll/static_file.rb - lib/jekyll/stevenson.rb - - lib/jekyll/tags/include.rb - lib/jekyll/theme.rb - lib/jekyll/url.rb - lib/jekyll/utils.rb diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index d4f31f2a05b..4c4c38a3ef6 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -12,17 +12,23 @@ def initialize(msg, path) end class IncludeTag < Liquid::Tag - VALID_SYNTAX = /([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/ - VARIABLE_SYNTAX = /(?[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+)(?.*)/ + VALID_SYNTAX = / + ([\w-]+)\s*=\s* + (?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+)) + /x + VARIABLE_SYNTAX = / + (?[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+) + (?.*) + /x def initialize(tag_name, markup, tokens) super matched = markup.strip.match(VARIABLE_SYNTAX) if matched - @file = matched['variable'].strip - @params = matched['params'].strip + @file = matched["variable"].strip + @params = matched["params"].strip else - @file, @params = markup.strip.split(' ', 2) + @file, @params = markup.strip.split(" ", 2) end validate_params if @params @tag_name = tag_name @@ -36,7 +42,7 @@ def parse_params(context) params = {} markup = @params - while match = VALID_SYNTAX.match(markup) do + while (match = VALID_SYNTAX.match(markup)) markup = markup[match.end(0)..-1] value = if match[2] @@ -53,8 +59,8 @@ def parse_params(context) end def validate_file_name(file) - if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./ - raise ArgumentError.new <<-eos + if file !~ %r!^[a-zA-Z0-9_/\.-]+$! || file =~ %r!\./! || file =~ %r!/\.! + raise ArgumentError, <<-eos Invalid syntax for include tag. File contains invalid characters or sequences: #{file} @@ -68,9 +74,9 @@ def validate_file_name(file) end def validate_params - full_valid_syntax = Regexp.compile('\A\s*(?:' + VALID_SYNTAX.to_s + '(?=\s|\z)\s*)*\z') + full_valid_syntax = /\A\s*(?:#{VALID_SYNTAX}(?=\s|\z)\s*)*\z/ unless @params =~ full_valid_syntax - raise ArgumentError.new <<-eos + raise ArgumentError, <<-eos Invalid syntax for include tag: #{@params} @@ -91,7 +97,10 @@ def file_read_opts(context) # Render the variable if required def render_variable(context) if @file.match(VARIABLE_SYNTAX) - partial = context.registers[:site].liquid_renderer.file("(variable)").parse(@file) + partial = context.registers[:site] + .liquid_renderer + .file("(variable)") + .parse(@file) partial.render!(context) end end @@ -106,9 +115,9 @@ def locate_include_file(context, file, safe) path = File.join(dir, file) return path if valid_include_file?(path, dir, safe) end - raise IOError, "Could not locate the included file '#{file}' in any of #{includes_dirs}." \ - " Ensure it exists in one of those directories and, if it is a symlink, " \ - "does not point outside your site source." + raise IOError, "Could not locate the included file '#{file}' in any of "\ + "#{includes_dirs}. Ensure it exists in one of those directories and, "\ + "if it is a symlink, does not point outside your site source." end def render(context) @@ -120,24 +129,23 @@ def render(context) path = locate_include_file(context, file, site.safe) return unless path - # Add include to dependency tree + add_include_to_dependency(site, path, context) + + partial = load_cached_partial(path, context) + + context.stack do + context["include"] = parse_params(context) if @params + partial.render!(context) + end + end + + def add_include_to_dependency(site, path, context) if context.registers[:page] && context.registers[:page].key?("path") site.regenerator.add_dependency( site.in_source_dir(context.registers[:page]["path"]), path ) end - - #begin - partial = load_cached_partial(path, context) - - context.stack do - context['include'] = parse_params(context) if @params - partial.render!(context) - end - #rescue => e - #raise IncludeTagError.new e.message, path - #end end def load_cached_partial(path, context) @@ -147,7 +155,10 @@ def load_cached_partial(path, context) if cached_partial.key?(path) cached_partial[path] else - cached_partial[path] = context.registers[:site].liquid_renderer.file(path).parse(read_file(path, context)) + cached_partial[path] = context.registers[:site] + .liquid_renderer + .file(path) + .parse(read_file(path, context)) end end @@ -188,5 +199,5 @@ def page_path(context) end end -Liquid::Template.register_tag('include', Jekyll::Tags::IncludeTag) -Liquid::Template.register_tag('include_relative', Jekyll::Tags::IncludeRelativeTag) +Liquid::Template.register_tag("include", Jekyll::Tags::IncludeTag) +Liquid::Template.register_tag("include_relative", Jekyll::Tags::IncludeRelativeTag) From 5c036cf3c4933f51f6a13221f11e424f862578c7 Mon Sep 17 00:00:00 2001 From: Anatoliy Yastreb Date: Wed, 25 May 2016 20:24:19 +0300 Subject: [PATCH 5/7] rubocop: fix code style --- lib/jekyll/tags/highlight.rb | 5 +++-- lib/jekyll/tags/link.rb | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/jekyll/tags/highlight.rb b/lib/jekyll/tags/highlight.rb index 550b3522e26..afbadb8553e 100644 --- a/lib/jekyll/tags/highlight.rb +++ b/lib/jekyll/tags/highlight.rb @@ -93,7 +93,7 @@ def render_pygments(code, is_safe) ) if highlighted_code.nil? - Jekyll.logger.error <<-eos + Jekyll.logger.error < @highlight_options[:linenos], :wrap => false + :line_numbers => @highlight_options[:linenos], + :wrap => false ) lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText formatter.format(lexer.lex(code)) diff --git a/lib/jekyll/tags/link.rb b/lib/jekyll/tags/link.rb index b2be119942b..e6dcde610c0 100644 --- a/lib/jekyll/tags/link.rb +++ b/lib/jekyll/tags/link.rb @@ -19,7 +19,6 @@ def render(context) raise ArgumentError, < Date: Wed, 25 May 2016 21:48:21 +0300 Subject: [PATCH 6/7] rubocop: fix spacing in code style --- lib/jekyll/tags/highlight.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/tags/highlight.rb b/lib/jekyll/tags/highlight.rb index afbadb8553e..b22001d3b14 100644 --- a/lib/jekyll/tags/highlight.rb +++ b/lib/jekyll/tags/highlight.rb @@ -112,7 +112,7 @@ def render_rouge(code) Jekyll::External.require_with_graceful_fail("rouge") formatter = Rouge::Formatters::HTML.new( :line_numbers => @highlight_options[:linenos], - :wrap => false + :wrap => false ) lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText formatter.format(lexer.lex(code)) From 001cbf2c073301fe3dc6ffd5100b4712fb78adaf Mon Sep 17 00:00:00 2001 From: Anatoliy Yastreb Date: Thu, 26 May 2016 12:17:31 +0300 Subject: [PATCH 7/7] rubocop: fix code style --- lib/jekyll/tags/highlight.rb | 2 +- lib/jekyll/tags/include.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/tags/highlight.rb b/lib/jekyll/tags/highlight.rb index b22001d3b14..af2a1e107aa 100644 --- a/lib/jekyll/tags/highlight.rb +++ b/lib/jekyll/tags/highlight.rb @@ -65,7 +65,7 @@ def sanitized_opts(opts, is_safe) def parse_options(input) options = {} - if defined?(input) && input != "" + unless input.empty? # Split along 3 possible forms -- key="", key=value, or key input.scan(/(?:\w="[^"]*"|\w=\w|\w)+/) do |opt| key, value = opt.split("=") diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index 4c4c38a3ef6..b1b02981140 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -28,7 +28,7 @@ def initialize(tag_name, markup, tokens) @file = matched["variable"].strip @params = matched["params"].strip else - @file, @params = markup.strip.split(" ", 2) + @file, @params = markup.strip.split(/\s+/, 2) end validate_params if @params @tag_name = tag_name