Update tag classes moving into a module

This commit is contained in:
Tom Bell 2013-01-19 23:50:44 +00:00
parent 10d980b6e1
commit 2c45150545
4 changed files with 122 additions and 121 deletions

View File

@ -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 <lang> [linenos]")
end
else
raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [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
<div>
<pre><code class='#{@lang}'>#{h(code).strip}</code></pre>
</div>
HTML
end
def add_code_tags(code, lang)
# Add nested <code> tags to code blocks
code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
code = code.sub(/<\/pre>/,"</code></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
<div>
<pre><code class='#{@lang}'>#{h(code).strip}</code></pre>
</div>
HTML
end
def add_code_tags(code, lang)
# Add nested <code> tags to code blocks
code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
code = code.sub(/<\/pre>/,"</code></pre>")
end
end
end
Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock)
Liquid::Template.register_tag('highlight', Jekyll::Tags::HighlightBlock)

View File

@ -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)

View File

@ -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)

View File

@ -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