kramdown-man 0.1.8 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+ require 'kramdown'
2
+ require 'kramdown/man'
3
+
4
+ require 'optparse'
5
+
6
+ module Kramdown
7
+ module Man
8
+ #
9
+ # Represents the `kramdown-man` command's logic.
10
+ #
11
+ # @api private
12
+ #
13
+ # @since 1.0.0
14
+ #
15
+ class CLI
16
+
17
+ # The program name.
18
+ PROGRAM_NAME = "kramdown-man"
19
+
20
+ # The URL to report bugs to.
21
+ BUG_REPORT_URL = "https://github.com/postmodern/kramdown-man/issues/new"
22
+
23
+ # The path to the man/ directory.
24
+ MAN_PAGE = File.join(__dir__,'..','..','..','man','kramdown-man.1')
25
+
26
+ # The command's option parser.
27
+ #
28
+ # @return [OptionParser]
29
+ attr_reader :option_parser
30
+
31
+ # The optional output file to write to.
32
+ #
33
+ # @return [String, nil]
34
+ attr_reader :output
35
+
36
+ #
37
+ # Initializes the command.
38
+ #
39
+ def initialize
40
+ @option_parser = option_parser
41
+
42
+ @output = nil
43
+ end
44
+
45
+ #
46
+ # Initializes and runs the command.
47
+ #
48
+ # @param [Array<String>] argv
49
+ # Command-line arguments.
50
+ #
51
+ # @return [Integer]
52
+ # The exit status of the command.
53
+ #
54
+ def self.run(argv)
55
+ new().run(argv)
56
+ rescue Interrupt
57
+ # https://tldp.org/LDP/abs/html/exitcodes.html
58
+ return 130
59
+ rescue Errno::EPIPE
60
+ # STDOUT pipe broken
61
+ return 0
62
+ end
63
+
64
+ #
65
+ # Runs the command.
66
+ #
67
+ # @param [Array<String>] argv
68
+ # Command-line arguments.
69
+ #
70
+ # @return [Integer]
71
+ # The return status code.
72
+ #
73
+ def run(argv=ARGV)
74
+ argv = begin
75
+ @option_parser.parse(argv)
76
+ rescue OptionParser::ParseError => error
77
+ print_error(error.message)
78
+ return -1
79
+ end
80
+
81
+ markdown = case argv.length
82
+ when 1
83
+ path = argv[0]
84
+
85
+ unless File.file?(path)
86
+ print_error "no such file or directory: #{path}"
87
+ return -1
88
+ end
89
+
90
+ File.read(path)
91
+ when 0
92
+ print_error "a MARKDOWN_FILE argument is required"
93
+ return -1
94
+ else
95
+ print_error "too many arguments given"
96
+ return -1
97
+ end
98
+
99
+ doc = Kramdown::Document.new(markdown)
100
+ man_page = doc.to_man
101
+
102
+ if @output
103
+ File.write(@output,man_page)
104
+ elsif $stdout.tty?
105
+ view_man_page(man_page)
106
+ else
107
+ puts man_page
108
+ end
109
+
110
+ return 0
111
+ rescue => error
112
+ print_backtrace(error)
113
+ return -1
114
+ end
115
+
116
+ #
117
+ # Displays the man page using the `man` command.
118
+ #
119
+ # @param [String] man_page
120
+ # The man page output.
121
+ #
122
+ def view_man_page(man_page)
123
+ io = IO.popen(%w[man -l -],'w')
124
+ pid = io.pid
125
+
126
+ begin
127
+ io.puts man_page
128
+ rescue Errno::EPIPE
129
+ ensure
130
+ io.close
131
+
132
+ begin
133
+ Process.waitpid(pid)
134
+ rescue Errno::EPIPE, Errno::ECHILD
135
+ end
136
+ end
137
+ end
138
+
139
+ #
140
+ # The option parser.
141
+ #
142
+ # @return [OptionParser]
143
+ #
144
+ def option_parser
145
+ OptionParser.new do |opts|
146
+ opts.banner = "usage: #{PROGRAM_NAME} [options] MARKDOWN_FILE"
147
+
148
+ opts.on('-o','--output FILE','Write the man page output to the file') do |file|
149
+ @output = file
150
+ end
151
+
152
+ opts.on('-V','--version','Print the version') do
153
+ puts "#{PROGRAM_NAME} #{VERSION}"
154
+ exit
155
+ end
156
+
157
+ opts.on('-h','--help','Print the help output') do
158
+ if $stdout.tty?
159
+ system('man',MAN_PAGE)
160
+ else
161
+ puts opts
162
+ end
163
+
164
+ exit
165
+ end
166
+
167
+ opts.separator ""
168
+ opts.separator "Examples:"
169
+ opts.separator " #{PROGRAM_NAME} -o man/myprogram.1 man/myprogram.1.md"
170
+ opts.separator " #{PROGRAM_NAME} man/myprogram.1.md"
171
+ opts.separator ""
172
+ end
173
+ end
174
+
175
+ #
176
+ # Prints an error message to stderr.
177
+ #
178
+ # @param [String] error
179
+ # The error message.
180
+ #
181
+ def print_error(error)
182
+ $stderr.puts "#{PROGRAM_NAME}: #{error}"
183
+ end
184
+
185
+ #
186
+ # Prints a backtrace to stderr.
187
+ #
188
+ # @param [Exception] exception
189
+ # The exception.
190
+ #
191
+ def print_backtrace(exception)
192
+ $stderr.puts "Oops! Looks like you've found a bug!"
193
+ $stderr.puts "Please report the following text to: #{BUG_REPORT_URL}"
194
+ $stderr.puts
195
+ $stderr.puts "```"
196
+ $stderr.puts "#{exception.full_message}"
197
+ $stderr.puts "```"
198
+ end
199
+
200
+ end
201
+ end
202
+ end