Class: Bookingit::Renderer
- Inherits:
-
Redcarpet::Render::HTML
- Object
- Redcarpet::Render::HTML
- Bookingit::Renderer
- 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
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#images ⇒ Object
Returns the value of attribute images.
-
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
-
#theme ⇒ Object
Returns the value of attribute theme.
Instance Method Summary collapse
- #block_code(code, language) ⇒ Object
- #current_chapter=(chapter) ⇒ Object
- #doc_footer ⇒ Object
- #doc_header ⇒ Object
- #header(text, header_level, anchor) ⇒ Object
- #identify_language(path) ⇒ Object
- #image(link, title, alt_text) ⇒ Object
-
#initialize(config) ⇒ Renderer
constructor
A new instance of Renderer.
- #record_sections=(record_sections) ⇒ Object
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() = config.rendering_config additional_languages = Hash[([: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([:basedir]).strip @basedir = '.' if @basedir == '' @stylesheets = Array([:stylesheets]) @theme = [:theme] || "default" @cachedir = [:cache] @config = config @images = [] end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
29 30 31 |
# File 'lib/bookingit/renderer.rb', line 29 def headers @headers end |
#images ⇒ Object
Returns the value of attribute images.
29 30 31 |
# File 'lib/bookingit/renderer.rb', line 29 def images @images end |
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
29 30 31 |
# File 'lib/bookingit/renderer.rb', line 29 def stylesheets @stylesheets end |
#theme ⇒ Object
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 |
#doc_footer ⇒ Object
59 60 61 |
# File 'lib/bookingit/renderer.rb', line 59 def Views::FooterView.new(@chapter,@config).render end |
#doc_header ⇒ Object
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 |