Update tag classes moving into a module
This commit is contained in:
parent
10d980b6e1
commit
2c45150545
|
@ -1,77 +1,77 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
|
module Tags
|
||||||
|
class HighlightBlock < Liquid::Block
|
||||||
|
include Liquid::StandardFilters
|
||||||
|
|
||||||
class HighlightBlock < Liquid::Block
|
# The regular expression syntax checker. Start with the language specifier.
|
||||||
include Liquid::StandardFilters
|
# 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.
|
def initialize(tag_name, markup, tokens)
|
||||||
# Follow that by zero or more space separated options that take one of two
|
super
|
||||||
# forms:
|
if markup.strip =~ SYNTAX
|
||||||
#
|
@lang = $1
|
||||||
# 1. name
|
@options = {}
|
||||||
# 2. name=value
|
if defined?($2) && $2 != ''
|
||||||
SYNTAX = /^([a-zA-Z0-9.+#-]+)((\s+\w+(=\w+)?)*)$/
|
$2.split.each do |opt|
|
||||||
|
key, value = opt.split('=')
|
||||||
def initialize(tag_name, markup, tokens)
|
if value.nil?
|
||||||
super
|
if key == 'linenos'
|
||||||
if markup.strip =~ SYNTAX
|
value = 'inline'
|
||||||
@lang = $1
|
else
|
||||||
@options = {}
|
value = true
|
||||||
if defined?($2) && $2 != ''
|
end
|
||||||
$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
|
end
|
||||||
@options[key] = value
|
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [linenos]")
|
||||||
end
|
end
|
||||||
else
|
|
||||||
raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [linenos]")
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
if context.registers[:site].pygments
|
if context.registers[:site].pygments
|
||||||
render_pygments(context, super)
|
render_pygments(context, super)
|
||||||
else
|
else
|
||||||
render_codehighlighter(context, super)
|
render_codehighlighter(context, super)
|
||||||
|
end
|
||||||
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
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock)
|
Liquid::Template.register_tag('highlight', Jekyll::Tags::HighlightBlock)
|
||||||
|
|
|
@ -1,37 +1,37 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
|
module Tags
|
||||||
class IncludeTag < Liquid::Tag
|
class IncludeTag < Liquid::Tag
|
||||||
def initialize(tag_name, file, tokens)
|
def initialize(tag_name, file, tokens)
|
||||||
super
|
super
|
||||||
@file = file.strip
|
@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"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
|
def render(context)
|
||||||
return "Include file '#{@file}' contains invalid characters or sequences"
|
includes_dir = File.join(context.registers[:site].source, '_includes')
|
||||||
end
|
|
||||||
|
|
||||||
Dir.chdir(includes_dir) do
|
if File.symlink?(includes_dir)
|
||||||
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
|
return "Includes directory '#{includes_dir}' cannot be a symlink"
|
||||||
if choices.include?(@file)
|
end
|
||||||
source = File.read(@file)
|
|
||||||
partial = Liquid::Template.parse(source)
|
if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
|
||||||
context.stack do
|
return "Include file '#{@file}' contains invalid characters or sequences"
|
||||||
partial.render(context)
|
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
|
end
|
||||||
else
|
|
||||||
"Included file '#{@file}' not found in _includes directory"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('include', Jekyll::IncludeTag)
|
Liquid::Template.register_tag('include', Jekyll::Tags::IncludeTag)
|
||||||
|
|
|
@ -1,38 +1,39 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
|
module Tags
|
||||||
|
class PostComparer
|
||||||
|
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
|
||||||
|
|
||||||
class PostComparer
|
attr_accessor :date, :slug
|
||||||
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
|
|
||||||
|
|
||||||
attr_accessor :date, :slug
|
def initialize(name)
|
||||||
|
who, cares, date, slug = *name.match(MATCHER)
|
||||||
def initialize(name)
|
@slug = slug
|
||||||
who, cares, date, slug = *name.match(MATCHER)
|
@date = Time.parse(date)
|
||||||
@slug = slug
|
end
|
||||||
@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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
class PostUrl < Liquid::Tag
|
||||||
site = context.registers[:site]
|
def initialize(tag_name, post, tokens)
|
||||||
|
super
|
||||||
site.posts.each do |p|
|
@orig_post = post.strip
|
||||||
if p == @post
|
@post = PostComparer.new(@orig_post)
|
||||||
return p.url
|
|
||||||
end
|
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('post_url', Jekyll::PostUrl)
|
Liquid::Template.register_tag('post_url', Jekyll::Tags::PostUrl)
|
||||||
|
|
|
@ -39,7 +39,7 @@ CONTENT
|
||||||
|
|
||||||
context "language name" do
|
context "language name" do
|
||||||
should "match only the required set of chars" 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, "ruby"
|
||||||
assert_match r, "c#"
|
assert_match r, "c#"
|
||||||
assert_match r, "xml+cheetah"
|
assert_match r, "xml+cheetah"
|
||||||
|
@ -55,19 +55,19 @@ CONTENT
|
||||||
|
|
||||||
context "initialized tag" do
|
context "initialized tag" do
|
||||||
should "work" 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))
|
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))
|
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))
|
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))
|
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))
|
assert_equal({ 'cssclass' => 'hl', 'linenos' => 'table' }, tag.instance_variable_get(:@options))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue