-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathum-read.rb
49 lines (41 loc) · 1.11 KB
/
um-read.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'tempfile'
require 'kramdown'
require_relative '../lib/um.rb'
options = Options.parse! do |available_opts, set_opts|
available_opts.banner = 'usage: um read [OPTIONS...] <page name>'
available_opts.on(
'-t', '--topic TOPIC', 'Set topic for a single invocation.'
) do |topic|
set_opts[:topic] = topic
end
available_opts.on('-h', '--help', 'Print this help message.') do
puts available_opts
exit 0
end
end
page_name = ARGV.first
if page_name.to_s.empty?
$stderr.puts options.available
exit 1
end
config = UmConfig.source
topic = options[:topic] || Topic.current(config)
page_path = config.existing_page_path(page_name, topic)
unless page_path
msg = %{No um page found for "#{page_name}" under topic "#{topic}."}
$stderr.puts msg
exit 2
end
if File.extname(page_path) == UmConfig::UM_MARKDOWN_EXT
begin
temp_file = Tempfile.new('um')
doc = Kramdown::Document.new(File.read(page_path))
File.write(temp_file.path, doc.to_man)
system(%{man "#{temp_file.path}"})
ensure
temp_file.unlink
end
else
pager = config[:pager]
exec(%{#{pager} "#{page_path}"})
end