octodown 1.8.0 → 1.8.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: f90a2ef4162d809a6a4b3b36317b68be7f5634c9
4
- data.tar.gz: 3ec9707771065783b2ec7450d51b6cfc3cd4b61d
3
+ metadata.gz: 873f8e9bccb9cea287afa902146a4932ca71e83a
4
+ data.tar.gz: 5d88a4c9b7a26f981d10671a76a7a5bfc5271959
5
5
  SHA512:
6
- metadata.gz: 55e9a5523ef91ce72831232c8ee4bd30a3e730bb05f62b490358acdfd3eea44fb3d32d06de1fe54d5d7c6c704816c82b9c2a1b2e9e56e74dba54114ba45a8471
7
- data.tar.gz: d5fe2d2797b00fb9f33cf073aacf8eb55556a7ea22c7a2148a3c1d190ca6b373aeef819ef0310f583a1e22dd3fd847fa9407ac168e76628dc7bcfa88b9aed72c
6
+ metadata.gz: 358cc97c065769a35c69c608ab41268d7eb2d4fa23fd84b92702cd3f727e51a8e486249609bc7d6da1f1fb91bd59ff4c63ce4a00c7555269b19b3ebc914781ae
7
+ data.tar.gz: 720403b3d42ce1b2c1b83f908aebab01dca2e3d8a8c6783b165951a8acfd3495c8cf76823dfd1c55f2de5baa521b3136d85a27df7f2773ca41ac1854fd5f1520
@@ -37,6 +37,11 @@ OptionParser.new do |opts|
37
37
  options[:logger].level = Logger::FATAL
38
38
  end
39
39
 
40
+ opts.on '--debug', 'Debug the gem' do
41
+ ENV['LISTEN_GEM_DEBUGGING'] = '2'
42
+ options[:logger].level = Logger::DEBUG
43
+ end
44
+
40
45
  opts.on '-r', '--raw', 'Print raw HTML to STDOUT' do
41
46
  options[:presenter] = :raw
42
47
  end
@@ -68,7 +73,6 @@ end.parse!
68
73
 
69
74
  require 'tty-prompt'
70
75
 
71
- STDOUT.sync = true
72
76
  TUI = TTY::Prompt.new(enable_color: true)
73
77
 
74
78
  options[:file] = if ARGF.file == STDIN && options[:stdin]
@@ -17,7 +17,6 @@ module Octodown
17
17
  @path = File.dirname(File.expand_path(file.path))
18
18
  @port = options[:port]
19
19
  @websockets = []
20
- @already_opened = false
21
20
  @mutex = Mutex.new
22
21
  end
23
22
 
@@ -27,7 +26,6 @@ module Octodown
27
26
  Thread.new do
28
27
  Thread.abort_on_exception = true
29
28
  maybe_launch_browser
30
- Thread.exit
31
29
  end
32
30
 
33
31
  boot_server
@@ -45,10 +43,11 @@ module Octodown
45
43
  sleep 2.5
46
44
 
47
45
  @mutex.synchronize do
48
- if @already_opened == false
49
- @already_opened = true
46
+ if @websockets.empty?
50
47
  logger.info 'Loading preview in a new browser tab'
51
48
  Launchy.open "http://localhost:#{port}"
49
+ else
50
+ logger.info 'Re-using existing browser tab'
52
51
  end
53
52
  end
54
53
  end
@@ -65,34 +64,25 @@ module Octodown
65
64
 
66
65
  private
67
66
 
68
- # rubocop:disable Metrics/MethodLength
69
67
  def render_ws(env)
70
68
  md = render_md(file)
71
69
 
72
70
  socket = ::Faye::WebSocket.new(env)
73
71
 
74
72
  socket.on(:open) do
75
- @mutex.synchronize do
76
- if @already_opened == false
77
- logger.info 'Re-using octodown client from previous browser tab'
78
- end
79
-
80
- @already_opened = true
81
- end
73
+ log_clients('Client joined')
82
74
 
83
75
  socket.send md
84
- logger.debug "Clients: #{@websockets.size}"
85
76
  end
86
77
 
87
78
  socket.on(:close) do
88
79
  @websockets = @websockets.reject { |s| s == socket }
89
- logger.debug "Clients: #{@websockets.size}"
80
+ log_clients('Client left')
90
81
  end
91
82
 
92
83
  @websockets << socket
93
84
  socket.rack_response
94
85
  end
95
- # rubocop:enable Metrics/MethodLength
96
86
 
97
87
  def render_http(env)
98
88
  Rack::Response.new.tap do |res|
@@ -124,6 +114,10 @@ module Octodown
124
114
  def render_md(f)
125
115
  Renderer::GithubMarkdown.render f, options
126
116
  end
117
+
118
+ def log_clients(msg)
119
+ logger.debug "#{msg}. Number of websocket clients: #{@websockets.size}"
120
+ end
127
121
  end
128
122
  end # Support
129
123
  end # Octodown
@@ -6,6 +6,7 @@ module Octodown
6
6
  FORMAT = "%-5s: %s\n".freeze
7
7
 
8
8
  def self.build(dev: STDOUT, level: ::Logger::INFO)
9
+ dev.sync = true
9
10
  logger = ::Logger.new(dev)
10
11
  logger.level = level
11
12
  logger.formatter = method(:formatter)
@@ -6,7 +6,8 @@ module Octodown
6
6
  require 'listen'
7
7
 
8
8
  path = File.dirname(File.expand_path(file.path))
9
- regex = Regexp.new("^#{File.basename(file.path)}$")
9
+ escaped_path = Regexp.escape(file.path)
10
+ regex = Regexp.new("^#{escaped_path}$")
10
11
 
11
12
  @listener ||= Listen.to(path, only: regex) do |modified, added, _rm|
12
13
  listener_callback.call if modified.any? || added.any?
@@ -8,37 +8,6 @@
8
8
 
9
9
  <%= highlight_stylesheet %>
10
10
  <%= stylesheet %>
11
-
12
- <script type='text/javascript'>
13
- 'use strict';
14
-
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
- });
40
- </script>
41
-
42
11
  </head>
43
12
 
44
13
  <body>
@@ -56,4 +25,21 @@
56
25
  </div>
57
26
  </div>
58
27
  </body>
28
+
29
+ <%# reconnecting-websocket.min.js %>
30
+
31
+ <script type='text/javascript'>
32
+ !function(a,b){"function"==typeof define&&define.amd?define([],b):"undefined"!=typeof module&&module.exports?module.exports=b():a.ReconnectingWebSocket=b()}(this,function(){function a(b,c,d){function l(a,b){var c=document.createEvent("CustomEvent");return c.initCustomEvent(a,!1,!1,b),c}var e={debug:!1,automaticOpen:!0,reconnectInterval:1e3,maxReconnectInterval:3e4,reconnectDecay:1.5,timeoutInterval:2e3};d||(d={});for(var f in e)this[f]="undefined"!=typeof d[f]?d[f]:e[f];this.url=b,this.reconnectAttempts=0,this.readyState=WebSocket.CONNECTING,this.protocol=null;var h,g=this,i=!1,j=!1,k=document.createElement("div");k.addEventListener("open",function(a){g.onopen(a)}),k.addEventListener("close",function(a){g.onclose(a)}),k.addEventListener("connecting",function(a){g.onconnecting(a)}),k.addEventListener("message",function(a){g.onmessage(a)}),k.addEventListener("error",function(a){g.onerror(a)}),this.addEventListener=k.addEventListener.bind(k),this.removeEventListener=k.removeEventListener.bind(k),this.dispatchEvent=k.dispatchEvent.bind(k),this.open=function(b){h=new WebSocket(g.url,c||[]),b||k.dispatchEvent(l("connecting")),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","attempt-connect",g.url);var d=h,e=setTimeout(function(){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","connection-timeout",g.url),j=!0,d.close(),j=!1},g.timeoutInterval);h.onopen=function(){clearTimeout(e),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onopen",g.url),g.protocol=h.protocol,g.readyState=WebSocket.OPEN,g.reconnectAttempts=0;var d=l("open");d.isReconnect=b,b=!1,k.dispatchEvent(d)},h.onclose=function(c){if(clearTimeout(e),h=null,i)g.readyState=WebSocket.CLOSED,k.dispatchEvent(l("close"));else{g.readyState=WebSocket.CONNECTING;var d=l("connecting");d.code=c.code,d.reason=c.reason,d.wasClean=c.wasClean,k.dispatchEvent(d),b||j||((g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onclose",g.url),k.dispatchEvent(l("close")));var e=g.reconnectInterval*Math.pow(g.reconnectDecay,g.reconnectAttempts);setTimeout(function(){g.reconnectAttempts++,g.open(!0)},e>g.maxReconnectInterval?g.maxReconnectInterval:e)}},h.onmessage=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onmessage",g.url,b.data);var c=l("message");c.data=b.data,k.dispatchEvent(c)},h.onerror=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onerror",g.url,b),k.dispatchEvent(l("error"))}},1==this.automaticOpen&&this.open(!1),this.send=function(b){if(h)return(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","send",g.url,b),h.send(b);throw"INVALID_STATE_ERR : Pausing to reconnect websocket"},this.close=function(a,b){"undefined"==typeof a&&(a=1e3),i=!0,h&&h.close(a,b)},this.refresh=function(){h&&h.close()}}return a.prototype.onopen=function(){},a.prototype.onclose=function(){},a.prototype.onconnecting=function(){},a.prototype.onmessage=function(){},a.prototype.onerror=function(){},a.debugAll=!1,a.CONNECTING=WebSocket.CONNECTING,a.OPEN=WebSocket.OPEN,a.CLOSING=WebSocket.CLOSING,a.CLOSED=WebSocket.CLOSED,a});
33
+ </script>
34
+
35
+ <script type='text/javascript'>
36
+ 'use strict';
37
+
38
+ var ws = new ReconnectingWebSocket(<%= host %>, [], { maxReconnectInterval: 1000 });
39
+
40
+ ws.onmessage = function(event) {
41
+ var body = document.querySelector('article.markdown-body');
42
+ if (body) body.innerHTML = event.data;
43
+ };
44
+ </script>
59
45
  </html>
@@ -1,3 +1,3 @@
1
1
  module Octodown
2
- VERSION = '1.8.0'.freeze
2
+ VERSION = '1.8.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.8.0
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer