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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb7a81f2c5a4368fe8f980db25ba2ccfd700a4db
4
- data.tar.gz: 337f31ef3bda894d4f4f76ecddec338c8fa71a32
3
+ metadata.gz: b1d5623ea9f4bd1f3a8dfedefff5fb0ce629fa89
4
+ data.tar.gz: 6b7da1b778422066e3ec3625a066d300b67e98c2
5
5
  SHA512:
6
- metadata.gz: 5b4a35f7edb06eed47bfb04c270495325d4aecafe94a09af61b996144c1f382986a5b5665aff7659e6bfa804482e493b3227fe3043c0cee5a0527f8e3f787343
7
- data.tar.gz: 83e7567923b1fb779d761f3c0b92a48b5687b666df7fbac0d7c2972f510e3622c652cbeda98d12416cc3cdf6d64dc7a13f08eb9986a982ce1c15d11595f2dd40
6
+ metadata.gz: 85d71df80b15bcc2addbe3218b397ae6d25bd0961a0d8f8653edc53a154f53fa71a4b3d07b91ea7ef07bc9958433b3870a1d4af38ba0f3dd7a096fda6e241107
7
+ data.tar.gz: e515ea2c8c934b28805860ac6aa56320369937474334dc4cca95c26998c54eb802452ff72b138f17069da20df7c4515102a6e104186241b8c97ff7c8b19e9a7b
@@ -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
@@ -27,7 +27,14 @@ module GfmLivePreview
27
27
  end
28
28
  end.parse(argv)
29
29
 
30
- [argv.last, options]
30
+ file = argv.last
31
+
32
+ if file.nil?
33
+ @argv = ['-h']
34
+ parse
35
+ end
36
+
37
+ [file, options]
31
38
  end
32
39
  end
33
40
  end
@@ -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 = File.read ::GfmLivePreview.file
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 doc_path
24
- File.expand_path '..', ::GfmLivePreview.file
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.websocket do |ws|
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
@@ -1,3 +1,3 @@
1
1
  module GfmLivePreview
2
- VERSION = "0.9.6"
2
+ VERSION = "0.9.7"
3
3
  end
@@ -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
- link.href += "?";
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 = document.getElementById("document");
26
+ var md_document = document.getElementById("document");
26
27
  var web_socket_url = "ws://" + window.location.host + window.location.pathname;
27
- var ws = new WebSocket(web_socket_url);
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.6
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-19 00:00:00.000000000 Z
11
+ date: 2014-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler