Merge branch 'ayastreb-tags'
* ayastreb-tags: rubocop: fix code style rubocop: fix spacing in code style rubocop: fix code style rubocop: fix include tag code style rubocop: fix post URL tag code style rubocop: fix link tag code style rubocop: fix highlight tag code style
This commit is contained in:
commit
5e46a02d71
|
@ -21,10 +21,6 @@ AllCops:
|
||||||
- lib/jekyll/renderer.rb
|
- lib/jekyll/renderer.rb
|
||||||
- lib/jekyll/site.rb
|
- lib/jekyll/site.rb
|
||||||
- lib/jekyll/static_file.rb
|
- lib/jekyll/static_file.rb
|
||||||
- lib/jekyll/tags/highlight.rb
|
|
||||||
- lib/jekyll/tags/include.rb
|
|
||||||
- lib/jekyll/tags/link.rb
|
|
||||||
- lib/jekyll/tags/post_url.rb
|
|
||||||
- lib/jekyll/theme.rb
|
- lib/jekyll/theme.rb
|
||||||
- lib/jekyll/url.rb
|
- lib/jekyll/url.rb
|
||||||
- lib/jekyll/utils.rb
|
- lib/jekyll/utils.rb
|
||||||
|
|
|
@ -14,22 +14,9 @@ module Jekyll
|
||||||
super
|
super
|
||||||
if markup.strip =~ SYNTAX
|
if markup.strip =~ SYNTAX
|
||||||
@lang = Regexp.last_match(1).downcase
|
@lang = Regexp.last_match(1).downcase
|
||||||
@highlight_options = {}
|
@highlight_options = parse_options(Regexp.last_match(2))
|
||||||
if defined?(Regexp.last_match(2)) && Regexp.last_match(2) != ''
|
|
||||||
# Split along 3 possible forms -- key="<quoted list>", 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
|
|
||||||
else
|
else
|
||||||
raise SyntaxError.new <<-eos
|
raise SyntaxError, <<-eos
|
||||||
Syntax Error in tag 'highlight' while parsing the following markup:
|
Syntax Error in tag 'highlight' while parsing the following markup:
|
||||||
|
|
||||||
#{markup}
|
#{markup}
|
||||||
|
@ -42,15 +29,15 @@ eos
|
||||||
def render(context)
|
def render(context)
|
||||||
prefix = context["highlighter_prefix"] || ""
|
prefix = context["highlighter_prefix"] || ""
|
||||||
suffix = context["highlighter_suffix"] || ""
|
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
|
is_safe = !!context.registers[:site].safe
|
||||||
|
|
||||||
output =
|
output =
|
||||||
case context.registers[:site].highlighter
|
case context.registers[:site].highlighter
|
||||||
when 'pygments'
|
when "pygments"
|
||||||
render_pygments(code, is_safe)
|
render_pygments(code, is_safe)
|
||||||
when 'rouge'
|
when "rouge"
|
||||||
render_rouge(code)
|
render_rouge(code)
|
||||||
else
|
else
|
||||||
render_codehighlighter(code)
|
render_codehighlighter(code)
|
||||||
|
@ -66,7 +53,7 @@ eos
|
||||||
[:startinline, opts.fetch(:startinline, nil)],
|
[:startinline, opts.fetch(:startinline, nil)],
|
||||||
[:hl_lines, opts.fetch(:hl_lines, nil)],
|
[:hl_lines, opts.fetch(:hl_lines, nil)],
|
||||||
[:linenos, opts.fetch(:linenos, nil)],
|
[:linenos, opts.fetch(:linenos, nil)],
|
||||||
[:encoding, opts.fetch(:encoding, 'utf-8')],
|
[:encoding, opts.fetch(:encoding, "utf-8")],
|
||||||
[:cssclass, opts.fetch(:cssclass, nil)]
|
[:cssclass, opts.fetch(:cssclass, nil)]
|
||||||
].reject { |f| f.last.nil? }]
|
].reject { |f| f.last.nil? }]
|
||||||
else
|
else
|
||||||
|
@ -74,8 +61,30 @@ eos
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def parse_options(input)
|
||||||
|
options = {}
|
||||||
|
unless input.empty?
|
||||||
|
# Split along 3 possible forms -- key="<quoted list>", 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)
|
def render_pygments(code, is_safe)
|
||||||
Jekyll::External.require_with_graceful_fail('pygments')
|
Jekyll::External.require_with_graceful_fail("pygments")
|
||||||
|
|
||||||
highlighted_code = Pygments.highlight(
|
highlighted_code = Pygments.highlight(
|
||||||
code,
|
code,
|
||||||
|
@ -84,22 +93,27 @@ eos
|
||||||
)
|
)
|
||||||
|
|
||||||
if highlighted_code.nil?
|
if highlighted_code.nil?
|
||||||
Jekyll.logger.error "There was an error highlighting your code:"
|
Jekyll.logger.error <<eos
|
||||||
puts
|
There was an error highlighting your code:
|
||||||
Jekyll.logger.error code
|
|
||||||
puts
|
#{code}
|
||||||
Jekyll.logger.error "While attempting to convert the above code, Pygments.rb" \
|
|
||||||
" returned an unacceptable value."
|
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."
|
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.")
|
eos
|
||||||
|
raise ArgumentError, "Pygments.rb returned an unacceptable value "\
|
||||||
|
"when attempting to highlight some code."
|
||||||
end
|
end
|
||||||
|
|
||||||
highlighted_code.sub('<div class="highlight"><pre>', '').sub('</pre></div>', '')
|
highlighted_code.sub('<div class="highlight"><pre>', "").sub("</pre></div>", "")
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_rouge(code)
|
def render_rouge(code)
|
||||||
Jekyll::External.require_with_graceful_fail('rouge')
|
Jekyll::External.require_with_graceful_fail("rouge")
|
||||||
formatter = Rouge::Formatters::HTML.new(:line_numbers => @highlight_options[:linenos], :wrap => false)
|
formatter = Rouge::Formatters::HTML.new(
|
||||||
|
:line_numbers => @highlight_options[:linenos],
|
||||||
|
:wrap => false
|
||||||
|
)
|
||||||
lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
|
lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
|
||||||
formatter.format(lexer.lex(code))
|
formatter.format(lexer.lex(code))
|
||||||
end
|
end
|
||||||
|
@ -110,13 +124,14 @@ eos
|
||||||
|
|
||||||
def add_code_tag(code)
|
def add_code_tag(code)
|
||||||
code_attributes = [
|
code_attributes = [
|
||||||
"class=\"language-#{@lang.to_s.tr('+', '-')}\"",
|
"class=\"language-#{@lang.to_s.tr("+", "-")}\"",
|
||||||
"data-lang=\"#{@lang}\""
|
"data-lang=\"#{@lang}\""
|
||||||
].join(" ")
|
].join(" ")
|
||||||
"<figure class=\"highlight\"><pre><code #{code_attributes}>#{code.chomp}</code></pre></figure>"
|
"<figure class=\"highlight\"><pre><code #{code_attributes}>"\
|
||||||
|
"#{code.chomp}</code></pre></figure>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('highlight', Jekyll::Tags::HighlightBlock)
|
Liquid::Template.register_tag("highlight", Jekyll::Tags::HighlightBlock)
|
||||||
|
|
|
@ -12,17 +12,23 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
class IncludeTag < Liquid::Tag
|
class IncludeTag < Liquid::Tag
|
||||||
VALID_SYNTAX = /([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/
|
VALID_SYNTAX = /
|
||||||
VARIABLE_SYNTAX = /(?<variable>[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+)(?<params>.*)/
|
([\w-]+)\s*=\s*
|
||||||
|
(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))
|
||||||
|
/x
|
||||||
|
VARIABLE_SYNTAX = /
|
||||||
|
(?<variable>[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+)
|
||||||
|
(?<params>.*)
|
||||||
|
/x
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
super
|
super
|
||||||
matched = markup.strip.match(VARIABLE_SYNTAX)
|
matched = markup.strip.match(VARIABLE_SYNTAX)
|
||||||
if matched
|
if matched
|
||||||
@file = matched['variable'].strip
|
@file = matched["variable"].strip
|
||||||
@params = matched['params'].strip
|
@params = matched["params"].strip
|
||||||
else
|
else
|
||||||
@file, @params = markup.strip.split(' ', 2)
|
@file, @params = markup.strip.split(/\s+/, 2)
|
||||||
end
|
end
|
||||||
validate_params if @params
|
validate_params if @params
|
||||||
@tag_name = tag_name
|
@tag_name = tag_name
|
||||||
|
@ -36,7 +42,7 @@ module Jekyll
|
||||||
params = {}
|
params = {}
|
||||||
markup = @params
|
markup = @params
|
||||||
|
|
||||||
while match = VALID_SYNTAX.match(markup) do
|
while (match = VALID_SYNTAX.match(markup))
|
||||||
markup = markup[match.end(0)..-1]
|
markup = markup[match.end(0)..-1]
|
||||||
|
|
||||||
value = if match[2]
|
value = if match[2]
|
||||||
|
@ -53,8 +59,8 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_file_name(file)
|
def validate_file_name(file)
|
||||||
if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./
|
if file !~ %r!^[a-zA-Z0-9_/\.-]+$! || file =~ %r!\./! || file =~ %r!/\.!
|
||||||
raise ArgumentError.new <<-eos
|
raise ArgumentError, <<-eos
|
||||||
Invalid syntax for include tag. File contains invalid characters or sequences:
|
Invalid syntax for include tag. File contains invalid characters or sequences:
|
||||||
|
|
||||||
#{file}
|
#{file}
|
||||||
|
@ -68,9 +74,9 @@ eos
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_params
|
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
|
unless @params =~ full_valid_syntax
|
||||||
raise ArgumentError.new <<-eos
|
raise ArgumentError, <<-eos
|
||||||
Invalid syntax for include tag:
|
Invalid syntax for include tag:
|
||||||
|
|
||||||
#{@params}
|
#{@params}
|
||||||
|
@ -91,7 +97,10 @@ eos
|
||||||
# Render the variable if required
|
# Render the variable if required
|
||||||
def render_variable(context)
|
def render_variable(context)
|
||||||
if @file.match(VARIABLE_SYNTAX)
|
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)
|
partial.render!(context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -106,9 +115,9 @@ eos
|
||||||
path = File.join(dir, file)
|
path = File.join(dir, file)
|
||||||
return path if valid_include_file?(path, dir, safe)
|
return path if valid_include_file?(path, dir, safe)
|
||||||
end
|
end
|
||||||
raise IOError, "Could not locate the included file '#{file}' in any of #{includes_dirs}." \
|
raise IOError, "Could not locate the included file '#{file}' in any of "\
|
||||||
" Ensure it exists in one of those directories and, if it is a symlink, " \
|
"#{includes_dirs}. Ensure it exists in one of those directories and, "\
|
||||||
"does not point outside your site source."
|
"if it is a symlink, does not point outside your site source."
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
|
@ -120,24 +129,23 @@ eos
|
||||||
path = locate_include_file(context, file, site.safe)
|
path = locate_include_file(context, file, site.safe)
|
||||||
return unless path
|
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")
|
if context.registers[:page] && context.registers[:page].key?("path")
|
||||||
site.regenerator.add_dependency(
|
site.regenerator.add_dependency(
|
||||||
site.in_source_dir(context.registers[:page]["path"]),
|
site.in_source_dir(context.registers[:page]["path"]),
|
||||||
path
|
path
|
||||||
)
|
)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def load_cached_partial(path, context)
|
def load_cached_partial(path, context)
|
||||||
|
@ -147,7 +155,10 @@ eos
|
||||||
if cached_partial.key?(path)
|
if cached_partial.key?(path)
|
||||||
cached_partial[path]
|
cached_partial[path]
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -188,5 +199,5 @@ eos
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('include', Jekyll::Tags::IncludeTag)
|
Liquid::Template.register_tag("include", Jekyll::Tags::IncludeTag)
|
||||||
Liquid::Template.register_tag('include_relative', Jekyll::Tags::IncludeRelativeTag)
|
Liquid::Template.register_tag("include_relative", Jekyll::Tags::IncludeRelativeTag)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
module Tags
|
module Tags
|
||||||
class Link < Liquid::Tag
|
class Link < Liquid::Tag
|
||||||
TagName = 'link'
|
TAG_NAME = "link".freeze
|
||||||
|
|
||||||
def initialize(tag_name, relative_path, tokens)
|
def initialize(tag_name, relative_path, tokens)
|
||||||
super
|
super
|
||||||
|
@ -16,11 +16,14 @@ module Jekyll
|
||||||
return document.url if document.relative_path == @relative_path
|
return document.url if document.relative_path == @relative_path
|
||||||
end
|
end
|
||||||
|
|
||||||
raise ArgumentError, "Could not find document '#{@relative_path}' in tag '#{TagName}'.\n\n" \
|
raise ArgumentError, <<eos
|
||||||
"Make sure the document exists and the path is correct."
|
Could not find document '#{@relative_path}' in tag '#{TAG_NAME}'.
|
||||||
|
|
||||||
|
Make sure the document exists and the path is correct.
|
||||||
|
eos
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag(Jekyll::Tags::Link::TagName, Jekyll::Tags::Link)
|
Liquid::Template.register_tag(Jekyll::Tags::Link::TAG_NAME, Jekyll::Tags::Link)
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
module Tags
|
module Tags
|
||||||
class PostComparer
|
class PostComparer
|
||||||
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
|
MATCHER = %r!^(.+/)*(\d+-\d+-\d+)-(.*)$!
|
||||||
|
|
||||||
attr_reader :path, :date, :slug, :name
|
attr_reader :path, :date, :slug, :name
|
||||||
|
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name
|
@name = name
|
||||||
|
|
||||||
all, @path, @date, @slug = *name.sub(/^\//, "").match(MATCHER)
|
all, @path, @date, @slug = *name.sub(%r!^/!, "").match(MATCHER)
|
||||||
unless all
|
unless all
|
||||||
raise Jekyll::Errors::InvalidPostNameError,
|
raise Jekyll::Errors::InvalidPostNameError,
|
||||||
"'#{name}' does not contain valid date and/or title."
|
"'#{name}' does not contain valid date and/or title."
|
||||||
|
@ -42,9 +42,9 @@ module Jekyll
|
||||||
def post_slug(other)
|
def post_slug(other)
|
||||||
path = other.basename.split("/")[0...-1].join("/")
|
path = other.basename.split("/")[0...-1].join("/")
|
||||||
if path.nil? || path == ""
|
if path.nil? || path == ""
|
||||||
other.data['slug']
|
other.data["slug"]
|
||||||
else
|
else
|
||||||
path + '/' + other.data['slug']
|
path + "/" + other.data["slug"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -78,7 +78,8 @@ eos
|
||||||
|
|
||||||
site.posts.docs.each do |p|
|
site.posts.docs.each do |p|
|
||||||
next unless @post.deprecated_equality 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 " \
|
"a post using the new matching method of checking name " \
|
||||||
"(path-date-slug) equality. Please make sure that you " \
|
"(path-date-slug) equality. Please make sure that you " \
|
||||||
"change this tag to match the post's name exactly."
|
"change this tag to match the post's name exactly."
|
||||||
|
@ -95,4 +96,4 @@ eos
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('post_url', Jekyll::Tags::PostUrl)
|
Liquid::Template.register_tag("post_url", Jekyll::Tags::PostUrl)
|
||||||
|
|
Loading…
Reference in New Issue