Class: CodeHighlight::MainController

Inherits:
Volt::ModelController
  • Object
show all
Defined in:
app/code_highlight/controllers/main_controller.rb

Instance Method Summary collapse

Instance Method Details

#before_index_removeObject



18
19
20
# File 'app/code_highlight/controllers/main_controller.rb', line 18

def before_index_remove
  @watcher.stop if @watcher
end

#code_tagObject



22
23
24
# File 'app/code_highlight/controllers/main_controller.rb', line 22

def code_tag
  first_element
end

#highlightObject



26
27
28
29
30
# File 'app/code_highlight/controllers/main_controller.rb', line 26

def highlight
  `hljs.configure({useBR: true});`
  block = code_tag
  `hljs.highlightBlock(block);`
end

#index_readyObject



14
15
16
# File 'app/code_highlight/controllers/main_controller.rb', line 14

def index_ready
  @watcher = -> { update_code }.watch!
end

#update_codeObject

Since the contents of the <code> tag will be replaced by highlight.js, we can’t use a content binding, instead we use jquery to insert the html, and then highlight it after each insert. We use a .watch! to update the code.



7
8
9
10
11
12
# File 'app/code_highlight/controllers/main_controller.rb', line 7

def update_code
  html = yield_html

  `$(#{code_tag}).html(html);`
  highlight
end