Class: Octopress::Pygments::Renderer
- Inherits:
-
Object
- Object
- Octopress::Pygments::Renderer
- Defined in:
- lib/octopress-pygments/renderer.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #captionize(caption, url, link_text) ⇒ Object
- #determine_lang(lang) ⇒ Object
- #encode_liquid(code) ⇒ Object
-
#get_range(code, start, endline) ⇒ Object
Public:.
- #highlight ⇒ Object
-
#initialize(code, options = {}) ⇒ Renderer
constructor
A new instance of Renderer.
- #render_pygments(code, lang) ⇒ Object
- #tableize_code(code, lang, options = {}) ⇒ Object
Constructor Details
#initialize(code, options = {}) ⇒ Renderer
Returns a new instance of Renderer.
6 7 8 9 10 11 12 13 |
# File 'lib/octopress-pygments/renderer.rb', line 6 def initialize(code, = {}) @code = code @options = .delete_if { |k,v| v.nil? } @options = DEFAULTS.merge(@options) @aliases = @options[:aliases] @aliases = (@aliases ? stringify_keys(@aliases) : {}) @lang = determine_lang(@options[:lang]) end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
4 5 6 |
# File 'lib/octopress-pygments/renderer.rb', line 4 def code @code end |
#lang ⇒ Object
Returns the value of attribute lang.
4 5 6 |
# File 'lib/octopress-pygments/renderer.rb', line 4 def lang @lang end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/octopress-pygments/renderer.rb', line 4 def @options end |
Instance Method Details
#captionize(caption, url, link_text) ⇒ Object
60 61 62 63 64 |
# File 'lib/octopress-pygments/renderer.rb', line 60 def (, url, link_text) = "<figcaption class='pygments-code-caption'><span class='pygments-code-caption-title'>#{}</span>" += "<a class='pygments-code-caption-link' href='#{url}'>#{(link_text || 'link').strip}</a>" if url += "</figcaption>" end |
#determine_lang(lang) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/octopress-pygments/renderer.rb', line 34 def determine_lang(lang) if lang == '' lang = 'plain' elsif ::Pygments::Lexer.find lang lang elsif !@aliases[lang].nil? and ::Pygments::Lexer.find @aliases[lang] @aliases[lang] else 'plain' end end |
#encode_liquid(code) ⇒ Object
101 102 103 104 |
# File 'lib/octopress-pygments/renderer.rb', line 101 def encode_liquid(code) code.gsub(/{{/, '{{') .gsub(/{%/, '{%') end |
#get_range(code, start, endline) ⇒ Object
Public:
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/octopress-pygments/renderer.rb', line 89 def get_range(code, start, endline) length = code.lines.count start endline ||= length if start > 1 or endline < length raise "#{filepath} is #{length} lines long, cannot begin at line #{start}" if start > length raise "#{filepath} is #{length} lines long, cannot read beyond line #{endline}" if endline > length code = code.split(/\n/).slice(start - 1, endline + 1 - start).join("\n") end code end |
#highlight ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/octopress-pygments/renderer.rb', line 15 def highlight @options[:title] ||= ' ' if @options[:url] if cache = Cache.read_cache(code, @options) cache else if @lang == 'plain' rendered_code = code.to_s.gsub('<','<') else rendered_code = render_pygments(code, @lang).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight"> end rendered_code = tableize_code(rendered_code, @lang, {linenos: @options[:linenos], start: @options[:start], marks: @options[:marks]}) title = (@options[:title], @options[:url], @options[:link_text]) if @options[:title] rendered_code = "<figure class='pygments-code-figure'>#{title}#{rendered_code}</figure>" rendered_code = "{% raw %}#{rendered_code}{% endraw %}" if @options[:escape] Cache.write_to_cache(rendered_code, @options) unless @options[:no_cache] rendered_code end end |
#render_pygments(code, lang) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/octopress-pygments/renderer.rb', line 46 def render_pygments(code, lang) highlighted_code = ::Pygments.highlight( code, { lexer: lang, formatter: 'html', options: { encoding: 'utf-8' } } ) encode_liquid(highlighted_code).to_s end |
#tableize_code(code, lang, options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/octopress-pygments/renderer.rb', line 66 def tableize_code (code, lang, = {}) start = [:start] lines = [:linenos] marks = [:marks] table = "<div class='pygments-code'>" table += "<pre class='pygments-code-pre #{lang}'>" code.lines.each_with_index do |line,index| classes = 'pygments-code-row' classes += lines ? ' numbered' : ' unnumbered' if marks.include? index + start classes += ' marked-line' classes += ' start-marked-line' unless marks.include? index - 1 + start classes += ' end-marked-line' unless marks.include? index + 1 + start end line = line.strip.empty? ? ' ' : line table += "<div data-line='#{index + start}' class='#{classes}'><div class='pygments-code-line'>#{line}</div></div>" end table +="</pre></div>" end |