Class: Jekyll::Engine

Inherits:
Object
  • Object
show all
Includes:
Filters
Defined in:
lib/jekyll/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filters

#array_to_sentence_string, #cgi_escape, #date_to_long_string, #date_to_string, #date_to_xmlschema, #number_of_words, #textilize, #xml_escape

Constructor Details

#initialize(payload) ⇒ Engine

Returns a new instance of Engine.



7
8
9
10
11
# File 'lib/jekyll/engine.rb', line 7

def initialize(payload)
  @site      = OpenStruct.new(payload['site'])
  @page      = OpenStruct.new(payload['page'])
  @paginator = OpenStruct.new(payload['paginator']) if payload['paginator']
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/jekyll/engine.rb', line 4

def content
  @content
end

#layoutObject

Returns the value of attribute layout.



5
6
7
# File 'lib/jekyll/engine.rb', line 5

def layout
  @layout
end

#pageObject (readonly)

Returns the value of attribute page.



4
5
6
# File 'lib/jekyll/engine.rb', line 4

def page
  @page
end

#siteObject (readonly)

Returns the value of attribute site.



4
5
6
# File 'lib/jekyll/engine.rb', line 4

def site
  @site
end

Instance Method Details

#add_code_tags(code, lang) ⇒ Object



66
67
68
69
70
# File 'lib/jekyll/engine.rb', line 66

def add_code_tags(code, lang)
  # Add nested <code> tags to code blocks
  code = code.sub(/<pre>/,'<pre><code class="' + lang.to_s + '">')
  code = code.sub(/<\/pre>/,"</code></pre>")
end

#erbObject



13
14
15
# File 'lib/jekyll/engine.rb', line 13

def erb
  ERB.new(@layout || @content)
end

#highlight(text, lang = :ruby) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/jekyll/engine.rb', line 38

def highlight(text, lang = :ruby)
  if @site.pygments
    render_pygments(text, lang)
  else
    render_codehighlighter(text, lang)
  end
end

#include_file(file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jekyll/engine.rb', line 21

def include_file(file)
  file = file.strip

  if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./
    return "Include file '#{file}' contains invalid characters or sequences"
  end
  Dir.chdir(File.join(@site.source, '_includes')) do
    choices = Dir['**/*'].reject { |x| File.symlink?(x) }
    if choices.include?(file)
      source = File.read(file)
      ERB.new(source).result(binding)
    else
      "Included file '#{@file}' not found in _includes directory"
    end
  end
end

#renderObject



17
18
19
# File 'lib/jekyll/engine.rb', line 17

def render
  erb.result(binding)
end

#render_codehighlighter(text, lang) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll/engine.rb', line 55

def render_codehighlighter(text, lang)
#The div is required because RDiscount blows ass
  <<-HTML
<div>
<pre>
<code class='#{lang}'>#{h(text).strip}</code>
</pre>
</div>
  HTML
end

#render_pygments(text, lang) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/jekyll/engine.rb', line 46

def render_pygments(text, lang)
  output = add_code_tags(Albino.new(text, lang).to_s, lang)
  case @page.content_type
    when "markdown" then "\n" + output + "\n"
    when "textile" then "<notextile>" + output + "</notextile>"
    else output
  end
end