Skip to content

Instantly share code, notes, and snippets.

@danasilver
Last active November 6, 2017 12:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danasilver/8121699 to your computer and use it in GitHub Desktop.
Save danasilver/8121699 to your computer and use it in GitHub Desktop.
Jekyll Pygments/Redcarpet Global Configs

Jekyll Pygments/Redcarpet Global Configs

A Jekyll plugin that enables global configuration options for Pygments.

  • Add pygments_global_config.rb to my-jekyll-project/_plugins/.

  • Add Pygments options to _config.yml

markdown: redcarpet
pygments: true
pygments_options: ['lineanchors', 'linenos=table']
  • Write highlight tags or fenced code blocks and have the pygments_options passed in automatically.
{% highlight ruby %}
puts "Hello, world!"
{% endhighlight %}
'''ruby
puts "Hello, world!"
'''

instead of

{% highlight ruby linenos=table lineanchors %}
puts "Hello, world!"
{% endhighlight %}

or the inability to pass Pygments options into the fenced code block.

module Jekyll
module Tags
class HighlightBlock < Liquid::Block
def render_pygments(context, code)
require 'pygments'
@options[:encoding] = 'utf-8'
@config = Jekyll.configuration({})
if @config['pygments_options']
@config['pygments_options'].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
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
end
end
end
module Jekyll
module Converters
class Markdown
class RedcarpetParser
module WithPygments
include CommonMethods
def block_code(code, lang)
require 'pygments'
lang = lang && lang.split.first || "text"
@options = {}
@config = Jekyll.configuration({})
@options[:encoding] = 'utf-8'
if @config['pygments_options']
@config['pygments_options'].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
output = add_code_tags(
Pygments.highlight(code, :lexer => lang, :options => @options),
lang
)
end
end
end
end
end
end
@drewsberry
Copy link

Hey, I've been using your excellent plugin to add lineanchors to all of my codeblocks, but since upgrading Jekyll to version 2.2.0 I'm getting the following error:

Liquid Exception: undefined method empty?' for false:FalseClass in _posts/`

Any idea why this might be?

Here's the stack trace: https://gist.github.com/drewsberry/81c4df87237d040cfa4b

@elboletaire
Copy link

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment