diff --git a/lib/jekyll/tags/highlight.rb b/lib/jekyll/tags/highlight.rb index e16602a7..b36a8a01 100644 --- a/lib/jekyll/tags/highlight.rb +++ b/lib/jekyll/tags/highlight.rb @@ -1,77 +1,77 @@ module Jekyll + module Tags + class HighlightBlock < Liquid::Block + include Liquid::StandardFilters - class HighlightBlock < Liquid::Block - include Liquid::StandardFilters + # The regular expression syntax checker. Start with the language specifier. + # Follow that by zero or more space separated options that take one of two + # forms: + # + # 1. name + # 2. name=value + SYNTAX = /^([a-zA-Z0-9.+#-]+)((\s+\w+(=\w+)?)*)$/ - # The regular expression syntax checker. Start with the language specifier. - # Follow that by zero or more space separated options that take one of two - # forms: - # - # 1. name - # 2. name=value - SYNTAX = /^([a-zA-Z0-9.+#-]+)((\s+\w+(=\w+)?)*)$/ - - def initialize(tag_name, markup, tokens) - super - if markup.strip =~ SYNTAX - @lang = $1 - @options = {} - if defined?($2) && $2 != '' - $2.split.each do |opt| - key, value = opt.split('=') - if value.nil? - if key == 'linenos' - value = 'inline' - else - value = true + def initialize(tag_name, markup, tokens) + super + if markup.strip =~ SYNTAX + @lang = $1 + @options = {} + if defined?($2) && $2 != '' + $2.split.each do |opt| + key, value = opt.split('=') + if value.nil? + if key == 'linenos' + value = 'inline' + else + value = true + end end + @options[key] = value end - @options[key] = value end + else + raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight [linenos]") end - else - raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight [linenos]") end - end - def render(context) - if context.registers[:site].pygments - render_pygments(context, super) - else - render_codehighlighter(context, super) + def render(context) + if context.registers[:site].pygments + render_pygments(context, super) + else + render_codehighlighter(context, super) + end end + + def render_pygments(context, code) + @options[:encoding] = 'utf-8' + + output = add_code_tags( + Pygments.highlight(code, :lexer => @lang, :options => @options), + @lang + ) + + output = context["pygments_prefix"] + output if context["pygments_prefix"] + output = output + context["pygments_suffix"] if context["pygments_suffix"] + output + end + + def render_codehighlighter(context, code) + #The div is required because RDiscount blows ass + <<-HTML +
+
#{h(code).strip}
+
+ HTML + end + + def add_code_tags(code, lang) + # Add nested tags to code blocks + code = code.sub(/
/,'
')
+        code = code.sub(/<\/pre>/,"
") + end + end - - def render_pygments(context, code) - @options[:encoding] = 'utf-8' - - output = add_code_tags( - Pygments.highlight(code, :lexer => @lang, :options => @options), - @lang - ) - - output = context["pygments_prefix"] + output if context["pygments_prefix"] - output = output + context["pygments_suffix"] if context["pygments_suffix"] - output - end - - def render_codehighlighter(context, code) - #The div is required because RDiscount blows ass - <<-HTML -
-
#{h(code).strip}
-
- HTML - end - - def add_code_tags(code, lang) - # Add nested tags to code blocks - code = code.sub(/
/,'
')
-      code = code.sub(/<\/pre>/,"
") - end - end - end -Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock) +Liquid::Template.register_tag('highlight', Jekyll::Tags::HighlightBlock) diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index e71d07f7..3c2fa984 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -1,37 +1,37 @@ module Jekyll - - class IncludeTag < Liquid::Tag - def initialize(tag_name, file, tokens) - super - @file = file.strip - end - - def render(context) - includes_dir = File.join(context.registers[:site].source, '_includes') - - if File.symlink?(includes_dir) - return "Includes directory '#{includes_dir}' cannot be a symlink" + module Tags + class IncludeTag < Liquid::Tag + def initialize(tag_name, file, tokens) + super + @file = file.strip end - if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./ - return "Include file '#{@file}' contains invalid characters or sequences" - end + def render(context) + includes_dir = File.join(context.registers[:site].source, '_includes') - Dir.chdir(includes_dir) do - choices = Dir['**/*'].reject { |x| File.symlink?(x) } - if choices.include?(@file) - source = File.read(@file) - partial = Liquid::Template.parse(source) - context.stack do - partial.render(context) + if File.symlink?(includes_dir) + return "Includes directory '#{includes_dir}' cannot be a symlink" + end + + if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./ + return "Include file '#{@file}' contains invalid characters or sequences" + end + + Dir.chdir(includes_dir) do + choices = Dir['**/*'].reject { |x| File.symlink?(x) } + if choices.include?(@file) + source = File.read(@file) + partial = Liquid::Template.parse(source) + context.stack do + partial.render(context) + end + else + "Included file '#{@file}' not found in _includes directory" end - else - "Included file '#{@file}' not found in _includes directory" end end end end - end -Liquid::Template.register_tag('include', Jekyll::IncludeTag) +Liquid::Template.register_tag('include', Jekyll::Tags::IncludeTag) diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb index 21d3a0b4..ff4a29b8 100644 --- a/lib/jekyll/tags/post_url.rb +++ b/lib/jekyll/tags/post_url.rb @@ -1,38 +1,39 @@ module Jekyll + module Tags + class PostComparer + MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/ - class PostComparer - MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/ + attr_accessor :date, :slug - attr_accessor :date, :slug - - def initialize(name) - who, cares, date, slug = *name.match(MATCHER) - @slug = slug - @date = Time.parse(date) - end - end - - class PostUrl < Liquid::Tag - def initialize(tag_name, post, tokens) - super - @orig_post = post.strip - @post = PostComparer.new(@orig_post) + def initialize(name) + who, cares, date, slug = *name.match(MATCHER) + @slug = slug + @date = Time.parse(date) + end end - def render(context) - site = context.registers[:site] - - site.posts.each do |p| - if p == @post - return p.url - end + class PostUrl < Liquid::Tag + def initialize(tag_name, post, tokens) + super + @orig_post = post.strip + @post = PostComparer.new(@orig_post) end - puts "ERROR: post_url: \"#{@orig_post}\" could not be found" + def render(context) + site = context.registers[:site] - return "#" + site.posts.each do |p| + if p == @post + return p.url + end + end + + puts "ERROR: post_url: \"#{@orig_post}\" could not be found" + + return "#" + end end end end -Liquid::Template.register_tag('post_url', Jekyll::PostUrl) +Liquid::Template.register_tag('post_url', Jekyll::Tags::PostUrl) diff --git a/test/test_tags.rb b/test/test_tags.rb index 3d5e80e4..3d97410b 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -39,7 +39,7 @@ CONTENT context "language name" do should "match only the required set of chars" do - r = Jekyll::HighlightBlock::SYNTAX + r = Jekyll::Tags::HighlightBlock::SYNTAX assert_match r, "ruby" assert_match r, "c#" assert_match r, "xml+cheetah" @@ -55,19 +55,19 @@ CONTENT context "initialized tag" do should "work" do - tag = Jekyll::HighlightBlock.new('highlight', 'ruby ', ["test", "{% endhighlight %}", "\n"]) + tag = Jekyll::Tags::HighlightBlock.new('highlight', 'ruby ', ["test", "{% endhighlight %}", "\n"]) assert_equal({}, tag.instance_variable_get(:@options)) - tag = Jekyll::HighlightBlock.new('highlight', 'ruby linenos ', ["test", "{% endhighlight %}", "\n"]) + tag = Jekyll::Tags::HighlightBlock.new('highlight', 'ruby linenos ', ["test", "{% endhighlight %}", "\n"]) assert_equal({ 'linenos' => 'inline' }, tag.instance_variable_get(:@options)) - tag = Jekyll::HighlightBlock.new('highlight', 'ruby linenos=table ', ["test", "{% endhighlight %}", "\n"]) + tag = Jekyll::Tags::HighlightBlock.new('highlight', 'ruby linenos=table ', ["test", "{% endhighlight %}", "\n"]) assert_equal({ 'linenos' => 'table' }, tag.instance_variable_get(:@options)) - tag = Jekyll::HighlightBlock.new('highlight', 'ruby linenos=table nowrap', ["test", "{% endhighlight %}", "\n"]) + tag = Jekyll::Tags::HighlightBlock.new('highlight', 'ruby linenos=table nowrap', ["test", "{% endhighlight %}", "\n"]) assert_equal({ 'linenos' => 'table', 'nowrap' => true }, tag.instance_variable_get(:@options)) - tag = Jekyll::HighlightBlock.new('highlight', 'ruby linenos=table cssclass=hl', ["test", "{% endhighlight %}", "\n"]) + tag = Jekyll::Tags::HighlightBlock.new('highlight', 'ruby linenos=table cssclass=hl', ["test", "{% endhighlight %}", "\n"]) assert_equal({ 'cssclass' => 'hl', 'linenos' => 'table' }, tag.instance_variable_get(:@options)) end end