octodown 1.7.0 → 1.8.0

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: 924195e613b34c37eb8ac469ef281003f395ffae
4
- data.tar.gz: a5ec1d270575c5beb67a44d99fb315a904dbfd18
3
+ metadata.gz: f90a2ef4162d809a6a4b3b36317b68be7f5634c9
4
+ data.tar.gz: 3ec9707771065783b2ec7450d51b6cfc3cd4b61d
5
5
  SHA512:
6
- metadata.gz: 35f8720e42935d3e16c50d79d915cae4c50fef7d10fbfd9297f39ed3d244e3fec4682a1a858335b8f6dd5f5a22f1fb47cf36f8b931e9aeb21050709492d7ed33
7
- data.tar.gz: 16e84424b7d6c52986b4bc8986d2443782250e7c4ee5db9fee916a95c9725ec75e1606e3030a047a9d21886799c97fd854095a342bbc01fc6096c47f1765d183
6
+ metadata.gz: 55e9a5523ef91ce72831232c8ee4bd30a3e730bb05f62b490358acdfd3eea44fb3d32d06de1fe54d5d7c6c704816c82b9c2a1b2e9e56e74dba54114ba45a8471
7
+ data.tar.gz: d5fe2d2797b00fb9f33cf073aacf8eb55556a7ea22c7a2148a3c1d190ca6b373aeef819ef0310f583a1e22dd3fd847fa9407ac168e76628dc7bcfa88b9aed72c
data/bin/octodown CHANGED
@@ -1,19 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'octodown'
4
+ require 'octodown/support/file_chooser'
5
+ require 'octodown/support/logger'
4
6
  require 'optparse'
5
7
  require 'launchy'
6
-
7
- include Octodown
8
-
9
8
  # Default options
10
9
  options = {
11
- port: 8080,
12
- presenter: :html,
13
- style: :github
10
+ port: 8887,
11
+ presenter: :server,
12
+ style: :github,
13
+ stdin: false,
14
+ logger: Octodown::Support::Logger.build,
15
+ no_open: false
14
16
  }
15
17
 
16
- # rubocop:disable Metrics/BlockLength
17
18
  OptionParser.new do |opts|
18
19
  opts.banner = 'Usage: octodown [options]'
19
20
 
@@ -28,6 +29,14 @@ OptionParser.new do |opts|
28
29
  options[:style] = s
29
30
  end
30
31
 
32
+ opts.on '--stdin', 'Read from STDIN' do
33
+ options[:stdin] = true
34
+ end
35
+
36
+ opts.on '--quiet', 'Shhhh.. keep quiet' do
37
+ options[:logger].level = Logger::FATAL
38
+ end
39
+
31
40
  opts.on '-r', '--raw', 'Print raw HTML to STDOUT' do
32
41
  options[:presenter] = :raw
33
42
  end
@@ -36,6 +45,10 @@ OptionParser.new do |opts|
36
45
  options[:presenter] = :server
37
46
  end
38
47
 
48
+ opts.on '-h', '--html', 'Render to HTML' do
49
+ options[:presenter] = :html
50
+ end
51
+
39
52
  opts.on(
40
53
  '-P', '--port [PORT]', "LiveReload port (default: #{options[:port]})"
41
54
  ) do |port|
@@ -47,6 +60,23 @@ OptionParser.new do |opts|
47
60
  puts opts
48
61
  exit
49
62
  end
63
+
64
+ opts.on '--no-open', 'Do not open the browser' do
65
+ options[:no_open] = true
66
+ end
50
67
  end.parse!
51
68
 
69
+ require 'tty-prompt'
70
+
71
+ STDOUT.sync = true
72
+ TUI = TTY::Prompt.new(enable_color: true)
73
+
74
+ options[:file] = if ARGF.file == STDIN && options[:stdin]
75
+ STDIN
76
+ elsif ARGF.file != STDIN
77
+ ARGF.file
78
+ else
79
+ FileChooser.new(prompt: TUI).choose_file
80
+ end
81
+
52
82
  Octodown.call options
@@ -10,11 +10,12 @@ module Octodown
10
10
  include HTML
11
11
  include Renderable
12
12
 
13
- attr_reader :options, :file
13
+ attr_reader :options, :file, :logger
14
14
 
15
15
  def initialize(file, options = {})
16
16
  @file = file
17
17
  @options = options
18
+ @logger = options[:logger]
18
19
  end
19
20
 
20
21
  def content
@@ -24,7 +25,7 @@ module Octodown
24
25
  begin
25
26
  File.open(file.path, 'r') { |f| buffer = f.read }
26
27
  rescue Errno::ENOENT
27
- puts '[WARN] something went wrong when trying to open the file'
28
+ logger.warn 'Something went wrong when trying to open the file'
28
29
  end
29
30
  end
30
31
  pipeline.call(buffer ||= 'could not read changes')[:output].to_s
@@ -7,9 +7,10 @@ module Octodown
7
7
  include Octodown::Support
8
8
  include Renderable
9
9
 
10
- attr_reader :rendered_markdown, :filepath, :options
10
+ attr_reader :rendered_markdown, :filepath, :options, :logger
11
11
 
12
12
  def initialize(rendered_markdown, options = {})
13
+ @logger = options[:logger]
13
14
  @rendered_markdown = rendered_markdown
14
15
  @options = options
15
16
  @filepath = File.join parent_dir, 'template', 'octodown.html.erb'
@@ -39,7 +40,11 @@ module Octodown
39
40
  end
40
41
 
41
42
  def present
42
- Launchy.open PersistentTempfile.create(content, :html).path
43
+ if options[:no_open]
44
+ logger.warn('--no-open argument was used so no browser will be opened')
45
+ else
46
+ Launchy.open PersistentTempfile.create(content, :html).path
47
+ end
43
48
  end
44
49
 
45
50
  private
@@ -12,7 +12,7 @@ module Octodown
12
12
  end
13
13
 
14
14
  def present
15
- puts content
15
+ STDOUT.puts content
16
16
  end
17
17
  end
18
18
  end
@@ -8,10 +8,11 @@ require 'launchy'
8
8
  module Octodown
9
9
  module Renderer
10
10
  class Server
11
- attr_reader :file, :path, :options, :port
11
+ attr_reader :file, :path, :options, :port, :logger
12
12
 
13
13
  def initialize(_content, options = {})
14
- @file = ARGF.file
14
+ @logger = options[:logger]
15
+ @file = options[:file]
15
16
  @options = options
16
17
  @path = File.dirname(File.expand_path(file.path))
17
18
  @port = options[:port]
@@ -33,19 +34,20 @@ module Octodown
33
34
  end
34
35
 
35
36
  def boot_server
36
- puts "[INFO] Server running on http://localhost:#{port}"
37
+ logger.info "#{file.path} is getting octodown'd"
38
+ logger.info "Server running on http://localhost:#{port}"
37
39
  Rack::Handler::Puma.run app, Host: 'localhost', Port: port, Silent: true
38
40
  end
39
41
 
40
42
  def maybe_launch_browser
41
- return if ENV['TEST']
43
+ return if options[:no_open]
42
44
 
43
45
  sleep 2.5
44
46
 
45
47
  @mutex.synchronize do
46
48
  if @already_opened == false
47
49
  @already_opened = true
48
- puts '[INFO] Loading preview in a new browser tab'
50
+ logger.info 'Loading preview in a new browser tab'
49
51
  Launchy.open "http://localhost:#{port}"
50
52
  end
51
53
  end
@@ -72,19 +74,19 @@ module Octodown
72
74
  socket.on(:open) do
73
75
  @mutex.synchronize do
74
76
  if @already_opened == false
75
- puts '[INFO] Re-using octodown client from previous browser tab'
77
+ logger.info 'Re-using octodown client from previous browser tab'
76
78
  end
77
79
 
78
80
  @already_opened = true
79
81
  end
80
82
 
81
83
  socket.send md
82
- puts "Clients: #{@websockets.size}" if ENV['DEBUG']
84
+ logger.debug "Clients: #{@websockets.size}"
83
85
  end
84
86
 
85
87
  socket.on(:close) do
86
88
  @websockets = @websockets.reject { |s| s == socket }
87
- puts "Clients: #{@websockets.size}" if ENV['DEBUG']
89
+ logger.debug "Clients: #{@websockets.size}"
88
90
  end
89
91
 
90
92
  @websockets << socket
@@ -111,7 +113,7 @@ module Octodown
111
113
 
112
114
  def register_listener
113
115
  Octodown::Support::Services::Riposter.call file do
114
- puts '[INFO] Recompiling markdown...'
116
+ logger.info "Changes to #{file.path} detected, updating"
115
117
  md = render_md(file)
116
118
  @websockets.each do |socket|
117
119
  socket.send md
@@ -0,0 +1,40 @@
1
+ class FileChooser
2
+ attr_reader :prompt
3
+
4
+ def initialize(prompt:)
5
+ @prompt = prompt
6
+ end
7
+
8
+ def choose_file
9
+ choices = all_markdown_files
10
+ choice = prompt.select('Which markdown file would you like to edit?', choices)
11
+ File.open(choice, 'r')
12
+ end
13
+
14
+ def abort_no_files_found!
15
+ prompt.error 'We could not find any markdown files in this folder.'
16
+ puts
17
+ prompt.error 'Try passing the file explicitly such as, i.e:'
18
+ prompt.error ' $ octodown README.md'
19
+ exit 1
20
+ end
21
+
22
+ def all_markdown_files
23
+ extensions = %w[markdown
24
+ mdown
25
+ mkdn
26
+ md
27
+ mkd
28
+ mdwn
29
+ mdtxt
30
+ mdtext
31
+ text
32
+ Rmd]
33
+
34
+ choices = Dir.glob "**/*.{#{extensions.join(',')}}"
35
+
36
+ abort_no_files_found! if choices.empty?
37
+
38
+ choices.sort_by! { |c| c.split(File::SEPARATOR).length }
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ require 'logger'
2
+
3
+ module Octodown
4
+ module Support
5
+ class Logger
6
+ FORMAT = "%-5s: %s\n".freeze
7
+
8
+ def self.build(dev: STDOUT, level: ::Logger::INFO)
9
+ logger = ::Logger.new(dev)
10
+ logger.level = level
11
+ logger.formatter = method(:formatter)
12
+ logger
13
+ end
14
+
15
+ def self.formatter(severity, _datetime, _progname, msg)
16
+ format(FORMAT, severity, msg)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Octodown
2
- VERSION = '1.7.0'.freeze
2
+ VERSION = '1.8.0'.freeze
3
3
  end
data/lib/octodown.rb CHANGED
@@ -13,7 +13,7 @@ module Octodown
13
13
  def self.call(options)
14
14
  include Octodown::Support::Services
15
15
 
16
- DocumentPresenter.call ARGF.file, options
16
+ DocumentPresenter.call options[:file], options
17
17
  end
18
18
 
19
19
  def self.root
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.7.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
@@ -156,6 +156,20 @@ dependencies:
156
156
  - - "~>"
157
157
  - !ruby/object:Gem::Version
158
158
  version: '3.1'
159
+ - !ruby/object:Gem::Dependency
160
+ name: tty-prompt
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - "~>"
164
+ - !ruby/object:Gem::Version
165
+ version: '0.16'
166
+ type: :runtime
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - "~>"
171
+ - !ruby/object:Gem::Version
172
+ version: '0.16'
159
173
  - !ruby/object:Gem::Dependency
160
174
  name: bundler
161
175
  requirement: !ruby/object:Gem::Requirement
@@ -227,25 +241,33 @@ dependencies:
227
241
  - !ruby/object:Gem::Version
228
242
  version: '3.3'
229
243
  - !ruby/object:Gem::Dependency
230
- name: rubocop
244
+ name: rspec-retry
231
245
  requirement: !ruby/object:Gem::Requirement
232
246
  requirements:
233
- - - "~>"
234
- - !ruby/object:Gem::Version
235
- version: '0.34'
236
247
  - - ">="
237
248
  - !ruby/object:Gem::Version
238
- version: 0.34.2
249
+ version: '0'
239
250
  type: :development
240
251
  prerelease: false
241
252
  version_requirements: !ruby/object:Gem::Requirement
253
+ requirements:
254
+ - - ">="
255
+ - !ruby/object:Gem::Version
256
+ version: '0'
257
+ - !ruby/object:Gem::Dependency
258
+ name: rubocop
259
+ requirement: !ruby/object:Gem::Requirement
242
260
  requirements:
243
261
  - - "~>"
244
262
  - !ruby/object:Gem::Version
245
- version: '0.34'
246
- - - ">="
263
+ version: '0.55'
264
+ type: :development
265
+ prerelease: false
266
+ version_requirements: !ruby/object:Gem::Requirement
267
+ requirements:
268
+ - - "~>"
247
269
  - !ruby/object:Gem::Version
248
- version: 0.34.2
270
+ version: '0.55'
249
271
  description:
250
272
  email:
251
273
  - i.kerseymer@gmail.com
@@ -266,6 +288,8 @@ files:
266
288
  - lib/octodown/renderer/raw.rb
267
289
  - lib/octodown/renderer/renderable.rb
268
290
  - lib/octodown/renderer/server.rb
291
+ - lib/octodown/support/file_chooser.rb
292
+ - lib/octodown/support/logger.rb
269
293
  - lib/octodown/support/persistent_tempfile.rb
270
294
  - lib/octodown/support/relative_root_filter.rb
271
295
  - lib/octodown/support/renderable.rb