Class: GfmLivePreview::Server
- Inherits:
-
Sinatra::Application
- Object
- Sinatra::Application
- GfmLivePreview::Server
- Defined in:
- lib/gfm_live_preview/server.rb
Instance Method Summary collapse
- #render_document(ws, relative_path) ⇒ Object
- #setup_websockets(request, relative_path = nil) ⇒ Object
- #syntax_highlight!(str) ⇒ Object
Instance Method Details
#render_document(ws, relative_path) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/gfm_live_preview/server.rb', line 17 def render_document(ws, relative_path) document = GfmLivePreview.read_file relative_path syntax_highlight! document doc = GitHub::Markdown.render_gfm document EM.next_tick { ws.send doc } end |
#setup_websockets(request, relative_path = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gfm_live_preview/server.rb', line 24 def setup_websockets(request, relative_path = nil) request.websocket do |ws| ws.onopen do render_document ws, relative_path listener = Listen.to GfmLivePreview.root do render_document ws, relative_path end listener.start settings.sockets << ws end ws.onclose do settings.sockets.delete ws end end end |
#syntax_highlight!(str) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/gfm_live_preview/server.rb', line 8 def syntax_highlight! str SyntaxHighlighter.highlight! str do |string, hexcolor| "<span style=\"color:##{hexcolor}\">#{string}</span>" end rescue => e puts "Exception during syntax highlighting: #{e}" e.backtrace.each do |line| puts line; end end |