├── README.md └── kramdown_pygments.rb /README.md: -------------------------------------------------------------------------------- 1 | Kramdown with Pygments 2 | ======== 3 | A Jekyll plugin that enables Pygments syntax highlighting for Kramdown-parsed fenced code blocks. 4 | Heavily based on [krampygs](https://github.com/navarroj/krampygs), but refactored to make it work with Jekyll 2.*. 5 | 6 | In the standard setup, Jekyll Kramdown only works with Pygments for syntax highlighting 7 | when you use Liquid tags. This plugin makes Kramdown also use Pygments when using 8 | fenced code blocks. That way you can use more Markdown and less Liquid. Yay! 9 | 10 | ## Usage 11 | 12 | * Clone this project into your `_plugins` directory. 13 | * Add the following lines to your `_config.yml` 14 | 15 | ```yaml 16 | markdown: KramdownPygments 17 | ``` 18 | 19 | Fenced code blocks can now be syntax highlighted using the power of Pygments. 20 | 21 | 22 | ~~~php 23 | print "Hello World" 24 | ~~~ 25 | 26 | 27 | The same goes for inline code: 28 | 29 | You could also do something like this: `var foo = 'bar'`{:.language-javascript}. Amazing! 30 | 31 | ## Setting the default language 32 | If you don't want to set the language for inline code blocks like that every time, 33 | you can define a global default language for the entire site in your `_config.yml` 34 | 35 | ```yaml 36 | kramdown: 37 | default_lang: php 38 | ``` 39 | 40 | If you want to override that for a single page, add the following at the top of 41 | that page, but below the front-matter 42 | 43 | ``` 44 | {::options kramdown_default_lang="php" /} 45 | ``` 46 | 47 | ## Options 48 | This plugin supports all options that the original kramdown converter supports: 49 | 50 | ```yaml 51 | kramdown: 52 | auto_ids: true 53 | footnote_nr: 1 54 | entity_output: as_char 55 | toc_levels: 1..6 56 | smart_quotes: lsquo,rsquo,ldquo,rdquo 57 | input: GFM 58 | ``` 59 | 60 | ## Tested with 61 | 62 | * kramdown 1.4.0 63 | * pygments.rb 0.6.0 64 | * jekyll 2.1.1 65 | * ruby 2.0.0p451 66 | 67 | ## Thanks 68 | 69 | This plugin is heavily based on [krampygs](https://github.com/navarroj/krampygs). 70 | Thanks to [@navarroj](https://github.com/navarroj) for developing the original plugin. 71 | -------------------------------------------------------------------------------- /kramdown_pygments.rb: -------------------------------------------------------------------------------- 1 | # We define the an additional option for the kramdown parser to look for 2 | module Kramdown 3 | module Options 4 | define(:kramdown_default_lang, Symbol, nil, <#{code}\n" 38 | end 39 | 40 | def convert_codespan(el, indent) 41 | attr = el.attr.dup 42 | lang = extract_code_language!(attr) || @options[:kramdown_default_lang] 43 | code = pygmentize(el.value, lang) 44 | if lang 45 | attr['class'] = "highlight" 46 | if attr.has_key?('class') 47 | attr['class'] += " language-#{lang}" 48 | else 49 | attr['class'] = "language-#{lang}" 50 | end 51 | end 52 | "#{code}" 53 | end 54 | 55 | def pygmentize(code, lang) 56 | if lang 57 | Pygments.highlight(code, 58 | :lexer => lang, 59 | :options => { :startinline => true, :encoding => 'utf-8', :nowrap => true }) 60 | else 61 | escape_html(code) 62 | end 63 | end 64 | end 65 | end 66 | end 67 | 68 | # This class is the actual custom Jekyll converter. 69 | class Jekyll::Converters::Markdown::KramdownPygments 70 | 71 | def initialize(config) 72 | require 'kramdown' 73 | @config = config 74 | rescue LoadError 75 | STDERR.puts 'You are missing a library required for Markdown. Please run:' 76 | STDERR.puts ' $ [sudo] gem install kramdown' 77 | raise FatalException.new("Missing dependency: kramdown") 78 | end 79 | 80 | def convert(content) 81 | html = Kramdown::Document.new(content, { 82 | :auto_ids => @config['kramdown']['auto_ids'], 83 | :footnote_nr => @config['kramdown']['footnote_nr'], 84 | :entity_output => @config['kramdown']['entity_output'], 85 | :toc_levels => @config['kramdown']['toc_levels'], 86 | :smart_quotes => @config['kramdown']['smart_quotes'], 87 | :kramdown_default_lang => @config['kramdown']['default_lang'], 88 | :input => @config['kramdown']['input'], 89 | :hard_wrap => @config['kramdown']['hard_wrap'] 90 | }).to_pygments_html 91 | return html; 92 | end 93 | end 94 | --------------------------------------------------------------------------------