-
Notifications
You must be signed in to change notification settings - Fork 5
/
realtime_markdown_viewer.rb
103 lines (93 loc) · 2.44 KB
/
realtime_markdown_viewer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env ruby
# -*- coding:utf-8 -*-
require 'sinatra'
require 'sinatra-websocket'
require 'redcarpet'
require 'coderay'
set :server, 'thin'
set :sockets, []
get '/' do
erb :index
end
class HTMLwithCoderay < Redcarpet::Render::Safe
def block_code(code, language)
begin
CodeRay.scan(code, language.to_sym).div
rescue
super
end
end
end
get '/emacs' do
request.websocket do |ws|
ws.onopen { puts "@@ connect from emacs" }
ws.onmessage do |msg|
renderer = HTMLwithCoderay.new()
markdown = Redcarpet::Markdown.new(renderer, :fenced_code_blocks => true, :no_intra_emphasis => true)
html = markdown.render(msg)
EM.next_tick do
settings.sockets.each{|s| s.send(html) }
end
end
ws.onclose do
settings.sockets.delete(ws)
end
end
end
get '/markdown' do
request.websocket do |ws|
ws.onopen do
settings.sockets << ws
end
ws.onclose do
warn("wetbsocket closed")
settings.sockets.delete(ws)
end
end
end
get '/static/:file.min.:ext' do |file, ext|
content_type ext
send_file "static/#{file}.min.#{ext}"
end
__END__
@@ index
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Realtime Markdown Viewer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="/static/jquery.min.js"></script>
<link rel="stylesheet" href="/static/bootstrap.min.css">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
<div class="container">
<header><h1>Demo</h1></header>
<div id="preview"></div>
</div>
<script type="text/javascript">
$(function () {
var ws = new WebSocket('ws://localhost:5021/markdown');
ws.onopen = function () {
console.log('connected');
};
ws.onclose = function (ev) {
console.log('closed');
};
ws.onmessage = function (ev) {
$('#preview').html(ev.data);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "preview"]);
};
ws.onerror = function (ev) {
console.log(ev);
};
});
</script>
</body>
</html>