Class: Bookingit::Renderer

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Includes:
FileUtils
Defined in:
lib/bookingit/renderer.rb

Constant Summary collapse

EXTENSION_TO_LANGUAGE =
{
  /\.rb$/    => 'ruby',
  /\.html$/  => 'html',
  /\.scala$/ => 'scala',
  /Gemfile$/ => 'ruby',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Renderer

Returns a new instance of Renderer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bookingit/renderer.rb', line 9

def initialize(config)
  super()
  options = config.rendering_config
  additional_languages = Hash[(options[:languages] || {}).map { |ext_or_regexp,language|
    if ext_or_regexp.kind_of? String
      [/#{ext_or_regexp}$/,language]
    else
      [ext_or_regexp,language]
    end
  }]
  @language_identifiers = EXTENSION_TO_LANGUAGE.merge(additional_languages)
  @basedir              = String(options[:basedir]).strip
  @basedir              = '.' if @basedir == ''
  @stylesheets          = Array(options[:stylesheets])
  @theme                = options[:theme] || "default"
  @cachedir             = options[:cache]
  @config               = config
  @images               = []
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



29
30
31
# File 'lib/bookingit/renderer.rb', line 29

def headers
  @headers
end

#imagesObject

Returns the value of attribute images.



29
30
31
# File 'lib/bookingit/renderer.rb', line 29

def images
  @images
end

#stylesheetsObject

Returns the value of attribute stylesheets.



29
30
31
# File 'lib/bookingit/renderer.rb', line 29

def stylesheets
  @stylesheets
end

#themeObject

Returns the value of attribute theme.



29
30
31
# File 'lib/bookingit/renderer.rb', line 29

def theme
  @theme
end

Instance Method Details

#block_code(code, language) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bookingit/renderer.rb', line 77

def block_code(code, language)
  result = nil
  filename = nil
  chdir @basedir do
    code,language,filename = CodeBlockInterpreter.new(code)
    .when_file(                &cache(:read_file))
    .when_git_diff(            &cache(:read_git_diff))
    .when_shell_command_in_git(&cache(:run_shell_command_in_git))
    .when_file_in_git(         &cache(:read_file_in_git))
    .when_shell_command(       &cache(:run_shell_command))
    .otherwise {
      [code,language,nil]
    }.result
  end
  Views::CodeView.new(code,filename,language,@config).render.strip
end

#current_chapter=(chapter) ⇒ Object



31
32
33
# File 'lib/bookingit/renderer.rb', line 31

def current_chapter=(chapter)
  @chapter = chapter
end


59
60
61
# File 'lib/bookingit/renderer.rb', line 59

def doc_footer
  Views::FooterView.new(@chapter,@config).render
end

#doc_headerObject



54
55
56
57
# File 'lib/bookingit/renderer.rb', line 54

def doc_header
  @headers = {}
  Views::HeaderView.new(@stylesheets,@theme,@config).render
end

#header(text, header_level, anchor) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/bookingit/renderer.rb', line 39

def header(text,header_level,anchor)
  @headers[header_level] ||= []
  @headers[header_level] << text
  if header_level == 2 && @record_sections
    @chapter.add_section(text,anchor)
  end
  "<a name='#{anchor}'></a><h#{header_level+1}>#{text}</h#{header_level+1}>"
end

#identify_language(path) ⇒ Object



70
71
72
73
74
# File 'lib/bookingit/renderer.rb', line 70

def identify_language(path)
  @language_identifiers.select { |matcher,language|
    path =~ matcher
  }.values.first
end

#image(link, title, alt_text) ⇒ Object



48
49
50
51
52
# File 'lib/bookingit/renderer.rb', line 48

def image(link, title, alt_text)
  title = title.gsub(/'/,'"') if title
  @images << link
  "<img src='#{link}' alt='#{alt_text}' title='#{title}'>"
end

#record_sections=(record_sections) ⇒ Object



35
36
37
# File 'lib/bookingit/renderer.rb', line 35

def record_sections=(record_sections)
  @record_sections = record_sections
end