gfm_live_preview 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gfm_live_preview.rb +9 -0
- data/lib/gfm_live_preview/cli.rb +8 -1
- data/lib/gfm_live_preview/server.rb +43 -18
- data/lib/gfm_live_preview/version.rb +1 -1
- data/lib/views/index.erb +5 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1d5623ea9f4bd1f3a8dfedefff5fb0ce629fa89
|
4
|
+
data.tar.gz: 6b7da1b778422066e3ec3625a066d300b67e98c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85d71df80b15bcc2addbe3218b397ae6d25bd0961a0d8f8653edc53a154f53fa71a4b3d07b91ea7ef07bc9958433b3870a1d4af38ba0f3dd7a096fda6e241107
|
7
|
+
data.tar.gz: e515ea2c8c934b28805860ac6aa56320369937474334dc4cca95c26998c54eb802452ff72b138f17069da20df7c4515102a6e104186241b8c97ff7c8b19e9a7b
|
data/lib/gfm_live_preview.rb
CHANGED
@@ -24,4 +24,13 @@ module GfmLivePreview
|
|
24
24
|
def config
|
25
25
|
@config ||= {}
|
26
26
|
end
|
27
|
+
|
28
|
+
def read_file(relative_path = nil)
|
29
|
+
path = relative_path ? root.join(relative_path) : file
|
30
|
+
File.read path
|
31
|
+
end
|
32
|
+
|
33
|
+
def root
|
34
|
+
Pathname(File.expand_path("..", file))
|
35
|
+
end
|
27
36
|
end
|
data/lib/gfm_live_preview/cli.rb
CHANGED
@@ -14,34 +14,59 @@ module GfmLivePreview
|
|
14
14
|
e.backtrace.each do |line| puts line; end
|
15
15
|
end
|
16
16
|
|
17
|
-
def render_document
|
18
|
-
document =
|
17
|
+
def render_document(ws, relative_path)
|
18
|
+
document = GfmLivePreview.read_file relative_path
|
19
19
|
syntax_highlight! document
|
20
|
-
GitHub::Markdown.render_gfm document
|
20
|
+
doc = GitHub::Markdown.render_gfm document
|
21
|
+
EM.next_tick { ws.send doc }
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
-
|
24
|
+
def setup_websockets(request, relative_path = nil)
|
25
|
+
request.websocket do |ws|
|
26
|
+
ws.onopen do
|
27
|
+
render_document ws, relative_path
|
28
|
+
listener = Listen.to GfmLivePreview.root do
|
29
|
+
render_document ws, relative_path
|
30
|
+
end
|
31
|
+
listener.start
|
32
|
+
settings.sockets << ws
|
33
|
+
end
|
34
|
+
ws.onclose do
|
35
|
+
settings.sockets.delete ws
|
36
|
+
end
|
37
|
+
end
|
25
38
|
end
|
26
39
|
|
27
40
|
get "/" do
|
28
41
|
if request.websocket?
|
29
|
-
request
|
30
|
-
ws.onopen do
|
31
|
-
ws.send render_document
|
32
|
-
listener = Listen.to doc_path do
|
33
|
-
ws.send render_document
|
34
|
-
end
|
35
|
-
listener.start
|
36
|
-
settings.sockets << ws
|
37
|
-
end
|
38
|
-
ws.onclose do
|
39
|
-
settings.sockets.delete ws
|
40
|
-
end
|
41
|
-
end
|
42
|
+
setup_websockets request
|
42
43
|
else
|
43
44
|
erb :index
|
44
45
|
end
|
45
46
|
end
|
47
|
+
|
48
|
+
# Support local assets like images
|
49
|
+
get "/*" do
|
50
|
+
path = params[:splat].first
|
51
|
+
|
52
|
+
local_path = ::GfmLivePreview.root.join path
|
53
|
+
|
54
|
+
halt 404 unless local_path.exist?
|
55
|
+
|
56
|
+
mime_type = MIME::Types.type_for(local_path.basename.to_s).first
|
57
|
+
|
58
|
+
if mime_type && mime_type.raw_media_type == "image"
|
59
|
+
content_type mime_type.content_type
|
60
|
+
File.read local_path
|
61
|
+
elsif %w(.md .markdown).include? local_path.extname
|
62
|
+
if request.websocket?
|
63
|
+
setup_websockets request, path
|
64
|
+
else
|
65
|
+
erb :index
|
66
|
+
end
|
67
|
+
else
|
68
|
+
halt 404
|
69
|
+
end
|
70
|
+
end
|
46
71
|
end
|
47
72
|
end
|
data/lib/views/index.erb
CHANGED
@@ -17,14 +17,16 @@ window.onload = function() {
|
|
17
17
|
for(var cc = 0; cc < links.length; cc++) {
|
18
18
|
var link = links[cc];
|
19
19
|
if(link.rel == "stylesheet") {
|
20
|
-
|
20
|
+
var token = (Math.random() + 1).toString(36).substring(7);
|
21
|
+
link.href = link.href.replace(/\?.*/, '') + '?' + token;
|
21
22
|
}
|
22
23
|
}
|
23
24
|
};
|
24
25
|
|
25
|
-
var md_document
|
26
|
+
var md_document = document.getElementById("document");
|
26
27
|
var web_socket_url = "ws://" + window.location.host + window.location.pathname;
|
27
|
-
var ws
|
28
|
+
var ws = new WebSocket(web_socket_url);
|
29
|
+
|
28
30
|
ws.onmessage = function(m) {
|
29
31
|
md_document.innerHTML = m.data;
|
30
32
|
reload_css();
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gfm_live_preview
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ntl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|