octodown 1.4.0 → 1.4.1

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: a15b5cceda8b3271dc0fda94c850817676019f1d
4
- data.tar.gz: ac6cbb077b6ff20c636b3418a2f5caf9063c9cc2
3
+ metadata.gz: 060715e5ebab9c22e3f9cccc9b87f2888f2da006
4
+ data.tar.gz: a53f8dc0b0024693d073f345274281416c56c8d7
5
5
  SHA512:
6
- metadata.gz: 6bb15c6a9de3ff7a1cc9eb5e99c5520a8117490db4c61270d9e4c04ff712c8916de7bff8daf6d6d3faebd18662ddeb13a80c1457cc69e2cd8aad65bbf49caa07
7
- data.tar.gz: 8f11f1fa05e51b65f34fc24d72912cdf983e8208036d69ce774275a5edcce4da2315cfc07fd5c23afcdc8801388532c6d11616d53232cb53e353964037cf7835
6
+ metadata.gz: 7d055ab5ff014d28beca6d8e93fce961bad0f9468a10ffd86490da6ade49df078ec811c414586f85708d95e2a2c87376a6c4be4bf569cca2038a32752c8d9157
7
+ data.tar.gz: 3679b776ed450685a3469b8f2525ff6f914ce37aef844fbd94486ce74bf494479e0113543065dcf5e431cef26ba0c243854f6c07fd91384832162ff7e52b1ed7
@@ -1,7 +1,7 @@
1
1
  module Octodown
2
2
  module Renderer
3
3
  class Server
4
- attr_reader :file, :path, :options, :port, :ws
4
+ attr_reader :file, :path, :options, :port
5
5
 
6
6
  def initialize(_content, options = {})
7
7
  init_ws
@@ -10,6 +10,7 @@ module Octodown
10
10
  @options = options
11
11
  @path = File.dirname(File.expand_path(file.path))
12
12
  @port = options[:port]
13
+ @websockets = []
13
14
  end
14
15
 
15
16
  def present
@@ -19,8 +20,8 @@ module Octodown
19
20
  # page in a browser. I hate relying on time here, but I can't think
20
21
  # of cleaner way currently.
21
22
  Thread.new do |t|
22
- sleep 1
23
- Launchy.open "http://localhost:#{port}"
23
+ sleep 2.5
24
+ Launchy.open "http://localhost:#{port}" if @websockets.empty?
24
25
  t.exit
25
26
  end
26
27
 
@@ -39,10 +40,26 @@ module Octodown
39
40
 
40
41
  private
41
42
 
43
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
42
44
  def render_ws(env)
43
- @ws = ::Faye::WebSocket.new env
44
- ws.rack_response
45
+ md = render_md(file)
46
+
47
+ socket = ::Faye::WebSocket.new(env)
48
+
49
+ socket.on(:open) do
50
+ socket.send md
51
+ puts "Clients: #{@websockets.size}" if ENV['DEBUG']
52
+ end
53
+
54
+ socket.on(:close) do
55
+ @websockets = @websockets.select { |s| s != socket }
56
+ puts "Clients: #{@websockets.size}" if ENV['DEBUG']
57
+ end
58
+
59
+ @websockets << socket
60
+ socket.rack_response
45
61
  end
62
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
46
63
 
47
64
  def render_http(env)
48
65
  Rack::Response.new.tap do |res|
@@ -58,22 +75,21 @@ module Octodown
58
75
 
59
76
  # Render HTML body from Markdown
60
77
  def body
61
- HTML.render render_md, options
78
+ HTML.render render_md(file), options
62
79
  end
63
80
 
64
81
  def register_listener
65
82
  Octodown::Support::Services::Riposter.call file do
66
- md = render_md
67
- ws.on(:open) { ws.send md }
68
- ws.send md
69
-
70
- puts "Reloading #{file.path}..."
83
+ md = render_md(file)
84
+ @websockets.each do |socket|
85
+ socket.send md
86
+ end
71
87
  end
72
88
  end
73
89
 
74
- def render_md
75
- file.rewind unless file.pos == 0
76
- Renderer::GithubMarkdown.render file, options
90
+ def render_md(f)
91
+ f.rewind unless f.pos == 0
92
+ Renderer::GithubMarkdown.render f, options
77
93
  end
78
94
 
79
95
  def init_ws
@@ -7,7 +7,13 @@ module Octodown
7
7
 
8
8
  path = File.dirname(File.expand_path(file.path))
9
9
  regex = Regexp.new(file.path)
10
- Listen.to(path, only: regex, &listener_callback).start
10
+
11
+ @listener ||= Listen.to(path, only: regex) do
12
+ puts "Reloading #{file.path}..."
13
+ listener_callback.call
14
+ end
15
+
16
+ @listener.start
11
17
  end
12
18
  end
13
19
  end
@@ -9,13 +9,36 @@
9
9
  <%= highlight_stylesheet %>
10
10
  <%= stylesheet %>
11
11
 
12
- <script type="text/javascript">
13
- var socket = new WebSocket(<%= host %>);
12
+ <script type='text/javascript'>
13
+ 'use strict';
14
14
 
15
- socket.onmessage = function(event) {
16
- document.querySelector('article.markdown-body').innerHTML = event.data;
17
- };
15
+ document.addEventListener("DOMContentLoaded", function() {
16
+ var ws = null;
17
+ var interval = 2500;
18
+
19
+ function start() {
20
+ ws = new WebSocket(<%= host %>);
21
+
22
+ ws.onmessage = function(event) {
23
+ var body = document.querySelector('article.markdown-body');
24
+ if (body) body.innerHTML = event.data;
25
+ };
26
+
27
+ ws.onclose = function() {
28
+ check();
29
+ };
30
+ }
31
+
32
+ function check(){
33
+ if(ws.readyState == 3) start();
34
+ }
35
+
36
+ start();
37
+
38
+ setInterval(check, interval);
39
+ });
18
40
  </script>
41
+
19
42
  </head>
20
43
 
21
44
  <body>
@@ -1,3 +1,3 @@
1
1
  module Octodown
2
- VERSION = '1.4.0'.freeze
2
+ VERSION = '1.4.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octodown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
@@ -294,7 +294,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
294
294
  version: '0'
295
295
  requirements: []
296
296
  rubyforge_project:
297
- rubygems_version: 2.5.1
297
+ rubygems_version: 2.4.5.1
298
298
  signing_key:
299
299
  specification_version: 4
300
300
  summary: GitHub Markdown straight from your shell.